Skip to content

@bloopjs/toodle


@bloopjs/toodle / backends/webgl2/glsl/quad.glsl / vertexShader

Variable: vertexShader

const vertexShader: "#version 300 es\nprecision highp float;\n\n// Engine uniforms\nuniform mat3 u_viewProjection;\nuniform vec2 u_resolution;\n\n// Instance data attributes\n// location 0-2 are the model matrix for this instanced quad (mat3 as 3 vec3s)\nlayout(location = 0) in vec4 a_model0;\nlayout(location = 1) in vec4 a_model1;\nlayout(location = 2) in vec4 a_model2;\n// location 3 is the tint color\nlayout(location = 3) in vec4 a_tint;\n// location 4 is the uv offset and scale\nlayout(location = 4) in vec4 a_uvOffsetAndScale;\n// location 5 is the crop offset and scale\nlayout(location = 5) in vec4 a_cropOffsetAndScale;\n// location 6 is the atlas index (integer attribute)\nlayout(location = 6) in uint a_atlasIndex;\n\n// Outputs to fragment shader\nout vec4 v_uv; // xy = atlas uv, zw = original uv\nout vec4 v_tint;\nflat out int v_atlasIndex;\n\n// Lookup tables for unit quad positions and UVs\nconst vec2 posLookup[4] = vec2[4](\n vec2(-0.5, 0.5),\n vec2(-0.5, -0.5),\n vec2(0.5, 0.5),\n vec2(0.5, -0.5)\n);\n\nconst vec2 uvLookup[4] = vec2[4](\n vec2(0.0, 0.0),\n vec2(0.0, 1.0),\n vec2(1.0, 0.0),\n vec2(1.0, 1.0)\n);\n\nvoid main() {\n // Reconstruct model matrix from instance data\n mat3 modelMatrix = mat3(a_model0.xyz, a_model1.xyz, a_model2.xyz);\n\n // Transform vertex position\n vec2 localPosition = posLookup[gl_VertexID];\n vec2 cropOffset = a_cropOffsetAndScale.xy;\n vec2 cropScale = a_cropOffsetAndScale.zw;\n vec2 croppedPosition = localPosition * cropScale + cropOffset;\n vec3 worldPosition = modelMatrix * vec3(croppedPosition, 1.0);\n vec3 clipPosition = u_viewProjection * worldPosition;\n gl_Position = vec4(clipPosition.xy, 0.0, 1.0);\n\n // Set UV coordinates\n vec2 originalUv = uvLookup[gl_VertexID];\n vec2 atlasUv = originalUv * a_uvOffsetAndScale.zw * cropScale + a_uvOffsetAndScale.xy;\n v_uv = vec4(atlasUv, originalUv);\n\n // Pass through tint and atlas index\n v_tint = a_tint;\n v_atlasIndex = int(a_atlasIndex);\n}\n\n"

Defined in: backends/webgl2/glsl/quad.glsl.ts:1