@@ -24,41 +24,47 @@ const getAlphaHashThreshold = /*@__PURE__*/ Fn( ( [ position ] ) => {
24
24
const maxDeriv = max (
25
25
length ( dFdx ( position . xyz ) ) ,
26
26
length ( dFdy ( position . xyz ) )
27
- ) . toVar ( 'maxDeriv' ) ;
27
+ ) ;
28
28
29
29
const pixScale = float ( 1 ) . div ( float ( ALPHA_HASH_SCALE ) . mul ( maxDeriv ) ) . toVar ( 'pixScale' ) ;
30
30
31
31
// Find two nearest log-discretized noise scales
32
32
const pixScales = vec2 (
33
33
exp2 ( floor ( log2 ( pixScale ) ) ) ,
34
34
exp2 ( ceil ( log2 ( pixScale ) ) )
35
- ) . toVar ( 'pixScales' ) ;
35
+ ) ;
36
36
37
37
// Compute alpha thresholds at our two noise scales
38
38
const alpha = vec2 (
39
39
hash3D ( floor ( pixScales . x . mul ( position . xyz ) ) ) ,
40
40
hash3D ( floor ( pixScales . y . mul ( position . xyz ) ) ) ,
41
- ) . toVar ( 'alpha' ) ;
41
+ ) ;
42
42
43
43
// Factor to interpolate lerp with
44
- const lerpFactor = fract ( log2 ( pixScale ) ) . toVar ( 'lerpFactor' ) ;
44
+ const lerpFactor = fract ( log2 ( pixScale ) ) ;
45
45
46
46
// Interpolate alpha threshold from noise at two scales
47
- const x = add ( mul ( lerpFactor . oneMinus ( ) , alpha . x ) , mul ( lerpFactor , alpha . y ) ) . toVar ( 'x' ) ;
47
+ const x = add ( mul ( lerpFactor . oneMinus ( ) , alpha . x ) , mul ( lerpFactor , alpha . y ) ) ;
48
48
49
49
// Pass into CDF to compute uniformly distrib threshold
50
- const a = min ( lerpFactor , lerpFactor . oneMinus ( ) ) . toVar ( 'a' ) ;
50
+ const a = min ( lerpFactor , lerpFactor . oneMinus ( ) ) ;
51
51
const cases = vec3 (
52
52
x . mul ( x ) . div ( mul ( 2.0 , a ) . mul ( sub ( 1.0 , a ) ) ) ,
53
53
x . sub ( mul ( 0.5 , a ) ) . div ( sub ( 1.0 , a ) ) ,
54
- sub ( 1.0 , sub ( 1.0 , x ) . mul ( sub ( 1.0 , x ) ) . div ( mul ( 2.0 , a ) . mul ( sub ( 1.0 , a ) ) ) ) ) . toVar ( 'cases' ) ;
54
+ sub ( 1.0 , sub ( 1.0 , x ) . mul ( sub ( 1.0 , x ) ) . div ( mul ( 2.0 , a ) . mul ( sub ( 1.0 , a ) ) ) ) ) ;
55
55
56
56
// Find our final, uniformly distributed alpha threshold (ατ)
57
57
const threshold = x . lessThan ( a . oneMinus ( ) ) . select ( x . lessThan ( a ) . select ( cases . x , cases . y ) , cases . z ) ;
58
58
59
59
// Avoids ατ == 0. Could also do ατ =1-ατ
60
60
return clamp ( threshold , 1.0e-6 , 1.0 ) ;
61
61
62
+ } ) . setLayout ( {
63
+ name : 'getAlphaHashThreshold' ,
64
+ type : 'float' ,
65
+ inputs : [
66
+ { name : 'position' , type : 'vec3' }
67
+ ]
62
68
} ) ;
63
69
64
70
export default getAlphaHashThreshold ;
0 commit comments