-
-
Notifications
You must be signed in to change notification settings - Fork 111
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
Cc/fix dark surfaces #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export default /* glsl */ `varying vec3 vNormal; | ||
export default /* glsl */ ` | ||
#extension GL_OES_standard_derivatives : enable | ||
varying vec3 vNormal; | ||
varying vec3 vLight; | ||
varying vec3 vPosition; | ||
|
||
|
@@ -16,7 +18,12 @@ vec4 getShadedColor(vec4 rgba) { | |
vec3 light = normalize(vLight); | ||
vec3 position = normalize(vPosition); | ||
|
||
float side = gl_FrontFacing ? -1.0 : 1.0; | ||
// Workaround to avoid gl_FrontFacing. See https://github.com/unconed/mathbox/pull/26 | ||
vec3 pdx = dFdx(vPosition); | ||
vec3 pdy = dFdy(vPosition); | ||
bool frontFacting = dot(vNormal, cross(pdx, pdy)) > 0.0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. whoops, slight typo with Wow, there is some magic going on in glsl. SO, indulge me in trying to understand what this does:
But I am missing my concepts here, since I would have expected I would love a one sentence description of the algorithm here for my own edification, and probably for future folks trying to debug. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I am not 100% clear on the coordinate systems involved here. I think the following sentence would be accurate: "vNormal (calculated in the vertex shader) and cross(pdx, pdy) (calculated in this fragment shader) are both surface normals, but might be in opposite directions depending on which side of the surface the camera is, hence dot product". My impression is that Understanding the shader stuff better is definitely a goal :/ I'm hoping not to tinker with it too much. In terms of posterity... I decided just leave a link to the PR as the code comment. (Though git history does that, too). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In particular, one thing I am confused about is
and then |
||
|
||
float side = frontFacting ? 1.0 : -1.0; | ||
float cosine = side * dot(normal, light); | ||
float diffuse = mix(max(0.0, cosine), .5 + .5 * cosine, .1); | ||
|
||
|
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.
@sritchie If you feel like futzing with this, try using
gl_FrontFacing ? +1.0 : - 1.0
...you'll see both sides of the surface turn light.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.
This one seems to bite many folks. I've been reading up on it, and would NOT have found this on my own. Great find on this fix.