32 "layout(std140) uniform;\n"
34 "uniform mediump sampler2DArray sHeightmap;\n"
36 "uniform mat4 uViewProjection;\n"
37 "uniform vec3 uCameraPos;\n"
38 "uniform float uInvLevelSize[10]; // GL doesn't allow unsized array when accessed from non-constant.\n"
40 "struct PerInstanceData\n"
42 " vec2 offset; // World-space offset in XZ plane.\n"
43 " vec2 texture_scale;\n"
44 " vec2 texture_offset; // Same as for world-space offset/scale, just for texture coordinates\n"
45 " float scale; // Scaling factor for vertex offsets (per-instance)\n"
46 " float level; // LOD-level to use when sampling heightmap\n"
49 "uniform InstanceData\n"
51 " PerInstanceData instance[256];\n"
54 "#define LOCATION_VERTEX 0\n"
55 "#define HEIGHTMAP_MIN -20.0 // Depends on the heightmap.\n"
56 "#define HEIGHTMAP_MAX 20.0\n"
58 "layout(location = LOCATION_VERTEX) in vec2 aVertex;\n"
59 "out float vHeight;\n"
65 " vec2 local_offset = aVertex * instance[gl_InstanceID].scale;\n"
66 " vec2 pos = instance[gl_InstanceID].offset + local_offset;\n"
68 " float level = instance[gl_InstanceID].level;\n"
69 " vec2 tex_offset = (aVertex + 0.5) * instance[gl_InstanceID].texture_scale; // 0.5 offset to sample mid-texel.\n"
70 " vec2 texcoord = instance[gl_InstanceID].texture_offset + tex_offset;\n"
72 " vec2 heights = texture(sHeightmap, vec3(texcoord, level)).rg;\n"
74 " // Find blending factors for heightmap. The detail level must not have any discontinuities or it shows as 'artifacts'.\n"
75 " vec2 dist = abs(pos - uCameraPos.xz) * uInvLevelSize[int(level)];\n"
76 " vec2 a = clamp((dist - 0.325) * 8.0, 0.0, 1.0);\n"
77 " float lod_factor = max(a.x, a.y);\n"
78 " float height = mix(heights.x, heights.y, lod_factor);\n"
80 " height = clamp(height, HEIGHTMAP_MIN, HEIGHTMAP_MAX); // To ensure frustum culling assumptions are met.\n"
82 " vec4 vert = vec4(pos.x, height, pos.y, 1.0);\n"
84 " gl_Position = uViewProjection * vert;\n"
85 " vHeight = height;\n"
86 " vLod = vec2(level, lod_factor);\n"
88 " vec3 dist_camera = uCameraPos - vert.xyz;\n"
89 " vFog = clamp(dot(dist_camera, dist_camera) / 250000.0, 0.0, 1.0); // Simple per-vertex fog.\n"
94 "layout(std140) uniform;\n"
95 "precision highp float;\n"
97 "out vec4 FragColor;\n"
102 "// Compress (-inf, +inf) to (0, 1).\n"
103 "float map_height(float h)\n"
105 " return 1.0 / (1.0 + exp(-h / 20.0));\n"
108 "// Make the heightmap look somewhat cloudy and fluffy.\n"
112 " vec3 color = vec3(1.2, 1.2, 1.0) * vec3(map_height(vHeight) + (vLod.x + vLod.y) * 0.1);\n"
113 " vec3 final_color = mix(color, vec3(0.5), vFog);\n"
114 " FragColor = vec4(final_color, 1.0);\n"
static const char vertex_shader_source[]
static const char fragment_shader_source[]