nitrogen.dishang.threejs.ts 9.5 KB

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