nitrogen.dishang.threejs.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. this.isLRAnimation = false;
  78. if (this.animationTimer) {
  79. clearTimeout(this.animationTimer);
  80. this.animationTimer = null;
  81. }
  82. intersects.find((intersect) => {
  83. const mesh = intersect.object;
  84. return false;
  85. });
  86. }
  87. mouseUpModel() {}
  88. // 播放动画
  89. play() {}
  90. /**
  91. * 生序排列模型的子元素
  92. */
  93. sortMeshChildren = (children: THREE.Mesh[]) => {
  94. //生序排列
  95. children.sort((x, y) => {
  96. return x.geometry.attributes.position.count - y.geometry.attributes.position.count;
  97. });
  98. return children;
  99. };
  100. /**
  101. * 设置模型透明
  102. */
  103. transparentModel = (model: THREE.Mesh) => {
  104. const transparentMaterial = new THREE.MeshBasicMaterial({
  105. transparent: true,
  106. opacity: 0,
  107. });
  108. model.material = transparentMaterial;
  109. };
  110. addCssText = () => {
  111. if (this.nitrogenNum > 0) {
  112. for (let i = 0; i < this.nitrogenNum; i++) {
  113. const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
  114. if (nitrogenModal && !nitrogenModal.getObjectByName('monitorNitrogenText')) {
  115. const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
  116. if (element) {
  117. const elementCopy = element.cloneNode(true) as HTMLElement;
  118. elementCopy.style.top = '0px';
  119. elementCopy.style.left = '0px';
  120. const nitrogenMonitorCSS3D = new CSS3DSprite(elementCopy);
  121. nitrogenMonitorCSS3D.name = 'monitorNitrogenText';
  122. nitrogenMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
  123. if (i == 0) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0);
  124. if (i == 1) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.04);
  125. if (i == 2) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  126. if (i == 3) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  127. nitrogenModal.add(nitrogenMonitorCSS3D);
  128. }
  129. }
  130. }
  131. }
  132. };
  133. clearCssText = () => {
  134. if (this.nitrogenNum > 0) {
  135. for (let i = 0; i < this.nitrogenNum; i++) {
  136. const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
  137. if (nitrogenModal && nitrogenModal.getObjectByName('monitorNitrogenText')) {
  138. nitrogenModal.remove(nitrogenModal.getObjectByName('monitorNitrogenText') as THREE.Object3D);
  139. }
  140. }
  141. }
  142. };
  143. /**
  144. * 处理杯子的纹理和杯子外层透明壳子
  145. */
  146. handleGlassAndWrap = (
  147. objects: THREE.Object3D,
  148. withVolume: THREE.Object3D[],
  149. glassModel: THREE.Mesh,
  150. params: THREE.MeshPhysicalMaterialParameters,
  151. scale: number,
  152. position: THREE.Vector3,
  153. rotation?: THREE.Vector3
  154. ) => {
  155. //辨别杯子和壳 大的是杯子 小的是壳 壳的点比杯子少
  156. const children = glassModel.children as THREE.Mesh[];
  157. this.sortMeshChildren(children);
  158. children.forEach((mesh) => {
  159. mesh.position.copy(position);
  160. mesh.scale.set(scale, scale, scale);
  161. rotation && mesh.rotation.setFromVector3(rotation, 'XYZ');
  162. });
  163. const [transparentWrap, glass] = children;
  164. this.transparentModel(transparentWrap);
  165. glass.material = new THREE.MeshPhysicalMaterial({
  166. side: THREE.DoubleSide,
  167. // specularColor: new Color("#ffffff"),
  168. // color: new Color(0xffa000),
  169. ...params,
  170. });
  171. objects.add(...children);
  172. //只检测壳子 减小开销
  173. withVolume.push(transparentWrap);
  174. };
  175. mountedThree(nitrogenNum) {
  176. // this.nitrogenNum = nitrogenNum;
  177. return new Promise((resolve) => {
  178. if (nitrogenNum < 1) {
  179. resolve(null);
  180. return;
  181. }
  182. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  183. for (let i = 0; i < nitrogenNum; i++) {
  184. const nitrogenModal = gltf[0].clone();
  185. nitrogenModal.name = 'nitrogenModal' + i;
  186. this.nitrogenGroup.push(nitrogenModal);
  187. // const c = Math.floor(nitrogenNum / 2);
  188. // if (flag) {
  189. // nitrogenModal.position.set(0, 0, 1.355 * (c - i));
  190. // } else {
  191. // nitrogenModal.position.set(0, 0, (c - i - 0.5) * 1.355);
  192. // }
  193. }
  194. // this.group = nitrogenGroup;
  195. this.addLight();
  196. resolve(null);
  197. });
  198. });
  199. }
  200. destroy() {
  201. this.nitrogenGroup.forEach((item) => {
  202. this.model.clearGroup(item);
  203. });
  204. this.nitrogenGroup = undefined;
  205. if (this.group) {
  206. this.model.clearGroup(this.group);
  207. }
  208. this.model = null;
  209. this.group = null;
  210. }
  211. }
  212. export default NitrogenOverground;