nitrogen.dishang.threejs.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import * as THREE from 'three';
  2. import { getTextCanvas, addEnvMap, setModalCenter } from '/@/utils/threejs/util';
  3. import { CSS3DSprite } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  4. // import * as dat from 'dat.gui';
  5. // const gui = new dat.GUI();
  6. // gui.domElement.style = 'position:absolute;top:100px;left:10px;z-index:99999999999999';
  7. class NitrogenOverground {
  8. model;
  9. modelName = 'nitrogen';
  10. group;
  11. animationTimer;
  12. isLRAnimation = true;
  13. direction = 1;
  14. playerStartClickTime1 = new Date().getTime();
  15. playerStartClickTime2 = new Date().getTime();
  16. deviceRunState = '';
  17. nitrogenNum = 0;
  18. nitrogenGroup = <THREE.Object3D[]>[];
  19. constructor(model) {
  20. this.model = model;
  21. this.group = new THREE.Object3D();
  22. this.group.name = this.modelName;
  23. }
  24. addLight() {
  25. const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  26. directionalLight.position.set(32, -1, 60);
  27. this.group.add(directionalLight);
  28. directionalLight.target = this.group;
  29. // gui.add(directionalLight.position, 'x', -500, 500)
  30. // gui.add(directionalLight.position, 'y', -500, 500)
  31. // gui.add(directionalLight.position, 'z', -200, 200)
  32. const spotLight = new THREE.SpotLight();
  33. spotLight.angle = Math.PI / 4;
  34. spotLight.penumbra = 0;
  35. spotLight.castShadow = true;
  36. spotLight.distance = 0;
  37. spotLight.position.set(-88, 85, -88);
  38. spotLight.target = this.group;
  39. this.group.add(spotLight);
  40. spotLight.shadow.camera.near = 0.5; // default
  41. spotLight.shadow.camera.far = 1000; // default
  42. spotLight.shadow.focus = 1;
  43. spotLight.shadow.bias = -0.000002;
  44. // gui.add(spotLight.position, 'x', -800, 800).onChange(function (value) {
  45. // spotLight.position.x = Number(value);
  46. // });
  47. // gui.add(spotLight.position, 'y', -800, 800).onChange(function (value) {
  48. // spotLight.position.y = Number(value);
  49. // });
  50. // gui.add(spotLight.position, 'z', -800, 800).onChange(function (value) {
  51. // spotLight.position.z = Number(value);
  52. // });
  53. }
  54. // 设置模型位置
  55. setModalPosition() {
  56. if (this.nitrogenNum == 4) {
  57. this.group.position.set(0, -17, 3);
  58. this.group?.scale.set(24.0, 24.0, 24.0);
  59. }
  60. if (this.nitrogenNum == 3) {
  61. this.group.position.set(0, -1, 3);
  62. this.group?.scale.set(24.0, 24.0, 24.0);
  63. }
  64. if (this.nitrogenNum == 2) {
  65. this.group.position.set(0, 0.42, 1.21);
  66. this.group?.scale.set(24.0, 24.0, 24.0);
  67. }
  68. if (this.nitrogenNum == 1) {
  69. this.group.position.set(0, 8, 3.0);
  70. this.group?.scale.set(24.0, 24.0, 24.0);
  71. }
  72. }
  73. /* 提取风门序列帧,初始化前后门动画 */
  74. initAnimation() {}
  75. /* 点击 */
  76. mousedownModel(intersects: THREE.Intersection<THREE.Object3D<THREE.Event>>[]) {
  77. if (this.animationTimer) {
  78. clearTimeout(this.animationTimer);
  79. this.animationTimer = null;
  80. }
  81. intersects.find((intersect) => {
  82. const mesh = intersect.object;
  83. return false;
  84. });
  85. }
  86. mouseUpModel() {}
  87. // 播放动画
  88. play() {}
  89. /**
  90. * 生序排列模型的子元素
  91. */
  92. sortMeshChildren = (children: THREE.Mesh[]) => {
  93. //生序排列
  94. children.sort((x, y) => {
  95. return x.geometry.attributes.position.count - y.geometry.attributes.position.count;
  96. });
  97. return children;
  98. };
  99. /**
  100. * 设置模型透明
  101. */
  102. transparentModel = (model: THREE.Mesh) => {
  103. const transparentMaterial = new THREE.MeshBasicMaterial({
  104. transparent: true,
  105. opacity: 0,
  106. });
  107. model.material = transparentMaterial;
  108. };
  109. addCssText = () => {
  110. if (this.nitrogenNum > 0) {
  111. for (let i = 0; i < this.nitrogenNum; i++) {
  112. const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
  113. if (!nitrogenModal) continue;
  114. const name = 'monitorNitrogenText' + i;
  115. const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
  116. let sprite = nitrogenModal.getObjectByName(name) as CSS3DSprite | undefined;
  117. // 已挂过就复用(原守卫的名字少了序号,导致每次 addCssText 都重复挂一批精灵);
  118. // 节点被 Vue 重建过则换成挂到当前节点,旧节点已是孤儿可安全移除
  119. if (sprite && element && sprite.element !== element) {
  120. sprite.removeFromParent();
  121. sprite = undefined;
  122. }
  123. if (sprite) {
  124. sprite.visible = true;
  125. continue;
  126. }
  127. if (!element) continue;
  128. sprite = new CSS3DSprite(element);
  129. sprite.name = name;
  130. sprite.scale.set(0.003, 0.003, 0.003);
  131. if (i == 0) sprite.position.set(-0.89, 0.31, 0);
  132. if (i == 1) sprite.position.set(-0.89, 0.31, 0.04);
  133. if (i == 2) sprite.position.set(-0.89, 0.31, 0.08);
  134. if (i == 3) sprite.position.set(-0.89, 0.31, 0.12);
  135. nitrogenModal.add(sprite);
  136. }
  137. }
  138. };
  139. clearCssText = () => {
  140. // 只隐藏、不移除:CSS3DObject 被移除时会把它手里的 DOM 元素一并从文档删掉,之后再 addCssText 就取不到元素
  141. if (this.nitrogenNum <= 0) return;
  142. for (let i = 0; i < this.nitrogenNum; i++) {
  143. const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
  144. const sprite = nitrogenModal?.getObjectByName('monitorNitrogenText' + i);
  145. if (sprite) (sprite as THREE.Object3D).visible = false;
  146. }
  147. };
  148. /**
  149. * 处理杯子的纹理和杯子外层透明壳子
  150. */
  151. handleGlassAndWrap = (
  152. objects: THREE.Object3D,
  153. withVolume: THREE.Object3D[],
  154. glassModel: THREE.Mesh,
  155. params: THREE.MeshPhysicalMaterialParameters,
  156. scale: number,
  157. position: THREE.Vector3,
  158. rotation?: THREE.Vector3
  159. ) => {
  160. //辨别杯子和壳 大的是杯子 小的是壳 壳的点比杯子少
  161. const children = glassModel.children as THREE.Mesh[];
  162. this.sortMeshChildren(children);
  163. children.forEach((mesh) => {
  164. mesh.position.copy(position);
  165. mesh.scale.set(scale, scale, scale);
  166. rotation && mesh.rotation.setFromVector3(rotation, 'XYZ');
  167. });
  168. const [transparentWrap, glass] = children;
  169. this.transparentModel(transparentWrap);
  170. glass.material = new THREE.MeshPhysicalMaterial({
  171. side: THREE.DoubleSide,
  172. // specularColor: new Color("#ffffff"),
  173. // color: new Color(0xffa000),
  174. ...params,
  175. });
  176. objects.add(...children);
  177. //只检测壳子 减小开销
  178. withVolume.push(transparentWrap);
  179. };
  180. mountedThree(nitrogenNum) {
  181. // this.nitrogenNum = nitrogenNum;
  182. return new Promise((resolve) => {
  183. if (nitrogenNum < 1) {
  184. resolve(null);
  185. return;
  186. }
  187. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  188. for (let i = 0; i < nitrogenNum; i++) {
  189. const nitrogenModal = gltf[0].clone();
  190. nitrogenModal.name = 'nitrogenModal' + i;
  191. this.nitrogenGroup.push(nitrogenModal);
  192. // const c = Math.floor(nitrogenNum / 2);
  193. // if (flag) {
  194. // nitrogenModal.position.set(0, 0, 1.355 * (c - i));
  195. // } else {
  196. // nitrogenModal.position.set(0, 0, (c - i - 0.5) * 1.355);
  197. // }
  198. }
  199. // this.group = nitrogenGroup;
  200. this.addLight();
  201. resolve(null);
  202. });
  203. });
  204. }
  205. destroy() {
  206. this.nitrogenGroup.forEach((item) => {
  207. this.model.clearGroup(item);
  208. });
  209. this.nitrogenGroup = undefined;
  210. if (this.group) {
  211. this.model.clearGroup(this.group);
  212. }
  213. this.model = null;
  214. this.group = null;
  215. }
  216. }
  217. export default NitrogenOverground;