fl-web-component 1.0.7 → 1.0.9

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/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "fl-web-component",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "scripts": {
5
5
  "dev": "vue-cli-service serve",
6
6
  "lint": "eslint \"{src,packages}/**/*.{vue,js}\" --fix",
7
7
  "prettier": "prettier --write \"packages/**/*.{js,css,less,scss,vue,html}\"",
8
8
  "watch": "npm run lint && vue-cli-service build --watch --mode production --target lib --name fl-web-component ./src/main.js",
9
- "build": "npm run lint && vue-cli-service build --target lib --name fl-web-component ./src/main.js"
9
+ "build": "npm run lint && vue-cli-service build --target lib --name fl-web-component ./src/main.js",
10
+ "publish": "npm run build && npm publish"
10
11
  },
11
12
  "files": [
12
13
  "dist",
@@ -1,5 +1,5 @@
1
1
  <template>
2
- <div id="instructions"></div>
2
+ <div id="fl-model"></div>
3
3
  </template>
4
4
  <script>
5
5
  var [
@@ -49,7 +49,11 @@
49
49
  var removeSpeed = 200,
50
50
  upSpeed = 200; //控制器移动速度 , //控制跳起时的速度
51
51
  var modelGroup;
52
-
52
+ var roamConfig = {
53
+ loop: false,
54
+ speed: 0, // 最大值为3
55
+ name: ''
56
+ }
53
57
  // 绘制对象映射实例表
54
58
  let drawObjMapInstance = {};
55
59
  import CameraControls from 'camera-controls';
@@ -75,10 +79,7 @@
75
79
  },
76
80
  data() {
77
81
  return {
78
- roamConfig: {
79
- loop: false,
80
- speed: 0, // 最大值为3
81
- },
82
+ arrowImg: require('@/assets/arrow-right.png'),
82
83
  };
83
84
  },
84
85
  created() {
@@ -88,7 +89,7 @@
88
89
  mouse = new this.THREE.Vector2();
89
90
  },
90
91
  mounted() {
91
- instructions = document.getElementById('instructions');
92
+ instructions = document.getElementById('fl-model');
92
93
  this.initRender();
93
94
  this.initScene();
94
95
  this.initCamera();
@@ -165,14 +166,15 @@
165
166
  /*
166
167
  参数:data 模型数据
167
168
  color: '' 初始化模型的颜色 在业务方 有这个需求
169
+ meshNameConfig: {}
168
170
  */
169
- drawModel(data, color = '') {
171
+ drawModel(data, color = '', meshNameConfig) {
170
172
  if (Object.keys(data).length === 0) {
171
173
  return;
172
174
  }
173
175
  const { instances, drawObjs } = parseData(data);
174
176
  if (instances.length > 0) {
175
- modelGroup = handleInstancedMeshModel(instances, drawObjs, '', scene, color);
177
+ modelGroup = handleInstancedMeshModel(instances, drawObjs, '', scene, color, meshNameConfig);
176
178
  scene.add(modelGroup);
177
179
  // this.compileShader();
178
180
  // cameraControls.fitToSphere(scene, true); // TODO 待处理,先用 setModelCenter 进行定位
@@ -355,6 +357,11 @@
355
357
  obj.material.opacity = ele.attr[key];
356
358
  obj.material.transparent = true;
357
359
  break;
360
+ case 'position':
361
+ targetObj.forEach(children => {
362
+ children.position.set(ele.attr[key].x, ele.attr[key].y, ele.attr[key].z)
363
+ })
364
+ break;
358
365
  }
359
366
  }
360
367
  // obj.material.needsUpdate = true;
@@ -497,6 +504,39 @@
497
504
  }
498
505
  });
499
506
  },
