-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathwrapFadeMaterial.js
More file actions
238 lines (148 loc) · 4.58 KB
/
wrapFadeMaterial.js
File metadata and controls
238 lines (148 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Adjusts the provided material to support fading in and out using a bayer pattern. Providing a "previous"
import { Discard, Fn, If, output, screenCoordinate, uniform } from 'three/tsl';
// before compile can be used to chain shader adjustments. Returns the added uniforms used for fading.
const FADE_PARAMS = Symbol( 'FADE_PARAMS' );
export function wrapFadeMaterial( material, previousOnBeforeCompile ) {
// if the material has already been wrapped then return the params
if ( material[ FADE_PARAMS ] ) {
return material[ FADE_PARAMS ];
}
const params = {
fadeIn: { value: 0 },
fadeOut: { value: 0 },
fadeTexture: { value: null },
};
material[ FADE_PARAMS ] = params;
if ( material.isNodeMaterial ) {
modifyNodeMaterial( material, params );
} else {
modifyMaterial( material, params, previousOnBeforeCompile );
}
return params;
}
function modifyMaterial( material, params, previousOnBeforeCompile ) {
material.defines = {
...( material.defines || {} ),
FEATURE_FADE: 0,
};
material.onBeforeCompile = shader => {
if ( previousOnBeforeCompile ) {
previousOnBeforeCompile( shader );
}
shader.uniforms = {
...shader.uniforms,
...params,
};
shader.vertexShader = shader.vertexShader
.replace(
/void\s+main\(\)\s+{/,
value => /* glsl */`
#ifdef USE_BATCHING_FRAG
varying float vBatchId;
#endif
${ value }
#ifdef USE_BATCHING_FRAG
// add 0.5 to the value to avoid floating error that may cause flickering
vBatchId = getIndirectIndex( gl_DrawID ) + 0.5;
#endif
`
);
shader.fragmentShader = shader.fragmentShader
.replace( /void main\(/, value => /* glsl */`
#if FEATURE_FADE
// adapted from https://www.shadertoy.com/view/Mlt3z8
float bayerDither2x2( vec2 v ) {
return mod( 3.0 * v.y + 2.0 * v.x, 4.0 );
}
float bayerDither4x4( vec2 v ) {
vec2 P1 = mod( v, 2.0 );
vec2 P2 = floor( 0.5 * mod( v, 4.0 ) );
return 4.0 * bayerDither2x2( P1 ) + bayerDither2x2( P2 );
}
// the USE_BATCHING define is not available in fragment shaders
#ifdef USE_BATCHING_FRAG
// functions for reading the fade state of a given batch id
uniform sampler2D fadeTexture;
varying float vBatchId;
vec2 getFadeValues( const in float i ) {
int size = textureSize( fadeTexture, 0 ).x;
int j = int( i );
int x = j % size;
int y = j / size;
return texelFetch( fadeTexture, ivec2( x, y ), 0 ).rg;
}
#else
uniform float fadeIn;
uniform float fadeOut;
#endif
#endif
${ value }
` )
.replace( /#include <dithering_fragment>/, value => /* glsl */`
${ value }
#if FEATURE_FADE
#ifdef USE_BATCHING_FRAG
vec2 fadeValues = getFadeValues( vBatchId );
float fadeIn = fadeValues.r;
float fadeOut = fadeValues.g;
#endif
float bayerValue = bayerDither4x4( floor( mod( gl_FragCoord.xy, 4.0 ) ) );
float bayerBins = 16.0;
float dither = ( 0.5 + bayerValue ) / bayerBins;
if ( dither >= fadeIn ) {
discard;
}
if ( dither < fadeOut ) {
discard;
}
#endif
` );
};
}
// adapted from https://www.shadertoy.com/view/Mlt3z8
const bayerDither2x2 = Fn( ( [ v ] ) => {
return v.y.mul( 3 ).add( v.x.mul( 2 ) ).mod( 4 );
} ).setLayout( {
name: 'bayerDither2x2',
type: 'float',
inputs: [ { name: 'v', type: 'vec2' } ]
} );
const bayerDither4x4 = Fn( ( [ v ] ) => {
const P1 = v.mod( 2 );
const P2 = v.mod( 4 ).mul( 0.5 ).floor();
return bayerDither2x2( P1 ).mul( 4 ).add( bayerDither2x2( P2 ) );
} ).setLayout( {
name: 'bayerDither4x4',
type: 'float',
inputs: [ { name: 'v', type: 'vec2' } ]
} );
// Define shared uniforms for fadeIn/fadeOut so that "outputNode" can be cached.
const fadeIn = uniform( 0 ).onObjectUpdate( ( { material } ) => material.params.fadeIn.value );
const fadeOut = uniform( 0 ).onObjectUpdate( ( { material } ) => material.params.fadeOut.value );
const outputNode = Fn( () => {
const bayerValue = bayerDither4x4( screenCoordinate.xy.mod( 4 ).floor() );
const bayerBins = 16;
const dither = bayerValue.add( 0.5 ).div( bayerBins );
If( dither.greaterThanEqual( fadeIn ), () => {
Discard();
} );
If( dither.lessThan( fadeOut ), () => {
Discard();
} );
return output;
} )();
function modifyNodeMaterial( material, params ) {
material.params = params;
let FEATURE_FADE = 0;
material.defines = {
get FEATURE_FADE() {
return FEATURE_FADE;
},
set FEATURE_FADE( value ) {
if ( value != FEATURE_FADE ) {
FEATURE_FADE = value;
material.outputNode = value ? outputNode : null;
}
}
};
}