-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
material.js shader() example #3720
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Code adopted from "Creating a Gradient Color in Fragment Shader" | ||
// by Bahadır on stackoverflow.com | ||
// https://stackoverflow.com/questions/47376499/creating-a-gradient-color-in-fragment-shader | ||
|
||
precision highp float; varying vec2 vPos; | ||
uniform float pos; | ||
|
||
void main() { | ||
|
||
vec2 st = vPos.xy + vec2(0,pos); | ||
|
||
vec3 color1 = vec3(1.9,0.55,0); | ||
vec3 color2 = vec3(0.226,0.000,0.615); | ||
|
||
float mixValue = distance(st,vec2(0,1)); | ||
vec3 color = mix(color1,color2,mixValue); | ||
|
||
gl_FragColor = vec4(color,mixValue); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stalgiag I agree, and yet the reason why I opted for the Mandel shader was exactly the familiarity of its reoccurrence throughout the reference manual. But the fact that it could seem like a blackbox for users/beginners is undeniable.
Perhaps I should change
pos
tooffset
. Then the shader could coherently be extended so that:vec2 st = vPos.xy + vec2(xOffset,yOffset);
color1 and color2 could perhaps be uniforms as well. That way the shader could potentially be reused with a certain level of modularity that is visually distinguishable:
this could allow two instances of this shader to properly reflect the effects of
shader()
for this particular example/commit:shader1: blue-orange moving vertically
shader2: red-green moving horizontally
what do yo think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or even better
uniform vec2 offset;
rather thanuniform float xOffset
anduniform float yOffset
, giving:vec2 st = vPos.xy + offset.xy;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as for the
quad()
andrect()
situation, we could perhaps opt in for something like this. Which as a solution, relies primarily on:gl_FragCoord.xy / resolution.xy
This allows for eliminating +-1 values for the quad and simply using
rect(0,0,width,height);
Yet ambiguity might arise from
setUniform('resolution',[width,height]);
On a side note, I tried looking for docs on the webgl vs p2d coordinate differences and failed to find any. Are you aware of such in the codebase/references?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the quick response! I like the offset with two gradient shader instances idea. I understand the the impulse to repeat the Mandel shader. It might be a good idea for familiarity sake but it also seems like a good time to start moving away from it and maybe filing an issue to simplify the other examples away from the Mandel shader as well.
I'll leave it up to you as to whether to change the vertex shader. I don't have extremely strong feelings and it would be
rect(-width/2, -height/2, width, height)
which when combined with setting the resolution uniform doesn't provide the largest gains in terms of simplicity.As for coordinate differences, the getting started with webgl wiki explains this a bit. This decision dates back to the first work on WebGL mode in 2015. There are pros and cons to doing this but I agree that maybe it makes sense to try to find another more prominent place for this in documentation. Not sure if there are other mentions that I am missing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the info, although I think it would have been
rect(-width/2, -height/2, width*2, height*2)
, I decided to just opt in forquad()
.I'm gonna close this pull req, and open another one with the latest commit.