Skip to content

Commit 54dd9cc

Browse files
committedAug 8, 2024·
feat(): clean particle cloud
1 parent 4c231db commit 54dd9cc

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed
 

‎src/components/3D/ParticleCloud.js

+9-19
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import * as THREE from 'three';
1010

1111
const defaultInteractionRadius = 1.5;
1212

13-
// Define the shader material
1413
const BlobShaderMaterial = shaderMaterial(
1514
{
1615
u_time: 0,
@@ -21,6 +20,7 @@ const BlobShaderMaterial = shaderMaterial(
2120
u_intensity: 2,
2221
u_interactionPosition: new THREE.Vector3(0, 0, 0),
2322
u_interactionRadius: defaultInteractionRadius,
23+
u_gravity: 0.1,
2424
},
2525
vertexShader,
2626
fragmentShader,
@@ -62,9 +62,9 @@ const ParticleCloud = () => {
6262
const cylinderRef = useRef();
6363
const planeMeshRef = useRef();
6464
const radiusRef = useRef(defaultInteractionRadius);
65-
const prevCameraPosition = useRef(new Vector3()); // To store the previous camera position
66-
const planeRef = useRef(new Plane()); // To store the plane reference
67-
const planeNormal = useRef(new Vector3()); // To store the previous plane normal
65+
const prevCameraPosition = useRef(new Vector3());
66+
const planeRef = useRef(new Plane());
67+
const planeNormal = useRef(new Vector3());
6868

6969
useEffect(() => {
7070
let gui;
@@ -109,17 +109,12 @@ const ParticleCloud = () => {
109109
}
110110
});
111111

112-
// Add a GUI slider to adjust the cylinder and touch radius
113112
const cylinderFolder = gui.addFolder('Interaction');
114113
cylinderFolder.add(params, 'radius', 0.1, 10).onChange((value) => {
115-
radiusRef.current = value; // Update the stored radius
116-
114+
radiusRef.current = value;
117115
if (cylinderRef.current) {
118-
// Update cylinder geometry with new radius
119-
const length = cylinderRef.current.geometry.parameters.height; // Preserve the existing length
116+
const length = cylinderRef.current.geometry.parameters.height;
120117
cylinderRef.current.geometry = new THREE.CylinderGeometry(value, value, length, 16);
121-
122-
// Update shader uniform with new radius
123118
if (materialRef.current) {
124119
materialRef.current.uniforms.u_interactionRadius.value = value;
125120
}
@@ -149,15 +144,9 @@ const ParticleCloud = () => {
149144
}
150145

151146
const currentCameraPosition = camera.position.clone();
152-
153-
// Check if the camera position has changed
154147
if (!prevCameraPosition.current.equals(currentCameraPosition)) {
155148
console.log('Camera position changed');
156-
157-
// Update previous camera position
158149
prevCameraPosition.current.copy(currentCameraPosition);
159-
160-
// Update the plane normal and plane reference when the camera position changes
161150
planeNormal.current.copy(camera.getWorldDirection(new Vector3()));
162151
planeRef.current.normal.copy(planeNormal.current);
163152
planeRef.current.constant = 0;
@@ -182,8 +171,9 @@ const ParticleCloud = () => {
182171
cylinderRef.current.rotation.copy(rotation);
183172
cylinderRef.current.geometry = new THREE.CylinderGeometry(radiusRef.current, radiusRef.current, length, 16);
184173

185-
// Update the shader uniforms for touch position and radius
186-
materialRef.current.uniforms.u_interactionPosition.value.copy(cylinderRef.current.position);
174+
if (materialRef.current) {
175+
materialRef.current.uniforms.u_interactionPosition.value.copy(cylinderRef.current.position);
176+
}
187177
}
188178
}
189179
}

0 commit comments

Comments
 (0)
Please sign in to comment.