Skip to content

Instantly share code, notes, and snippets.

@AtkinsSJ
Created September 18, 2014 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AtkinsSJ/b43ac377696a80a73c91 to your computer and use it in GitHub Desktop.
Save AtkinsSJ/b43ac377696a80a73c91 to your computer and use it in GitHub Desktop.
Problematic shader code
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform vec3 u_playerPosition;
uniform float u_playerLightRangeScaled;
void main()
{
vec4 tex = v_color * texture2D(u_texture, v_texCoords);
float texLight = (tex.r + tex.g + tex.b) / 3.0;
float d = distance(gl_FragCoord, u_playerPosition.xy);
// Make things dark and blueish
gl_FragColor = vec4(texLight/5.0, texLight/5.0, texLight/3.0, tex.a);
// If we're within range of the player, light up a bit, based on distance
if (d < u_playerLightRangeScaled) {
float fadeAmount = d / u_playerLightRangeScaled;
gl_FragColor = (tex * (1.0 - fadeAmount)) + (gl_FragColor * fadeAmount);
}
}
@AtkinsSJ
Copy link
Author

On my desktop, this works fine, but on my tablet which uses OpenGL ES 3.0, it doesn't.

The error I'm getting is: No matching overload for function 'distance' found but the docs say that distance() is compatible with all versions. I don't think it should be using the genDType version, as u_playerPosition is a vec3.
Any ideas?

@AtkinsSJ
Copy link
Author

Argh! It was because I used gl_FragCoord rather than gl_FragCoord.xy on line 17. Why did this not throw an error when I first wrote it? Argh.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment