Support tangent-space normal maps in the bumpmap builtin.
Default u_bumpMode to normal maps so authored assets like metal-floor work without height-map conversion. Co-authored-by: Cursor <cursoragent@cursor.com>
@@ -186,27 +186,36 @@ Shader.Builtin.BUMPMAP_FRAG = [
186
186
"uniform vec3 u_lightColor[16];",
187
187
"uniform float u_ambient;",
188
188
"uniform float u_specular;",
189
// 0 = height map (R), 1 = tangent-space normal map (RGB)
190
"uniform float u_bumpMode;",
189
191
"varying vec2 v_texCoord;",
190
192
"void main() {",
191
193
" vec4 albedo = texture2D(u_texture, v_texCoord);",
192
" float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",
193
" float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",
194
" float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",
195
" float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",
196
" vec3 normal = normalize(vec3(hL - hR, hU - hD, 0.35));",
197
" vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",
194
" vec3 normal;",
195
" if (u_bumpMode < 0.5) {",
196
" float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",
197
" float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",
198
" float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",
199
" float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",
200
" normal = normalize(vec3(hL - hR, hU - hD, 0.35));",
201
" } else {",
202
" vec3 nSample = texture2D(u_bump, v_texCoord).xyz * 2.0 - 1.0;",
203
" normal = normalize(nSample);",
204
" }",
198
205
" float c = cos(u_rotationRad);",
199
206
" float s = sin(u_rotationRad);",
207
" normal = normalize(vec3(c * normal.x - s * normal.y, s * normal.x + c * normal.y, normal.z));",
208
" vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",
200
209
" vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",
201
210
" vec3 lighting = vec3(u_ambient);",
202
211
" for (int i = 0; i < 16; i++) {",
203
212
" if (i >= u_lightCount) { break; }",
204
213
" vec2 toLight = u_lightPos[i] - world;",
205
214
" float dist = max(length(toLight), 0.001);",
206
" vec3 lightDir = normalize(vec3(toLight / dist, 0.65));",
215
" vec3 lightDir = normalize(vec3(toLight / dist, 0.75));",
207
216
" float diff = max(dot(normal, lightDir), 0.0);",
208
" float atten = 1.0 / (1.0 + dist * 0.008 + dist * dist * 0.00005);",
209
" float spec = pow(diff, 16.0) * u_specular;",
217
" float atten = 1.0 / (1.0 + dist * 0.006 + dist * dist * 0.00004);",
218
" float spec = pow(diff, 24.0) * u_specular;",
210
219
" lighting += u_lightColor[i] * (diff + spec) * atten;",
211
220
" }",
212
221
" gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",
@@ -186,27 +186,36 @@ Shader.Builtin.BUMPMAP_FRAG = [186
"uniform vec3 u_lightColor[16];",186
"uniform vec3 u_lightColor[16];",187
"uniform float u_ambient;",187
"uniform float u_ambient;",188
"uniform float u_specular;",188
"uniform float u_specular;",189
// 0 = height map (R), 1 = tangent-space normal map (RGB)190
"uniform float u_bumpMode;",189
"varying vec2 v_texCoord;",191
"varying vec2 v_texCoord;",190
"void main() {",192
"void main() {",191
" vec4 albedo = texture2D(u_texture, v_texCoord);",193
" vec4 albedo = texture2D(u_texture, v_texCoord);",192
" float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",194
" vec3 normal;",193
" float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",195
" if (u_bumpMode < 0.5) {",194
" float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",196
" float hL = texture2D(u_bump, v_texCoord + vec2(-0.01, 0.0)).r;",195
" float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",197
" float hR = texture2D(u_bump, v_texCoord + vec2(0.01, 0.0)).r;",196
" vec3 normal = normalize(vec3(hL - hR, hU - hD, 0.35));",198
" float hU = texture2D(u_bump, v_texCoord + vec2(0.0, -0.01)).r;",197
" vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",199
" float hD = texture2D(u_bump, v_texCoord + vec2(0.0, 0.01)).r;",200
" normal = normalize(vec3(hL - hR, hU - hD, 0.35));",201
" } else {",202
" vec3 nSample = texture2D(u_bump, v_texCoord).xyz * 2.0 - 1.0;",203
" normal = normalize(nSample);",204
" }",198
" float c = cos(u_rotationRad);",205
" float c = cos(u_rotationRad);",199
" float s = sin(u_rotationRad);",206
" float s = sin(u_rotationRad);",207
" normal = normalize(vec3(c * normal.x - s * normal.y, s * normal.x + c * normal.y, normal.z));",208
" vec2 local = (v_texCoord - vec2(0.5)) * u_worldSize;",200
" vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",209
" vec2 world = u_worldCenter + vec2(c * local.x - s * local.y, s * local.x + c * local.y);",201
" vec3 lighting = vec3(u_ambient);",210
" vec3 lighting = vec3(u_ambient);",202
" for (int i = 0; i < 16; i++) {",211
" for (int i = 0; i < 16; i++) {",203
" if (i >= u_lightCount) { break; }",212
" if (i >= u_lightCount) { break; }",204
" vec2 toLight = u_lightPos[i] - world;",213
" vec2 toLight = u_lightPos[i] - world;",205
" float dist = max(length(toLight), 0.001);",214
" float dist = max(length(toLight), 0.001);",206
" vec3 lightDir = normalize(vec3(toLight / dist, 0.65));",215
" vec3 lightDir = normalize(vec3(toLight / dist, 0.75));",207
" float diff = max(dot(normal, lightDir), 0.0);",216
" float diff = max(dot(normal, lightDir), 0.0);",208
" float atten = 1.0 / (1.0 + dist * 0.008 + dist * dist * 0.00005);",217
" float atten = 1.0 / (1.0 + dist * 0.006 + dist * dist * 0.00004);",209
" float spec = pow(diff, 16.0) * u_specular;",218
" float spec = pow(diff, 24.0) * u_specular;",210
" lighting += u_lightColor[i] * (diff + spec) * atten;",219
" lighting += u_lightColor[i] * (diff + spec) * atten;",211
" }",220
" }",212
" gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",221
" gl_FragColor = vec4(albedo.rgb * lighting, albedo.a * u_alpha);",@@ -395,6 +395,8 @@ class GLContext
395
395
{ this._setUniform(info, "u_ambient", 0.35); }
396
396
if (info.uniforms.u_specular && (!options.uniforms || options.uniforms.u_specular == null))
397
397
{ this._setUniform(info, "u_specular", 0.25); }
398
if (info.uniforms.u_bumpMode && (!options.uniforms || options.uniforms.u_bumpMode == null))
399
{ this._setUniform(info, "u_bumpMode", 1); }
398
400
399
401
if (options.uniforms)
400
402
{
@@ -395,6 +395,8 @@ class GLContext395
{ this._setUniform(info, "u_ambient", 0.35); }395
{ this._setUniform(info, "u_ambient", 0.35); }396
if (info.uniforms.u_specular && (!options.uniforms || options.uniforms.u_specular == null))396
if (info.uniforms.u_specular && (!options.uniforms || options.uniforms.u_specular == null))397
{ this._setUniform(info, "u_specular", 0.25); }397
{ this._setUniform(info, "u_specular", 0.25); }398
if (info.uniforms.u_bumpMode && (!options.uniforms || options.uniforms.u_bumpMode == null))399
{ this._setUniform(info, "u_bumpMode", 1); }398
400
399
if (options.uniforms)401
if (options.uniforms)400
{402
{