-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
44 lines (38 loc) · 1.47 KB
/
utils.js
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
import * as THREE from 'three';
export const createSnowFlakesMesh = (geometry, material) => {
const count = 200;
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.castShadow = true;
mesh.receiveShadow = true;
for (let i = 0; i < count; i++) {
const radius = Math.random();
const coords = new THREE.Vector3().randomDirection().multiplyScalar(16);
coords.y = Math.abs(coords.y);
const matrix = new THREE.Matrix4();
matrix.scale(new THREE.Vector3(radius, radius, radius))
matrix.setPosition(new THREE.Vector3(...coords));
mesh.setMatrixAt(i, matrix);
}
mesh.instanceMatrix.needsUpdate = true;
mesh.userData.update = (delta) => {
for (let i = 0; i < count; i++) {
const matrix = new THREE.Matrix4();
mesh.getMatrixAt(i, matrix);
const dummy = new THREE.Object3D();
matrix.decompose(dummy.position, dummy.rotation, dummy.scale);
const shakeDirection = new THREE.Vector3().randomDirection().multiplyScalar(2 * delta);
shakeDirection.y = delta * dummy.scale.x * -2;
dummy.position.add(shakeDirection);
dummy.rotateY(Math.random() * .1);
const coords = new THREE.Vector3().randomDirection().multiplyScalar(16);
coords.y = Math.abs(coords.y);
if (dummy.position.y + dummy.scale.y / 2 < 0) {
dummy.position.set(...coords);
}
dummy.updateMatrix()
mesh.setMatrixAt(i, dummy.matrix);
}
mesh.instanceMatrix.needsUpdate = true;
}
return mesh;
}