-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Material: Add "material.alphaHash" transparency mode (#24271)
* Material: Add .alphaHash and .alphaHashScale Material.alphaHash: Add example Clean up. Update builds Examples: Clean up alpha hash params Fix background color changes, add GammaCorrectionShader TAARenderPass: Initialize .accumulateIndex Alpha Hash: Initialize sampleLevel=0 in example Avoids flashing from TAARenderPass Clean up alpha hash example. Lint Update builds Add screenshot * Material: Move alphahash to separate shader chunks. * Material: Remove alphaHashScale * Clean up * Clean up * Clean up * Clean up
- Loading branch information
1 parent
c1c3625
commit 77ba743
Showing
20 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default /* glsl */` | ||
#ifdef USE_ALPHAHASH | ||
if ( diffuseColor.a < getAlphaHashThreshold( vPosition ) ) discard; | ||
#endif | ||
`; |
68 changes: 68 additions & 0 deletions
68
src/renderers/shaders/ShaderChunk/alphahash_pars_fragment.glsl.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
export default /* glsl */` | ||
#ifdef USE_ALPHAHASH | ||
/** | ||
* See: https://casual-effects.com/research/Wyman2017Hashed/index.html | ||
*/ | ||
const float ALPHA_HASH_SCALE = 0.05; // Derived from trials only, and may be changed. | ||
float hash2D( vec2 value ) { | ||
return fract( 1.0e4 * sin( 17.0 * value.x + 0.1 * value.y ) * ( 0.1 + abs( sin( 13.0 * value.y + value.x ) ) ) ); | ||
} | ||
float hash3D( vec3 value ) { | ||
return hash2D( vec2( hash2D( value.xy ), value.z ) ); | ||
} | ||
float getAlphaHashThreshold( vec3 position ) { | ||
// Find the discretized derivatives of our coordinates | ||
float maxDeriv = max( | ||
length( dFdx( position.xyz ) ), | ||
length( dFdy( position.xyz ) ) | ||
); | ||
float pixScale = 1.0 / ( ALPHA_HASH_SCALE * maxDeriv ); | ||
// Find two nearest log-discretized noise scales | ||
vec2 pixScales = vec2( | ||
exp2( floor( log2( pixScale ) ) ), | ||
exp2( ceil( log2( pixScale ) ) ) | ||
); | ||
// Compute alpha thresholds at our two noise scales | ||
vec2 alpha = vec2( | ||
hash3D( floor( pixScales.x * position.xyz ) ), | ||
hash3D( floor( pixScales.y * position.xyz ) ) | ||
); | ||
// Factor to interpolate lerp with | ||
float lerpFactor = fract( log2( pixScale ) ); | ||
// Interpolate alpha threshold from noise at two scales | ||
float x = ( 1.0 - lerpFactor ) * alpha.x + lerpFactor * alpha.y; | ||
// Pass into CDF to compute uniformly distrib threshold | ||
float a = min( lerpFactor, 1.0 - lerpFactor ); | ||
vec3 cases = vec3( | ||
x * x / ( 2.0 * a * ( 1.0 - a ) ), | ||
( x - 0.5 * a ) / ( 1.0 - a ), | ||
1.0 - ( ( 1.0 - x ) * ( 1.0 - x ) / ( 2.0 * a * ( 1.0 - a ) ) ) | ||
); | ||
// Find our final, uniformly distributed alpha threshold (ατ) | ||
float threshold = ( x < ( 1.0 - a ) ) | ||
? ( ( x < a ) ? cases.x : cases.y ) | ||
: cases.z; | ||
// Avoids ατ == 0. Could also do ατ =1-ατ | ||
return clamp( threshold , 1.0e-6, 1.0 ); | ||
} | ||
#endif | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
export default /* glsl */` | ||
vec3 transformed = vec3( position ); | ||
#ifdef USE_ALPHAHASH | ||
vPosition = vec3( position ); | ||
#endif | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters