@react-three/drei 9.51.8 → 9.51.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -23
- package/core/GizmoHelper.cjs.js +1 -1
- package/core/GizmoHelper.d.ts +2 -3
- package/core/GizmoHelper.js +17 -50
- package/core/GizmoViewcube.cjs.js +1 -1
- package/core/GizmoViewcube.js +2 -6
- package/core/GizmoViewport.cjs.js +1 -1
- package/core/GizmoViewport.js +1 -3
- package/core/Hud.cjs.js +1 -1
- package/core/Hud.js +6 -1
- package/core/MeshTransmissionMaterial.cjs.js +1 -1
- package/core/MeshTransmissionMaterial.d.ts +9 -8
- package/core/MeshTransmissionMaterial.js +245 -47
- package/index.cjs.js +1 -1
- package/package.json +1 -1
|
@@ -6,25 +6,34 @@ import { useFBO } from './useFBO.js';
|
|
|
6
6
|
|
|
7
7
|
class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
8
8
|
constructor({
|
|
9
|
-
samples =
|
|
9
|
+
samples = 6,
|
|
10
10
|
...args
|
|
11
11
|
} = {}) {
|
|
12
12
|
super(args);
|
|
13
13
|
this.uniforms = {
|
|
14
|
-
|
|
15
|
-
value: 0
|
|
14
|
+
chromaticAberration: {
|
|
15
|
+
value: 0.05
|
|
16
|
+
},
|
|
17
|
+
transmission: {
|
|
18
|
+
value: 1
|
|
19
|
+
},
|
|
20
|
+
thickness: {
|
|
21
|
+
value: 1
|
|
16
22
|
},
|
|
17
|
-
|
|
18
|
-
value: 0.
|
|
23
|
+
anisotropy: {
|
|
24
|
+
value: 0.1
|
|
19
25
|
},
|
|
20
|
-
|
|
21
|
-
value: 0
|
|
26
|
+
time: {
|
|
27
|
+
value: 0
|
|
28
|
+
},
|
|
29
|
+
distortion: {
|
|
30
|
+
value: 0.0
|
|
22
31
|
},
|
|
23
|
-
|
|
24
|
-
value:
|
|
32
|
+
distortionScale: {
|
|
33
|
+
value: 0.5
|
|
25
34
|
},
|
|
26
|
-
|
|
27
|
-
value:
|
|
35
|
+
temporalDistortion: {
|
|
36
|
+
value: 0.5
|
|
28
37
|
},
|
|
29
38
|
buffer: {
|
|
30
39
|
value: null
|
|
@@ -39,44 +48,232 @@ class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
|
39
48
|
...this.uniforms
|
|
40
49
|
}; // Head
|
|
41
50
|
|
|
42
|
-
shader.fragmentShader =
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
uniform float
|
|
46
|
-
uniform
|
|
47
|
-
uniform float
|
|
48
|
-
uniform float
|
|
51
|
+
shader.fragmentShader =
|
|
52
|
+
/*glsl*/
|
|
53
|
+
`
|
|
54
|
+
uniform float chromaticAberration;
|
|
55
|
+
uniform vec2 resolution;
|
|
56
|
+
uniform float anisotropy;
|
|
57
|
+
uniform float time;
|
|
58
|
+
uniform float distortion;
|
|
59
|
+
uniform float distortionScale;
|
|
60
|
+
uniform float temporalDistortion;
|
|
49
61
|
uniform sampler2D buffer;
|
|
50
62
|
|
|
51
63
|
vec3 sat(vec3 rgb, float adjustment) {
|
|
52
64
|
const vec3 W = vec3(0.2125, 0.7154, 0.0721);
|
|
53
65
|
vec3 intensity = vec3(dot(rgb, W));
|
|
54
66
|
return mix(intensity, rgb, adjustment);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
67
|
+
}
|
|
68
|
+
vec3 random3(vec3 c) {
|
|
69
|
+
float j = 4096.0*sin(dot(c,vec3(17.0, 59.4, 15.0)));
|
|
70
|
+
vec3 r;
|
|
71
|
+
r.z = fract(512.0*j);
|
|
72
|
+
j *= .125;
|
|
73
|
+
r.x = fract(512.0*j);
|
|
74
|
+
j *= .125;
|
|
75
|
+
r.y = fract(512.0*j);
|
|
76
|
+
return r-0.5;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
float seed = 0.0;
|
|
80
|
+
uint hash( uint x ) {
|
|
81
|
+
x += ( x << 10u );
|
|
82
|
+
x ^= ( x >> 6u );
|
|
83
|
+
x += ( x << 3u );
|
|
84
|
+
x ^= ( x >> 11u );
|
|
85
|
+
x += ( x << 15u );
|
|
86
|
+
return x;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Compound versions of the hashing algorithm I whipped together.
|
|
90
|
+
uint hash( uvec2 v ) { return hash( v.x ^ hash(v.y) ); }
|
|
91
|
+
uint hash( uvec3 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ); }
|
|
92
|
+
uint hash( uvec4 v ) { return hash( v.x ^ hash(v.y) ^ hash(v.z) ^ hash(v.w) ); }
|
|
93
|
+
|
|
94
|
+
// Construct a float with half-open range [0:1] using low 23 bits.
|
|
95
|
+
// All zeroes yields 0.0, all ones yields the next smallest representable value below 1.0.
|
|
96
|
+
float floatConstruct( uint m ) {
|
|
97
|
+
const uint ieeeMantissa = 0x007FFFFFu; // binary32 mantissa bitmask
|
|
98
|
+
const uint ieeeOne = 0x3F800000u; // 1.0 in IEEE binary32
|
|
99
|
+
|
|
100
|
+
m &= ieeeMantissa; // Keep only mantissa bits (fractional part)
|
|
101
|
+
m |= ieeeOne; // Add fractional part to 1.0
|
|
102
|
+
|
|
103
|
+
float f = uintBitsToFloat( m ); // Range [1:2]
|
|
104
|
+
return f - 1.0; // Range [0:1]
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Pseudo-random value in half-open range [0:1].
|
|
108
|
+
float random( float x ) { return floatConstruct(hash(floatBitsToUint(x))); }
|
|
109
|
+
float random( vec2 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
|
|
110
|
+
float random( vec3 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
|
|
111
|
+
float random( vec4 v ) { return floatConstruct(hash(floatBitsToUint(v))); }
|
|
112
|
+
|
|
113
|
+
float rand() {
|
|
114
|
+
float result = random(vec3(gl_FragCoord.xy, seed));
|
|
115
|
+
seed += 1.0;
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const float F3 = 0.3333333;
|
|
120
|
+
const float G3 = 0.1666667;
|
|
121
|
+
|
|
122
|
+
float snoise(vec3 p) {
|
|
123
|
+
vec3 s = floor(p + dot(p, vec3(F3)));
|
|
124
|
+
vec3 x = p - s + dot(s, vec3(G3));
|
|
125
|
+
vec3 e = step(vec3(0.0), x - x.yzx);
|
|
126
|
+
vec3 i1 = e*(1.0 - e.zxy);
|
|
127
|
+
vec3 i2 = 1.0 - e.zxy*(1.0 - e);
|
|
128
|
+
vec3 x1 = x - i1 + G3;
|
|
129
|
+
vec3 x2 = x - i2 + 2.0*G3;
|
|
130
|
+
vec3 x3 = x - 1.0 + 3.0*G3;
|
|
131
|
+
vec4 w, d;
|
|
132
|
+
w.x = dot(x, x);
|
|
133
|
+
w.y = dot(x1, x1);
|
|
134
|
+
w.z = dot(x2, x2);
|
|
135
|
+
w.w = dot(x3, x3);
|
|
136
|
+
w = max(0.6 - w, 0.0);
|
|
137
|
+
d.x = dot(random3(s), x);
|
|
138
|
+
d.y = dot(random3(s + i1), x1);
|
|
139
|
+
d.z = dot(random3(s + i2), x2);
|
|
140
|
+
d.w = dot(random3(s + 1.0), x3);
|
|
141
|
+
w *= w;
|
|
142
|
+
w *= w;
|
|
143
|
+
d *= w;
|
|
144
|
+
return dot(d, vec4(52.0));
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
float snoiseFractal(vec3 m) {
|
|
148
|
+
return 0.5333333* snoise(m)
|
|
149
|
+
+0.2666667* snoise(2.0*m)
|
|
150
|
+
+0.1333333* snoise(4.0*m)
|
|
151
|
+
+0.0666667* snoise(8.0*m);
|
|
152
|
+
}\n` + shader.fragmentShader; // Replace transmission
|
|
153
|
+
|
|
154
|
+
shader.fragmentShader = shader.fragmentShader.replace('#include <transmission_pars_fragment>',
|
|
155
|
+
/*glsl*/
|
|
156
|
+
`
|
|
157
|
+
#ifdef USE_TRANSMISSION
|
|
158
|
+
// Transmission code is based on glTF-Sampler-Viewer
|
|
159
|
+
// https://github.com/KhronosGroup/glTF-Sample-Viewer
|
|
160
|
+
uniform float transmission;
|
|
161
|
+
uniform float thickness;
|
|
162
|
+
uniform float attenuationDistance;
|
|
163
|
+
uniform vec3 attenuationColor;
|
|
164
|
+
#ifdef USE_TRANSMISSIONMAP
|
|
165
|
+
uniform sampler2D transmissionMap;
|
|
166
|
+
#endif
|
|
167
|
+
#ifdef USE_THICKNESSMAP
|
|
168
|
+
uniform sampler2D thicknessMap;
|
|
169
|
+
#endif
|
|
170
|
+
uniform vec2 transmissionSamplerSize;
|
|
171
|
+
uniform sampler2D transmissionSamplerMap;
|
|
172
|
+
uniform mat4 modelMatrix;
|
|
173
|
+
uniform mat4 projectionMatrix;
|
|
174
|
+
varying vec3 vWorldPosition;
|
|
175
|
+
vec3 getVolumeTransmissionRay( const in vec3 n, const in vec3 v, const in float thickness, const in float ior, const in mat4 modelMatrix ) {
|
|
176
|
+
// Direction of refracted light.
|
|
177
|
+
vec3 refractionVector = refract( - v, normalize( n ), 1.0 / ior );
|
|
178
|
+
// Compute rotation-independant scaling of the model matrix.
|
|
179
|
+
vec3 modelScale;
|
|
180
|
+
modelScale.x = length( vec3( modelMatrix[ 0 ].xyz ) );
|
|
181
|
+
modelScale.y = length( vec3( modelMatrix[ 1 ].xyz ) );
|
|
182
|
+
modelScale.z = length( vec3( modelMatrix[ 2 ].xyz ) );
|
|
183
|
+
// The thickness is specified in local space.
|
|
184
|
+
return normalize( refractionVector ) * thickness * modelScale;
|
|
185
|
+
}
|
|
186
|
+
float applyIorToRoughness( const in float roughness, const in float ior ) {
|
|
187
|
+
// Scale roughness with IOR so that an IOR of 1.0 results in no microfacet refraction and
|
|
188
|
+
// an IOR of 1.5 results in the default amount of microfacet refraction.
|
|
189
|
+
return roughness * clamp( ior * 2.0 - 2.0, 0.0, 1.0 );
|
|
190
|
+
}
|
|
191
|
+
vec4 getTransmissionSample( const in vec2 fragCoord, const in float roughness, const in float ior ) {
|
|
192
|
+
return texture2D(buffer, fragCoord.xy);
|
|
193
|
+
}
|
|
194
|
+
vec3 applyVolumeAttenuation( const in vec3 radiance, const in float transmissionDistance, const in vec3 attenuationColor, const in float attenuationDistance ) {
|
|
195
|
+
if ( isinf( attenuationDistance ) ) {
|
|
196
|
+
// Attenuation distance is +∞, i.e. the transmitted color is not attenuated at all.
|
|
197
|
+
return radiance;
|
|
198
|
+
} else {
|
|
199
|
+
// Compute light attenuation using Beer's law.
|
|
200
|
+
vec3 attenuationCoefficient = -log( attenuationColor ) / attenuationDistance;
|
|
201
|
+
vec3 transmittance = exp( - attenuationCoefficient * transmissionDistance ); // Beer's law
|
|
202
|
+
return transmittance * radiance;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
vec4 getIBLVolumeRefraction( const in vec3 n, const in vec3 v, const in float roughness, const in vec3 diffuseColor,
|
|
206
|
+
const in vec3 specularColor, const in float specularF90, const in vec3 position, const in mat4 modelMatrix,
|
|
207
|
+
const in mat4 viewMatrix, const in mat4 projMatrix, const in float ior, const in float thickness,
|
|
208
|
+
const in vec3 attenuationColor, const in float attenuationDistance ) {
|
|
209
|
+
vec3 transmissionRay = getVolumeTransmissionRay( n, v, thickness, ior, modelMatrix );
|
|
210
|
+
vec3 refractedRayExit = position + transmissionRay;
|
|
211
|
+
// Project refracted vector on the framebuffer, while mapping to normalized device coordinates.
|
|
212
|
+
vec4 ndcPos = projMatrix * viewMatrix * vec4( refractedRayExit, 1.0 );
|
|
213
|
+
vec2 refractionCoords = ndcPos.xy / ndcPos.w;
|
|
214
|
+
refractionCoords += 1.0;
|
|
215
|
+
refractionCoords /= 2.0;
|
|
216
|
+
// Sample framebuffer to get pixel the refracted ray hits.
|
|
217
|
+
vec4 transmittedLight = getTransmissionSample( refractionCoords, roughness, ior );
|
|
218
|
+
vec3 attenuatedColor = applyVolumeAttenuation( transmittedLight.rgb, length( transmissionRay ), attenuationColor, attenuationDistance );
|
|
219
|
+
// Get the specular component.
|
|
220
|
+
vec3 F = EnvironmentBRDF( n, v, specularColor, specularF90, roughness );
|
|
221
|
+
return vec4( ( 1.0 - F ) * attenuatedColor * diffuseColor, transmittedLight.a );
|
|
222
|
+
}
|
|
223
|
+
#endif\n`);
|
|
224
|
+
shader.fragmentShader = shader.fragmentShader.replace('#include <transmission_fragment>',
|
|
225
|
+
/*glsl*/
|
|
226
|
+
`
|
|
227
|
+
vec2 uv = gl_FragCoord.xy / resolution.xy;
|
|
228
|
+
// Improve the refraction to use the world pos
|
|
229
|
+
material.transmission = transmission;
|
|
230
|
+
material.transmissionAlpha = 1.0;
|
|
231
|
+
material.thickness = thickness;
|
|
232
|
+
material.attenuationDistance = attenuationDistance;
|
|
233
|
+
material.attenuationColor = attenuationColor;
|
|
234
|
+
#ifdef USE_TRANSMISSIONMAP
|
|
235
|
+
material.transmission *= texture2D( transmissionMap, vUv ).r;
|
|
236
|
+
#endif
|
|
237
|
+
#ifdef USE_THICKNESSMAP
|
|
238
|
+
material.thickness *= texture2D( thicknessMap, vUv ).g;
|
|
239
|
+
#endif
|
|
240
|
+
|
|
241
|
+
vec3 pos = vWorldPosition;
|
|
242
|
+
vec3 v = normalize( cameraPosition - pos );
|
|
243
|
+
vec3 n = inverseTransformDirection( normal, viewMatrix );
|
|
244
|
+
vec3 transmission = vec3(0.0);
|
|
245
|
+
float transmissionR, transmissionB, transmissionG;
|
|
246
|
+
float randomCoords = rand();
|
|
247
|
+
float thickness_smear = thickness * max(pow(roughness, 0.33), anisotropy);
|
|
248
|
+
vec3 distortionNormal = vec3(0.0);
|
|
249
|
+
vec3 temporalOffset = vec3(time, -time, -time) * temporalDistortion;
|
|
250
|
+
if (distortion > 0.0) {
|
|
251
|
+
distortionNormal = distortion * vec3(snoiseFractal(vec3((pos * distortionScale + temporalOffset))), snoiseFractal(vec3(pos.zxy * distortionScale - temporalOffset)), snoiseFractal(vec3(pos.yxz * distortionScale + temporalOffset)));
|
|
252
|
+
}
|
|
253
|
+
for (float i = 0.0; i < ${samples}.0; i ++) {
|
|
254
|
+
vec3 sampleNorm = normalize(n + roughness * roughness * 2.0 * normalize(vec3(rand() - 0.5, rand() - 0.5, rand() - 0.5)) * pow(rand(), 0.33) + distortionNormal);
|
|
255
|
+
transmissionR = getIBLVolumeRefraction(
|
|
256
|
+
sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
|
|
257
|
+
pos, modelMatrix, viewMatrix, projectionMatrix, material.ior, material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
|
|
258
|
+
material.attenuationColor, material.attenuationDistance
|
|
259
|
+
).r;
|
|
260
|
+
transmissionG = getIBLVolumeRefraction(
|
|
261
|
+
sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
|
|
262
|
+
pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + chromaticAberration * (i + randomCoords) / float(${samples})) , material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
|
|
263
|
+
material.attenuationColor, material.attenuationDistance
|
|
264
|
+
).g;
|
|
265
|
+
transmissionB = getIBLVolumeRefraction(
|
|
266
|
+
sampleNorm, v, material.roughness, material.diffuseColor, material.specularColor, material.specularF90,
|
|
267
|
+
pos, modelMatrix, viewMatrix, projectionMatrix, material.ior * (1.0 + 2.0 * chromaticAberration * (i + randomCoords) / float(${samples})), material.thickness + thickness_smear * (i + randomCoords) / float(${samples}),
|
|
268
|
+
material.attenuationColor, material.attenuationDistance
|
|
269
|
+
).b;
|
|
270
|
+
transmission.r += transmissionR;
|
|
271
|
+
transmission.g += transmissionG;
|
|
272
|
+
transmission.b += transmissionB;
|
|
76
273
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
274
|
+
transmission /= ${samples}.0;
|
|
275
|
+
material.transmissionAlpha = 1.0;
|
|
276
|
+
totalDiffuse = mix( totalDiffuse, transmission.rgb, material.transmission );\n`);
|
|
80
277
|
};
|
|
81
278
|
|
|
82
279
|
Object.keys(this.uniforms).forEach(name => Object.defineProperty(this, name, {
|
|
@@ -89,8 +286,9 @@ class MeshTransmissionMaterialImpl extends THREE.MeshPhysicalMaterial {
|
|
|
89
286
|
|
|
90
287
|
const MeshTransmissionMaterial = /*#__PURE__*/React.forwardRef(({
|
|
91
288
|
buffer,
|
|
289
|
+
distortionSpeed = 1,
|
|
92
290
|
samples = 10,
|
|
93
|
-
resolution
|
|
291
|
+
resolution,
|
|
94
292
|
background,
|
|
95
293
|
...props
|
|
96
294
|
}, fref) => {
|
|
@@ -103,15 +301,13 @@ const MeshTransmissionMaterial = /*#__PURE__*/React.forwardRef(({
|
|
|
103
301
|
viewport
|
|
104
302
|
} = useThree();
|
|
105
303
|
const fbo = useFBO(resolution);
|
|
106
|
-
const config = React.useMemo(() => ({
|
|
107
|
-
samples
|
|
108
|
-
}), [samples]);
|
|
109
304
|
let oldBg;
|
|
110
305
|
let oldVis;
|
|
111
306
|
let parent;
|
|
112
307
|
useFrame(state => {
|
|
113
308
|
if (!buffer) {
|
|
114
309
|
parent = ref.current.__r3f.parent;
|
|
310
|
+
ref.current.time = state.clock.getElapsedTime() * distortionSpeed;
|
|
115
311
|
|
|
116
312
|
if (parent) {
|
|
117
313
|
// Hide the outer groups contents
|
|
@@ -135,7 +331,9 @@ const MeshTransmissionMaterial = /*#__PURE__*/React.forwardRef(({
|
|
|
135
331
|
|
|
136
332
|
React.useImperativeHandle(fref, () => ref.current, []);
|
|
137
333
|
return /*#__PURE__*/React.createElement("meshTransmissionMaterial", _extends({
|
|
138
|
-
args: [
|
|
334
|
+
args: [React.useMemo(() => ({
|
|
335
|
+
samples
|
|
336
|
+
}), [samples])],
|
|
139
337
|
ref: ref,
|
|
140
338
|
buffer: buffer || fbo.texture,
|
|
141
339
|
resolution: [size.width * viewport.dpr, size.height * viewport.dpr]
|