nitrogen.dishang.threejs.ts 7.1 KB

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