Skip to content

Commit

Permalink
fix: use rgba8 as downgrade shadow texture (#1033)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfengzzz authored Sep 11, 2022
1 parent 1bc314f commit f6567e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
20 changes: 19 additions & 1 deletion packages/core/src/shaderlib/extra/shadow-map.fs.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
#ifdef OASIS_NO_DEPTH_TEXTURE
/**
* Decompose and save depth value.
*/
vec4 pack (float depth) {
// Use rgba 4 bytes with a total of 32 bits to store the z value, and the accuracy of 1 byte is 1/256.
const vec4 bitShift = vec4(1.0, 256.0, 256.0 * 256.0, 256.0 * 256.0 * 256.0);
const vec4 bitMask = vec4(1.0/256.0, 1.0/256.0, 1.0/256.0, 0.0);

vec4 rgbaDepth = fract(depth * bitShift); // Calculate the z value of each point

// Cut off the value which do not fit in 8 bits
rgbaDepth -= rgbaDepth.gbaa * bitMask;

return rgbaDepth;
}
#endif

void main() {
#ifdef OASIS_NO_DEPTH_TEXTURE
gl_FragColor = vec4(gl_FragCoord.z, 0.0, 0.0, 0.0);
gl_FragColor = pack(gl_FragCoord.z);
#else
gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
#endif
Expand Down
13 changes: 12 additions & 1 deletion packages/core/src/shaderlib/shadow/shadow_frag_share.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,18 @@
#define TEXTURE2D_SHADOW_PARAM(shadowMap) mediump sampler2DShadow shadowMap
#else
uniform sampler2D u_shadowMap;
#define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) (texture2D(textureName, coord3.xy).r < coord3.z ? 0.0 : 1.0)
#ifdef OASIS_NO_DEPTH_TEXTURE
const vec4 bitShift = vec4(1.0, 1.0/256.0, 1.0/(256.0*256.0), 1.0/(256.0*256.0*256.0));
/**
* Unpack depth value.
*/
float unpack(const in vec4 rgbaDepth) {
return dot(rgbaDepth, bitShift);
}
#define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) (unpack(texture2D(textureName, coord3.xy)) < coord3.z ? 0.0 : 1.0)
#else
#define SAMPLE_TEXTURE2D_SHADOW(textureName, coord3) (texture2D(textureName, coord3.xy).r < coord3.z ? 0.0 : 1.0)
#endif
#define TEXTURE2D_SHADOW_PARAM(shadowMap) mediump sampler2D shadowMap
#endif

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/shadow/ShadowUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class ShadowUtils {
if (supportDepthTexture) {
return TextureFormat.Depth16;
} else {
return TextureFormat.R16G16B16A16;
return TextureFormat.R8G8B8A8;
}
}

Expand Down

0 comments on commit f6567e6

Please sign in to comment.