507
+ // 删除场景中所有的实体
508
+ removeAll() {
509
+ if (scene) {
510
+ scene.traverse(item => {
511
+ item.material && item.material.dispose();
512
+ item.geometry && item.geometry.dispose();
513
+ if (item instanceof this.THREE.Mesh) {
514
+ item.clear();
515
+ }
516
+ scene.remove(item);
517
+ })
518
+ scene.clear()
519
+ }
520
+ },
521
+ // 销毁场景 释放内存
522
+ destroyScene() {
523
+ cancelAnimationFrame(animateId)
524
+ if (scene) {
525
+ scene.traverse(child => {
526
+ child.material && child.material.dispose()
527
+ child.geometry && child.geometry.dispose()
528
+ child = null
529
+ })
530
+ scene.clear()
531
+ scene = null
532
+ }
533
+ renderer.forceContextLoss()
534
+ renderer.dispose()
535
+ camera = null
536
+ cameraControls = null
537
+ renderer.domElement = null
538
+ renderer = null
539
+ },
500
540
  // 绘制曲线
501
541
  /*
502
542
  参数声明:Object
@@ -526,6 +566,13 @@
526
566
  }
527
567
  },
528
568
  // 绘制贴图曲线
569
+ /*
570
+ 参数声明:Object
571
+ {
572
+ path: [{x:0, y: 0, z: 0}], 坐标点信息
573
+ name: '', // 线条的名字, 用来清除时使用的
574
+ }
575
+ */
529
576
  drawTextureCurve(params) {
530
577
  this.removeObjectByName(params.name);
531
578
  const route = [];
@@ -538,7 +585,7 @@
538
585
  curve = new this.THREE.CatmullRomCurve3(route, false, 'catmullrom', 0);
539
586
  const geometry = new this.THREE.TubeGeometry(curve, 5000, 0.5, 4);
540
587
  //加载纹理
541
- lineTexture = new this.THREE.TextureLoader().load('/static/arrow/arrow-right.png');
588
+ lineTexture = new this.THREE.TextureLoader().load(this.arrowImg);
542
589
  lineTexture.wrapS = this.THREE.RepeatWrapping;
543
590
  lineTexture.wrapT = this.THREE.RepeatWrapping;
544
591
  lineTexture.repeat.set(20, 1); //水平重复20次
@@ -552,8 +599,10 @@
552
599
  let line = new this.THREE.Line(geometry, material);
553
600
  line.name = params.name;
554
601
  scene.add(line);
602
+ clock = new this.THREE.Clock();
555
603
  }
556
604
  },
605
+ //
557
606
  // 开启漫游
558
607
  /*
559
608
  参数为Object
@@ -564,37 +613,38 @@
564
613
  */
565
614
  startRoam(params) {
566
615
  progress = 0;
567
- this.roamConfig = Object.assign(
568
- {},
569
- {
570
- loop: params.loop,
571
- speed: params.speed,
572
- roamName: params.name,
573
- }
574
- );
616
+ roamConfig = {
617
+ loop: params.loop,
618
+ speed: params.speed,
619
+ }
575
620
  this.drawTextureCurve({
576
621
  name: params.name,
577
622
  path: params.path,
578
623
  });
624
+ roaming = true
579
625
  clock = new this.THREE.Clock();
580
626
  },
