Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

1.1 KB
Raw Blame
#pragma shader "LitNormal"
#pragma version 300 es
#pragma domain Lit
#pragma param u_normal texture default(white)

#pragma vertex
layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in float a_texIndex;

out vec4 v_color;
out vec2 v_texCoord;
out highp vec2 v_worldPos;

void main() {
    gl_Position = u_projection * vec4(a_position, 1.0);
    v_color = a_color;
    v_texCoord = a_texCoord;
    v_worldPos = a_position.xy;
}
#pragma end

#pragma fragment
precision mediump float;

in vec4 v_color;
in vec2 v_texCoord;
in highp vec2 v_worldPos;

uniform sampler2D u_textures[8];

out vec4 fragColor;

// sampleNormal + applyLighting2D + the LightConstants block are injected by ShaderParser
// for `#pragma domain Lit`. The surface normal comes from the material's u_normal map, so
// lighting varies per texel.
void main() {
    vec4 base = texture(u_textures[0], v_texCoord) * v_color;
    vec3 N = sampleNormal(u_normal, v_texCoord);
    vec3 lit = applyLighting2D(base.rgb, N, v_worldPos);
    fragColor = vec4(lit, base.a);
}
#pragma end