Skip to content

Instantly share code, notes, and snippets.

@nothings
Created August 11, 2018 04:01
Show Gist options
  • Save nothings/fad573fdf292ffc5631a32310bded8f4 to your computer and use it in GitHub Desktop.
Save nothings/fad573fdf292ffc5631a32310bded8f4 to your computer and use it in GitHub Desktop.
uniform vec3 base;
uniform usamplerBuffer heightfield;
uniform float edge_lod[5];
out vec4 vcolor;
out vec2 norm_tc;
out vec3 world_pos;
const ivec2 v[4] = ivec2[4](ivec2(0,0),ivec2(-1,0),ivec2(0,-1),ivec2(-1, -1));
void main()
{
vec3 pos;
// reconstruct the worldspace position from the mesh + base location
pos.xy = gl_Vertex.xy + base.xy;
uint packedi = uint(gl_Vertex.z);
// unpack which direction it collapses in to match next LOD level
int type = int(packedi & 3u);
// collapse distance
int step = int(base.z);
// get the LOD for this vertex based on which edge it's associated with
// (or if it's internal it's the 5th type)
float lod = edge_lod[int(packedi>>2u)];
// get coordinates to look up in the heightfield
ivec2 s0 = ivec2(pos.x,pos.y); // uncollapsed position
ivec2 s1 = s0 + v[type]*step; // collapsed position
float base_z = float(texelFetch(heightfield, s0.x+s0.y*10012).r);
float v_z = float(texelFetch(heightfield, s1.x+s1.y*10012).r);
// morph between the two positions
pos.xy = mix(pos.xy, vec2(s1), lod);
pos.z = mix(base_z, v_z, lod);
// various demo-specific stuff
pos.z *= 950.0/65536.0;
world_pos = pos;
gl_Position = gl_ModelViewProjectionMatrix * vec4(pos,1.0);
norm_tc.xy = (pos.xy+0.5)/10012.0;
vcolor = gl_Color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment