@react-three/drei 9.74.11 → 9.74.13

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.
Files changed (189) hide show
  1. package/core/CatmullRomLine.js +2 -0
  2. package/core/Cloud.js +3 -0
  3. package/core/CubeCamera.js +1 -0
  4. package/core/CubicBezierLine.js +2 -0
  5. package/core/Environment.js +1 -0
  6. package/core/Example.js +6 -0
  7. package/core/FaceControls.cjs.js +1 -1
  8. package/core/FaceControls.js +2 -0
  9. package/core/FaceLandmarker.cjs.js +1 -1
  10. package/core/FaceLandmarker.js +3 -2
  11. package/core/Facemesh.js +2 -0
  12. package/core/GizmoHelper.js +3 -0
  13. package/core/GizmoViewcube.js +4 -0
  14. package/core/GizmoViewport.js +4 -0
  15. package/core/Gltf.js +4 -0
  16. package/core/MeshDiscardMaterial.js +2 -0
  17. package/core/MeshDistortMaterial.js +2 -1
  18. package/core/MeshReflectorMaterial.js +1 -0
  19. package/core/MeshRefractionMaterial.js +2 -0
  20. package/core/MeshTransmissionMaterial.js +1 -0
  21. package/core/OrthographicCamera.js +1 -0
  22. package/core/PerspectiveCamera.js +1 -0
  23. package/core/QuadraticBezierLine.js +2 -0
  24. package/core/Reflector.js +1 -0
  25. package/core/SpotLight.js +2 -1
  26. package/core/Stage.js +7 -0
  27. package/core/Text3D.js +1 -0
  28. package/core/Wireframe.js +1 -0
  29. package/core/index.cjs.js +1 -1
  30. package/core/index.js +0 -1
  31. package/core/useKTX2.js +1 -0
  32. package/core/useMatcapTexture.js +2 -0
  33. package/core/useNormalTexture.js +1 -0
  34. package/index.cjs.js +1 -1
  35. package/materials/DiscardMaterial.js +1 -0
  36. package/materials/MeshRefractionMaterial.js +1 -1
  37. package/native/index.cjs.js +1 -1
  38. package/native/index.js +0 -1
  39. package/package.json +9 -9
  40. package/src/core/AccumulativeShadows.js +311 -0
  41. package/src/core/AdaptiveDpr.js +28 -0
  42. package/src/core/AdaptiveEvents.js +20 -0
  43. package/src/core/ArcballControls.js +67 -0
  44. package/src/core/AsciiRenderer.js +60 -0
  45. package/src/core/BBAnchor.js +37 -0
  46. package/src/core/Backdrop.js +36 -0
  47. package/src/core/BakeShadows.js +16 -0
  48. package/src/core/Billboard.js +41 -0
  49. package/src/core/Bounds.js +245 -0
  50. package/src/core/CameraControls.js +92 -0
  51. package/src/core/CameraShake.js +61 -0
  52. package/src/core/CatmullRomLine.js +44 -0
  53. package/src/core/Caustics.js +375 -0
  54. package/src/core/Center.js +66 -0
  55. package/src/core/Clone.js +102 -0
  56. package/src/core/Cloud.js +59 -0
  57. package/src/core/ComputedAttribute.js +34 -0
  58. package/src/core/ContactShadows.js +114 -0
  59. package/src/core/CubeCamera.js +43 -0
  60. package/src/core/CubicBezierLine.js +28 -0
  61. package/src/core/CurveModifier.js +34 -0
  62. package/src/core/Decal.js +92 -0
  63. package/src/core/Detailed.js +34 -0
  64. package/src/core/DeviceOrientationControls.js +51 -0
  65. package/src/core/Edges.js +36 -0
  66. package/src/core/Effects.js +93 -0
  67. package/src/core/Environment.js +132 -0
  68. package/src/core/Example.js +42 -0
  69. package/src/core/FaceControls.js +315 -0
  70. package/src/core/FaceLandmarker.js +43 -0
  71. package/src/core/Facemesh.js +4545 -0
  72. package/src/core/FirstPersonControls.js +38 -0
  73. package/src/core/Float.js +36 -0
  74. package/src/core/FlyControls.js +55 -0
  75. package/src/core/GizmoHelper.js +111 -0
  76. package/src/core/GizmoViewcube.js +152 -0
  77. package/src/core/GizmoViewport.js +152 -0
  78. package/src/core/Gltf.js +20 -0
  79. package/src/core/GradientTexture.js +33 -0
  80. package/src/core/Grid.js +112 -0
  81. package/src/core/Hud.js +58 -0
  82. package/src/core/Image.js +119 -0
  83. package/src/core/Instances.js +215 -0
  84. package/src/core/Lightformer.js +51 -0
  85. package/src/core/Line.js +68 -0
  86. package/src/core/MapControls.js +64 -0
  87. package/src/core/MarchingCubes.js +76 -0
  88. package/src/core/Mask.js +42 -0
  89. package/src/core/MeshDiscardMaterial.js +15 -0
  90. package/src/core/MeshDistortMaterial.js +79 -0
  91. package/src/core/MeshPortalMaterial.js +381 -0
  92. package/src/core/MeshReflectorMaterial.js +182 -0
  93. package/src/core/MeshRefractionMaterial.js +79 -0
  94. package/src/core/MeshTransmissionMaterial.js +395 -0
  95. package/src/core/MeshWobbleMaterial.js +65 -0
  96. package/src/core/OrbitControls.js +82 -0
  97. package/src/core/OrthographicCamera.js +77 -0
  98. package/src/core/PerformanceMonitor.js +121 -0
  99. package/src/core/PerspectiveCamera.js +73 -0
  100. package/src/core/PointMaterial.js +36 -0
  101. package/src/core/PointerLockControls.js +90 -0
  102. package/src/core/Points.js +227 -0
  103. package/src/core/PositionalAudio.js +51 -0
  104. package/src/core/Preload.js +45 -0
  105. package/src/core/QuadraticBezierLine.js +44 -0
  106. package/src/core/Reflector.js +182 -0
  107. package/src/core/RenderTexture.js +96 -0
  108. package/src/core/Resize.js +39 -0
  109. package/src/core/RoundedBox.js +52 -0
  110. package/src/core/Sampler.js +85 -0
  111. package/src/core/ScreenQuad.js +24 -0
  112. package/src/core/ScreenSpace.js +25 -0
  113. package/src/core/Segments.js +102 -0
  114. package/src/core/Shadow.js +42 -0
  115. package/src/core/Sky.js +39 -0
  116. package/src/core/Sparkles.js +139 -0
  117. package/src/core/SpotLight.js +231 -0
  118. package/src/core/SpriteAnimator.js +342 -0
  119. package/src/core/Stage.js +135 -0
  120. package/src/core/Stars.js +108 -0
  121. package/src/core/Stats.js +30 -0
  122. package/src/core/Svg.js +59 -0
  123. package/src/core/Text.js +58 -0
  124. package/src/core/Text3D.js +76 -0
  125. package/src/core/TrackballControls.js +72 -0
  126. package/src/core/Trail.js +161 -0
  127. package/src/core/TransformControls.js +106 -0
  128. package/src/core/Wireframe.js +185 -0
  129. package/src/core/meshBounds.js +38 -0
  130. package/src/core/shaderMaterial.js +39 -0
  131. package/src/core/shapes.js +68 -0
  132. package/src/core/softShadows.js +161 -0
  133. package/src/core/useAnimations.js +56 -0
  134. package/src/core/useAspect.js +10 -0
  135. package/src/core/useBVH.js +90 -0
  136. package/src/core/useBoxProjectedEnv.js +98 -0
  137. package/src/core/useCamera.js +27 -0
  138. package/src/core/useContextBridge.js +21 -0
  139. package/src/core/useCubeCamera.js +49 -0
  140. package/src/core/useCubeTexture.js +18 -0
  141. package/src/core/useDepthBuffer.js +38 -0
  142. package/src/core/useDetectGPU.js +6 -0
  143. package/src/core/useEnvironment.js +48 -0
  144. package/src/core/useFBO.js +53 -0
  145. package/src/core/useFBX.js +12 -0
  146. package/src/core/useFont.js +20 -0
  147. package/src/core/useGLTF.js +37 -0
  148. package/src/core/useHelper.js +31 -0
  149. package/src/core/useIntersect.js +39 -0
  150. package/src/core/useKTX2.js +38 -0
  151. package/src/core/useMatcapTexture.js +45 -0
  152. package/src/core/useNormalTexture.js +30 -0
  153. package/src/core/useProgress.js +50 -0
  154. package/src/core/useTexture.js +35 -0
  155. package/src/core/useTrailTexture.js +164 -0
  156. package/src/core/useVideoTexture.js +41 -0
  157. package/src/helpers/environment-assets.js +14 -0
  158. package/src/helpers/useEffectfulState.js +18 -0
  159. package/src/materials/BlurPass.js +75 -0
  160. package/src/materials/ConvolutionMaterial.js +99 -0
  161. package/src/materials/DiscardMaterial.js +5 -0
  162. package/src/materials/MeshReflectorMaterial.js +290 -0
  163. package/src/materials/MeshRefractionMaterial.js +171 -0
  164. package/src/materials/SpotLightMaterial.js +111 -0
  165. package/src/materials/WireframeMaterial.js +235 -0
  166. package/src/web/CycleRaycast.js +97 -0
  167. package/src/web/Html.js +417 -0
  168. package/src/web/KeyboardControls.js +101 -0
  169. package/src/web/Loader.js +98 -0
  170. package/src/web/PresentationControls.js +86 -0
  171. package/src/web/ScrollControls.js +246 -0
  172. package/src/web/Select.js +175 -0
  173. package/src/web/View.js +206 -0
  174. package/src/web/pivotControls/AxisArrow.js +202 -0
  175. package/src/web/pivotControls/AxisRotator.js +235 -0
  176. package/src/web/pivotControls/PlaneSlider.js +211 -0
  177. package/src/web/pivotControls/context.js +5 -0
  178. package/src/web/pivotControls/index.js +223 -0
  179. package/src/web/useCursor.js +12 -0
  180. package/web/Loader.js +2 -0
  181. package/web/index.cjs.js +1 -1
  182. package/web/index.js +0 -1
  183. package/web/pivotControls/AxisArrow.js +3 -0
  184. package/web/pivotControls/AxisRotator.js +3 -0
  185. package/web/pivotControls/PlaneSlider.js +3 -0
  186. package/web/pivotControls/index.js +5 -0
  187. /package/{helpers → src/helpers}/glsl/DefaultSpotlightShadowShadows.glsl.js +0 -0
  188. /package/{helpers → src/helpers}/glsl/distort.vert.glsl.js +0 -0
  189. /package/{index.js → src/index.js} +0 -0
