three-base 2.0.58 → 2.0.59

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.d.ts CHANGED
@@ -9,7 +9,8 @@ export default class MeshBase extends Group {
9
9
  animationActions: {
10
10
  [key: string]: AnimationAction;
11
11
  };
12
- loadModel(url: string, suffix?: string): Promise<void>;
12
+ getSuffix(filename: string): string;
13
+ loadModel(url: string, filename?: string): Promise<void>;
13
14
  frustumCulled: boolean;
14
15
  castShadow: boolean;
15
16
  receiveShadow: boolean;
package/MeshBase.js CHANGED
@@ -2,6 +2,7 @@ import { AnimationMixer, Clock, Group, Object3D, Vector3 } from "three";
2
2
  import threeUtils from "./threeUtils";
3
3
  import threeLoader from "./threeLoader";
4
4
  import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
5
+ import { get } from "lodash";
5
6
  // 模型基类
6
7
  export default class MeshBase extends Group {
7
8
  constructor(url) {
@@ -20,25 +21,32 @@ export default class MeshBase extends Group {
20
21
  mixer;
21
22
  //动画剪辑对象数组
22
23
  animationActions = {};
24
+ // 获取文件名的后缀
25
+ getSuffix(filename) {
26
+ const match = filename.match(/\.([^\.]+)$/);
27
+ return match ? match[1].toLowerCase() : '';
28
+ }
23
29
  // 如果url 是blob地址的话,就用suffix字段来制定模型的类型
24
- async loadModel(url, suffix = "") {
30
+ async loadModel(url, filename = "") {
25
31
  if (!url) {
26
32
  this.clear();
27
33
  return;
28
34
  }
35
+ // 获取后缀
36
+ const suffix = (get((filename || url).match(/\.([^\.]+)$/), [1]) || "").toLowerCase();
29
37
  this.loaded = false;
30
38
  // 没有模型并且没有加载任务时就去加载模型
31
39
  if (!MeshBase.models.get(url) && !MeshBase.loadModeling.get(url)) {
32
40
  MeshBase.loadModeling.set(url, true);
33
41
  let modal = new Object3D();
34
- if (/\.fbx$/i.test(suffix || url)) {
42
+ if (["fbx"].includes(suffix)) {
35
43
  await threeLoader.fbxLoader.loadAsync(url).then(res => {
36
44
  modal = res;
37
45
  }).catch(() => {
38
46
  console.error(`${url},加载失败`);
39
47
  });
40
48
  }
41
- else if (/\.(gltf|glb)$/i.test(suffix || url)) {
49
+ else if (["gltf", "glb"].includes(suffix)) {
42
50
  await threeLoader.gltfLoader.loadAsync(url).then(res => {
43
51
  modal = res.scene;
44
52
  modal.animations = res.animations;
@@ -46,7 +54,7 @@ export default class MeshBase extends Group {
46
54
  console.error(`${url},加载失败`);
47
55
  });
48
56
  }
49
- else if (/\.(usdz)$/i.test(suffix || url)) {
57
+ else if (["usdz"].includes(suffix)) {
50
58
  await threeLoader.usdzLoader.loadAsync(url).then(res => {
51
59
  modal = res; // usdz 目前还不支持骨骼动画
52
60
  modal.animations = res.animations;
package/MeshBase.ts CHANGED
@@ -3,6 +3,7 @@ import { AnimationAction, AnimationMixer, Clock, DoubleSide, Group, Material, Me
3
3
  import threeUtils from "./threeUtils";
4
4
  import threeLoader from "./threeLoader";
5
5
  import * as SkeletonUtils from 'three/examples/jsm/utils/SkeletonUtils';
6
+ import { get, includes } from "lodash";
6
7
 
7
8
  // 模型基类
8
9
  export default class MeshBase extends Group {
@@ -29,13 +30,22 @@ export default class MeshBase extends Group {
29
30
  //动画剪辑对象数组
30
31
  animationActions: { [key: string]: AnimationAction } = {};
31
32
 
33
+ // 获取文件名的后缀
34
+ getSuffix(filename: string) {
35
+ const match = filename.match(/\.([^\.]+)$/);
36
+ return match ? match[1].toLowerCase() : '';
37
+ }
38
+
32
39
  // 如果url 是blob地址的话,就用suffix字段来制定模型的类型
33
- async loadModel(url: string, suffix: string = "") {
40
+ async loadModel(url: string, filename: string = "") {
34
41
  if (!url) {
35
42
  this.clear();
36
43
  return;
37
44
  }
38
45
 
46
+ // 获取后缀
47
+ const suffix = (get((filename || url).match(/\.([^\.]+)$/), [1]) || "").toLowerCase();
48
+
39
49
  this.loaded = false;
40
50
 
41
51
  // 没有模型并且没有加载任务时就去加载模型
@@ -44,20 +54,20 @@ export default class MeshBase extends Group {
44
54
 
45
55
  let modal = new Object3D();
46
56
 
47
- if (/\.fbx$/i.test(suffix || url)) {
57
+ if (["fbx"].includes(suffix)) {
48
58
  await threeLoader.fbxLoader.loadAsync(url).then(res => {
49
59
  modal = res;
50
60
  }).catch(() => {
51
61
  console.error(`${url},加载失败`);
52
62
  })
53
- } else if (/\.(gltf|glb)$/i.test(suffix || url)) {
63
+ } else if (["gltf", "glb"].includes(suffix)) {
54
64
  await threeLoader.gltfLoader.loadAsync(url).then(res => {
55
65
  modal = res.scene;
56
66
  modal.animations = res.animations;
57
67
  }).catch(() => {
58
68
  console.error(`${url},加载失败`);
59
69
  })
60
- } else if (/\.(usdz)$/i.test(suffix || url)) {
70
+ } else if (["usdz"].includes(suffix)) {
61
71
  await threeLoader.usdzLoader.loadAsync(url).then(res => {
62
72
  modal = res; // usdz 目前还不支持骨骼动画
63
73
  modal.animations = res.animations;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "three-base",
3
- "version": "2.0.58",
3
+ "version": "2.0.59",
4
4
  "description": "three 3d three-base",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"