-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Add an example for resetShader() #5265
Conversation
Looks good to me, thanks for making the example @JetStarBlues! |
Actually, one small nit. I wonder if it would be better to inline the shaders (rather than loading from files), so that the user can see the custom shader more clearly. |
I've also been thinking quite a bit about this. There's a similar issue with all* the shader examples - both in the Reference and Examples sections of the website. It's a bit of an ask to require users to dig through the p5 source code to find the referenced shader files. I'm not sure what code is used to display the "live code" view above (with the edit/reset/copy buttons). However, it seems to work with the assumption that all the code needed to run the program resides in a single "sketch.js" file. The shader files seem to be treated as an unchangeable asset like images and sound. If there was a way to somehow make the live-code-viewer (not sure correct name) show the shader files, I think that would be a nice solution, as you would:
If this isn't an easy thing to do, (or not in the spirit of the live-code-viewer design), (or in the meanwhile), I can go ahead and change this example to use inline shader code. Should this inline change be propogated to all examples (excluding * except |
Here's a live version of the code as inline. |
If the comments are removed from the inline shader code, the code becomes more succinct, and looks much nicer (live version). In this case, the assumption becomes that the user is comfortable with GLSL, or that they will go to another resource to learn about it (and the uniforms/attributes provided by p5). |
@limzykenneth I was looking through the source code, and saw that you did most of the work on the code renderer. What do you think? Is the renderer capable of multi-file viewing/editing? |
Ah, this file. 😝 I didn't actually write it, just refactored it and fixed a few bugs when I was working on the reference. In essence I know almost as much as you do from just reading the code. Looking at the idea you are discussing above I'm guessing something like including the shader code in the documentation code block and then importing it as string to be compiled when the example sketch is running would be something that you are thinking of? (Not sure if this description is clear enough) We can do something similar with the CSS Not sure how easy or possible with the current example renderer but I had wanted to rewrite it for a long time now, just didn't have the time to do it as it's going to be a relatively big project. |
Thank you for the detailed reply and insight into how the code works!
I see, so in this scenario, the shader files would not reside in the <!-- shader.vert -->
<div class="shader" id="shader.vert">
<code></code>
</div>
<!-- shader.frag -->
<div class="shader" id="shader.frag">
<code></code>
</div>
<!-- sketch.js -->
<code></code> The class I'm not sure how this would work without intercepting calls to --- A thought. If,
let vertSrc = /*glsl*/ `
// vertex position
attribute vec3 aPosition;
// main
void main () {
gl_Position = vec4(aPosition, 1.0);
}
`; Then it might be possible to get some of the benefits of both worlds. The examples can all be updated to use |
I can start a new issue for this (editor handling of shader files), and copy over some of the discussion here onto it. |
That I'm not sure, I don't think it does but you can look it up.
prism.js supports GLSL syntax highlighting, not to inline strings but it can highlight it if it is in its own code block.
If we go for this it means there's no need for inline GLSL syntax highlighting. |
I guess what I'm wondering is how the functions, lets say Given that
Unfortunately it doesn't. It seems to be a bare bones editor/renderer. |
I don't think intercepting calls to I'm guessing the above works but I'm not sure what you mean by string pointers as Javascript strings are immutable so they effectively can't be passed by reference. |
Thank you for taking the time to reply! Sorry, it's taking me a couple of tries to form a mental model. Using the example above, would the documentation look like this? <!-- vertSrc -->
<div class="shader">
<code id="vertSrc">
attribute vec3 aPosition;
attribute vec2 aTexCoord;
uniform mat4 uProjectionMatrix;
uniform mat4 uModelViewMatrix;
varying vec2 vTexCoord;
void main() {
vTexCoord = aTexCoord;
vec4 position = vec4(aPosition, 1.0);
gl_Position = uProjectionMatrix * uModelViewMatrix * position;
}
</code>
</div>
<!-- fragSrc -->
<div class="shader">
<code id="fragSrc">
precision mediump float;
varying vec2 vTexCoord;
void main() {
vec2 uv = vTexCoord;
vec3 color = vec3(uv.x, uv.y, min(uv.x + uv.y, 1.0));
gl_FragColor = vec4(color, 1.0);
}
</code>
</div>
<!-- sketch.js -->
<code>
function setup() {
// Shaders require WEBGL mode to work
createCanvas(100, 100, WEBGL);
// Create our shader
shaderProgram = createShader(vertSrc, fragSrc);
}
function draw() {
// Clear the scene
background(200);
// Draw a box using our shader
// shader() sets the active shader with our shader
shader(shaderProgram);
push();
translate(-width / 4, 0, 0);
rotateX(millis() * 0.00025);
rotateY(millis() * 0.0005);
box(width / 4);
pop();
// Draw a box using the default fill shader
// resetShader() restores the default fill shader
resetShader();
fill(255, 0, 0);
push();
translate(width / 4, 0, 0);
rotateX(millis() * 0.00025);
rotateY(millis() * 0.0005);
box(width / 4);
pop();
}
</code> Where, even though |
@JetStarBlues I could be wrong but I think you would need to add the ID to the code element itself and then query it like so
Also, do you not need to wrap the entire code element inside of a |
Ah I see, thank you. I will update the
So behind the scenes, this line will be injected into the code passed to
It looks like the renderer does this. |
Something along these lines yes. Though I am having second thoughts about this idea as it breaks the flow of the user just copying the code and be able to run it directly. |
@stalgiag I think this PR can be merged. |
Changes:
Adds an example for resetShader(). Here is a link to a live demo.
Screenshot of the change: