three-base 2.0.31 → 2.0.33
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/MeshBase.ts +2 -2
- package/ThreeBase.d.ts +6 -2
- package/ThreeBase.js +86 -28
- package/ThreeBase.ts +91 -30
- package/package.json +1 -1
package/MeshBase.ts
CHANGED
|
@@ -82,7 +82,7 @@ export default class MeshBase extends Group {
|
|
|
82
82
|
if (MeshBase.models.get(url)) {
|
|
83
83
|
|
|
84
84
|
this.clear();
|
|
85
|
-
const mesh = MeshBase.models.get(url)
|
|
85
|
+
const mesh = MeshBase.models.get(url)!;
|
|
86
86
|
// 判断是否有动画
|
|
87
87
|
this.hasAnimation = mesh.animations.length > 0;
|
|
88
88
|
|
|
@@ -92,7 +92,7 @@ export default class MeshBase extends Group {
|
|
|
92
92
|
|
|
93
93
|
this.animationActions = {};
|
|
94
94
|
mesh.animations.map(animation => {
|
|
95
|
-
const animationAction = this.mixer
|
|
95
|
+
const animationAction = this.mixer!.clipAction(animation).play();
|
|
96
96
|
animationAction.weight = 0;
|
|
97
97
|
this.animationActions[animation.name] = animationAction;
|
|
98
98
|
});
|
package/ThreeBase.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AxesHelper, BufferGeometry,
|
|
1
|
+
import { AxesHelper, BufferGeometry, DepthTexture, EventDispatcher, GridHelper, Object3D, OrthographicCamera, PerspectiveCamera, Scene, Texture, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
2
2
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
3
3
|
import ThreeMouse from "./ThreeMouse";
|
|
4
4
|
import Transition from "./Transition";
|
|
@@ -12,7 +12,11 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
12
12
|
tempVector3: Vector3;
|
|
13
13
|
size: Vector2;
|
|
14
14
|
renderer: WebGLRenderer;
|
|
15
|
-
|
|
15
|
+
perspectiveCamera: PerspectiveCamera;
|
|
16
|
+
orthographicCamera: OrthographicCamera;
|
|
17
|
+
cameraType: "perspectiveCamera" | "orthographicCamera";
|
|
18
|
+
setCameraType(cameraType: "perspectiveCamera" | "orthographicCamera"): void;
|
|
19
|
+
getCamera(): PerspectiveCamera | OrthographicCamera;
|
|
16
20
|
scene: Scene;
|
|
17
21
|
threeMouse: ThreeMouse;
|
|
18
22
|
controls: OrbitControls;
|
package/ThreeBase.js
CHANGED
|
@@ -5,17 +5,17 @@
|
|
|
5
5
|
* @Last Modified time: 2024-01-20 05:33:28
|
|
6
6
|
*/
|
|
7
7
|
import { round } from "lodash";
|
|
8
|
-
import { AxesHelper, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, LinearSRGBColorSpace,
|
|
8
|
+
import { AxesHelper, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, LinearSRGBColorSpace, MOUSE, Mesh, NoBlending, NormalBlending, OrthographicCamera, PerspectiveCamera, RGBAFormat, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
9
9
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
10
|
-
import { generateUUID } from "three/src/math/MathUtils";
|
|
10
|
+
import { degToRad, generateUUID } from "three/src/math/MathUtils";
|
|
11
|
+
import ThreeMouse from "./ThreeMouse";
|
|
12
|
+
import Transition from "./Transition";
|
|
11
13
|
import glow_pass from "./pass/glow_pass";
|
|
12
14
|
import stroke_pass from "./pass/stroke_pass";
|
|
13
15
|
import { clearShader, coptyShader, fullScreenMesh, gaussBlurShader, strokeShader } from "./shaders";
|
|
16
|
+
import FXAAShaderMaterial from "./shaders/FXAAShaderMaterial";
|
|
14
17
|
import threeLoader from "./threeLoader";
|
|
15
|
-
import ThreeMouse from "./ThreeMouse";
|
|
16
18
|
import threeUtils from "./threeUtils";
|
|
17
|
-
import Transition from "./Transition";
|
|
18
|
-
import FXAAShaderMaterial from "./shaders/FXAAShaderMaterial";
|
|
19
19
|
// three.js 的基础对象
|
|
20
20
|
export default class ThreeBase extends EventDispatcher {
|
|
21
21
|
constructor() {
|
|
@@ -72,14 +72,39 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
72
72
|
return renderer;
|
|
73
73
|
})();
|
|
74
74
|
//透视相机
|
|
75
|
-
|
|
75
|
+
perspectiveCamera = new PerspectiveCamera(45, 1, 0.0001, 1000000);
|
|
76
|
+
//正交相机
|
|
77
|
+
orthographicCamera = new OrthographicCamera(-1, 1, 1, -1, 0.0001, 1000000);
|
|
78
|
+
//相机的类型
|
|
79
|
+
cameraType = "perspectiveCamera";
|
|
80
|
+
setCameraType(cameraType) {
|
|
81
|
+
if (this.cameraType !== cameraType) {
|
|
82
|
+
this.cameraType = cameraType;
|
|
83
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
84
|
+
this.threeMouse._camera = this.perspectiveCamera;
|
|
85
|
+
this.controls.object = this.perspectiveCamera;
|
|
86
|
+
}
|
|
87
|
+
else if (this.cameraType === "orthographicCamera") {
|
|
88
|
+
this.threeMouse._camera = this.orthographicCamera;
|
|
89
|
+
this.controls.object = this.orthographicCamera;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
//获取当前使用的的相机
|
|
94
|
+
getCamera() {
|
|
95
|
+
//判断需要使用那个相机
|
|
96
|
+
const camera = ((this.cameraType === "perspectiveCamera") ?
|
|
97
|
+
this.perspectiveCamera :
|
|
98
|
+
this.orthographicCamera);
|
|
99
|
+
return camera;
|
|
100
|
+
}
|
|
76
101
|
// 场景对象
|
|
77
102
|
scene = new Scene();
|
|
78
103
|
///鼠标事件对象
|
|
79
|
-
threeMouse = new ThreeMouse(this.renderer, this.
|
|
104
|
+
threeMouse = new ThreeMouse(this.renderer, this.perspectiveCamera, this.scene);
|
|
80
105
|
// 轨道控制器
|
|
81
106
|
controls = (() => {
|
|
82
|
-
const controls = new OrbitControls(this.
|
|
107
|
+
const controls = new OrbitControls(this.perspectiveCamera, this.renderer.domElement);
|
|
83
108
|
controls.addEventListener("change", () => {
|
|
84
109
|
this.needRender = true;
|
|
85
110
|
});
|
|
@@ -106,10 +131,14 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
106
131
|
}
|
|
107
132
|
// 修改尺寸
|
|
108
133
|
setSize(width, height) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
134
|
+
//修改透视相机的比例
|
|
135
|
+
this.perspectiveCamera.aspect = width / height;
|
|
136
|
+
this.perspectiveCamera.updateProjectionMatrix();
|
|
137
|
+
//修改正交相机的比例
|
|
138
|
+
// this.orthographicCamera.top = height / width;
|
|
139
|
+
// this.orthographicCamera.bottom = -height / width;
|
|
140
|
+
this.orthographicCamera.left = -width / height;
|
|
141
|
+
this.orthographicCamera.right = width / height;
|
|
113
142
|
this.renderer.setPixelRatio(window.devicePixelRatio);
|
|
114
143
|
this.renderer.setSize(width, height);
|
|
115
144
|
// 设置帧缓存区的尺寸
|
|
@@ -134,7 +163,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
134
163
|
controlsCenter.setDuration(1000);
|
|
135
164
|
controlsCenter.onChange(({ x, y, z }) => {
|
|
136
165
|
this.controls.target.set(x, y, z);
|
|
137
|
-
this.
|
|
166
|
+
this.perspectiveCamera.lookAt(this.controls.target);
|
|
167
|
+
this.orthographicCamera.lookAt(this.controls.target);
|
|
138
168
|
this.needRender = true;
|
|
139
169
|
});
|
|
140
170
|
return controlsCenter;
|
|
@@ -145,7 +175,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
145
175
|
controlsPosition.setDuration(1000);
|
|
146
176
|
controlsPosition.onChange(({ x, y, z }) => {
|
|
147
177
|
// const distance = this.controlsDistance.get("distance");
|
|
148
|
-
this.
|
|
178
|
+
this.perspectiveCamera.position.set(x + 0.01, y, z + 0.01);
|
|
149
179
|
// .sub(this.controls.target)
|
|
150
180
|
// .setLength(distance)
|
|
151
181
|
// .add(this.controls.target);
|
|
@@ -162,7 +192,14 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
162
192
|
// directionVecA.applyAxisAngle(axis, angle);
|
|
163
193
|
// // 输出旋转后的方向向量
|
|
164
194
|
// console.log(directionVecA);
|
|
165
|
-
this.
|
|
195
|
+
this.perspectiveCamera.lookAt(this.controls.target);
|
|
196
|
+
//计算正交相机的缓动
|
|
197
|
+
this.orthographicCamera.position
|
|
198
|
+
.set(x + 0.01, y, z + 0.01)
|
|
199
|
+
.sub(this.controls.target)
|
|
200
|
+
.setLength(250000)
|
|
201
|
+
.add(this.controls.target);
|
|
202
|
+
this.orthographicCamera.lookAt(this.controls.target);
|
|
166
203
|
this.needRender = true;
|
|
167
204
|
});
|
|
168
205
|
return controlsPosition;
|
|
@@ -197,7 +234,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
197
234
|
});
|
|
198
235
|
}
|
|
199
236
|
// 重置控制器的位置
|
|
200
|
-
resetLookAt({ position = this.
|
|
237
|
+
resetLookAt({ position = this.perspectiveCamera.position, center = this.controls.target, }) {
|
|
201
238
|
// 停止控制器的惯性
|
|
202
239
|
this.controls.enableDamping = false;
|
|
203
240
|
this.controls.saveState();
|
|
@@ -210,19 +247,19 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
210
247
|
this.needRender = true;
|
|
211
248
|
}
|
|
212
249
|
// 设置控制器的位置 以动画的方式移动控制器
|
|
213
|
-
async setLookAt({ position = this.
|
|
250
|
+
async setLookAt({ position = this.perspectiveCamera.position, center = this.controls.target, duration = 1000 }) {
|
|
214
251
|
// 停止控制器的惯性
|
|
215
252
|
this.controls.enableDamping = false;
|
|
216
253
|
this.controls.saveState();
|
|
217
254
|
this.controls.update();
|
|
218
255
|
this.controls.reset();
|
|
219
256
|
this.controls.enableDamping = true;
|
|
220
|
-
let currentPosition = this.
|
|
257
|
+
let currentPosition = this.perspectiveCamera.position.clone();
|
|
221
258
|
let currentCenter = this.controls.target.clone();
|
|
222
259
|
// 等待动画执行完成
|
|
223
260
|
await Promise.all([
|
|
224
261
|
new Promise(async (resolve, reject) => {
|
|
225
|
-
this.controlsDistance.setDuration(duration).reset({ distance: this.
|
|
262
|
+
this.controlsDistance.setDuration(duration).reset({ distance: this.perspectiveCamera.position.distanceTo(this.controls.target) });
|
|
226
263
|
await this.controlsDistance.set({ distance: new Vector3(position.x, position.y, position.z).distanceTo(new Vector3(center.x, center.y, center.z)) });
|
|
227
264
|
resolve();
|
|
228
265
|
}),
|
|
@@ -240,7 +277,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
240
277
|
}
|
|
241
278
|
// 获取控制的位置和朝向 保留3位小数
|
|
242
279
|
getLookAt() {
|
|
243
|
-
|
|
280
|
+
//判断需要使用那个相机
|
|
281
|
+
const position = this.perspectiveCamera.position.toArray().map(v => round(v, 3));
|
|
244
282
|
const center = this.controls.target.toArray().map(v => round(v, 3));
|
|
245
283
|
return {
|
|
246
284
|
center: new Vector3(...center),
|
|
@@ -250,7 +288,9 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
250
288
|
}
|
|
251
289
|
// 三维向量转化成屏幕上的2维向量
|
|
252
290
|
v3tov2(v3) {
|
|
253
|
-
|
|
291
|
+
//判断需要使用那个相机
|
|
292
|
+
const camera = this.getCamera();
|
|
293
|
+
v3.project(camera);
|
|
254
294
|
const v2 = new Vector2((v3.x + 1) * this.size.x * 0.5, (-v3.y + 1) * this.size.y * 0.5);
|
|
255
295
|
return v2;
|
|
256
296
|
}
|
|
@@ -304,6 +344,24 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
304
344
|
this.stopRender();
|
|
305
345
|
return;
|
|
306
346
|
}
|
|
347
|
+
//透视相机和正交相机参数的时时转换
|
|
348
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
349
|
+
const { center } = this.getLookAt();
|
|
350
|
+
const { fov } = this.perspectiveCamera;
|
|
351
|
+
const distaned = this.perspectiveCamera.position.clone().sub(center).length();
|
|
352
|
+
const zoom = 1 / (Math.tan(degToRad(fov) / 2) * distaned);
|
|
353
|
+
this.orthographicCamera.zoom = zoom;
|
|
354
|
+
this.orthographicCamera.position.copy(this.perspectiveCamera.position.clone().sub(center)).setLength(250000).add(center);
|
|
355
|
+
}
|
|
356
|
+
else if (this.cameraType === "orthographicCamera") {
|
|
357
|
+
const { center } = this.getLookAt();
|
|
358
|
+
const { fov } = this.perspectiveCamera;
|
|
359
|
+
const { zoom } = this.orthographicCamera;
|
|
360
|
+
const distaned = (1 / zoom) / Math.tan(degToRad(fov) / 2);
|
|
361
|
+
this.perspectiveCamera.position.copy(this.orthographicCamera.position.clone().sub(center)).setLength(distaned).add(center);
|
|
362
|
+
}
|
|
363
|
+
//判断需要使用那个相机
|
|
364
|
+
const camera = this.getCamera();
|
|
307
365
|
//判断尺寸发生改变时重置尺寸
|
|
308
366
|
const parentNode = this.renderer.domElement.parentNode;
|
|
309
367
|
if (parentNode.clientWidth !== this.size.x || parentNode.clientHeight !== this.size.y) {
|
|
@@ -326,17 +384,17 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
326
384
|
// 把基础场景渲染到 帧缓冲区 在后期通道中会用到
|
|
327
385
|
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
328
386
|
this.renderer.clear();
|
|
329
|
-
this.renderer.render(this.scene,
|
|
387
|
+
this.renderer.render(this.scene, camera);
|
|
330
388
|
this.renderer.setRenderTarget(null);
|
|
331
389
|
}
|
|
332
390
|
// 清空主画布
|
|
333
391
|
this.renderer.clear();
|
|
334
|
-
this.renderer.render(this.scene,
|
|
392
|
+
this.renderer.render(this.scene, camera);
|
|
335
393
|
if (has_stroke_or_pass) {
|
|
336
394
|
// 查找需要绘制描边的对象,并且绘制描边
|
|
337
395
|
this.scene.traverse(obj => {
|
|
338
|
-
stroke_pass(this.renderer,
|
|
339
|
-
glow_pass(this.renderer,
|
|
396
|
+
stroke_pass(this.renderer, camera, obj, this.baseDepthTexture);
|
|
397
|
+
glow_pass(this.renderer, camera, obj, this.baseDepthTexture);
|
|
340
398
|
});
|
|
341
399
|
}
|
|
342
400
|
this.dispatchEvent({ type: "afterRender" });
|
|
@@ -365,7 +423,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
365
423
|
const originRenderTarget = this.renderer.getRenderTarget();
|
|
366
424
|
this.renderer.setRenderTarget(this._preRT);
|
|
367
425
|
this._preMesh.material.map = texture;
|
|
368
|
-
this.renderer.render(this._preMesh, this.
|
|
426
|
+
this.renderer.render(this._preMesh, this.perspectiveCamera);
|
|
369
427
|
this.renderer.setRenderTarget(originRenderTarget);
|
|
370
428
|
});
|
|
371
429
|
}
|
|
@@ -376,7 +434,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
376
434
|
const originRenderTarget = this.renderer.getRenderTarget();
|
|
377
435
|
this.renderer.setRenderTarget(this._preRT);
|
|
378
436
|
this._preMesh.geometry = geometry;
|
|
379
|
-
this.renderer.render(this._preMesh, this.
|
|
437
|
+
this.renderer.render(this._preMesh, this.perspectiveCamera);
|
|
380
438
|
this.renderer.setRenderTarget(originRenderTarget);
|
|
381
439
|
});
|
|
382
440
|
}
|
|
@@ -411,7 +469,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
411
469
|
this.renderer.setRenderTarget(rendererTarget);
|
|
412
470
|
this.renderer.getSize(this.tempVector2);
|
|
413
471
|
rendererTarget.setSize(this.tempVector2.x, this.tempVector2.y);
|
|
414
|
-
this.renderer.render(obj, this.
|
|
472
|
+
this.renderer.render(obj, this.getCamera());
|
|
415
473
|
this.renderer.setRenderTarget(originTarget);
|
|
416
474
|
}
|
|
417
475
|
// 给渲染目标的内容加描边
|
package/ThreeBase.ts
CHANGED
|
@@ -5,18 +5,18 @@
|
|
|
5
5
|
* @Last Modified time: 2024-01-20 05:33:28
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import { round } from "lodash";
|
|
9
|
-
import { AxesHelper, BufferGeometry,
|
|
8
|
+
import { add, round } from "lodash";
|
|
9
|
+
import { AxesHelper, BufferGeometry, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, LinearSRGBColorSpace, MOUSE, Mesh, MeshBasicMaterial, NoBlending, NormalBlending, Object3D, OrthographicCamera, PerspectiveCamera, RGBAFormat, Scene, TOUCH, Texture, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
10
10
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
11
|
-
import { generateUUID } from "three/src/math/MathUtils";
|
|
11
|
+
import { degToRad, generateUUID } from "three/src/math/MathUtils";
|
|
12
|
+
import ThreeMouse from "./ThreeMouse";
|
|
13
|
+
import Transition from "./Transition";
|
|
12
14
|
import glow_pass from "./pass/glow_pass";
|
|
13
15
|
import stroke_pass from "./pass/stroke_pass";
|
|
14
16
|
import { clearShader, coptyShader, fullScreenMesh, gaussBlurShader, strokeShader } from "./shaders";
|
|
17
|
+
import FXAAShaderMaterial from "./shaders/FXAAShaderMaterial";
|
|
15
18
|
import threeLoader from "./threeLoader";
|
|
16
|
-
import ThreeMouse from "./ThreeMouse";
|
|
17
19
|
import threeUtils from "./threeUtils";
|
|
18
|
-
import Transition from "./Transition";
|
|
19
|
-
import FXAAShaderMaterial from "./shaders/FXAAShaderMaterial";
|
|
20
20
|
|
|
21
21
|
// three.js 的基础对象
|
|
22
22
|
export default class ThreeBase extends EventDispatcher {
|
|
@@ -85,17 +85,42 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
85
85
|
})();
|
|
86
86
|
|
|
87
87
|
//透视相机
|
|
88
|
-
|
|
88
|
+
perspectiveCamera = new PerspectiveCamera(45, 1, 0.0001, 1000000);
|
|
89
|
+
//正交相机
|
|
90
|
+
orthographicCamera = new OrthographicCamera(- 1, 1, 1, - 1, 0.0001, 1000000);
|
|
91
|
+
|
|
92
|
+
//相机的类型
|
|
93
|
+
cameraType: "perspectiveCamera" | "orthographicCamera" = "perspectiveCamera";
|
|
94
|
+
setCameraType(cameraType: "perspectiveCamera" | "orthographicCamera") {
|
|
95
|
+
if (this.cameraType !== cameraType) {
|
|
96
|
+
this.cameraType = cameraType;
|
|
97
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
98
|
+
this.threeMouse._camera = this.perspectiveCamera;
|
|
99
|
+
this.controls.object = this.perspectiveCamera;
|
|
100
|
+
} else if (this.cameraType === "orthographicCamera") {
|
|
101
|
+
this.threeMouse._camera = this.orthographicCamera;
|
|
102
|
+
this.controls.object = this.orthographicCamera;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
//获取当前使用的的相机
|
|
107
|
+
getCamera() {
|
|
108
|
+
//判断需要使用那个相机
|
|
109
|
+
const camera = ((this.cameraType === "perspectiveCamera") ?
|
|
110
|
+
this.perspectiveCamera :
|
|
111
|
+
this.orthographicCamera);
|
|
112
|
+
return camera;
|
|
113
|
+
}
|
|
89
114
|
|
|
90
115
|
// 场景对象
|
|
91
116
|
scene = new Scene();
|
|
92
117
|
|
|
93
118
|
///鼠标事件对象
|
|
94
|
-
threeMouse = new ThreeMouse(this.renderer, this.
|
|
119
|
+
threeMouse = new ThreeMouse(this.renderer, this.perspectiveCamera, this.scene);
|
|
95
120
|
|
|
96
121
|
// 轨道控制器
|
|
97
122
|
controls = (() => {
|
|
98
|
-
const controls = new OrbitControls(this.
|
|
123
|
+
const controls = new OrbitControls(this.perspectiveCamera, this.renderer.domElement);
|
|
99
124
|
controls.addEventListener("change", () => {
|
|
100
125
|
this.needRender = true;
|
|
101
126
|
});
|
|
@@ -127,10 +152,15 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
127
152
|
|
|
128
153
|
// 修改尺寸
|
|
129
154
|
setSize(width: number, height: number) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
155
|
+
//修改透视相机的比例
|
|
156
|
+
this.perspectiveCamera.aspect = width / height;
|
|
157
|
+
this.perspectiveCamera.updateProjectionMatrix();
|
|
158
|
+
|
|
159
|
+
//修改正交相机的比例
|
|
160
|
+
// this.orthographicCamera.top = height / width;
|
|
161
|
+
// this.orthographicCamera.bottom = -height / width;
|
|
162
|
+
this.orthographicCamera.left = -width / height;
|
|
163
|
+
this.orthographicCamera.right = width / height;
|
|
134
164
|
|
|
135
165
|
this.renderer.setPixelRatio(window.devicePixelRatio);
|
|
136
166
|
this.renderer.setSize(width, height);
|
|
@@ -161,7 +191,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
161
191
|
controlsCenter.setDuration(1000);
|
|
162
192
|
controlsCenter.onChange(({ x, y, z }) => {
|
|
163
193
|
this.controls.target.set(x, y, z);
|
|
164
|
-
this.
|
|
194
|
+
this.perspectiveCamera.lookAt(this.controls.target);
|
|
195
|
+
this.orthographicCamera.lookAt(this.controls.target);
|
|
165
196
|
this.needRender = true;
|
|
166
197
|
});
|
|
167
198
|
return controlsCenter;
|
|
@@ -173,7 +204,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
173
204
|
controlsPosition.setDuration(1000);
|
|
174
205
|
controlsPosition.onChange(({ x, y, z }) => {
|
|
175
206
|
// const distance = this.controlsDistance.get("distance");
|
|
176
|
-
this.
|
|
207
|
+
this.perspectiveCamera.position.set(x + 0.01, y, z + 0.01)
|
|
177
208
|
// .sub(this.controls.target)
|
|
178
209
|
// .setLength(distance)
|
|
179
210
|
// .add(this.controls.target);
|
|
@@ -197,8 +228,16 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
197
228
|
// // 输出旋转后的方向向量
|
|
198
229
|
// console.log(directionVecA);
|
|
199
230
|
|
|
231
|
+
this.perspectiveCamera.lookAt(this.controls.target);
|
|
232
|
+
|
|
233
|
+
//计算正交相机的缓动
|
|
234
|
+
this.orthographicCamera.position
|
|
235
|
+
.set(x + 0.01, y, z + 0.01)
|
|
236
|
+
.sub(this.controls.target)
|
|
237
|
+
.setLength(250000)
|
|
238
|
+
.add(this.controls.target)
|
|
239
|
+
this.orthographicCamera.lookAt(this.controls.target);
|
|
200
240
|
|
|
201
|
-
this.camera.lookAt(this.controls.target);
|
|
202
241
|
this.needRender = true;
|
|
203
242
|
});
|
|
204
243
|
return controlsPosition;
|
|
@@ -231,7 +270,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
231
270
|
this.dispatchEvent({ type: "loadding" } as never);
|
|
232
271
|
this.needRender = true;
|
|
233
272
|
});
|
|
234
|
-
|
|
273
|
+
|
|
235
274
|
threeLoader.addEventListener("onLoad", () => {
|
|
236
275
|
this.dispatchEvent({ type: "onLoad" } as never);
|
|
237
276
|
this.needRender = true;
|
|
@@ -241,7 +280,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
241
280
|
|
|
242
281
|
// 重置控制器的位置
|
|
243
282
|
resetLookAt({
|
|
244
|
-
position = this.
|
|
283
|
+
position = this.perspectiveCamera.position,
|
|
245
284
|
center = this.controls.target,
|
|
246
285
|
}: { position: Vector3, center: Vector3 }) {
|
|
247
286
|
|
|
@@ -260,7 +299,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
260
299
|
|
|
261
300
|
// 设置控制器的位置 以动画的方式移动控制器
|
|
262
301
|
async setLookAt({
|
|
263
|
-
position = this.
|
|
302
|
+
position = this.perspectiveCamera.position,
|
|
264
303
|
center = this.controls.target,
|
|
265
304
|
duration = 1000
|
|
266
305
|
}: { position: Vector3, center: Vector3, duration?: number }) {
|
|
@@ -272,13 +311,13 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
272
311
|
this.controls.reset();
|
|
273
312
|
this.controls.enableDamping = true;
|
|
274
313
|
|
|
275
|
-
let currentPosition = this.
|
|
314
|
+
let currentPosition = this.perspectiveCamera.position.clone();
|
|
276
315
|
let currentCenter = this.controls.target.clone();
|
|
277
316
|
|
|
278
317
|
// 等待动画执行完成
|
|
279
318
|
await Promise.all([
|
|
280
319
|
new Promise<void>(async (resolve, reject) => {
|
|
281
|
-
this.controlsDistance.setDuration(duration).reset({ distance: this.
|
|
320
|
+
this.controlsDistance.setDuration(duration).reset({ distance: this.perspectiveCamera.position.distanceTo(this.controls.target) });
|
|
282
321
|
await this.controlsDistance.set({ distance: new Vector3(position.x, position.y, position.z).distanceTo(new Vector3(center.x, center.y, center.z)) });
|
|
283
322
|
resolve();
|
|
284
323
|
}),
|
|
@@ -298,7 +337,8 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
298
337
|
|
|
299
338
|
// 获取控制的位置和朝向 保留3位小数
|
|
300
339
|
getLookAt() {
|
|
301
|
-
|
|
340
|
+
//判断需要使用那个相机
|
|
341
|
+
const position = this.perspectiveCamera.position.toArray().map(v => round(v, 3));
|
|
302
342
|
const center = this.controls.target.toArray().map(v => round(v, 3));
|
|
303
343
|
return {
|
|
304
344
|
center: new Vector3(...center),
|
|
@@ -309,7 +349,9 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
309
349
|
|
|
310
350
|
// 三维向量转化成屏幕上的2维向量
|
|
311
351
|
v3tov2(v3: Vector3) {
|
|
312
|
-
|
|
352
|
+
//判断需要使用那个相机
|
|
353
|
+
const camera = this.getCamera();
|
|
354
|
+
v3.project(camera);
|
|
313
355
|
const v2 = new Vector2((v3.x + 1) * this.size.x * 0.5, (-v3.y + 1) * this.size.y * 0.5);
|
|
314
356
|
return v2;
|
|
315
357
|
}
|
|
@@ -375,6 +417,25 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
375
417
|
return;
|
|
376
418
|
}
|
|
377
419
|
|
|
420
|
+
//透视相机和正交相机参数的时时转换
|
|
421
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
422
|
+
const { center } = this.getLookAt();
|
|
423
|
+
const { fov } = this.perspectiveCamera;
|
|
424
|
+
const distaned = this.perspectiveCamera.position.clone().sub(center).length();
|
|
425
|
+
const zoom = 1 / (Math.tan(degToRad(fov) / 2) * distaned);
|
|
426
|
+
this.orthographicCamera.zoom = zoom;
|
|
427
|
+
this.orthographicCamera.position.copy(this.perspectiveCamera.position.clone().sub(center)).setLength(250000).add(center);
|
|
428
|
+
} else if (this.cameraType === "orthographicCamera") {
|
|
429
|
+
const { center } = this.getLookAt();
|
|
430
|
+
const { fov } = this.perspectiveCamera;
|
|
431
|
+
const { zoom } = this.orthographicCamera;
|
|
432
|
+
const distaned = (1 / zoom) / Math.tan(degToRad(fov) / 2);
|
|
433
|
+
this.perspectiveCamera.position.copy(this.orthographicCamera.position.clone().sub(center)).setLength(distaned).add(center);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
//判断需要使用那个相机
|
|
437
|
+
const camera = this.getCamera();
|
|
438
|
+
|
|
378
439
|
//判断尺寸发生改变时重置尺寸
|
|
379
440
|
const parentNode = this.renderer.domElement.parentNode as HTMLDivElement;
|
|
380
441
|
if (parentNode.clientWidth !== this.size.x || parentNode.clientHeight !== this.size.y) {
|
|
@@ -403,19 +464,19 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
403
464
|
// 把基础场景渲染到 帧缓冲区 在后期通道中会用到
|
|
404
465
|
this.renderer.setRenderTarget(this.baseRenderTarget);
|
|
405
466
|
this.renderer.clear();
|
|
406
|
-
this.renderer.render(this.scene,
|
|
467
|
+
this.renderer.render(this.scene, camera);
|
|
407
468
|
this.renderer.setRenderTarget(null);
|
|
408
469
|
}
|
|
409
470
|
|
|
410
471
|
// 清空主画布
|
|
411
472
|
this.renderer.clear();
|
|
412
|
-
this.renderer.render(this.scene,
|
|
473
|
+
this.renderer.render(this.scene, camera);
|
|
413
474
|
|
|
414
475
|
if (has_stroke_or_pass) {
|
|
415
476
|
// 查找需要绘制描边的对象,并且绘制描边
|
|
416
477
|
this.scene.traverse(obj => {
|
|
417
|
-
stroke_pass(this.renderer,
|
|
418
|
-
glow_pass(this.renderer,
|
|
478
|
+
stroke_pass(this.renderer, camera, obj, this.baseDepthTexture);
|
|
479
|
+
glow_pass(this.renderer, camera, obj, this.baseDepthTexture);
|
|
419
480
|
});
|
|
420
481
|
}
|
|
421
482
|
|
|
@@ -450,7 +511,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
450
511
|
const originRenderTarget = this.renderer.getRenderTarget();
|
|
451
512
|
this.renderer.setRenderTarget(this._preRT);
|
|
452
513
|
(this._preMesh.material as MeshBasicMaterial).map = texture;
|
|
453
|
-
this.renderer.render(this._preMesh, this.
|
|
514
|
+
this.renderer.render(this._preMesh, this.perspectiveCamera);
|
|
454
515
|
this.renderer.setRenderTarget(originRenderTarget);
|
|
455
516
|
});
|
|
456
517
|
}
|
|
@@ -463,7 +524,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
463
524
|
const originRenderTarget = this.renderer.getRenderTarget();
|
|
464
525
|
this.renderer.setRenderTarget(this._preRT);
|
|
465
526
|
this._preMesh.geometry = geometry;
|
|
466
|
-
this.renderer.render(this._preMesh, this.
|
|
527
|
+
this.renderer.render(this._preMesh, this.perspectiveCamera);
|
|
467
528
|
this.renderer.setRenderTarget(originRenderTarget);
|
|
468
529
|
});
|
|
469
530
|
}
|
|
@@ -501,7 +562,7 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
501
562
|
this.renderer.setRenderTarget(rendererTarget);
|
|
502
563
|
this.renderer.getSize(this.tempVector2);
|
|
503
564
|
rendererTarget.setSize(this.tempVector2.x, this.tempVector2.y);
|
|
504
|
-
this.renderer.render(obj, this.
|
|
565
|
+
this.renderer.render(obj, this.getCamera());
|
|
505
566
|
this.renderer.setRenderTarget(originTarget);
|
|
506
567
|
}
|
|
507
568
|
|