-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransmission.js
More file actions
354 lines (332 loc) · 15 KB
/
transmission.js
File metadata and controls
354 lines (332 loc) · 15 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/** Author: @N8Programs https://github.com/N8python
* https://gist.github.com/N8python/eb42d25c7cd00d12e965ac9cba544317
* Inspired by: @ore_ukonpower and http://next.junni.co.jp
* https://github.com/junni-inc/next.junni.co.jp/blob/master/src/ts/MainScene/World/Sections/Section2/Transparents/Transparent/shaders/transparent.fs
*/
import * as THREE from "three";
export class MeshTransmissionMaterial extends THREE.MeshPhysicalMaterial {
constructor({
// Transmission material properties
samples = 6,
transmissionSampler = false,
chromaticAberration = 0.05,
anisotropicBlur = 0.1,
time = 0,
distortion = 0.0,
distortionScale = 0.5,
temporalDistortion = 0.0,
buffer = null,
// Physical material properties
transparent = true,
opacity = 0.75,
color = new THREE.Color("white"),
roughness = 0.25,
metalness = 0.1,
transmission = 1.0,
thickness = 1.0,
ior = 2.4,
clearcoat = 1.0,
clearcoatRoughness = 0.05,
envMapIntensity = 1.0,
iridescence = true,
iridescenceIOR = 1.144,
dispersion = 1.0,
side = THREE.DoubleSide,
// Maps
envMap = null,
map = null,
transmissionMap = null,
thicknessMap = null,
// Internal transmission values
_transmission = 1,
attenuationDistance = Infinity,
attenuationColor = new THREE.Color("white"),
} = {}) {
super();
// Set physical properties
Object.assign(this, {
transparent,
opacity,
color,
dispersion,
// roughness,
metalness,
transmission,
thickness,
ior,
clearcoat,
clearcoatRoughness,
envMapIntensity,
iridescence,
iridescenceIOR,
side,
});
// Set maps
if (envMap) this.envMap = envMap;
if (map) this.map = map;
this.uniforms = {
chromaticAberration: { value: chromaticAberration },
// Transmission must always be 0, unless transmissionSampler is being used
transmission: { value: transmission },
// Instead a workaround is used, see below for reasons why
_transmission: { value: _transmission },
transmissionMap: { value: transmissionMap },
// Roughness is 1 in THREE.MeshPhysicalMaterial but it makes little sense in a transmission material
roughness: { value: roughness },
thickness: { value: thickness },
thicknessMap: { value: thicknessMap },
attenuationDistance: { value: attenuationDistance },
attenuationColor: { value: attenuationColor },
anisotropicBlur: { value: anisotropicBlur },
time: { value: time },
distortion: { value: distortion },
distortionScale: { value: distortionScale },
temporalDistortion: { value: temporalDistortion },
buffer: { value: buffer },
};
// @ts-ignore
this.onBeforeCompile = (shader) => {
shader.uniforms = {
...shader.uniforms,
...this.uniforms,
};
// If the transmission sampler is active inject a flag
if (transmissionSampler) shader.defines.USE_SAMPLER = "";
// Otherwise we do use use .transmission and must therefore force USE_TRANSMISSION
// because threejs won't inject it for us
else shader.defines.USE_TRANSMISSION = "";
// Head
shader.fragmentShader =
/*glsl*/ `
uniform float chromaticAberration;
uniform float anisotropicBlur;
uniform float time;
uniform float distortion;
uniform float distortionScale;
uniform float temporalDistortion;
uniform sampler2D buffer;
vec3 random3(vec3 c) {
float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
vec3 r;
r.z = fract(512.0*j);
j *= .125;
r.x = fract(512.0*j);
j *= .125;
r.y = fract(512.0*j);
return r-0.5;
}
uint hash( uint x ) {
x += ( x << 10u );
x ^= ( x >> 6u );
x += ( x << 3u );
x ^= ( x >> 11u );
x += ( x << 15u );
return x;
}
// Compound versions of the hashing algorithm I whipped together.
uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }
uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }
uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
// Construct a float with half-open range [0:1] using low 23 bits.
// All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.
float floatConstruct( uint m ) {
const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask
const uint ieeeOne = 0x3F800000u; // 1.0 in IEEE binary32
m &= ieeeMantissa; // Keep only mantissa bits (fractional part)
m |= ieeeOne; // Add fractional part to 1.0
float f = uintBitsToFloat( m ); // Range [1:2]
return f - 1.0; // Range [0:1]
}
// Pseudo-random value in half-open range [0:1].
float randomBase( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
float randomBase( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float randomBase( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float randomBase( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
float rand(float seed) {
float result = randomBase(vec3(gl_FragCoord.xy, seed));
return result;
}
const float F3 = 0.3333333;
const float G3 = 0.1666667;
float snoise(vec3 p) {
vec3 s = floor(p + dot(p, vec3(F3)));
vec3 x = p - s + dot(s, vec3(G3));
vec3 e = step(vec3(0.0), x - x.yzx);
vec3 i1 = e*(1.0 - e.zxy);
vec3 i2 = 1.0 - e.zxy*(1.0 - e);
vec3 x1 = x - i1 + G3;
vec3 x2 = x - i2 + 2.0*G3;
vec3 x3 = x - 1.0 + 3.0*G3;
vec4 w, d;
w.x = dot(x, x);
w.y = dot(x1, x1);
w.z = dot(x2, x2);
w.w = dot(x3, x3);
w = max(0.6 - w, 0.0);
d.x = dot(random3(s), x);
d.y = dot(random3(s + i1), x1);
d.z = dot(random3(s + i2), x2);
d.w = dot(random3(s + 1.0), x3);
w *= w;
w *= w;
d *= w;
return dot(d, vec4(52.0));
}
float snoiseFractal(vec3 m) {
return 0.5333333* snoise(m)
+0.2666667* snoise(2.0*m)
+0.1333333* snoise(4.0*m)
+0.0666667* snoise(8.0*m);
}\n` + shader.fragmentShader;
// Remove transmission
shader.fragmentShader = shader.fragmentShader.replace(
"#include <transmission_pars_fragment>",
/*glsl*/ `
#ifdef USE_TRANSMISSION
// Transmission code is based on glTF-Sampler-Viewer
// https://github.com/KhronosGroup/glTF-Sample-Viewer
uniform float _transmission;
uniform float thickness;
uniform float attenuationDistance;
uniform vec3 attenuationColor;
#ifdef USE_TRANSMISSIONMAP
uniform sampler2D transmissionMap;
#endif
#ifdef USE_THICKNESSMAP
uniform sampler2D thicknessMap;
#endif
uniform vec2 transmissionSamplerSize;
uniform sampler2D transmissionSamplerMap;
uniform mat4 modelMatrix;
uniform mat4 projectionMatrix;
varying vec3 vWorldPosition;
vec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) {
// Direction of refracted light.
vec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior );
// Compute rotation-independant scaling of the model matrix.
vec3 modelScale;
modelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) );
modelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) );
modelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) );
// The thickness is specified in local space.
return normalize( refractionVector ) * thickness * modelScale;
}
float applyIorToRoughness( const in float roughness, const in float ior ) {
// Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
// an IOR of 1.5 results in the default amount of microfacet refraction.
return roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 );
}
vec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) {
float framebufferLod = log2( transmissionSamplerSize.x ) * applyIorToRoughness( roughness, ior );
#ifdef USE_SAMPLER
#ifdef texture2DLodEXT
return texture2DLodEXT(transmissionSamplerMap, fragCoord.xy, framebufferLod);
#else
return texture2D(transmissionSamplerMap, fragCoord.xy, framebufferLod);
#endif
#else
return texture2D(buffer, fragCoord.xy);
#endif
}
vec3 applyVolumeAttenuation( const in vec3 radiance, const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) {
if ( isinf( attenuationDistance ) ) {
// Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
return radiance;
} else {
// Compute light attenuation using Beer's law.
vec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance;
vec3 transmittance = exp( - attenuationCoefficient * transmissionDistance ); // Beer's law
return transmittance * radiance;
}
}
vec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor,
const in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix,
const in mat4 viewMatrix, const in mat4 projMatrix, const in float ior, const in float thickness,
const in vec3 attenuationColor, const in float attenuationDistance ) {
vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
vec3 refractedRayExit = position + transmissionRay;
// Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );
vec2 refractionCoords = ndcPos.xy / ndcPos.w;
refractionCoords += 1.0;
refractionCoords /= 2.0;
// Sample framebuffer to get pixel the refracted ray hits.
vec4 transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
vec3 attenuatedColor = applyVolumeAttenuation( transmittedLight.rgb, length( transmissionRay ), attenuationColor, attenuationDistance );
// Get the specular component.
vec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness );
return vec4( ( 1.0 - F ) * attenuatedColor * diffuseColor, transmittedLight.a );
}
#endif\n`
);
// Add refraction
shader.fragmentShader = shader.fragmentShader.replace(
"#include <transmission_fragment>",
/*glsl*/ `
// Improve the refraction to use the world pos
material.transmission = _transmission;
material.transmissionAlpha = 1.0;
material.thickness = thickness;
material.attenuationDistance = attenuationDistance;
material.attenuationColor = attenuationColor;
#ifdef USE_TRANSMISSIONMAP
material.transmission *= texture2D( transmissionMap, vUv ).r;
#endif
#ifdef USE_THICKNESSMAP
material.thickness *= texture2D( thicknessMap, vUv ).g;
#endif
vec3 pos = vWorldPosition;
float runningSeed = 0.0;
vec3 v = normalize( cameraPosition - pos );
vec3 n = inverseTransformDirection( normal, viewMatrix );
vec3 transmission = vec3(0.0);
float transmissionR, transmissionB, transmissionG;
float randomCoords = rand(runningSeed++);
float thickness_smear = thickness * max(pow(roughnessFactor, 0.33), anisotropicBlur);
vec3 distortionNormal = vec3(0.0);
vec3 temporalOffset = vec3(time, -time, -time) * temporalDistortion;
if (distortion > 0.0) {
distortionNormal = distortion * vec3(snoiseFractal(vec3((pos * distortionScale + temporalOffset))), snoiseFractal(vec3(pos.zxy * distortionScale - temporalOffset)), snoiseFractal(vec3(pos.yxz * distortionScale + temporalOffset)));
}
for (float i = 0.0; i < ${samples}.0; i ++) {
vec3 sampleNorm = normalize(n + roughnessFactor * roughnessFactor * 2.0 * normalize(vec3(rand(runningSeed++) - 0.5, rand(runningSeed++) - 0.5, rand(runningSeed++) - 0.5)) * pow(rand(runningSeed++), 0.33) + distortionNormal);
transmissionR = getIBLVolumeRefraction(
sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
pos, modelMatrix, viewMatrix, projectionMatrix, material.ior, material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
material.attenuationColor, material.attenuationDistance
).r;
transmissionG = getIBLVolumeRefraction(
sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + chromaticAberration * (i + randomCoords) / float(${samples})) , material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
material.attenuationColor, material.attenuationDistance
).g;
transmissionB = getIBLVolumeRefraction(
sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + 2.0 * chromaticAberration * (i + randomCoords) / float(${samples})), material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
material.attenuationColor, material.attenuationDistance
).b;
transmission.r += transmissionR;
transmission.g += transmissionG;
transmission.b += transmissionB;
}
transmission /= ${samples}.0;
totalDiffuse = mix( totalDiffuse, transmission.rgb, material.transmission );\n`
);
};
Object.keys(this.uniforms).forEach((name) =>
Object.defineProperty(this, name, {
get: () => this.uniforms[name].value,
set: (v) => (this.uniforms[name].value = v),
})
);
}
// Add convenience methods for updating textures
setTransmissionMap(map) {
this.uniforms.transmissionMap.value = map;
this.needsUpdate = true;
}
setThicknessMap(map) {
this.uniforms.thicknessMap.value = map;
this.needsUpdate = true;
}
}