-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreeScene.tsx
140 lines (134 loc) · 4.25 KB
/
ThreeScene.tsx
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { useCallback, useEffect, useRef } from "react";
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
const ThreeScene = ({
numLeaves,
angle,
}: {
numLeaves: number;
angle: number;
}) => {
const containerRef = useRef<HTMLDivElement>(null);
const sceneRef = useRef<THREE.Scene>(new THREE.Scene());
const drawLeaves = useCallback(() => {
if (!sceneRef.current) return;
const scene = sceneRef.current;
Array.from(Array(numLeaves + 1), (_, i) => i).forEach((i) => {
const theta = angle * i;
const radius = (7.5 / numLeaves) * i;
// line
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 10 - (10 / numLeaves) * i, 0),
new THREE.Vector3(
Math.cos(theta) * radius,
10 - (10 / numLeaves) * i,
Math.sin(theta) * radius
),
]),
new THREE.LineBasicMaterial({ color: 0x808080 })
);
line.name = "line";
scene.add(line);
// leaf
const leaf = new THREE.Mesh(
new THREE.CylinderGeometry(
0.25 * (i / numLeaves / 2 + 0.5),
0.25 * (i / numLeaves / 2 + 0.5),
0.1,
32
),
new THREE.MeshStandardMaterial({
color: "ForestGreen",
name: "leaf",
})
);
leaf.position.set(
Math.cos(theta) * radius,
10 - (10 / numLeaves) * i,
Math.sin(theta) * radius
);
leaf.name = "leaf";
scene.add(leaf);
});
}, [numLeaves, angle]);
useEffect(() => {
if (!containerRef.current) return;
// scene, camera, renderer
const scene = sceneRef.current;
const camera = new THREE.PerspectiveCamera(
75,
containerRef.current.clientWidth / containerRef.current.clientHeight,
0.1,
1000
);
camera.position.set(0, 15, 0);
camera.lookAt(0, 0, 0);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(
containerRef.current.clientWidth,
containerRef.current.clientHeight
);
containerRef.current.appendChild(renderer.domElement);
// light
const ambientLight = new THREE.AmbientLight(0xffffff, 1.0);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
const pointLight = new THREE.PointLight(0xffffff, 100.0, 0);
pointLight.position.set(0, 15, 0);
scene.add(pointLight);
// controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.enablePan = false;
controls.maxPolarAngle = Math.PI / 2.0;
// objects
const cube = new THREE.Mesh(
new THREE.CylinderGeometry(0.05, 0.05, 11, 32),
new THREE.MeshBasicMaterial({ color: "Burlywood" })
);
cube.position.set(0, 4.5, 0);
scene.add(cube);
// handle resize
const resizeObserver = new ResizeObserver(() => {
if (!containerRef.current) return;
const height = containerRef.current.clientHeight;
const width = containerRef.current.clientWidth;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
resizeObserver.observe(containerRef.current);
// animation
const animate = () => {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
};
animate();
const current = containerRef.current;
return () => {
resizeObserver.disconnect();
current.removeChild(renderer.domElement);
renderer.dispose();
sceneRef.current = new THREE.Scene();
};
}, []);
useEffect(() => {
if (!sceneRef.current) return;
// remove previous leaves
const prevs = sceneRef.current.children.filter(
(child) => child.name === "line" || child.name === "leaf"
);
prevs.forEach((child) => {
(child as THREE.Mesh).geometry.dispose();
((child as THREE.Mesh).material as THREE.Material).dispose();
});
sceneRef.current.remove(...prevs);
// draw new leaves
drawLeaves();
}, [numLeaves, angle, drawLeaves]);
return <div className="h-full bg-black" ref={containerRef} />;
};
export default ThreeScene;