581
- // 暂停漫游
582
- pauseRoaming() {
583
- this.$set(this.roamConfig, 'speed', 0);
627
+ // 更新漫游的配置
628
+ /*
629
+ 参数为Object
630
+ */
631
+ updateRoamConfig(params) {
632
+ for (let key in params) {
633
+ roamConfig[key] = key === 'speed' ? params[key] / 300 : params[key]
634
+ }
584
635
  },
585
636
  // 结束/退出漫游
586
637
  endRoam() {
587
638
  roaming = false;
588
639
  progress = 0;
589
- this.removeObjectByName(this.roamConfig.roamName);
590
- this.roamConfig = Object.assign(
591
- {},
592
- {
593
- loop: false,
594
- speed: 0,
595
- roamName: '',
596
- }
597
- );
640
+ this.removeObjectByName(this.roamConfig.name);
641
+ roamConfig = {
642
+ loop: false,
643
+ speed: 0,
644
+ name: '',
645
+ }
646
+ roaming = false
647
+ lineTexture = null
598
648
  },
599
649
  // 相机跟随轨道
600
650
  cameraTrack() {
@@ -611,16 +661,16 @@
611
661
  camera.lookAt(pointBox.x, pointBox.y + 5, pointBox.z);
612
662
  cameraControls.setPosition(point.x, point.y + 5, point.z, false);
613
663
  cameraControls.setTarget(pointBox.x, pointBox.y + 5, pointBox.z, false);
614
- progress += this.roamConfig.speed / 300;
664
+ progress += roamConfig.speed / 300;
615
665
  } else {
616
666
  // 循环漫游
617
- if (this.roamConfig.loop) {
667
+ if (roamConfig.loop) {
618
668
  progress = 0;
619
669
  }
620
670
  }
621
671
  } else {
622
672
  lineTexture = null;
623
- this.roamConfig.progress = 0;
673
+ progress = 0;
624
674
  }
625
675
  },
626
676
  // 全局整体炸开
@@ -1065,7 +1115,7 @@
1065
1115
  };
1066
1116
  </script>
1067
1117
  <style lang="scss" scoped>
1068
- #instructions {
1118
+ #fl-model {
1069
1119
  width: 100%;
1070
1120
  height: 100%;
1071
1121
  cursor: pointer;
Binary file
@@ -25,73 +25,70 @@ let drawObjMapInstance = {};
25
25
  * @param {Object} instance - 实例对象,包含绘制对象ID和实例ID等数据
26
26
  * @param {Array} drawObjs - 绘制对象数组,包含几何数据
27
27
  */
