three-base 1.0.13 → 1.0.15

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/index.ts CHANGED
@@ -14,6 +14,9 @@ export { default as KeyFrameAnimation } from "./lib/KeyFrameAnimation";
14
14
  export type { KeyframeAnimationProps } from "./lib/KeyFrameAnimation";
15
15
  export type { Keyframe } from "./lib/KeyFrameAnimation";
16
16
 
17
+ //实例模型组对象
18
+ export { default as InstancedGroupMesh } from "./lib/InstancedGroupMesh";
19
+
17
20
  //点光源
18
21
  export { default as PointLight } from "./lib/PointLight";
19
22
 
@@ -0,0 +1,71 @@
1
+ /*
2
+ * @Author: xiaosihan
3
+ * @Date: 2023-02-21 09:55:52
4
+ * @Last Modified by: xiaosihan
5
+ * @Last Modified time: 2023-02-21 10:12:33
6
+ */
7
+
8
+ import * as THREE from 'three'
9
+
10
+ // 模型组转成 实例对象
11
+ export default class InstancedGroupMesh extends THREE.Mesh {
12
+ isInstancedGroupMesh = true
13
+
14
+ count: number = 0
15
+
16
+ meshCollect: Record<string, THREE.Mesh[]> = {}
17
+
18
+ instanceCollect: Record<string, THREE.InstancedMesh> = {}
19
+
20
+ static matrix = new THREE.Matrix4()
21
+
22
+ constructor(group: THREE.Group, count: number) {
23
+ super()
24
+
25
+ this.count = count
26
+
27
+ const { meshCollect, instanceCollect } = this
28
+
29
+ // collect all mesh counts, in case there are more than one copy of the same mesh in this group
30
+ group.traverse((obj) => {
31
+ if (obj.type !== 'Mesh') return
32
+ const mesh = obj as THREE.Mesh
33
+
34
+ // this is important
35
+ mesh.updateMatrix()
36
+
37
+ // same geometry and same material consider as the same mesh
38
+ const uuid = mesh.geometry.uuid + '/' + (mesh.material as THREE.Material).uuid
39
+
40
+ if (meshCollect[uuid]) {
41
+ meshCollect[uuid].push(mesh)
42
+ } else {
43
+ meshCollect[uuid] = [mesh]
44
+ }
45
+ })
46
+
47
+ // create all instances
48
+ Object.keys(meshCollect).forEach(uuid => {
49
+ const meshes = meshCollect[uuid]
50
+ const mesh = meshes[0]
51
+
52
+ const instancedMesh = new THREE.InstancedMesh(mesh.geometry, mesh.material, meshes.length * count)
53
+ instanceCollect[uuid] = instancedMesh
54
+ this.add(instancedMesh)
55
+ })
56
+ }
57
+
58
+ setMatrixAt(index: number, matrix: THREE.Matrix4) {
59
+ Object.keys(this.meshCollect).forEach(uuid => {
60
+ const instancedMesh = this.instanceCollect[uuid]
61
+ const collect = this.meshCollect[uuid]
62
+ collect.forEach((mesh: THREE.Mesh, i: number) => {
63
+ InstancedGroupMesh.matrix.copy(mesh.matrix)
64
+ InstancedGroupMesh.matrix.premultiply(matrix)
65
+
66
+ instancedMesh.setMatrixAt(collect.length * index + i, InstancedGroupMesh.matrix);
67
+ instancedMesh.instanceMatrix.needsUpdate = true;
68
+ })
69
+ })
70
+ }
71
+ }
@@ -20,6 +20,7 @@ export type Keyframe = {
20
20
 
21
21
 
22
22
  export type KeyframeAnimationProps = {
23
+ speedScale?: number; // 速度调整
23
24
  object3d: Object3D; // 需要移动的对象
24
25
  keyframes: Array<Keyframe>,
25
26
  }
@@ -30,6 +31,9 @@ export default class KeyframeAnimation extends Object3D {
30
31
  constructor(props: KeyframeAnimationProps) {
31
32
  super();
32
33
 
34
+ // 速度调整
35
+ this.speedScale = props.speedScale || 1;
36
+
33
37
  // 设置关键帧对象数组
34
38
  this.setKeyframes(props.keyframes);
35
39
 
@@ -39,6 +43,9 @@ export default class KeyframeAnimation extends Object3D {
39
43
 
40
44
  }
41
45
 
46
+ // 速度调整
47
+ speedScale = 1;
48
+
42
49
  // 需要需要移动的对象
43
50
  object3d: Object3D;
44
51
 
@@ -5,7 +5,7 @@
5
5
  * @Last Modified time: 2023-02-07 11:11:48
6
6
  */
7
7
 
8
- import { DefaultLoadingManager, EventDispatcher, RepeatWrapping, Texture, TextureLoader } from "three";
8
+ import { DefaultLoadingManager, EventDispatcher, RepeatWrapping, Texture, TextureLoader, VideoTexture } from "three";
9
9
  import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader";
10
10
  import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
11
11
  import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader";
@@ -60,9 +60,26 @@ class ThreeLoader extends EventDispatcher {
60
60
  let texture = this.textureMap.get(src);
61
61
 
62
62
  if (!texture) {
63
- texture = this.textureLoader.load(src, tex => {
64
- tex.image.crossOrigin = "anonymous";
65
- });
63
+ if (/\.mp4$/.test(src)) {
64
+ const videoDom = document.createElement("video");
65
+ videoDom.autoplay = true;
66
+ videoDom.loop = true;
67
+ videoDom.muted = true;
68
+ videoDom.preload = "auto";
69
+ videoDom.playsInline = true;
70
+ videoDom.src = src;
71
+ document.addEventListener("pointerup", () => {
72
+ videoDom.pause();
73
+ videoDom.play();
74
+ });
75
+ texture = new VideoTexture(videoDom);
76
+ } else {
77
+
78
+ texture = this.textureLoader.load(src, tex => {
79
+ tex.image.crossOrigin = "anonymous";
80
+ });
81
+ }
82
+
66
83
  this.textureMap.set(src, texture);
67
84
  }
68
85
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "three 3d three-base",
5
5
  "keywords": [
6
6
  "three",