nitrogen.dishang.threejs.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 && !nitrogenModal.getObjectByName('monitorNitrogenText')) {
  114. const element = document.getElementById('nitrogenMonitor' + (i + 1)) as HTMLElement;
  115. if (element) {
  116. const nitrogenMonitorCSS3D = new CSS3DSprite(element);
  117. nitrogenMonitorCSS3D.name = 'monitorNitrogenText' + i;
  118. nitrogenMonitorCSS3D.scale.set(0.003, 0.003, 0.003);
  119. if (i == 0) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0);
  120. if (i == 1) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.04);
  121. if (i == 2) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.08);
  122. if (i == 3) nitrogenMonitorCSS3D.position.set(-0.89, 0.31, 0.12);
  123. nitrogenModal.add(nitrogenMonitorCSS3D);
  124. }
  125. }
  126. }
  127. }
  128. };
  129. clearCssText = () => {
  130. if (this.nitrogenNum > 0) {
  131. for (let i = 0; i < this.nitrogenNum; i++) {
  132. const nitrogenModal = this.group.getObjectByName('nitrogenModal' + i) as THREE.Object3D;
  133. if (nitrogenModal && nitrogenModal.getObjectByName('monitorNitrogenText')) {
  134. nitrogenModal.remove(nitrogenModal.getObjectByName('monitorNitrogenText') as THREE.Object3D);
  135. }
  136. }
  137. }
  138. };
  139. /**
  140. * 处理杯子的纹理和杯子外层透明壳子
  141. */
  142. handleGlassAndWrap = (
  143. objects: THREE.Object3D,
  144. withVolume: THREE.Object3D[],
  145. glassModel: THREE.Mesh,
  146. params: THREE.MeshPhysicalMaterialParameters,
  147. scale: number,
  148. position: THREE.Vector3,
  149. rotation?: THREE.Vector3
  150. ) => {
  151. //辨别杯子和壳 大的是杯子 小的是壳 壳的点比杯子少
  152. const children = glassModel.children as THREE.Mesh[];
  153. this.sortMeshChildren(children);
  154. children.forEach((mesh) => {
  155. mesh.position.copy(position);
  156. mesh.scale.set(scale, scale, scale);
  157. rotation && mesh.rotation.setFromVector3(rotation, 'XYZ');
  158. });
  159. const [transparentWrap, glass] = children;
  160. this.transparentModel(transparentWrap);
  161. glass.material = new THREE.MeshPhysicalMaterial({
  162. side: THREE.DoubleSide,
  163. // specularColor: new Color("#ffffff"),
  164. // color: new Color(0xffa000),
  165. ...params,
  166. });
  167. objects.add(...children);
  168. //只检测壳子 减小开销
  169. withVolume.push(transparentWrap);
  170. };
  171. mountedThree(nitrogenNum) {
  172. // this.nitrogenNum = nitrogenNum;
  173. return new Promise((resolve) => {
  174. if (nitrogenNum < 1) {
  175. resolve(null);
  176. return;
  177. }
  178. this.model.setGLTFModel([this.modelName]).then(async (gltf) => {
  179. for (let i = 0; i < nitrogenNum; i++) {
  180. const nitrogenModal = gltf[0].clone();
  181. nitrogenModal.name = 'nitrogenModal' + i;
  182. this.nitrogenGroup.push(nitrogenModal);
  183. // const c = Math.floor(nitrogenNum / 2);
  184. // if (flag) {
  185. // nitrogenModal.position.set(0, 0, 1.355 * (c - i));
  186. // } else {
  187. // nitrogenModal.position.set(0, 0, (c - i - 0.5) * 1.355);
  188. // }
  189. }
  190. // this.group = nitrogenGroup;
  191. this.addLight();
  192. resolve(null);
  193. });
  194. });
  195. }
  196. destroy() {
  197. this.nitrogenGroup.forEach((item) => {
  198. this.model.clearGroup(item);
  199. });
  200. this.nitrogenGroup = undefined;
  201. if (this.group) {
  202. this.model.clearGroup(this.group);
  203. }
  204. this.model = null;
  205. this.group = null;
  206. }
  207. }
  208. export default NitrogenOverground;