@@ -0,0 +1,375 @@
1
+ import _extends from '@babel/runtime/helpers/esm/extends';
2
+ import * as THREE from 'three';
3
+ import * as React from 'react';
4
+ import { extend, useThree, useFrame } from '@react-three/fiber';
5
+ import { useFBO } from './useFBO.js';
6
+ import { useHelper } from './useHelper.js';
7
+ import { shaderMaterial } from './shaderMaterial.js';
8
+ import { Edges } from './Edges.js';
9
+ import { FullScreenQuad } from 'three-stdlib';
10
+
11
+ function createNormalMaterial(side = THREE.FrontSide) {
12
+ const viewMatrix = {
13
+ value: new THREE.Matrix4()
14
+ };
15
+ return Object.assign(new THREE.MeshNormalMaterial({
16
+ side
17
+ }), {
18
+ viewMatrix,
19
+ onBeforeCompile: shader => {
20
+ shader.uniforms.viewMatrix = viewMatrix;
21
+ shader.fragmentShader = `vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
22
+ return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
23
+ }\n` + shader.fragmentShader.replace('#include <normal_fragment_maps>', `#include <normal_fragment_maps>
24
+ normal = inverseTransformDirection( normal, viewMatrix );\n`);
25
+ }
26
+ });
27
+ }
28
+
29
+ const CausticsProjectionMaterial = shaderMaterial({
30
+ causticsTexture: null,
31
+ causticsTextureB: null,
32
+ color: new THREE.Color(),
33
+ lightProjMatrix: new THREE.Matrix4(),
34
+ lightViewMatrix: new THREE.Matrix4()
35
+ }, `varying vec3 vWorldPosition;
36
+ void main() {
37
+ gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.);
38
+ vec4 worldPosition = modelMatrix * vec4(position, 1.);
39
+ vWorldPosition = worldPosition.xyz;
40
+ }`, `varying vec3 vWorldPosition;
41
+ uniform vec3 color;
42
+ uniform sampler2D causticsTexture;
43
+ uniform sampler2D causticsTextureB;
44
+ uniform mat4 lightProjMatrix;
45
+ uniform mat4 lightViewMatrix;
46
+ void main() {
47
+ // Apply caustics
48
+ vec4 lightSpacePos = lightProjMatrix * lightViewMatrix * vec4(vWorldPosition, 1.0);
49
+ lightSpacePos.xyz /= lightSpacePos.w;
50
+ lightSpacePos.xyz = lightSpacePos.xyz * 0.5 + 0.5;
51
+ vec3 front = texture2D(causticsTexture, lightSpacePos.xy).rgb;
52
+ vec3 back = texture2D(causticsTextureB, lightSpacePos.xy).rgb;
53
+ gl_FragColor = vec4((front + back) * color, 1.0);
54
+ #include <tonemapping_fragment>
55
+ #include <encodings_fragment>
56
+ }`);
57
+ const CausticsMaterial = shaderMaterial({
58
+ cameraMatrixWorld: new THREE.Matrix4(),
59
+ cameraProjectionMatrixInv: new THREE.Matrix4(),
60
+ normalTexture: null,
61
+ depthTexture: null,
62
+ lightDir: new THREE.Vector3(0, 1, 0),
63
+ lightPlaneNormal: new THREE.Vector3(0, 1, 0),
64
+ lightPlaneConstant: 0,
65
+ near: 0.1,
66
+ far: 100,
67
+ modelMatrix: new THREE.Matrix4(),
68
+ worldRadius: 1 / 40,
69
+ ior: 1.1,
70
+ bounces: 0,
71
+ resolution: 1024,
72
+ size: 10,
73
+ intensity: 0.5
74
+ },
75
+ /* glsl */
76
+ `
77
+ varying vec2 vUv;
78
+ void main() {
79
+ vUv = uv;
80
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
81
+ }`,
82
+ /* glsl */
83
+ `
84
+ uniform mat4 cameraMatrixWorld;
85
+ uniform mat4 cameraProjectionMatrixInv;
86
+ uniform vec3 lightDir;
87
+ uniform vec3 lightPlaneNormal;
88
+ uniform float lightPlaneConstant;
89
+ uniform float near;
90
+ uniform float far;
91
+ uniform float time;
92
+ uniform float worldRadius;
93
+ uniform float resolution;
94
+ uniform float size;
95
+ uniform float intensity;
96
+ uniform float ior;
97
+ precision highp isampler2D;
98
+ precision highp usampler2D;
99
+ uniform sampler2D normalTexture;
100
+ uniform sampler2D depthTexture;
101
+ uniform float bounces;
102
+ varying vec2 vUv;
103
+ vec3 WorldPosFromDepth(float depth, vec2 coord) {
104
+ float z = depth * 2.0 - 1.0;
105
+ vec4 clipSpacePosition = vec4(coord * 2.0 - 1.0, z, 1.0);
106
+ vec4 viewSpacePosition = cameraProjectionMatrixInv * clipSpacePosition;
107
+ // Perspective division
108
+ viewSpacePosition /= viewSpacePosition.w;
109
+ vec4 worldSpacePosition = cameraMatrixWorld * viewSpacePosition;
110
+ return worldSpacePosition.xyz;
111
+ }
112
+ float sdPlane( vec3 p, vec3 n, float h ) {
113
+ // n must be normalized
114
+ return dot(p,n) + h;
115
+ }
116
+ float planeIntersect( vec3 ro, vec3 rd, vec4 p ) {
117
+ return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz);
118
+ }
119
+ vec3 totalInternalReflection(vec3 ro, vec3 rd, vec3 pos, vec3 normal, float ior, out vec3 rayOrigin, out vec3 rayDirection) {
120
+ rayOrigin = ro;
121
+ rayDirection = rd;
122
+ rayDirection = refract(rayDirection, normal, 1.0 / ior);
123
+ rayOrigin = pos + rayDirection * 0.1;
124
+ return rayDirection;
125
+ }
126
+ void main() {
127
+ // Each sample consists of random offset in the x and y direction
128
+ float caustic = 0.0;
129
+ float causticTexelSize = (1.0 / resolution) * size * 2.0;
130
+ float texelsNeeded = worldRadius / causticTexelSize;
131
+ float sampleRadius = texelsNeeded / resolution;
132
+ float sum = 0.0;
133
+ if (texture2D(depthTexture, vUv).x == 1.0) {
134
+ gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
135
+ return;
136
+ }
137
+ vec2 offset1 = vec2(-0.5, -0.5);//vec2(rand() - 0.5, rand() - 0.5);
138
+ vec2 offset2 = vec2(-0.5, 0.5);//vec2(rand() - 0.5, rand() - 0.5);
139
+ vec2 offset3 = vec2(0.5, 0.5);//vec2(rand() - 0.5, rand() - 0.5);
140
+ vec2 offset4 = vec2(0.5, -0.5);//vec2(rand() - 0.5, rand() - 0.5);
141
+ vec2 uv1 = vUv + offset1 * sampleRadius;
142
+ vec2 uv2 = vUv + offset2 * sampleRadius;
143
+ vec2 uv3 = vUv + offset3 * sampleRadius;
144
+ vec2 uv4 = vUv + offset4 * sampleRadius;
145
+ vec3 normal1 = texture2D(normalTexture, uv1, -10.0).rgb * 2.0 - 1.0;
146
+ vec3 normal2 = texture2D(normalTexture, uv2, -10.0).rgb * 2.0 - 1.0;
147
+ vec3 normal3 = texture2D(normalTexture, uv3, -10.0).rgb * 2.0 - 1.0;
148
+ vec3 normal4 = texture2D(normalTexture, uv4, -10.0).rgb * 2.0 - 1.0;
149
+ float depth1 = texture2D(depthTexture, uv1, -10.0).x;
150
+ float depth2 = texture2D(depthTexture, uv2, -10.0).x;
151
+ float depth3 = texture2D(depthTexture, uv3, -10.0).x;
152
+ float depth4 = texture2D(depthTexture, uv4, -10.0).x;
153
+ // Sanity check the depths
154
+ if (depth1 == 1.0 || depth2 == 1.0 || depth3 == 1.0 || depth4 == 1.0) {
155
+ gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
156
+ return;
157
+ }
158
+ vec3 pos1 = WorldPosFromDepth(depth1, uv1);
159
+ vec3 pos2 = WorldPosFromDepth(depth2, uv2);
160
+ vec3 pos3 = WorldPosFromDepth(depth3, uv3);
161
+ vec3 pos4 = WorldPosFromDepth(depth4, uv4);
162
+ vec3 originPos1 = WorldPosFromDepth(0.0, uv1);
163
+ vec3 originPos2 = WorldPosFromDepth(0.0, uv2);
164
+ vec3 originPos3 = WorldPosFromDepth(0.0, uv3);
165
+ vec3 originPos4 = WorldPosFromDepth(0.0, uv4);
166
+ vec3 endPos1, endPos2, endPos3, endPos4;
167
+ vec3 endDir1, endDir2, endDir3, endDir4;
168
+ totalInternalReflection(originPos1, lightDir, pos1, normal1, ior, endPos1, endDir1);
169
+ totalInternalReflection(originPos2, lightDir, pos2, normal2, ior, endPos2, endDir2);
170
+ totalInternalReflection(originPos3, lightDir, pos3, normal3, ior, endPos3, endDir3);
171
+ totalInternalReflection(originPos4, lightDir, pos4, normal4, ior, endPos4, endDir4);
172
+ float lightPosArea = length(cross(originPos2 - originPos1, originPos3 - originPos1)) + length(cross(originPos3 - originPos1, originPos4 - originPos1));
173
+ float t1 = planeIntersect(endPos1, endDir1, vec4(lightPlaneNormal, lightPlaneConstant));
174
+ float t2 = planeIntersect(endPos2, endDir2, vec4(lightPlaneNormal, lightPlaneConstant));
175
+ float t3 = planeIntersect(endPos3, endDir3, vec4(lightPlaneNormal, lightPlaneConstant));
176
+ float t4 = planeIntersect(endPos4, endDir4, vec4(lightPlaneNormal, lightPlaneConstant));
177
+ vec3 finalPos1 = endPos1 + endDir1 * t1;
178
+ vec3 finalPos2 = endPos2 + endDir2 * t2;
179
+ vec3 finalPos3 = endPos3 + endDir3 * t3;
180
+ vec3 finalPos4 = endPos4 + endDir4 * t4;
181
+ float finalArea = length(cross(finalPos2 - finalPos1, finalPos3 - finalPos1)) + length(cross(finalPos3 - finalPos1, finalPos4 - finalPos1));
182
+ caustic += intensity * (lightPosArea / finalArea);
183
+ // Calculate the area of the triangle in light spaces
184
+ gl_FragColor = vec4(vec3(max(caustic, 0.0)), 1.0);
185
+ }`);
186
+ const NORMALPROPS = {
187
+ depth: true,
188
+ minFilter: THREE.LinearFilter,
189
+ magFilter: THREE.LinearFilter,
190
+ encoding: THREE.LinearEncoding,
191
+ type: THREE.UnsignedByteType
192
+ };
193
+ const CAUSTICPROPS = {
194
+ minFilter: THREE.LinearMipmapLinearFilter,
195
+ magFilter: THREE.LinearFilter,
196
+ encoding: THREE.LinearEncoding,
197
+ format: THREE.RGBAFormat,
198
+ type: THREE.FloatType,
199
+ generateMipmaps: true
200
+ };
201
+ const Caustics = /*#__PURE__*/React.forwardRef(({
202
+ debug,
203
+ children,
204
+ frames = 1,
205
+ ior = 1.1,
206
+ color = 'white',
207
+ causticsOnly = false,
208
+ backside = false,
209
+ backsideIOR = 1.1,
210
+ worldRadius = 0.3125,
211
+ intensity = 0.05,
212
+ resolution = 2024,
213
+ lightSource = [5, 5, 5],
214
+ ...props
215
+ }, fref) => {
216
+ extend({
217
+ CausticsProjectionMaterial
218
+ });
219
+ const ref = React.useRef(null);
220
+ const camera = React.useRef(null);
221
+ const scene = React.useRef(null);
222
+ const plane = React.useRef(null);
223
+ const gl = useThree(state => state.gl);
224
+ const helper = useHelper(debug && camera, THREE.CameraHelper); // Buffers for front and back faces
225
+
226
+ const normalTarget = useFBO(resolution, resolution, NORMALPROPS);
227
+ const normalTargetB = useFBO(resolution, resolution, NORMALPROPS);
228
+ const causticsTarget = useFBO(resolution, resolution, CAUSTICPROPS);
229
+ const causticsTargetB = useFBO(resolution, resolution, CAUSTICPROPS); // Normal materials for front and back faces
230
+
231
+ const [normalMat] = React.useState(() => createNormalMaterial());
232
+ const [normalMatB] = React.useState(() => createNormalMaterial(THREE.BackSide)); // The quad that catches the caustics
233
+
234
+ const [causticsMaterial] = React.useState(() => new CausticsMaterial());
235
+ const [causticsQuad] = React.useState(() => new FullScreenQuad(causticsMaterial));
236
+ React.useLayoutEffect(() => {
237
+ ref.current.updateWorldMatrix(false, true);
238
+ });
239
+ let count = 0;
240
+ const v = new THREE.Vector3();
241
+ const lpF = new THREE.Frustum();
242
+ const lpM = new THREE.Matrix4();
243
+ const lpP = new THREE.Plane();
244
+ const lightDir = new THREE.Vector3();
245
+ const lightDirInv = new THREE.Vector3();
246
+ const bounds = new THREE.Box3();
247
+ const focusPos = new THREE.Vector3();
248
+ useFrame((state, delta) => {
249
+ if (frames === Infinity || count++ < frames) {
250
+ var _scene$current$parent, _helper$current;
251
+
252
+ if (Array.isArray(lightSource)) lightDir.fromArray(lightSource).normalize();else lightDir.copy(ref.current.worldToLocal(lightSource.current.getWorldPosition(v)).normalize());
253
+ lightDirInv.copy(lightDir).multiplyScalar(-1);
254
+ let boundsVertices = [];
255
+ (_scene$current$parent = scene.current.parent) == null ? void 0 : _scene$current$parent.matrixWorld.identity();
256
+ bounds.setFromObject(scene.current, true);
257
+ boundsVertices.push(new THREE.Vector3(bounds.min.x, bounds.min.y, bounds.min.z));
258
+ boundsVertices.push(new THREE.Vector3(bounds.min.x, bounds.min.y, bounds.max.z));
259
+ boundsVertices.push(new THREE.Vector3(bounds.min.x, bounds.max.y, bounds.min.z));
260
+ boundsVertices.push(new THREE.Vector3(bounds.min.x, bounds.max.y, bounds.max.z));
261
+ boundsVertices.push(new THREE.Vector3(bounds.max.x, bounds.min.y, bounds.min.z));
262
+ boundsVertices.push(new THREE.Vector3(bounds.max.x, bounds.min.y, bounds.max.z));
263
+ boundsVertices.push(new THREE.Vector3(bounds.max.x, bounds.max.y, bounds.min.z));
264
+ boundsVertices.push(new THREE.Vector3(bounds.max.x, bounds.max.y, bounds.max.z));
265
+ const worldVerts = boundsVertices.map(v => v.clone());
266
+ bounds.getCenter(focusPos);
267
+ boundsVertices = boundsVertices.map(v => v.clone().sub(focusPos));
268
+ const lightPlane = lpP.set(lightDirInv, 0);
269
+ const projectedVerts = boundsVertices.map(v => lightPlane.projectPoint(v, new THREE.Vector3()));
270
+ const centralVert = projectedVerts.reduce((a, b) => a.add(b), v.set(0, 0, 0)).divideScalar(projectedVerts.length);
271
+ const radius = projectedVerts.map(v => v.distanceTo(centralVert)).reduce((a, b) => Math.max(a, b));
272
+ const dirLength = boundsVertices.map(x => x.dot(lightDir)).reduce((a, b) => Math.max(a, b)); // Shadows
273
+
274
+ camera.current.position.copy(lightDir.clone().multiplyScalar(dirLength).add(focusPos));
275
+ camera.current.lookAt(scene.current.localToWorld(focusPos.clone()));
276
+ const dirMatrix = lpM.lookAt(camera.current.position, focusPos, v.set(0, 1, 0));
277
+ camera.current.left = -radius;
278
+ camera.current.right = radius;
279
+ camera.current.top = radius;
280
+ camera.current.bottom = -radius;
281
+ const yOffset = v.set(0, radius, 0).applyMatrix4(dirMatrix);
282
+ const yTime = (camera.current.position.y + yOffset.y) / lightDir.y;
283
+ camera.current.near = 0.1;
284
+ camera.current.far = yTime;
285
+ camera.current.updateProjectionMatrix();
286
+ camera.current.updateMatrixWorld(); // Now find size of ground plane
287
+
288
+ const groundProjectedCoords = worldVerts.map(v => v.add(lightDir.clone().multiplyScalar(-v.y / lightDir.y)));
289
+ const centerPos = groundProjectedCoords.reduce((a, b) => a.add(b), v.set(0, 0, 0)).divideScalar(groundProjectedCoords.length);
290
+ const maxSize = 2 * groundProjectedCoords.map(v => Math.hypot(v.x - centerPos.x, v.z - centerPos.z)).reduce((a, b) => Math.max(a, b));
291
+ plane.current.scale.setScalar(maxSize);
292
+ plane.current.position.copy(centerPos);
293
+ if (debug) (_helper$current = helper.current) == null ? void 0 : _helper$current.update(); // Inject uniforms
294
+
295
+ normalMatB.viewMatrix.value = normalMat.viewMatrix.value = camera.current.matrixWorldInverse;
296
+ const dirLightNearPlane = lpF.setFromProjectionMatrix(lpM.multiplyMatrices(camera.current.projectionMatrix, camera.current.matrixWorldInverse)).planes[4];
297
+ causticsMaterial.cameraMatrixWorld = camera.current.matrixWorld;
298
+ causticsMaterial.cameraProjectionMatrixInv = camera.current.projectionMatrixInverse;
299
+ causticsMaterial.lightDir = lightDirInv;
300
+ causticsMaterial.lightPlaneNormal = dirLightNearPlane.normal;
301
+ causticsMaterial.lightPlaneConstant = dirLightNearPlane.constant;
302
+ causticsMaterial.near = camera.current.near;
303
+ causticsMaterial.far = camera.current.far;
304
+ causticsMaterial.resolution = resolution;
305
+ causticsMaterial.size = radius;
306
+ causticsMaterial.intensity = intensity;
307
+ causticsMaterial.worldRadius = worldRadius; // Switch the scene on
308
+
309
+ scene.current.visible = true; // Render front face normals
310
+
311
+ gl.setRenderTarget(normalTarget);
312
+ gl.clear();
313
+ scene.current.overrideMaterial = normalMat;
314
+ gl.render(scene.current, camera.current); // Render back face normals, if enabled
315
+
316
+ gl.setRenderTarget(normalTargetB);
317
+ gl.clear();
318
+
319
+ if (backside) {
320
+ scene.current.overrideMaterial = normalMatB;
321
+ gl.render(scene.current, camera.current);
322
+ } // Remove the override material
323
+
324
+
325
+ scene.current.overrideMaterial = null; // Render front face caustics
326
+
327
+ causticsMaterial.ior = ior;
328
+ plane.current.material.lightProjMatrix = camera.current.projectionMatrix;
329
+ plane.current.material.lightViewMatrix = camera.current.matrixWorldInverse;
330
+ causticsMaterial.normalTexture = normalTarget.texture;
331
+ causticsMaterial.depthTexture = normalTarget.depthTexture;
332
+ gl.setRenderTarget(causticsTarget);
333
+ gl.clear();
334
+ causticsQuad.render(gl); // Render back face caustics, if enabled
335
+
336
+ causticsMaterial.ior = backsideIOR;
337
+ causticsMaterial.normalTexture = normalTargetB.texture;
338
+ causticsMaterial.depthTexture = normalTargetB.depthTexture;
339
+ gl.setRenderTarget(causticsTargetB);
340
+ gl.clear();
341
+ if (backside) causticsQuad.render(gl); // Reset render target
342
+
343
+ gl.setRenderTarget(null); // Switch the scene off if caustics is all that's wanted
344
+
345
+ if (causticsOnly) scene.current.visible = false;
346
+ }
347
+ });
348
+ React.useImperativeHandle(fref, () => ref.current, []);
349
+ return /*#__PURE__*/React.createElement("group", _extends({
350
+ ref: ref
351
+ }, props), /*#__PURE__*/React.createElement("scene", {
352
+ ref: scene
353
+ }, /*#__PURE__*/React.createElement("orthographicCamera", {
354
+ ref: camera,
355
+ up: [0, 1, 0]
356
+ }), children), /*#__PURE__*/React.createElement("mesh", {
357
+ renderOrder: 2,
358
+ ref: plane,
359
+ "rotation-x": -Math.PI / 2
360
+ }, /*#__PURE__*/React.createElement("planeGeometry", null), /*#__PURE__*/React.createElement("causticsProjectionMaterial", {
361
+ transparent: true,
362
+ color: color,
363
+ causticsTexture: causticsTarget.texture,
364
+ causticsTextureB: causticsTargetB.texture,
365
+ blending: THREE.CustomBlending,
366
+ blendSrc: THREE.OneFactor,
367
+ blendDst: THREE.SrcAlphaFactor,
368
+ depthWrite: false
369
+ }), debug && /*#__PURE__*/React.createElement(Edges, null, /*#__PURE__*/React.createElement("lineBasicMaterial", {
370
+ color: "#ffff00",
371
+ toneMapped: false
372
+ }))));
373
+ });
374
+
375
+ export { Caustics };
@@ -0,0 +1,66 @@
1
+ import _extends from '@babel/runtime/helpers/esm/extends';
2
+ import { Box3, Vector3, Sphere } from 'three';
3
+ import * as React from 'react';
4
+
5
+ const Center = /*#__PURE__*/React.forwardRef(function Center({
6
+ children,
7
+ disable,
8
+ disableX,
9
+ disableY,
10
+ disableZ,
11
+ left,
12
+ right,
13
+ top,
14
+ bottom,
15
+ front,
16
+ back,
17
+ onCentered,
18
+ precise = true,
19
+ cacheKey = 0,
20
+ ...props
21
+ }, fRef) {
22
+ const ref = React.useRef(null);
23
+ const outer = React.useRef(null);
24
+ const inner = React.useRef(null);
25
+ React.useLayoutEffect(() => {
26
+ outer.current.matrixWorld.identity();
27
+ const box3 = new Box3().setFromObject(inner.current, precise);
28
+ const center = new Vector3();
29
+ const sphere = new Sphere();
30
+ const width = box3.max.x - box3.min.x;
31
+ const height = box3.max.y - box3.min.y;
32
+ const depth = box3.max.z - box3.min.z;
33
+ box3.getCenter(center);
34
+ box3.getBoundingSphere(sphere);
35
+ const vAlign = top ? height / 2 : bottom ? -height / 2 : 0;
36
+ const hAlign = left ? -width / 2 : right ? width / 2 : 0;
37
+ const dAlign = front ? depth / 2 : back ? -depth / 2 : 0;
38
+ outer.current.position.set(disable || disableX ? 0 : -center.x + hAlign, disable || disableY ? 0 : -center.y + vAlign, disable || disableZ ? 0 : -center.z + dAlign); // Only fire onCentered if the bounding box has changed
39
+
40
+ if (typeof onCentered !== 'undefined') {
41
+ onCentered({
42
+ parent: ref.current.parent,
43
+ container: ref.current,
44
+ width,
45
+ height,
46
+ depth,
47
+ boundingBox: box3,
48
+ boundingSphere: sphere,
49
+ center: center,
50
+ verticalAlignment: vAlign,
51
+ horizontalAlignment: hAlign,
52
+ depthAlignment: dAlign
53
+ });
54
+ }
55
+ }, [cacheKey, onCentered, top, left, front, disable, disableX, disableY, disableZ, precise, right, bottom, back]);
56
+ React.useImperativeHandle(fRef, () => ref.current, []);
57
+ return /*#__PURE__*/React.createElement("group", _extends({
58
+ ref: ref
59
+ }, props), /*#__PURE__*/React.createElement("group", {
60
+ ref: outer
61
+ }, /*#__PURE__*/React.createElement("group", {
62
+ ref: inner
63
+ }, children)));
64
+ });
65
+
66
+ export { Center };
@@ -0,0 +1,102 @@
1
+ import _extends from '@babel/runtime/helpers/esm/extends';
2
+ import * as THREE from 'three';
3
+ import * as React from 'react';
4
+ import pick from 'lodash.pick';
5
+ import { SkeletonUtils } from 'three-stdlib';
6
+
7
+ function createSpread(child, {
8
+ keys = ['near', 'far', 'color', 'distance', 'decay', 'penumbra', 'angle', 'intensity', 'skeleton', 'visible', 'castShadow', 'receiveShadow', 'morphTargetDictionary', 'morphTargetInfluences', 'name', 'geometry', 'material', 'position', 'rotation', 'scale', 'up', 'userData', 'bindMode', 'bindMatrix', 'bindMatrixInverse', 'skeleton'],
9
+ deep,
10
+ inject,
11
+ castShadow,
12
+ receiveShadow
13
+ }) {
14
+ let spread = pick(child, keys);
15
+
16
+ if (deep) {
17
+ if (spread.geometry && deep !== 'materialsOnly') spread.geometry = spread.geometry.clone();
18
+ if (spread.material && deep !== 'geometriesOnly') spread.material = spread.material.clone();
19
+ }
20
+
21
+ if (inject) {
22
+ if (typeof inject === 'function') spread = { ...spread,
23
+ children: inject(child)
24
+ };else if ( /*#__PURE__*/React.isValidElement(inject)) spread = { ...spread,
25
+ children: inject
26
+ };else spread = { ...spread,
27
+ ...inject
28
+ };
29
+ }
30
+
31
+ if (child instanceof THREE.Mesh) {
32
+ if (castShadow) spread.castShadow = true;
33
+ if (receiveShadow) spread.receiveShadow = true;
34
+ }
35
+
36
+ return spread;
37
+ }
38
+
39
+ const Clone = /*#__PURE__*/React.forwardRef(({
40
+ isChild = false,
41
+ object,
42
+ children,
43
+ deep,
44
+ castShadow,
45
+ receiveShadow,
46
+ inject,
47
+ keys,
48
+ ...props
49
+ }, forwardRef) => {
50
+ var _object;
51
+
52
+ const config = {
53
+ keys,
54
+ deep,
55
+ inject,
56
+ castShadow,
57
+ receiveShadow
58
+ };
59
+ object = React.useMemo(() => {
60
+ if (isChild === false && !Array.isArray(object)) {
61
+ let isSkinned = false;
62
+ object.traverse(object => {
63
+ if (object.isSkinnedMesh) isSkinned = true;
64
+ });
65
+ if (isSkinned) return SkeletonUtils.clone(object);
66
+ }
67
+
68
+ return object;
69
+ }, [object, isChild]); // Deal with arrayed clones
70
+
71
+ if (Array.isArray(object)) {
72
+ return /*#__PURE__*/React.createElement("group", _extends({}, props, {
73
+ ref: forwardRef
74
+ }), object.map(o => /*#__PURE__*/React.createElement(Clone, _extends({
75
+ key: o.uuid,
76
+ object: o
77
+ }, config))), children);
78
+ } // Singleton clones
79
+
80
+
81
+ const {
82
+ children: injectChildren,
83
+ ...spread
84
+ } = createSpread(object, config);
85
+ const Element = object.type[0].toLowerCase() + object.type.slice(1);
86
+ return /*#__PURE__*/React.createElement(Element, _extends({}, spread, props, {
87
+ ref: forwardRef
88
+ }), ((_object = object) == null ? void 0 : _object.children).map(child => {
89
+ if (child.type === 'Bone') return /*#__PURE__*/React.createElement("primitive", _extends({
90
+ key: child.uuid,
91
+ object: child
92
+ }, config));
93
+ return /*#__PURE__*/React.createElement(Clone, _extends({
94
+ key: child.uuid,
95
+ object: child
96
+ }, config, {
97
+ isChild: true
98
+ }));
99
+ }), children, injectChildren);
100
+ });
101
+
102
+ export { Clone };
@@ -0,0 +1,59 @@
1
+ import * as React from 'react';
2
+ import { useFrame } from '@react-three/fiber';
3
+ import { Billboard } from './Billboard.js';
4
+ import { Plane } from './shapes.js';
5
+ import { useTexture } from './useTexture.js';
6
+
7
+ const CLOUD_URL = 'https://rawcdn.githack.com/pmndrs/drei-assets/9225a9f1fbd449d9411125c2f419b843d0308c9f/cloud.png';
8
+ function Cloud({
9
+ opacity = 0.5,
10
+ speed = 0.4,
11
+ width = 10,
12
+ depth = 1.5,
13
+ segments = 20,
14
+ texture = CLOUD_URL,
15
+ color = '#ffffff',
16
+ depthTest = true,
17
+ ...props
18
+ }) {
19
+ const group = React.useRef();
20
+ const cloudTexture = useTexture(texture);
21
+ const clouds = React.useMemo(() => [...new Array(segments)].map((_, index) => ({
22
+ x: width / 2 - Math.random() * width,
23
+ y: width / 2 - Math.random() * width,
24
+ scale: 0.4 + Math.sin((index + 1) / segments * Math.PI) * ((0.2 + Math.random()) * 10),
25
+ density: Math.max(0.2, Math.random()),
26
+ rotation: Math.max(0.002, 0.005 * Math.random()) * speed
27
+ })), [width, segments, speed]);
28
+ useFrame(state => {
29
+ var _group$current;
30
+
31
+ return (_group$current = group.current) == null ? void 0 : _group$current.children.forEach((cloud, index) => {
32
+ cloud.children[0].rotation.z += clouds[index].rotation;
33
+ cloud.children[0].scale.setScalar(clouds[index].scale + (1 + Math.sin(state.clock.getElapsedTime() / 10)) / 2 * index / 10);
34
+ });
35
+ });
36
+ return /*#__PURE__*/React.createElement("group", props, /*#__PURE__*/React.createElement("group", {
37
+ position: [0, 0, segments / 2 * depth],
38
+ ref: group
39
+ }, clouds.map(({
40
+ x,
41
+ y,
42
+ scale,
43
+ density
44
+ }, index) => /*#__PURE__*/React.createElement(Billboard, {
45
+ key: index,
46
+ position: [x, y, -index * depth]
47
+ }, /*#__PURE__*/React.createElement(Plane, {
48
+ scale: scale,
49
+ rotation: [0, 0, 0]
50
+ }, /*#__PURE__*/React.createElement("meshStandardMaterial", {
51
+ map: cloudTexture,
52
+ transparent: true,
53
+ opacity: scale / 6 * density * opacity,
54
+ depthTest: depthTest,
55
+ color: color
56
+ }))))));
57
+ }
58
+
59
+ export { Cloud };
@@ -0,0 +1,34 @@
1
+ import _extends from '@babel/runtime/helpers/esm/extends';
2
+ import * as React from 'react';
3
+ import { BufferAttribute } from 'three';
4
+
5
+ /**
6
+ * Used exclusively as a child of a BufferGeometry.
7
+ * Computes the BufferAttribute by calling the `compute` function
8
+ * and attaches the attribute to the geometry.
9
+ */
10
+ const ComputedAttribute = ({
11
+ compute,
12
+ name,
13
+ ...props
14
+ }) => {
15
+ const [bufferAttribute] = React.useState(() => new BufferAttribute(new Float32Array(0), 1));
16
+ const primitive = React.useRef(null);
17
+ React.useLayoutEffect(() => {
18
+ if (primitive.current) {
19
+ var _ref;
20
+
21
+ // @ts-expect-error brittle
22
+ const parent = (_ref = primitive.current.parent) !== null && _ref !== void 0 ? _ref : primitive.current.__r3f.parent;
23
+ const attr = compute(parent);
24
+ primitive.current.copy(attr);
25
+ }
26
+ }, [compute]);
27
+ return /*#__PURE__*/React.createElement("primitive", _extends({
28
+ ref: primitive,
29
+ object: bufferAttribute,
30
+ attach: `attributes-${name}`
31
+ }, props));
32
+ };
33
+
34
+ export { ComputedAttribute };