@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.
- package/core/CatmullRomLine.js +2 -0
- package/core/Cloud.js +3 -0
- package/core/CubeCamera.js +1 -0
- package/core/CubicBezierLine.js +2 -0
- package/core/Environment.js +1 -0
- package/core/Example.js +6 -0
- package/core/FaceControls.cjs.js +1 -1
- package/core/FaceControls.js +2 -0
- package/core/FaceLandmarker.cjs.js +1 -1
- package/core/FaceLandmarker.js +3 -2
- package/core/Facemesh.js +2 -0
- package/core/GizmoHelper.js +3 -0
- package/core/GizmoViewcube.js +4 -0
- package/core/GizmoViewport.js +4 -0
- package/core/Gltf.js +4 -0
- package/core/MeshDiscardMaterial.js +2 -0
- package/core/MeshDistortMaterial.js +2 -1
- package/core/MeshReflectorMaterial.js +1 -0
- package/core/MeshRefractionMaterial.js +2 -0
- package/core/MeshTransmissionMaterial.js +1 -0
- package/core/OrthographicCamera.js +1 -0
- package/core/PerspectiveCamera.js +1 -0
- package/core/QuadraticBezierLine.js +2 -0
- package/core/Reflector.js +1 -0
- package/core/SpotLight.js +2 -1
- package/core/Stage.js +7 -0
- package/core/Text3D.js +1 -0
- package/core/Wireframe.js +1 -0
- package/core/index.cjs.js +1 -1
- package/core/index.js +0 -1
- package/core/useKTX2.js +1 -0
- package/core/useMatcapTexture.js +2 -0
- package/core/useNormalTexture.js +1 -0
- package/index.cjs.js +1 -1
- package/materials/DiscardMaterial.js +1 -0
- package/materials/MeshRefractionMaterial.js +1 -1
- package/native/index.cjs.js +1 -1
- package/native/index.js +0 -1
- package/package.json +9 -9
- package/src/core/AccumulativeShadows.js +311 -0
- package/src/core/AdaptiveDpr.js +28 -0
- package/src/core/AdaptiveEvents.js +20 -0
- package/src/core/ArcballControls.js +67 -0
- package/src/core/AsciiRenderer.js +60 -0
- package/src/core/BBAnchor.js +37 -0
- package/src/core/Backdrop.js +36 -0
- package/src/core/BakeShadows.js +16 -0
- package/src/core/Billboard.js +41 -0
- package/src/core/Bounds.js +245 -0
- package/src/core/CameraControls.js +92 -0
- package/src/core/CameraShake.js +61 -0
- package/src/core/CatmullRomLine.js +44 -0
- package/src/core/Caustics.js +375 -0
- package/src/core/Center.js +66 -0
- package/src/core/Clone.js +102 -0
- package/src/core/Cloud.js +59 -0
- package/src/core/ComputedAttribute.js +34 -0
- package/src/core/ContactShadows.js +114 -0
- package/src/core/CubeCamera.js +43 -0
- package/src/core/CubicBezierLine.js +28 -0
- package/src/core/CurveModifier.js +34 -0
- package/src/core/Decal.js +92 -0
- package/src/core/Detailed.js +34 -0
- package/src/core/DeviceOrientationControls.js +51 -0
- package/src/core/Edges.js +36 -0
- package/src/core/Effects.js +93 -0
- package/src/core/Environment.js +132 -0
- package/src/core/Example.js +42 -0
- package/src/core/FaceControls.js +315 -0
- package/src/core/FaceLandmarker.js +43 -0
- package/src/core/Facemesh.js +4545 -0
- package/src/core/FirstPersonControls.js +38 -0
- package/src/core/Float.js +36 -0
- package/src/core/FlyControls.js +55 -0
- package/src/core/GizmoHelper.js +111 -0
- package/src/core/GizmoViewcube.js +152 -0
- package/src/core/GizmoViewport.js +152 -0
- package/src/core/Gltf.js +20 -0
- package/src/core/GradientTexture.js +33 -0
- package/src/core/Grid.js +112 -0
- package/src/core/Hud.js +58 -0
- package/src/core/Image.js +119 -0
- package/src/core/Instances.js +215 -0
- package/src/core/Lightformer.js +51 -0
- package/src/core/Line.js +68 -0
- package/src/core/MapControls.js +64 -0
- package/src/core/MarchingCubes.js +76 -0
- package/src/core/Mask.js +42 -0
- package/src/core/MeshDiscardMaterial.js +15 -0
- package/src/core/MeshDistortMaterial.js +79 -0
- package/src/core/MeshPortalMaterial.js +381 -0
- package/src/core/MeshReflectorMaterial.js +182 -0
- package/src/core/MeshRefractionMaterial.js +79 -0
- package/src/core/MeshTransmissionMaterial.js +395 -0
- package/src/core/MeshWobbleMaterial.js +65 -0
- package/src/core/OrbitControls.js +82 -0
- package/src/core/OrthographicCamera.js +77 -0
- package/src/core/PerformanceMonitor.js +121 -0
- package/src/core/PerspectiveCamera.js +73 -0
- package/src/core/PointMaterial.js +36 -0
- package/src/core/PointerLockControls.js +90 -0
- package/src/core/Points.js +227 -0
- package/src/core/PositionalAudio.js +51 -0
- package/src/core/Preload.js +45 -0
- package/src/core/QuadraticBezierLine.js +44 -0
- package/src/core/Reflector.js +182 -0
- package/src/core/RenderTexture.js +96 -0
- package/src/core/Resize.js +39 -0
- package/src/core/RoundedBox.js +52 -0
- package/src/core/Sampler.js +85 -0
- package/src/core/ScreenQuad.js +24 -0
- package/src/core/ScreenSpace.js +25 -0
- package/src/core/Segments.js +102 -0
- package/src/core/Shadow.js +42 -0
- package/src/core/Sky.js +39 -0
- package/src/core/Sparkles.js +139 -0
- package/src/core/SpotLight.js +231 -0
- package/src/core/SpriteAnimator.js +342 -0
- package/src/core/Stage.js +135 -0
- package/src/core/Stars.js +108 -0
- package/src/core/Stats.js +30 -0
- package/src/core/Svg.js +59 -0
- package/src/core/Text.js +58 -0
- package/src/core/Text3D.js +76 -0
- package/src/core/TrackballControls.js +72 -0
- package/src/core/Trail.js +161 -0
- package/src/core/TransformControls.js +106 -0
- package/src/core/Wireframe.js +185 -0
- package/src/core/meshBounds.js +38 -0
- package/src/core/shaderMaterial.js +39 -0
- package/src/core/shapes.js +68 -0
- package/src/core/softShadows.js +161 -0
- package/src/core/useAnimations.js +56 -0
- package/src/core/useAspect.js +10 -0
- package/src/core/useBVH.js +90 -0
- package/src/core/useBoxProjectedEnv.js +98 -0
- package/src/core/useCamera.js +27 -0
- package/src/core/useContextBridge.js +21 -0
- package/src/core/useCubeCamera.js +49 -0
- package/src/core/useCubeTexture.js +18 -0
- package/src/core/useDepthBuffer.js +38 -0
- package/src/core/useDetectGPU.js +6 -0
- package/src/core/useEnvironment.js +48 -0
- package/src/core/useFBO.js +53 -0
- package/src/core/useFBX.js +12 -0
- package/src/core/useFont.js +20 -0
- package/src/core/useGLTF.js +37 -0
- package/src/core/useHelper.js +31 -0
- package/src/core/useIntersect.js +39 -0
- package/src/core/useKTX2.js +38 -0
- package/src/core/useMatcapTexture.js +45 -0
- package/src/core/useNormalTexture.js +30 -0
- package/src/core/useProgress.js +50 -0
- package/src/core/useTexture.js +35 -0
- package/src/core/useTrailTexture.js +164 -0
- package/src/core/useVideoTexture.js +41 -0
- package/src/helpers/environment-assets.js +14 -0
- package/src/helpers/useEffectfulState.js +18 -0
- package/src/materials/BlurPass.js +75 -0
- package/src/materials/ConvolutionMaterial.js +99 -0
- package/src/materials/DiscardMaterial.js +5 -0
- package/src/materials/MeshReflectorMaterial.js +290 -0
- package/src/materials/MeshRefractionMaterial.js +171 -0
- package/src/materials/SpotLightMaterial.js +111 -0
- package/src/materials/WireframeMaterial.js +235 -0
- package/src/web/CycleRaycast.js +97 -0
- package/src/web/Html.js +417 -0
- package/src/web/KeyboardControls.js +101 -0
- package/src/web/Loader.js +98 -0
- package/src/web/PresentationControls.js +86 -0
- package/src/web/ScrollControls.js +246 -0
- package/src/web/Select.js +175 -0
- package/src/web/View.js +206 -0
- package/src/web/pivotControls/AxisArrow.js +202 -0
- package/src/web/pivotControls/AxisRotator.js +235 -0
- package/src/web/pivotControls/PlaneSlider.js +211 -0
- package/src/web/pivotControls/context.js +5 -0
- package/src/web/pivotControls/index.js +223 -0
- package/src/web/useCursor.js +12 -0
- package/web/Loader.js +2 -0
- package/web/index.cjs.js +1 -1
- package/web/index.js +0 -1
- package/web/pivotControls/AxisArrow.js +3 -0
- package/web/pivotControls/AxisRotator.js +3 -0
- package/web/pivotControls/PlaneSlider.js +3 -0
- package/web/pivotControls/index.js +5 -0
- /package/{helpers → src/helpers}/glsl/DefaultSpotlightShadowShadows.glsl.js +0 -0
- /package/{helpers → src/helpers}/glsl/distort.vert.glsl.js +0 -0
- /package/{index.js → src/index.js} +0 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import * as THREE from 'three';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { shaderMaterial } from '../core/shaderMaterial.js';
|
|
4
|
+
|
|
5
|
+
const WireframeMaterialShaders = {
|
|
6
|
+
uniforms: {
|
|
7
|
+
strokeOpacity: 1,
|
|
8
|
+
fillOpacity: 0.25,
|
|
9
|
+
fillMix: 0,
|
|
10
|
+
thickness: 0.05,
|
|
11
|
+
colorBackfaces: false,
|
|
12
|
+
dashInvert: true,
|
|
13
|
+
dash: false,
|
|
14
|
+
dashRepeats: 4,
|
|
15
|
+
dashLength: 0.5,
|
|
16
|
+
squeeze: false,
|
|
17
|
+
squeezeMin: 0.2,
|
|
18
|
+
squeezeMax: 1,
|
|
19
|
+
stroke: new THREE.Color('#ff0000'),
|
|
20
|
+
backfaceStroke: new THREE.Color('#0000ff'),
|
|
21
|
+
fill: new THREE.Color('#00ff00')
|
|
22
|
+
},
|
|
23
|
+
vertex:
|
|
24
|
+
/* glsl */
|
|
25
|
+
`
|
|
26
|
+
attribute vec3 barycentric;
|
|
27
|
+
|
|
28
|
+
varying vec3 v_edges_Barycentric;
|
|
29
|
+
varying vec3 v_edges_Position;
|
|
30
|
+
|
|
31
|
+
void initWireframe() {
|
|
32
|
+
v_edges_Barycentric = barycentric;
|
|
33
|
+
v_edges_Position = position.xyz;
|
|
34
|
+
}
|
|
35
|
+
`,
|
|
36
|
+
fragment:
|
|
37
|
+
/* glsl */
|
|
38
|
+
`
|
|
39
|
+
#ifndef PI
|
|
40
|
+
#define PI 3.1415926535897932384626433832795
|
|
41
|
+
#endif
|
|
42
|
+
|
|
43
|
+
varying vec3 v_edges_Barycentric;
|
|
44
|
+
varying vec3 v_edges_Position;
|
|
45
|
+
|
|
46
|
+
uniform float strokeOpacity;
|
|
47
|
+
uniform float fillOpacity;
|
|
48
|
+
uniform float fillMix;
|
|
49
|
+
uniform float thickness;
|
|
50
|
+
uniform bool colorBackfaces;
|
|
51
|
+
|
|
52
|
+
// Dash
|
|
53
|
+
uniform bool dashInvert;
|
|
54
|
+
uniform bool dash;
|
|
55
|
+
uniform bool dashOnly;
|
|
56
|
+
uniform float dashRepeats;
|
|
57
|
+
uniform float dashLength;
|
|
58
|
+
|
|
59
|
+
// Squeeze
|
|
60
|
+
uniform bool squeeze;
|
|
61
|
+
uniform float squeezeMin;
|
|
62
|
+
uniform float squeezeMax;
|
|
63
|
+
|
|
64
|
+
// Colors
|
|
65
|
+
uniform vec3 stroke;
|
|
66
|
+
uniform vec3 backfaceStroke;
|
|
67
|
+
uniform vec3 fill;
|
|
68
|
+
|
|
69
|
+
// This is like
|
|
70
|
+
float wireframe_aastep(float threshold, float dist) {
|
|
71
|
+
float afwidth = fwidth(dist) * 0.5;
|
|
72
|
+
return smoothstep(threshold - afwidth, threshold + afwidth, dist);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
float wireframe_map(float value, float min1, float max1, float min2, float max2) {
|
|
76
|
+
return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
float getWireframe() {
|
|
80
|
+
vec3 barycentric = v_edges_Barycentric;
|
|
81
|
+
|
|
82
|
+
// Distance from center of each triangle to its edges.
|
|
83
|
+
float d = min(min(barycentric.x, barycentric.y), barycentric.z);
|
|
84
|
+
|
|
85
|
+
// for dashed rendering, we can use this to get the 0 .. 1 value of the line length
|
|
86
|
+
float positionAlong = max(barycentric.x, barycentric.y);
|
|
87
|
+
if (barycentric.y < barycentric.x && barycentric.y < barycentric.z) {
|
|
88
|
+
positionAlong = 1.0 - positionAlong;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// the thickness of the stroke
|
|
92
|
+
float computedThickness = wireframe_map(thickness, 0.0, 1.0, 0.0, 0.34);
|
|
93
|
+
|
|
94
|
+
// if we want to shrink the thickness toward the center of the line segment
|
|
95
|
+
if (squeeze) {
|
|
96
|
+
computedThickness *= mix(squeezeMin, squeezeMax, (1.0 - sin(positionAlong * PI)));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Create dash pattern
|
|
100
|
+
if (dash) {
|
|
101
|
+
// here we offset the stroke position depending on whether it
|
|
102
|
+
// should overlap or not
|
|
103
|
+
float offset = 1.0 / dashRepeats * dashLength / 2.0;
|
|
104
|
+
if (!dashInvert) {
|
|
105
|
+
offset += 1.0 / dashRepeats / 2.0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// if we should animate the dash or not
|
|
109
|
+
// if (dashAnimate) {
|
|
110
|
+
// offset += time * 0.22;
|
|
111
|
+
// }
|
|
112
|
+
|
|
113
|
+
// create the repeating dash pattern
|
|
114
|
+
float pattern = fract((positionAlong + offset) * dashRepeats);
|
|
115
|
+
computedThickness *= 1.0 - wireframe_aastep(dashLength, pattern);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// compute the anti-aliased stroke edge
|
|
119
|
+
float edge = 1.0 - wireframe_aastep(computedThickness, d);
|
|
120
|
+
|
|
121
|
+
return edge;
|
|
122
|
+
}
|
|
123
|
+
`
|
|
124
|
+
};
|
|
125
|
+
const WireframeMaterial = shaderMaterial(WireframeMaterialShaders.uniforms, WireframeMaterialShaders.vertex +
|
|
126
|
+
/* glsl */
|
|
127
|
+
`
|
|
128
|
+
void main() {
|
|
129
|
+
initWireframe();
|
|
130
|
+
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
|
|
131
|
+
}
|
|
132
|
+
`, WireframeMaterialShaders.fragment +
|
|
133
|
+
/* glsl */
|
|
134
|
+
`
|
|
135
|
+
void main () {
|
|
136
|
+
// Compute color
|
|
137
|
+
|
|
138
|
+
float edge = getWireframe();
|
|
139
|
+
vec4 colorStroke = vec4(stroke, edge);
|
|
140
|
+
|
|
141
|
+
#ifdef FLIP_SIDED
|
|
142
|
+
colorStroke.rgb = backfaceStroke;
|
|
143
|
+
#endif
|
|
144
|
+
|
|
145
|
+
vec4 colorFill = vec4(fill, fillOpacity);
|
|
146
|
+
vec4 outColor = mix(colorFill, colorStroke, edge * strokeOpacity);
|
|
147
|
+
|
|
148
|
+
gl_FragColor = outColor;
|
|
149
|
+
}
|
|
150
|
+
`);
|
|
151
|
+
function setWireframeOverride(material, uniforms) {
|
|
152
|
+
material.onBeforeCompile = shader => {
|
|
153
|
+
shader.uniforms = { ...shader.uniforms,
|
|
154
|
+
...uniforms
|
|
155
|
+
};
|
|
156
|
+
shader.vertexShader = shader.vertexShader.replace('void main() {', `
|
|
157
|
+
${WireframeMaterialShaders.vertex}
|
|
158
|
+
void main() {
|
|
159
|
+
initWireframe();
|
|
160
|
+
`);
|
|
161
|
+
shader.fragmentShader = shader.fragmentShader.replace('void main() {', `
|
|
162
|
+
${WireframeMaterialShaders.fragment}
|
|
163
|
+
void main() {
|
|
164
|
+
`);
|
|
165
|
+
shader.fragmentShader = shader.fragmentShader.replace('#include <color_fragment>',
|
|
166
|
+
/* glsl */
|
|
167
|
+
`
|
|
168
|
+
#include <color_fragment>
|
|
169
|
+
float edge = getWireframe();
|
|
170
|
+
vec4 colorStroke = vec4(stroke, edge);
|
|
171
|
+
#ifdef FLIP_SIDED
|
|
172
|
+
colorStroke.rgb = backfaceStroke;
|
|
173
|
+
#endif
|
|
174
|
+
vec4 colorFill = vec4(mix(diffuseColor.rgb, fill, fillMix), mix(diffuseColor.a, fillOpacity, fillMix));
|
|
175
|
+
vec4 outColor = mix(colorFill, colorStroke, edge * strokeOpacity);
|
|
176
|
+
|
|
177
|
+
diffuseColor.rgb = outColor.rgb;
|
|
178
|
+
diffuseColor.a *= outColor.a;
|
|
179
|
+
`);
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
material.side = THREE.DoubleSide;
|
|
183
|
+
material.transparent = true;
|
|
184
|
+
}
|
|
185
|
+
function useWireframeUniforms(uniforms, props) {
|
|
186
|
+
React.useEffect(() => {
|
|
187
|
+
var _props$fillOpacity;
|
|
188
|
+
|
|
189
|
+
return void (uniforms.fillOpacity.value = (_props$fillOpacity = props.fillOpacity) !== null && _props$fillOpacity !== void 0 ? _props$fillOpacity : uniforms.fillOpacity.value);
|
|
190
|
+
}, [props.fillOpacity]);
|
|
191
|
+
React.useEffect(() => {
|
|
192
|
+
var _props$fillMix;
|
|
193
|
+
|
|
194
|
+
return void (uniforms.fillMix.value = (_props$fillMix = props.fillMix) !== null && _props$fillMix !== void 0 ? _props$fillMix : uniforms.fillMix.value);
|
|
195
|
+
}, [props.fillMix]);
|
|
196
|
+
React.useEffect(() => {
|
|
197
|
+
var _props$strokeOpacity;
|
|
198
|
+
|
|
199
|
+
return void (uniforms.strokeOpacity.value = (_props$strokeOpacity = props.strokeOpacity) !== null && _props$strokeOpacity !== void 0 ? _props$strokeOpacity : uniforms.strokeOpacity.value);
|
|
200
|
+
}, [props.strokeOpacity]);
|
|
201
|
+
React.useEffect(() => {
|
|
202
|
+
var _props$thickness;
|
|
203
|
+
|
|
204
|
+
return void (uniforms.thickness.value = (_props$thickness = props.thickness) !== null && _props$thickness !== void 0 ? _props$thickness : uniforms.thickness.value);
|
|
205
|
+
}, [props.thickness]);
|
|
206
|
+
React.useEffect(() => void (uniforms.colorBackfaces.value = !!props.colorBackfaces), [props.colorBackfaces]);
|
|
207
|
+
React.useEffect(() => void (uniforms.dash.value = !!props.dash), [props.dash]);
|
|
208
|
+
React.useEffect(() => void (uniforms.dashInvert.value = !!props.dashInvert), [props.dashInvert]);
|
|
209
|
+
React.useEffect(() => {
|
|
210
|
+
var _props$dashRepeats;
|
|
211
|
+
|
|
212
|
+
return void (uniforms.dashRepeats.value = (_props$dashRepeats = props.dashRepeats) !== null && _props$dashRepeats !== void 0 ? _props$dashRepeats : uniforms.dashRepeats.value);
|
|
213
|
+
}, [props.dashRepeats]);
|
|
214
|
+
React.useEffect(() => {
|
|
215
|
+
var _props$dashLength;
|
|
216
|
+
|
|
217
|
+
return void (uniforms.dashLength.value = (_props$dashLength = props.dashLength) !== null && _props$dashLength !== void 0 ? _props$dashLength : uniforms.dashLength.value);
|
|
218
|
+
}, [props.dashLength]);
|
|
219
|
+
React.useEffect(() => void (uniforms.squeeze.value = !!props.squeeze), [props.squeeze]);
|
|
220
|
+
React.useEffect(() => {
|
|
221
|
+
var _props$squeezeMin;
|
|
222
|
+
|
|
223
|
+
return void (uniforms.squeezeMin.value = (_props$squeezeMin = props.squeezeMin) !== null && _props$squeezeMin !== void 0 ? _props$squeezeMin : uniforms.squeezeMin.value);
|
|
224
|
+
}, [props.squeezeMin]);
|
|
225
|
+
React.useEffect(() => {
|
|
226
|
+
var _props$squeezeMax;
|
|
227
|
+
|
|
228
|
+
return void (uniforms.squeezeMax.value = (_props$squeezeMax = props.squeezeMax) !== null && _props$squeezeMax !== void 0 ? _props$squeezeMax : uniforms.squeezeMax.value);
|
|
229
|
+
}, [props.squeezeMax]);
|
|
230
|
+
React.useEffect(() => void (uniforms.stroke.value = props.stroke ? new THREE.Color(props.stroke) : uniforms.stroke.value), [props.stroke]);
|
|
231
|
+
React.useEffect(() => void (uniforms.fill.value = props.fill ? new THREE.Color(props.fill) : uniforms.fill.value), [props.fill]);
|
|
232
|
+
React.useEffect(() => void (uniforms.backfaceStroke.value = props.backfaceStroke ? new THREE.Color(props.backfaceStroke) : uniforms.backfaceStroke.value), [props.backfaceStroke]);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export { WireframeMaterial, WireframeMaterialShaders, setWireframeOverride, useWireframeUniforms };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { useThree } from '@react-three/fiber';
|
|
3
|
+
|
|
4
|
+
function CycleRaycast({
|
|
5
|
+
onChanged,
|
|
6
|
+
portal,
|
|
7
|
+
preventDefault = true,
|
|
8
|
+
scroll = true,
|
|
9
|
+
keyCode = 9
|
|
10
|
+
}) {
|
|
11
|
+
const cycle = React.useRef(0);
|
|
12
|
+
const setEvents = useThree(state => state.setEvents);
|
|
13
|
+
const get = useThree(state => state.get);
|
|
14
|
+
const gl = useThree(state => state.gl);
|
|
15
|
+
React.useEffect(() => {
|
|
16
|
+
var _portal$current;
|
|
17
|
+
|
|
18
|
+
let hits = [];
|
|
19
|
+
let lastEvent = undefined;
|
|
20
|
+
const prev = get().events.filter;
|
|
21
|
+
const target = (_portal$current = portal == null ? void 0 : portal.current) !== null && _portal$current !== void 0 ? _portal$current : gl.domElement.parentNode; // Render custom status
|
|
22
|
+
|
|
23
|
+
const renderStatus = () => target && onChanged && onChanged(hits, Math.round(cycle.current) % hits.length); // Overwrite the raycasters custom filter (this only exists in r3f)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
setEvents({
|
|
27
|
+
filter: (intersections, state) => {
|
|
28
|
+
// Reset cycle when the intersections change
|
|
29
|
+
let clone = [...intersections];
|
|
30
|
+
|
|
31
|
+
if (clone.length !== hits.length || !hits.every(hit => clone.map(e => e.object.uuid).includes(hit.object.uuid))) {
|
|
32
|
+
cycle.current = 0;
|
|
33
|
+
hits = clone;
|
|
34
|
+
renderStatus();
|
|
35
|
+
} // Run custom filter if there is one
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
if (prev) clone = prev(clone, state); // Cycle through the actual raycast intersects
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < Math.round(cycle.current) % clone.length; i++) {
|
|
41
|
+
const first = clone.shift();
|
|
42
|
+
clone = [...clone, first];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return clone;
|
|
46
|
+
}
|
|
47
|
+
}); // Cycle, refresh events and render status
|
|
48
|
+
|
|
49
|
+
const refresh = fn => {
|
|
50
|
+
var _get$events$handlers, _get$events$handlers2;
|
|
51
|
+
|
|
52
|
+
cycle.current = fn(cycle.current); // Cancel hovered elements and fake a pointer-move
|
|
53
|
+
|
|
54
|
+
(_get$events$handlers = get().events.handlers) == null ? void 0 : _get$events$handlers.onPointerCancel(undefined);
|
|
55
|
+
(_get$events$handlers2 = get().events.handlers) == null ? void 0 : _get$events$handlers2.onPointerMove(lastEvent);
|
|
56
|
+
renderStatus();
|
|
57
|
+
}; // Key events
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
const tabEvent = event => {
|
|
61
|
+
if (event.keyCode || event.which === keyCode) {
|
|
62
|
+
if (preventDefault) event.preventDefault();
|
|
63
|
+
if (hits.length > 1) refresh(current => current + 1);
|
|
64
|
+
}
|
|
65
|
+
}; // Wheel events
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
const wheelEvent = event => {
|
|
69
|
+
if (preventDefault) event.preventDefault();
|
|
70
|
+
let delta = 0;
|
|
71
|
+
if (!event) event = window.event;
|
|
72
|
+
if (event.wheelDelta) delta = event.wheelDelta / 120;else if (event.detail) delta = -event.detail / 3;
|
|
73
|
+
if (hits.length > 1) refresh(current => Math.abs(current - delta));
|
|
74
|
+
}; // Catch last move event and position custom status
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
const moveEvent = event => lastEvent = event;
|
|
78
|
+
|
|
79
|
+
document.addEventListener('pointermove', moveEvent, {
|
|
80
|
+
passive: true
|
|
81
|
+
});
|
|
82
|
+
if (scroll) document.addEventListener('wheel', wheelEvent);
|
|
83
|
+
if (keyCode !== undefined) document.addEventListener('keydown', tabEvent);
|
|
84
|
+
return () => {
|
|
85
|
+
// Clean up
|
|
86
|
+
setEvents({
|
|
87
|
+
filter: prev
|
|
88
|
+
});
|
|
89
|
+
if (keyCode !== undefined) document.removeEventListener('keydown', tabEvent);
|
|
90
|
+
if (scroll) document.removeEventListener('wheel', wheelEvent);
|
|
91
|
+
document.removeEventListener('pointermove', moveEvent);
|
|
92
|
+
};
|
|
93
|
+
}, [gl, get, setEvents, preventDefault, scroll, keyCode]);
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export { CycleRaycast };
|