nitrogen.dishang.threejs.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. import * as THREE from 'three';
  2. import { setModalCenter } from '/@/utils/threejs/util';
  3. import { CSS3DSprite } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  4. import { CSS3DRenderer } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  5. // import * as dat from 'dat.gui';
  6. // const gui = new dat.GUI();
  7. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  8. class Nitrogen {
  9. model;
  10. modelName = 'compressor';
  11. group: THREE.Object3D = new THREE.Object3D();
  12. animationTimer;
  13. isLRAnimation = true;
  14. direction = 1;
  15. playerStartClickTime1 = new Date().getTime();
  16. playerStartClickTime2 = new Date().getTime();
  17. deviceRunState = '';
  18. nitrogenNum = 0;
  19. singGltf: THREE.Object3D | undefined = undefined;
  20. CSSCanvasContainer;
  21. css3dRender;
  22. canvasContainer;
  23. constructor(model) {
  24. this.model = model;
  25. this.group.name = this.modelName;
  26. }
  27. addLight() {
  28. const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
  29. directionalLight.position.set(141, 259, 121);
  30. this.group.add(directionalLight);
  31. directionalLight.target = this.group;
  32. // gui.add(directionalLight.position, 'x', -100, 200).onChange(function (value) {
  33. // directionalLight.position.x = Number(value);
  34. // });
  35. // gui.add(directionalLight.position, 'y', -500, 500).onChange(function (value) {
  36. // directionalLight.position.y = Number(value);
  37. // });
  38. // gui.add(directionalLight.position, 'z', -200, 200).onChange(function (value) {
  39. // directionalLight.position.z = Number(value);
  40. // });
  41. // const spotLight = new THREE.SpotLight();
  42. // spotLight.angle = Math.PI / 4;
  43. // spotLight.penumbra = 0;
  44. // spotLight.castShadow = true;
  45. // spotLight.distance = 0;
  46. // spotLight.position.set(-36, 68, 189);
  47. // spotLight.target = this.group;
  48. // this.group.add(spotLight);
  49. // spotLight.shadow.camera.near = 0.5; // default
  50. // spotLight.shadow.camera.far = 1000; // default
  51. // spotLight.shadow.focus = 1;
  52. // spotLight.shadow.bias = -0.000002;
  53. // gui.add(spotLight.position, 'x', -800, 800).onChange(function (value) {
  54. // spotLight.position.x = Number(value);
  55. // });
  56. // gui.add(spotLight.position, 'y', -800, 800).onChange(function (value) {
  57. // spotLight.position.y = Number(value);
  58. // });
  59. // gui.add(spotLight.position, 'z', -800, 800).onChange(function (value) {
  60. // spotLight.position.z = Number(value);
  61. // });
  62. }
  63. // 设置模型位置
  64. setModalPosition() {
  65. this.group.position.set(0, 0, 0);
  66. this.group?.scale.set(24.0, 24.0, 24.0);
  67. // if (this.nitrogenNum == 4) {
  68. // this.group.position.set(0, -17, 3);
  69. // }
  70. // if (this.nitrogenNum == 3) {
  71. // this.group.position.set(0, -12, 3);
  72. // }
  73. // if (this.nitrogenNum == 2) {
  74. // this.group.position.set(0, 0.42, 1.21);
  75. // }
  76. // if (this.nitrogenNum == 1) {
  77. // this.group.position.set(0, 8, 3.0);
  78. // }
  79. }
  80. /* 提取风门序列帧,初始化前后门动画 */
  81. initAnimation() {}
  82. /* 点击 */
  83. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  84. this.isLRAnimation = false;
  85. if (this.animationTimer) {
  86. clearTimeout(this.animationTimer);
  87. this.animationTimer = null;
  88. }
  89. intersects.find((intersect) => {
  90. const mesh = intersect.object;
  91. return false;
  92. });
  93. }
  94. mouseUpModel() {}
  95. // 播放动画
  96. play() {}
  97. /**
  98. * 生序排列模型的子元素
  99. */
  100. sortMeshChildren = (children: THREE.Mesh[]) => {
  101. //生序排列
  102. children.sort((x, y) => {
  103. return x.geometry.attributes.position.count - y.geometry.attributes.position.count;
  104. });
  105. return children;
  106. };
  107. /**
  108. * 设置模型透明
  109. */
  110. transparentModel = (model: THREE.Mesh) => {
  111. const transparentMaterial = new THREE.MeshBasicMaterial({
  112. transparent: true,
  113. opacity: 0,
  114. });
  115. model.material = transparentMaterial;
  116. };
  117. addCssText = (monitorDataGroup) => {
  118. if (this.nitrogenNum > 0) {
  119. for (let i = 0; i < this.nitrogenNum; i++) {
  120. const n = monitorDataGroup[i];
  121. const compressorModal = this.group.getObjectByName('compressorModal' + i) as THREE.Object3D;
  122. if (!compressorModal.getObjectByName('monitorNitrogenText')) {
  123. const element = document.getElementById('nitrogenMonitor' + n) as HTMLElement;
  124. if (element) {
  125. element.style.top = '0px';
  126. element.style.left = '0px';
  127. const compressorMonitorCSS3D = new CSS3DSprite(element);
  128. compressorMonitorCSS3D.name = 'monitorNitrogenText';
  129. compressorMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
  130. if (i == 0) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0);
  131. if (i == 1) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.04);
  132. if (i == 2) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  133. if (i == 3) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  134. compressorModal.add(compressorMonitorCSS3D);
  135. }
  136. }
  137. if (!compressorModal.getObjectByName('cqgMonitorText')) {
  138. const element = document.getElementById('cqgMonitor' + n) as HTMLElement;
  139. if (element) {
  140. element.style.top = '0px';
  141. element.style.left = '0px';
  142. const cqgMonitorCSS3D = new CSS3DSprite(element);
  143. cqgMonitorCSS3D.name = 'cqgMonitorText';
  144. cqgMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
  145. if (i == 0) cqgMonitorCSS3D.position.set(1.24, 0.49, 0.0);
  146. if (i == 1) cqgMonitorCSS3D.position.set(1.24, 0.49, 0.04);
  147. if (i == 2) cqgMonitorCSS3D.position.set(1.24, 0.49, 0.08);
  148. if (i == 3) cqgMonitorCSS3D.position.set(1.24, 0.49, 0.12);
  149. compressorModal.add(cqgMonitorCSS3D);
  150. }
  151. }
  152. }
  153. }
  154. };
  155. /**
  156. * 处理杯子的纹理和杯子外层透明壳子
  157. */
  158. handleGlassAndWrap = (
  159. objects: THREE.Object3D,
  160. withVolume: THREE.Object3D[],
  161. glassModel: THREE.Mesh,
  162. params: THREE.MeshPhysicalMaterialParameters,
  163. scale: number,
  164. position: THREE.Vector3,
  165. rotation?: THREE.Vector3
  166. ) => {
  167. //辨别杯子和壳 大的是杯子 小的是壳 壳的点比杯子少
  168. const children = glassModel.children as THREE.Mesh[];
  169. this.sortMeshChildren(children);
  170. children.forEach((mesh) => {
  171. mesh.position.copy(position);
  172. mesh.scale.set(scale, scale, scale);
  173. rotation && mesh.rotation.setFromVector3(rotation, 'XYZ');
  174. });
  175. const [transparentWrap, glass] = children;
  176. this.transparentModel(transparentWrap);
  177. glass.material = new THREE.MeshPhysicalMaterial({
  178. side: THREE.DoubleSide,
  179. // specularColor: new Color("#ffffff"),
  180. // color: new Color(0xffa000),
  181. ...params,
  182. });
  183. objects.add(...children);
  184. //只检测壳子 减小开销
  185. withVolume.push(transparentWrap);
  186. };
  187. setModal(nitrogenNum) {
  188. if (nitrogenNum < 1) {
  189. return;
  190. }
  191. if (this.singGltf) {
  192. this.nitrogenNum = nitrogenNum;
  193. const nitrogenGroup = new THREE.Object3D();
  194. const flag = nitrogenNum % 2 == 0 ? 0 : 1;
  195. for (let i = 0; i < nitrogenNum; i++) {
  196. const compressorModal = this.singGltf?.clone();
  197. compressorModal.name = 'compressorModal' + i;
  198. const c = Math.floor(nitrogenNum / 2);
  199. if (flag) {
  200. compressorModal.position.set(0, 0, 1.29 * (c - i));
  201. } else {
  202. compressorModal.position.set(0, 0, (c - i - 0.5) * 1.29);
  203. }
  204. nitrogenGroup.add(compressorModal);
  205. }
  206. this.group = nitrogenGroup;
  207. this.group.name = this.modelName;
  208. setModalCenter(this.group);
  209. this.setModalPosition();
  210. }
  211. }
  212. mountedThree(monitorDataGroup, dom) {
  213. return new Promise((resolve) => {
  214. this.CSSCanvasContainer = document.querySelector(dom);
  215. if (this.CSSCanvasContainer) {
  216. this.css3dRender = new CSS3DRenderer() as CSS3DRenderer;
  217. this.css3dRender.setSize(this.model.canvasContainer.clientWidth, this.model.canvasContainer.clientHeight);
  218. this.CSSCanvasContainer?.appendChild(this.css3dRender.domElement);
  219. this.css3dRender.render(this.model.scene as THREE.Scene, this.model.camera as THREE.PerspectiveCamera);
  220. }
  221. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  222. this.singGltf = gltf[0];
  223. this.setModal(monitorDataGroup.length);
  224. this.addCssText(monitorDataGroup);
  225. resolve(null);
  226. this.addLight();
  227. });
  228. });
  229. }
  230. destroy() {
  231. if (this.group) {
  232. this.model.clearGroup(this.group);
  233. }
  234. this.model = null;
  235. this.group = null;
  236. }
  237. }
  238. export default Nitrogen;