-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.2 相机_相机漫游.jsx
288 lines (258 loc) · 9.03 KB
/
2.2 相机_相机漫游.jsx
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { useEffect } from "react";
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import * as dat from "dat.gui";
3;
import { HeartCurve } from "three/examples/jsm/curves/CurveExtras";
/*
1. lookAt的作用
2. 曲线的分解,给取现分成n个段
*/
/*
总结:
1. 曲线的拆解
2. 给曲线分了n个段
3. lookat的妙用,代码中用了camerahelper来显示小球的视椎体
*/
const Page = () => {
useEffect(() => {
const $ = {
cameraIndex: 0,
createScene() {
const canvas = document.getElementById("c");
const width = window.innerWidth;
const height = window.innerHeight;
canvas.width = width;
canvas.height = height;
this.canvas = canvas;
this.width = width;
this.height = height;
// 创建3D场景对象
const scene = new THREE.Scene();
this.scene = scene;
},
createLights() {
// 添加全局光照
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
this.scene.add(ambientLight, directionalLight);
},
createObjects() {
// 2 创建立方体的几何体
const geometry = new THREE.BoxGeometry(1, 1, 1);
console.log(geometry);
// 3 创建立方体的基础材质
const material = new THREE.MeshLambertMaterial({
color: 0x1890ff,
});
// 4 创建3d物体对象
const mesh = new THREE.Mesh(geometry, material);
mesh.geometry.computeBoundingBox();
console.log(mesh);
this.scene.add(mesh);
this.mesh = mesh;
},
createCamera() {
// 创建相机对象
const pCamera = new THREE.PerspectiveCamera(
75,
this.width / this.height,
0.1,
1000
);
pCamera.position.set(0, 0, 20); //
// pCamera.up.set(0,-1,0) //
pCamera.lookAt(this.scene.position); //
this.scene.add(pCamera);
this.pCamera = pCamera;
this.camera = pCamera;
// 创建相机对象
const watcherCamera = new THREE.PerspectiveCamera(
75,
this.width / this.height,
0.1,
1000
);
watcherCamera.position.set(0, 0, 20);
watcherCamera.lookAt(this.scene.position);
this.watcherCamera = watcherCamera;
this.scene.add(watcherCamera);
// this.camera = watcherCamera;
// 通过camera计算出视锥
const frustum = new THREE.Frustum();
this.pCamera.updateProjectionMatrix(); // 更新以保证拿到最正确的结果
frustum.setFromProjectionMatrix(
new THREE.Matrix4().multiplyMatrices(
this.pCamera.projectionMatrix,
this.pCamera.matrixWorldInverse
)
); // 得到视锥体的矩阵
const result = frustum.intersectsBox(this.mesh.geometry.boundingBox);
console.log(result); // true为相交或包含
},
curveGenerator() {
const curve = new HeartCurve(1);
const tubeGeometry = new THREE.TubeGeometry(curve, 200, 0.01, 8, true);
const material = new THREE.MeshBasicMaterial({
color: 0x00ff00,
});
const tubeMesh = new THREE.Mesh(tubeGeometry, material);
// 把曲线分割成3000段
this.points = curve.getPoints(3000);
// pi是弧度 2pi 是360度,所以pi/2是90度
tubeMesh.rotation.x = -Math.PI / 2; // 沿x轴旋转90度
this.scene.add(tubeMesh);
this.curve = curve;
const sphereGrometry = new THREE.SphereGeometry(0.5, 32, 64);
const sphereMaterial = new THREE.MeshBasicMaterial({
color: 0xffff00,
});
const sphereMesh = new THREE.Mesh(sphereGrometry, sphereMaterial);
// 这个mesh放到p相机的位置
sphereMesh.position.copy(this.pCamera.position);
this.sphereMesh = sphereMesh;
this.scene.add(sphereMesh);
},
datGui() {
const gui = new dat.GUI();
const params = {
color: 0x1890ff,
wireframe: false,
swatchCamera: () => {
this.orbitControls.dispose(); // 销毁旧的控制器
if (this.cameraIndex === 0) {
// 是第一个相机
this.camera = this.watcherCamera;
this.cameraIndex = 1;
} else {
this.camera = this.pCamera;
this.cameraIndex = 0;
}
this.orbitControls = new OrbitControls(this.camera, this.canvas);
},
};
// gui.add(this.camera.position,'x',0.1,10,0.1).name('positionX')
gui
.add(this.camera.position, "x")
.min(-10)
.max(10)
.step(0.1)
.name("positionX");
gui.add(this.camera.rotation, "x", 0.1, 10, 0.1).name("rotationX");
gui.add(this.pCamera, "near", 0.01, 10, 0.01).onChange((val) => {
console.log(val);
this.pCamera.near = val;
this.pCamera.updateProjectionMatrix(); // 更新以保证拿到最正确的结果
this.camera.updateProjectionMatrix();
// 通过camera计算出视锥
const frustum = new THREE.Frustum();
this.pCamera.updateProjectionMatrix(); // 更新以保证拿到最正确的结果
// frustum.setFromProjectionMatrix(
// new THREE.Matrix4().multiplyMatrices(
// this.pCamera.projectionMatrix,
// this.pCamera.matrixWorldInverse
// )
// ); // 得到视锥体的矩阵
// const result = frustum.intersectsBox(this.mesh.geometry.boundingBox);
// console.log(result); // true为相交或包含
});
gui.add(this.pCamera, "far", 1, 100, 1).onChange((val) => {
console.log(val);
this.pCamera.far = val;
this.pCamera.updateProjectionMatrix();
});
gui.add(this.camera, "zoom", 0.1, 10, 0.1).onChange((val) => {
console.log(val);
this.camera.zoom = val;
this.camera.updateProjectionMatrix();
});
gui.add(params, "wireframe").onChange((val) => {
this.mesh.material.wireframe = val;
});
console.log(this.camera);
gui.add(this.camera, "fov", 40, 150, 1).onChange((val) => {
this.camera.fov = val;
this.camera.updateProjectionMatrix();
});
gui.add(params, "swatchCamera").onChange((val) => {});
gui.addColor(params, "color").onChange((val) => {
console.log(val, this.mesh);
this.mesh.material.color.set(val);
});
},
helpers() {
// 创建辅助坐标系
const axesHelper = new THREE.AxesHelper();
const cameraHelper = new THREE.CameraHelper(this.pCamera);
// 创建辅助平面
this.scene.add(axesHelper, cameraHelper);
},
render() {
// 创建渲染器
const renderer = new THREE.WebGLRenderer({
canvas: this.canvas,
antialias: true,
});
// 设置渲染器屏幕像素比
renderer.setPixelRatio(window.devicePixelRatio || 1);
// 设置渲染器大小
renderer.setSize(this.width, this.height);
// 执行渲染
renderer.render(this.scene, this.camera);
this.renderer = renderer;
},
controls() {
// 创建轨道控制器
const orbitControls = new OrbitControls(this.camera, this.canvas);
orbitControls.enableDamping = true;
this.orbitControls = orbitControls;
},
count: 0, // 当前点的索引
moveCamera() {
const index = this.count % this.points.length; //
const point = this.points[index];
const nextPoint =
this.points[index + 1 >= this.points.length ? 0 : index + 1];
this.pCamera.position.set(point.x, 0, -point.y);
this.pCamera.lookAt(nextPoint.x, 0, -nextPoint.y); // 让人眼视角沿着路径观察
this.sphereMesh.position.set(point.x, 0, -point.y);
this.count++;
},
tick() {
// this.mesh.rotation.y += 0.01;
// update objects
this.orbitControls.update();
this.moveCamera();
this.renderer.render(this.scene, this.camera);
window.requestAnimationFrame(() => this.tick());
},
fitView() {
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
},
init() {
this.createScene();
this.createLights();
this.createObjects();
this.createCamera();
this.helpers();
this.render();
this.controls();
this.curveGenerator();
this.tick();
this.fitView();
this.datGui();
},
};
$.init();
}, []);
return (
<>
<canvas id="c" />;
</>
);
};
export default Page;