Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add p5.js integration #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
PWhiddy merged 2 commits into shader-park:main from davepagurek:p5-plugin
Apr 21, 2023
Merged

Add p5.js integration #96

PWhiddy merged 2 commits into shader-park:main from davepagurek:p5-plugin
Apr 21, 2023

Conversation

Copy link
Contributor

@davepagurek davepagurek commented Apr 21, 2023

This adds a new build that can be included in p5 sketches to mix Shader Park sdfs with other p5 objects, including depth support, so that p5 objects don't clip the sdf in unexpected ways.

A small demo with two Shader Park spheres and a p5 cube:

shaderpark-in-p5.mov

Live here: https://editor.p5js.org/davepagurek/sketches/elqEKyApr

This exposes a few function people can use:

  • loadShaderPark(url, options): Call this in preload() to load source code from a file
    • Annoyingly, this doesn't work in the p5 editor if your shader park source file ends in .js, because the web editor renames .js files to a jumble of letters and numbers. It works with .txt, but loses syntax highlighting. So this option is probably best only for people using a different setup.
  • createShaderParkFromSrc(src, options): Creates a shader park object from string source code
  • createShaderPark(fn, options): Like the createShaderParkFromSrc option, but it lets you pass in a js function. Under the hood, this just calls .toString() on the function. This is maybe the easiest option within the p5 editor that also gives you js syntax highlighting.

For all of those methods:

  • You can call the method globally when using p5 in global mode
  • You can call it on a p5 instance when using p5's instance mode
  • You can also call this on a createGraphics object to get an object you can use on that instead of the main canvas
  • The options object is optional but can include:
    • scale: Defaults to 1, and scales up or down the sdf
    • drawGeometry: A function that draws the host object the shader will run on. Defaults to a sphere, but could be anything.

To use this, you'll need a build of p5 off of the main branch (there isn't a release yet with WebGL2 support, but it'll be in the next one!) and you'll need to add a script tag referencing dist/shader-park-p5.js.

PWhiddy reacted with hooray emoji
Copy link
Member

@PWhiddy PWhiddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super cool! ❤️

I've got just a couple very minor comments.

Thanks for sending this, we really appreciate having some fresh eyes on it :)

targets/p5.js Outdated
source = source.toString();
source = source.slice(source.indexOf("{") + 1, source.lastIndexOf("}"));
} else if (!(typeof source === "string")) {
throw "sculptToHydraRenderer requires the source code to be a function, or a string";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can change "Hyrda" -> to "p5"

Copy link
Contributor Author

@davepagurek davepagurek Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops good catch! Also I forgot that this copy-and-pasted function already handles passing in a function as the source instead of a string, so I don't need to export two separate functions for this.

PWhiddy reacted with thumbs up emoji
targets/p5.js Outdated
[width * target.pixelDensity(), height * target.pixelDensity()]
);
drawGeometry();
gl.disable(gl.CULL_FACE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this disable face culling when a p5 user wants it to stay on? (even if it did I wouldn't imagine it would affect many users.)

Copy link
Contributor Author

@davepagurek davepagurek Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, I'll grab the current parameter values at the start and restore them here

PWhiddy reacted with thumbs up emoji

// browser-friendly UMD build for p5
{
input: 'targets/p5.js',
Copy link
Member

@PWhiddy PWhiddy Apr 21, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thinking here. This is something we could probably add in the future for our other targets like threejs

"\n" +
generatedGLSL.colorGLSL +
"\n" +
fragFooter.replace(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is another good idea that we could implement in our other targets to reduce duplication. thanks!

@PWhiddy PWhiddy merged commit 9ed2318 into shader-park:main Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@PWhiddy PWhiddy PWhiddy approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /