three-base 2.0.56 → 2.0.58
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/ThreeBase.d.ts +11 -1
- package/ThreeBase.js +47 -3
- package/ThreeBase.ts +62 -3
- package/package.json +1 -1
package/ThreeBase.d.ts
CHANGED
|
@@ -54,6 +54,13 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
54
54
|
position: Vector3;
|
|
55
55
|
centerPosition: string;
|
|
56
56
|
};
|
|
57
|
+
lookatObjects({ objects, duration, direction, offset, distance }: {
|
|
58
|
+
objects: Array<Object3D>;
|
|
59
|
+
duration?: number;
|
|
60
|
+
direction?: Vector3;
|
|
61
|
+
offset?: Vector3;
|
|
62
|
+
distance?: number;
|
|
63
|
+
}): void;
|
|
57
64
|
v3tov2(v3: Vector3): Vector2;
|
|
58
65
|
controlType: "OrbitControls" | "MapControls";
|
|
59
66
|
originScreenSpacePanning: boolean;
|
|
@@ -64,7 +71,10 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
64
71
|
hasPass(): boolean;
|
|
65
72
|
render(): void;
|
|
66
73
|
private renderBindThis;
|
|
67
|
-
|
|
74
|
+
getThumbBlob(): Promise<Blob>;
|
|
75
|
+
getThumbBase64(): string;
|
|
76
|
+
getThumbFile(): Promise<File>;
|
|
77
|
+
download(fileName?: string): Promise<void>;
|
|
68
78
|
private _preMesh;
|
|
69
79
|
private _preRT;
|
|
70
80
|
preloadTexture(src: string): Promise<void>;
|
package/ThreeBase.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* @Last Modified time: 2024-06-18 21:03:02
|
|
6
6
|
*/
|
|
7
7
|
import { round } from "lodash";
|
|
8
|
-
import { AxesHelper, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, MOUSE, Mesh, NoBlending, NormalBlending, OrthographicCamera, PerspectiveCamera, RGBAFormat, Scene, TOUCH, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
8
|
+
import { AxesHelper, Box3, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, 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
10
|
import { degToRad, generateUUID } from "three/src/math/MathUtils";
|
|
11
11
|
import ThreeMouse from "./ThreeMouse";
|
|
@@ -297,6 +297,27 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
297
297
|
centerPosition: `center: new Vector3(${center.join(",")}),position: new Vector3(${position.join(",")})`
|
|
298
298
|
};
|
|
299
299
|
}
|
|
300
|
+
// 查看对象
|
|
301
|
+
lookatObjects({ objects, duration = 1000, direction = new Vector3(0.3, 0.3, 1), offset = new Vector3(0, 0, 0), distance = 1 }) {
|
|
302
|
+
const box3 = new Box3();
|
|
303
|
+
objects.map(o => {
|
|
304
|
+
o.traverse(oo => {
|
|
305
|
+
box3.expandByObject(oo);
|
|
306
|
+
});
|
|
307
|
+
});
|
|
308
|
+
const size = new Vector3();
|
|
309
|
+
box3.getSize(size);
|
|
310
|
+
const center = new Vector3();
|
|
311
|
+
box3.getCenter(center);
|
|
312
|
+
center.add(offset);
|
|
313
|
+
const modelSize = size.length() * distance;
|
|
314
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
315
|
+
const { fov } = this.getCamera();
|
|
316
|
+
const cameraDistance = modelSize / (2 * Math.tan(fov * Math.PI / 360));
|
|
317
|
+
const position = direction.setLength(cameraDistance).add(center);
|
|
318
|
+
this.setLookAt({ center, position, duration });
|
|
319
|
+
}
|
|
320
|
+
}
|
|
300
321
|
// 三维向量转化成屏幕上的2维向量
|
|
301
322
|
v3tov2(v3) {
|
|
302
323
|
//判断需要使用那个相机
|
|
@@ -413,13 +434,36 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
413
434
|
this.dispatchEvent({ type: "afterRender" });
|
|
414
435
|
}
|
|
415
436
|
renderBindThis = this.render.bind(this);
|
|
437
|
+
//获取渲染图blob对象
|
|
438
|
+
async getThumbBlob() {
|
|
439
|
+
this.render();
|
|
440
|
+
const blob = await new Promise((resolve, reject) => {
|
|
441
|
+
this.renderer.domElement.toBlob(blob => {
|
|
442
|
+
resolve(blob);
|
|
443
|
+
}, "image/png", 1);
|
|
444
|
+
});
|
|
445
|
+
return blob;
|
|
446
|
+
}
|
|
447
|
+
//获取渲染图base64
|
|
448
|
+
getThumbBase64() {
|
|
449
|
+
this.render();
|
|
450
|
+
const base64 = this.renderer.domElement.toDataURL('image/png');
|
|
451
|
+
return base64;
|
|
452
|
+
}
|
|
453
|
+
//获取渲染图文件对象
|
|
454
|
+
async getThumbFile() {
|
|
455
|
+
const blob = await this.getThumbBlob();
|
|
456
|
+
const file = new File([blob], 'thumb.png', { type: blob.type });
|
|
457
|
+
return file;
|
|
458
|
+
}
|
|
416
459
|
// 下载渲染图
|
|
417
|
-
download(fileName = "download") {
|
|
460
|
+
async download(fileName = "download") {
|
|
418
461
|
this.render();
|
|
462
|
+
const blob = await this.getThumbBlob();
|
|
419
463
|
const base64 = this.renderer.domElement.toDataURL('image/png');
|
|
420
464
|
let saveLink = document.createElement("a");
|
|
421
465
|
saveLink.download = `${fileName}.png`;
|
|
422
|
-
saveLink.href =
|
|
466
|
+
saveLink.href = URL.createObjectURL(blob);
|
|
423
467
|
document.body.append(saveLink);
|
|
424
468
|
saveLink.click();
|
|
425
469
|
}
|
package/ThreeBase.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
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, SRGBColorSpace, Scene, TOUCH, Texture, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
9
|
+
import { AxesHelper, Box3, BufferGeometry, DepthFormat, DepthTexture, EventDispatcher, GridHelper, LinearFilter, LinearSRGBColorSpace, MOUSE, Mesh, MeshBasicMaterial, NoBlending, NormalBlending, Object3D, OrthographicCamera, PerspectiveCamera, RGBAFormat, SRGBColorSpace, Scene, TOUCH, Texture, UnsignedShortType, Vector2, Vector3, WebGLRenderTarget, WebGLRenderer } from "three";
|
|
10
10
|
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
|
|
11
11
|
import { degToRad, generateUUID } from "three/src/math/MathUtils";
|
|
12
12
|
import ThreeMouse from "./ThreeMouse";
|
|
@@ -359,6 +359,41 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
359
359
|
}
|
|
360
360
|
}
|
|
361
361
|
|
|
362
|
+
// 查看对象
|
|
363
|
+
lookatObjects({
|
|
364
|
+
objects,
|
|
365
|
+
duration = 1000,
|
|
366
|
+
direction = new Vector3(0.3, 0.3, 1),
|
|
367
|
+
offset = new Vector3(0, 0, 0),
|
|
368
|
+
distance = 1
|
|
369
|
+
}: {
|
|
370
|
+
objects: Array<Object3D>,
|
|
371
|
+
duration?: number,
|
|
372
|
+
direction?: Vector3,
|
|
373
|
+
offset?: Vector3,
|
|
374
|
+
distance?: number
|
|
375
|
+
}) {
|
|
376
|
+
const box3 = new Box3();
|
|
377
|
+
objects.map(o => {
|
|
378
|
+
o.traverse(oo => {
|
|
379
|
+
box3.expandByObject(oo);
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
const size = new Vector3();
|
|
383
|
+
box3.getSize(size);
|
|
384
|
+
const center = new Vector3();
|
|
385
|
+
box3.getCenter(center);
|
|
386
|
+
|
|
387
|
+
center.add(offset);
|
|
388
|
+
const modelSize = size.length() * distance;
|
|
389
|
+
if (this.cameraType === "perspectiveCamera") {
|
|
390
|
+
const { fov } = this.getCamera() as PerspectiveCamera;
|
|
391
|
+
const cameraDistance = modelSize / (2 * Math.tan(fov * Math.PI / 360));
|
|
392
|
+
const position = direction.setLength(cameraDistance).add(center);
|
|
393
|
+
this.setLookAt({ center, position, duration });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
362
397
|
// 三维向量转化成屏幕上的2维向量
|
|
363
398
|
v3tov2(v3: Vector3) {
|
|
364
399
|
//判断需要使用那个相机
|
|
@@ -499,13 +534,37 @@ export default class ThreeBase extends EventDispatcher {
|
|
|
499
534
|
|
|
500
535
|
private renderBindThis = this.render.bind(this);
|
|
501
536
|
|
|
537
|
+
//获取渲染图blob对象
|
|
538
|
+
async getThumbBlob() {
|
|
539
|
+
this.render();
|
|
540
|
+
const blob = await new Promise<Blob>((resolve, reject) => {
|
|
541
|
+
this.renderer.domElement.toBlob(blob => {
|
|
542
|
+
resolve(blob as Blob);
|
|
543
|
+
}, "image/png", 1);
|
|
544
|
+
});
|
|
545
|
+
return blob;
|
|
546
|
+
}
|
|
547
|
+
//获取渲染图base64
|
|
548
|
+
getThumbBase64() {
|
|
549
|
+
this.render();
|
|
550
|
+
const base64 = this.renderer.domElement.toDataURL('image/png');
|
|
551
|
+
return base64;
|
|
552
|
+
}
|
|
553
|
+
//获取渲染图文件对象
|
|
554
|
+
async getThumbFile() {
|
|
555
|
+
const blob = await this.getThumbBlob();
|
|
556
|
+
const file = new File([blob], 'thumb.png', { type: blob.type });
|
|
557
|
+
return file;
|
|
558
|
+
}
|
|
559
|
+
|
|
502
560
|
// 下载渲染图
|
|
503
|
-
download(fileName: string = "download") {
|
|
561
|
+
async download(fileName: string = "download") {
|
|
504
562
|
this.render();
|
|
563
|
+
const blob = await this.getThumbBlob();
|
|
505
564
|
const base64 = this.renderer.domElement.toDataURL('image/png');
|
|
506
565
|
let saveLink = document.createElement("a");
|
|
507
566
|
saveLink.download = `${fileName}.png`;
|
|
508
|
-
saveLink.href =
|
|
567
|
+
saveLink.href = URL.createObjectURL(blob);
|
|
509
568
|
document.body.append(saveLink);
|
|
510
569
|
saveLink.click();
|
|
511
570
|
}
|