Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

WIP hardware shadow compare

Nguyen Xuan An76ee4a531991
diff --git a/src/post.js b/src/post.js
index 15c1e47c..7f3975ab 100644
--- a/src/post.js
+++ b/src/post.js
@@ -23,7 +23,7 @@ uniform float explosionProgress;
uniform mat4 inverseViewProjection;
uniform vec3 eye;
uniform sampler2D sceneDepth;
-uniform sampler2D volumeShadowMap;
+uniform highp sampler2DShadow volumeShadowMap;
uniform mat4 volumeLightProjection;
uniform vec3 volumeLightDirection;
uniform vec3 volumeLightColor;
@@ -136,7 +136,7 @@ vec3 volumeFog(vec3 background) {
vec3 shadow = lightPosition.xyz / lightPosition.w * 0.5 + 0.5;
float visibility = 1.0;
if (all(greaterThanEqual(shadow, vec3(0))) && all(lessThanEqual(shadow, vec3(1)))) {
- visibility = step(shadow.z - 0.0008, texture(volumeShadowMap, shadow.xy).r);
+ visibility = texture(volumeShadowMap, vec3(shadow.xy, shadow.z - 0.0008));
}
vec4 skyCell = POST_SKY ? texture(fogSkyVisibility, (position - volumeMinimum) / (volumeMaximum - volumeMinimum)) : vec4(0);
float shadowShare = POST_SKY ? 0.1 + 0.9 * min(1.0, 30.0 * skyCell.r) : volumeShadowAmbient;
diff --git a/src/renderer.js b/src/renderer.js
index 3a1a07d2..02e3a896 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -617,6 +617,22 @@ export class Renderer67 {
this.uniforms[name] = this.context.getUniformLocation(this.program, name);
}
for (const name of ENVIRONMENT_UNIFORMS) this.uniforms[name] = this.context.getUniformLocation(this.program, name);
+ this.keepSamplerTypesApart();
+ }
+
+ // A sampler nobody has set reads texture unit 0, which holds the shadow map as a comparison sampler. One unit may not serve
+ // two sampler types, so every 2D sampler starts on unit 1 until a draw sets it.
+ keepSamplerTypesApart() {
+ const context = this.context;
+ const previousProgram = context.getParameter(context.CURRENT_PROGRAM);
+ context.useProgram(this.program);
+ const count = context.getProgramParameter(this.program, context.ACTIVE_UNIFORMS);
+ for (let index = 0; index < count; index++) {
+ const uniform = context.getActiveUniform(this.program, index);
+ if (uniform.type !== context.SAMPLER_2D) continue;
+ context.uniform1iv(context.getUniformLocation(this.program, uniform.name), new Int32Array(uniform.size).fill(1));
+ }
+ context.useProgram(previousProgram);
}
get samples() {
@@ -1221,8 +1237,11 @@ export class Renderer67 {
context.activeTexture(context.TEXTURE0);
context.bindTexture(context.TEXTURE_2D, this.shadowTexture);
if (allocateShadow) context.texImage2D(context.TEXTURE_2D, 0, context.DEPTH_COMPONENT24, SHADOW_MAP_SIZE, SHADOW_MAP_SIZE, 0, context.DEPTH_COMPONENT, context.UNSIGNED_INT, null);
- context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.NEAREST);
- context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.NEAREST);
+ // Compare on fetch with linear filtering gives a 2 x 2 percentage-closer result from one fetch.
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MIN_FILTER, context.LINEAR);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_MAG_FILTER, context.LINEAR);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_COMPARE_MODE, context.COMPARE_REF_TO_TEXTURE);
+ context.texParameteri(context.TEXTURE_2D, context.TEXTURE_COMPARE_FUNC, context.LEQUAL);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_S, context.CLAMP_TO_EDGE);
context.texParameteri(context.TEXTURE_2D, context.TEXTURE_WRAP_T, context.CLAMP_TO_EDGE);
if (!this.shadowFramebuffer) this.shadowFramebuffer = context.createFramebuffer();
diff --git a/src/shaders.js b/src/shaders.js
index 00ce777d..29cd22c4 100644
--- a/src/shaders.js
+++ b/src/shaders.js
@@ -409,7 +409,7 @@ uniform float fogDensity;
uniform vec3 waterRipple;
uniform float rippleTime;
uniform float explosionProgress;
-uniform sampler2D shadowMap;
+uniform highp sampler2DShadow shadowMap;
uniform float shadowBiasPerSlope;
uniform sampler2D baseMap;
uniform float hasBaseMap;
@@ -469,30 +469,17 @@ vec3 fresnel(float cosine, vec3 reflectance) {
return reflectance + (1.0 - reflectance) * pow(1.0 - cosine, 5.0);
}
-float filteredShadow(vec2 coordinates, float receiverDepth, ivec2 dimensions) {
- vec2 pixel = coordinates * vec2(dimensions) - 0.5;
- ivec2 origin = ivec2(floor(pixel));
- vec2 blend = fract(pixel);
- ivec2 maximum = dimensions - 1;
- float lowerLeft = step(receiverDepth, texelFetch(shadowMap, clamp(origin, ivec2(0), maximum), 0).r);
- float lowerRight = step(receiverDepth, texelFetch(shadowMap, clamp(origin + ivec2(1, 0), ivec2(0), maximum), 0).r);
- float upperLeft = step(receiverDepth, texelFetch(shadowMap, clamp(origin + ivec2(0, 1), ivec2(0), maximum), 0).r);
- float upperRight = step(receiverDepth, texelFetch(shadowMap, clamp(origin + ivec2(1, 1), ivec2(0), maximum), 0).r);
- return mix(mix(lowerLeft, lowerRight, blend.x), mix(upperLeft, upperRight, blend.x), blend.y);
-}
-
float shadowVisibility(vec3 normal) {
vec3 projected = shadowPosition.xyz / shadowPosition.w * 0.5 + 0.5;
if (projected.z > 1.0 || projected.z < 0.0 || any(lessThan(projected.xy, vec2(0.0))) || any(greaterThan(projected.xy, vec2(1.0)))) return 1.0;
float cosine = max(dot(normal, normalize(lightDirection)), 0.05);
float bias = shadowBiasPerSlope * clamp(sqrt(1.0 - cosine * cosine) / cosine, 0.25, 4.0);
float visibility = 0.0;
- ivec2 dimensions = textureSize(shadowMap, 0);
- vec2 texel = 1.0 / vec2(dimensions);
+ vec2 texel = 1.0 / vec2(textureSize(shadowMap, 0));
for (int row = -1; row <= 1; row++) {
for (int column = -1; column <= 1; column++) {
- float comparison = filteredShadow(projected.xy + vec2(column, row) * texel * 2.5, projected.z - bias, dimensions);
- visibility += comparison;
+ // The sampler compares in hardware and filters the four texels bilinearly: one fetch instead of four.
+ visibility += texture(shadowMap, vec3(projected.xy + vec2(column, row) * texel * 2.5, projected.z - bias));
}
}
return visibility / 9.0;