Skip to content

Instantly share code, notes, and snippets.

@TuckerBMorgan
Created October 4, 2022 21:03
Show Gist options
  • Select an option

  • Save TuckerBMorgan/d50dae1527bf234a9450d2109f5b5608 to your computer and use it in GitHub Desktop.

Select an option

Save TuckerBMorgan/d50dae1527bf234a9450d2109f5b5608 to your computer and use it in GitHub Desktop.
My Terrain shader
precision highp float;
precision highp int;
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec3 col;
out vec3 v_col;
out float v_toSunDot;
uniform sampler2D HeightMapOffset;
layout(std140) uniform vertex {
mat4 ortho;
};
vec4 uv_lut[4] = vec4[4](
vec4(1.0, 0.0, 1.0, 0.0), // left bottom
vec4(1.0, 0.0, 0.0, 1.0), // left top
vec4(0.0, 1.0, 1.0, 0.0), // right bottom
vec4(0.0, 1.0, 0.0, 1.0)); // right top
vec3 size_lut[4] = vec3[4](
vec3(0.0, 0.0, 1.0), // left top
vec3(0.0, 0.0, 0.0), // right top
vec3(1.0, 0.0, 1.0), // left bottom
vec3(1.0, 0.0, 0.0)); // right bottom
vec4 calculate_position(int vertexId) {
vec4 temp = uv_lut[vertexId];
vec2 calculated_uv = vec2((temp.x + temp.y), (temp.z + temp.w));
int x_vertex_offset = vertexId / 2;
int y_vertex_offset = vertexId % 2;
float map_size = 200.0;
int map_x = 200;
int map_z = 200;
v_col = col;
float x_index = float(gl_InstanceID / map_x);
float z_index = float(gl_InstanceID % map_z);
v_col.r = (z_index / map_size) * 1.0;
vec3 size = 10.0 * size_lut[vertexId];
float shift_amount = 1.0 / map_size;
vec2 temp_uv = vec2((temp.x + temp.y) / map_size, (temp.z + temp.w) / map_size);
temp_uv.x += shift_amount * (x_index + float(x_vertex_offset));
temp_uv.y += shift_amount * (z_index - float(y_vertex_offset));
vec4 height_offset_sample = texture(HeightMapOffset, temp_uv);
vec3 pos = a_pos + size;
pos.x = pos.x + x_index * 10.0;
pos.z = pos.z + z_index * 10.0;
pos.y = height_offset_sample.r * map_size;
return ortho * vec4(pos, 1.0);
}
void main() {
vec4 a = calculate_position((gl_InstanceID + 1) % 4);
vec4 b = calculate_position((gl_InstanceID + 2) % 4);
vec4 final = calculate_position(gl_VertexID);
vec4 bort = b - final;
vec4 ernie = a - final;
vec3 normla = cross(vec3(bort), vec3(ernie));
vec3 lightToPoint = normalize(vec3(0.0, 20000.0, 0.0) - vec3(final));
float toSunDot = dot(lightToPoint, normla);
if (toSunDot < 0.1) {
toSunDot = 0.1;
}
v_toSunDot = toSunDot;
gl_Position = final;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment