Skip to content

Instantly share code, notes, and snippets.

@edankwan
Created July 11, 2016 09:33
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 edankwan/dc3684bdce7c8d9d242d6a9aae5906b4 to your computer and use it in GitHub Desktop.
Save edankwan/dc3684bdce7c8d9d242d6a9aae5906b4 to your computer and use it in GitHub Desktop.
Idea of using instancing with pixel discard in WebGL
// vertex shader
attribute float a_eyeOffset;
varying float v_eyeOffset;
...
vec4 mvPosition = modelViewPosition * vec4(position, 1.0);
mvPosition.x -= a_eyeOffset;
gl_Position = projectionMatrix * mvPosition;
gl_Position.x = gl_Position.x * 0.5 + sign(a_eyeOffset) * 0.5 * gl_Position.w;
v_eyeOffset = a_eyeOffset;
// fragment shader
uniform vec2 u_screenResolution;
varying float v_eyeOffset;
...
void main() {
if((step(0.5, gl_FragCoord.x / u_screenResolution.x) - 0.5) * v_eyeOffset < 0.0) discard;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment