UNPKG

mdx-m3-viewer

Version:

A browser WebGL model viewer. Mainly focused on models of the games Warcraft 3 and Starcraft 2.

3 lines (2 loc) 1.58 kB
declare const shader = "\nuniform mat4 u_VP;\nuniform sampler2D u_heightMap;\nuniform vec2 u_pixel;\nuniform vec2 u_centerOffset;\n\nattribute vec3 a_position;\nattribute vec3 a_normal;\nattribute vec2 a_uv;\nattribute vec3 a_instancePosition;\nattribute float a_instanceTexture;\n\nvarying vec3 v_normal;\nvarying vec2 v_uv;\nvarying float v_texture;\nvarying vec3 v_position;\n\nvoid main() {\n // Half of a pixel in the cliff height map.\n vec2 halfPixel = u_pixel * 0.5;\n\n // The bottom left corner of the map tile this vertex is on.\n vec2 corner = floor((a_instancePosition.xy - vec2(1.0, 0.0) - u_centerOffset.xy) / 128.0);\n\n // Get the 4 closest heights in the height map.\n float bottomLeft = texture2D(u_heightMap, corner * u_pixel + halfPixel).a;\n float bottomRight = texture2D(u_heightMap, (corner + vec2(1.0, 0.0)) * u_pixel + halfPixel).a;\n float topLeft = texture2D(u_heightMap, (corner + vec2(0.0, 1.0)) * u_pixel + halfPixel).a;\n float topRight = texture2D(u_heightMap, (corner + vec2(1.0, 1.0)) * u_pixel + halfPixel).a;\n \n // Do a bilinear interpolation between the heights to get the final value.\n float bottom = mix(bottomRight, bottomLeft, -a_position.x / 128.0);\n float top = mix(topRight, topLeft, -a_position.x / 128.0);\n float height = mix(bottom, top, a_position.y / 128.0);\n\n v_normal = a_normal;\n v_uv = a_uv;\n v_texture = a_instanceTexture;\n v_position = a_position + vec3(a_instancePosition.xy, a_instancePosition.z + height * 128.0);\n\n gl_Position = u_VP * vec4(v_position, 1.0);\n}\n"; export default shader;