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.2 KB
Raw Blame
#pragma shader "LitFlatNormal"
#pragma version 300 es
#pragma domain Lit
#pragma param u_normal texture default(flatnormal)

#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;

// The material leaves u_normal unset, so it binds the shader's declared `default(flatnormal)`
// (a flat tangent normal (0,0,1)) — proving texture-param defaults actually bind. With a
// straight-on light, a flat normal gives N·L = 1 (full bright); a wrong default would not.
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