28
- function handleInstancedMeshModel(instances, drawObjs, type, scene, customColor) {
28
+ function handleInstancedMeshModel(instances, drawObjs, type, scene, customColor, meshNameConfig) {
29
29
  // 第一阶段:构建实例映射表
30
30
  let modelGroup = new THREE.Group();
31
31
  for (let i = 0; i < instances.length; i++) {
32
32
  formatInstancedMap(instances[i], drawObjs);
33
33
  }
34
- console.log('drawObjMapInstance', drawObjMapInstance)
35
34
  // 第二阶段:遍历所有实例进行处理
36
35
  for (let i = 0; i < instances.length; i++) {
37
36
  let targetGroup, instancedMeshIndex, drawObjectName;
38
- // for (let drawObjectId in drawObjMapInstance) {
39
- // const drawObj = drawObjMapInstance[drawObjectId];
40
- // drawObj.MapInstance.forEach((instance, index) => {
41
- // if (instance.instanceId == instances[i].instanceId) {
42
- // instancedMeshIndex = index;
43
- // drawObjectName = drawObjectId;
44
- // }
45
- // });
46
- // }
47
-
48
37
  const drawObjInstance = drawObjMapInstance[instances[i].drawObject];
49
- drawObjInstance.MapInstance.forEach((instance, index) => {
50
- if (instance.instanceId == instances[i].instanceId) {
51
- instancedMeshIndex = index;
52
- drawObjectName = instances[i].drawObject;
53
- }
54
- });
55
- if (drawObjectName) {
56
- targetGroup = scene.getObjectByName(drawObjectName);
57
- }
58
-
59
- if (!targetGroup) {
60
- const drawObj = drawObjMapInstance[instances[i].drawObject];
61
- const group = new THREE.Group();
62
- group.name = instances[i].drawObject;
63
- group.userData.isInstancedMeshGroup = true;
64
-
65
- const instanceCount = drawObj.MapInstance.length;
66
- drawObj.MapMesh?.forEach(mesh => {
67
- const model = drawModel(mesh, group.name, instanceCount, customColor);
68
- if (!model) {
69
- return;
38
+ if (drawObjInstance.MapMesh.length > 0) {
39
+ drawObjInstance.MapInstance.forEach((instance, index) => {
40
+ if (instance.instanceId == instances[i].instanceId) {
41
+ instancedMeshIndex = index;
42
+ drawObjectName = instances[i].drawObject;
70
43
  }
71
- drawObj.MapInstance.forEach((instance, index) => {
72
- if (instance.instanceId == instances[i].instanceId) {
73
- model.userData.instanceIndex = index;
74
- model.name = instances[i].instanceId;
75
- const matrixVal = instance.matrix?.val;
76
- if (matrixVal) {
77
- const m4 = new THREE.Matrix4();
78
- // m4.setPosition(new THREE.Vector3(9999999, 9999999, 9999999)); // TODO 临时隐藏方案
79
- m4.elements = instance.matrix.val;
80
- model.setMatrixAt(index, m4);
81
- }
82
- // 需要先设置全部实例颜色,否则后续设置颜色无效
83
- const { color } = mesh.prop;
84
- const meshColor = customColor
85
- ? new THREE.Color(customColor)
86
- : new THREE.Color(`rgb(${color[0]}, ${color[1]}, ${color[2]})`);
87
- model.setColorAt(index, meshColor);
44
+ });
45
+ if (drawObjectName) {
46
+ targetGroup = scene.getObjectByName(drawObjectName);
47
+ }
48
+
49
+ if (!targetGroup) {
50
+ const drawObj = drawObjMapInstance[instances[i].drawObject];
51
+ const group = new THREE.Group();
52
+ group.name = instances[i].drawObject;
53
+ group.userData.isInstancedMeshGroup = true;
54
+
55
+ const instanceCount = drawObj.MapInstance.length;
56
+ drawObj.MapMesh?.forEach(mesh => {
57
+ const model = drawModel(mesh, group.name, instanceCount, customColor);
58
+ if (!model) {
59
+ return;
60
+ }
61
+ let meshName = ''
62
+ for (const key in meshNameConfig) {
63
+ model.userData[key] = meshNameConfig[key]
64
+ meshName += ':' + meshNameConfig[key]
88
65
  }
66
+ drawObj.MapInstance.forEach((instance, index) => {
67
+ if (instance.instanceId == instances[i].instanceId) {
68
+ model.userData.instanceIndex = index;
69
+ model.name = meshName !== '' ? (instances[i].instanceId + meshName) : instances[i].instanceId;
70
+ const matrixVal = instance.matrix?.val;
71
+ if (matrixVal) {
72
+ const m4 = new THREE.Matrix4();
73
+ // m4.setPosition(new THREE.Vector3(9999999, 9999999, 9999999)); // TODO 临时隐藏方案
74
+ m4.elements = instance.matrix.val;
75
+ model.setMatrixAt(index, m4);
76
+ }
77
+ // 需要先设置全部实例颜色,否则后续设置颜色无效
78
+ const { color } = mesh.prop;
79
+ const meshColor = customColor
80
+ ? new THREE.Color(customColor)
81
+ : new THREE.Color(`rgb(${color[0]}, ${color[1]}, ${color[2]})`);
82
+ model.setColorAt(index, meshColor);
83
+ }
84
+ });
85
+ // model.instanceColor.needsUpdate = true;
86
+ group.add(model);
89
87
  });
90
- // model.instanceColor.needsUpdate = true;
91
- group.add(model);
92
- });
93
- modelGroup.add(group);
88
+ modelGroup.add(group);
89
+ }
94
90
  }
91
+
95
92
  }
96
93
  console.log('modelGroup', modelGroup)
97
94
  return modelGroup;
package/src/utils/mock.js CHANGED
@@ -46,7 +46,7 @@ function mockRequest(options = {}) {
46
46
  }
47
47
  }
48
48
 
49
- // openMock = 1;
49
+ openMock = 1;
50
50
 
51
51
  openMock && setTimeout(() => {
52
52
  console.log(`%c已加载本地模型数据`, "color:red;font-size:3em;");