nitrogen.dishang.threejs.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. 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 && this.group) {
  119. for (let i = 0; i < monitorDataGroup.length; i++) {
  120. const n = monitorDataGroup[i];
  121. const compressorModal = this.group.getObjectByName('compressorModal' + i) as THREE.Object3D;
  122. const compressorGroup = this.cssGroupParent.getObjectByName('compressorGroup' + n);
  123. if (!compressorGroup) {
  124. const cssGroup = new THREE.Object3D();
  125. cssGroup.name = 'compressorGroup' + n;
  126. cssGroup.applyMatrix4(compressorModal.matrixWorld);
  127. const compressorElement = document.getElementById('nitrogenMonitor' + n) as HTMLElement;
  128. if (compressorElement) {
  129. compressorElement.style.top = '0px';
  130. compressorElement.style.left = '0px';
  131. const compressorMonitorCSS3D = new CSS3DSprite(compressorElement);
  132. compressorMonitorCSS3D.name = 'monitorNitrogenText' + n;
  133. if (monitorDataGroup.length > 4) {
  134. compressorMonitorCSS3D.scale.set(0.0035, 0.0035, 0.0035);
  135. } else {
  136. compressorMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
  137. }
  138. if (i == 0) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0);
  139. if (i == 1) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.04);
  140. if (i == 2) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  141. if (i == 3) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  142. if (i == 4) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.16);
  143. if (i == 5) compressorMonitorCSS3D.position.set(-0.89, 0.31, 0.2);
  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 (i == 5) cqgMonitorCSS3D.position.set(1.24, 0.31, 0.2);
  158. if (monitorDataGroup.length > 4) {
  159. cqgMonitorCSS3D.scale.set(0.0035, 0.0035, 0.0035);
  160. } else {
  161. cqgMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
  162. }
  163. cssGroup.add(cqgMonitorCSS3D);
  164. }
  165. this.cssGroupParent.add(cssGroup);
  166. } else {
  167. }
  168. }
  169. }
  170. };
  171. clearCssText = () => {
  172. for (let i = 0; i < this.cssGroupParent.children.length; i++) {
  173. const item = this.cssGroupParent.children[i];
  174. this.cssGroupParent.remove(item);
  175. }
  176. };
  177. /**
  178. * 处理杯子的纹理和杯子外层透明壳子
  179. */
  180. handleGlassAndWrap = (
  181. objects: THREE.Object3D,
  182. withVolume: THREE.Object3D[],
  183. glassModel: THREE.Mesh,
  184. params: THREE.MeshPhysicalMaterialParameters,
  185. scale: number,
  186. position: THREE.Vector3,
  187. rotation?: THREE.Vector3
  188. ) => {
  189. //辨别杯子和壳 大的是杯子 小的是壳 壳的点比杯子少
  190. const children = glassModel.children as THREE.Mesh[];
  191. this.sortMeshChildren(children);
  192. children.forEach((mesh) => {
  193. mesh.position.copy(position);
  194. mesh.scale.set(scale, scale, scale);
  195. rotation && mesh.rotation.setFromVector3(rotation, 'XYZ');
  196. });
  197. const [transparentWrap, glass] = children;
  198. this.transparentModel(transparentWrap);
  199. glass.material = new THREE.MeshPhysicalMaterial({
  200. side: THREE.DoubleSide,
  201. // specularColor: new Color("#ffffff"),
  202. // color: new Color(0xffa000),
  203. ...params,
  204. });
  205. objects.add(...children);
  206. //只检测壳子 减小开销
  207. withVolume.push(transparentWrap);
  208. };
  209. setModal(nitrogenNum) {
  210. if (nitrogenNum < 1) {
  211. return;
  212. }
  213. if (this.singGltf) {
  214. this.nitrogenNum = nitrogenNum;
  215. const nitrogenGroup = new THREE.Object3D();
  216. const flag = nitrogenNum % 2 == 0 ? 0 : 1;
  217. for (let i = 0; i < nitrogenNum; i++) {
  218. const compressorModal = this.singGltf?.clone();
  219. compressorModal.name = 'compressorModal' + i;
  220. const c = Math.floor(nitrogenNum / 2);
  221. if (flag) {
  222. compressorModal.position.set(0, 0, 1.29 * (c - i));
  223. } else {
  224. compressorModal.position.set(0, 0, (c - i - 0.5) * 1.29);
  225. }
  226. nitrogenGroup.add(compressorModal);
  227. }
  228. this.group = nitrogenGroup;
  229. this.group.name = this.modelName;
  230. setModalCenter(this.group);
  231. this.setModalPosition();
  232. }
  233. }
  234. mountedThree(monitorDataGroup, dom) {
  235. return new Promise((resolve) => {
  236. this.CSSCanvasContainer = document.querySelector(dom);
  237. if (this.CSSCanvasContainer) {
  238. this.css3dRender = new CSS3DRenderer() as CSS3DRenderer;
  239. this.css3dRender.setSize(this.model.canvasContainer.clientWidth, this.model.canvasContainer.clientHeight);
  240. this.CSSCanvasContainer?.appendChild(this.css3dRender.domElement);
  241. }
  242. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  243. this.singGltf = gltf[0];
  244. resolve(null);
  245. this.setModal(monitorDataGroup.length);
  246. this.addCssText(monitorDataGroup);
  247. // this.addLight();
  248. this.group.add(this.cssGroupParent);
  249. });
  250. });
  251. }
  252. destroy() {
  253. if (this.group) {
  254. this.model.clearGroup(this.group);
  255. }
  256. this.model = null;
  257. this.group = null;
  258. }
  259. }
  260. export default Nitrogen;