useThree.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. import * as THREE from 'three';
  2. // 导入轨道控制器
  3. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  4. import { CSS3DRenderer } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  5. import { CSS2DRenderer } from 'three/examples/jsm/renderers/CSS2DRenderer.js';
  6. import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
  7. // import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader.js';
  8. import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
  9. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  10. import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader.js';
  11. import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
  12. import Stats from 'three/examples/jsm/libs/stats.module.js';
  13. import { useModelStore } from '/@/store/modules/threejs';
  14. import TWEEN from 'three/examples/jsm/libs/tween.module.js';
  15. import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
  16. import { useGlobSetting } from '/@/hooks/setting';
  17. import { getList } from '@/views/vent/sys/resources/file.api';
  18. import { saveModel } from '/@/utils/threejs/util';
  19. const globSetting = useGlobSetting();
  20. const baseApiUrl = globSetting.domainUrl;
  21. export function useThree(containerID: string, css3dContainerID: string, css2dContainerID: string) {}
  22. class UseThree {
  23. constructor(canvasSelector, css3Canvas?, css2Canvas?) {
  24. this.canvasContainer = document.querySelector(canvasSelector);
  25. //初始化
  26. this.init(css3Canvas, css2Canvas);
  27. // this.animate();
  28. window.addEventListener('resize', this.resizeRenderer.bind(this));
  29. // 添加滚动事件,鼠标滚动模型执行动画
  30. // window.addEventListener('wheel', this.wheelRenderer.bind(this));
  31. // this.canvasContainer?.appendChild(gui.domElement);
  32. }
  33. init(css3Canvas?, css2Canvas?) {
  34. // 初始化场景
  35. this.initScene();
  36. // 初始化环境光
  37. // this.initLight();
  38. // 初始化相机
  39. this.initCamera();
  40. //初始化渲染器
  41. this.initRenderer();
  42. // 初始化控制器
  43. this.initControles();
  44. if (css3Canvas) {
  45. this.initCSS3Renderer(css3Canvas);
  46. }
  47. if (css2Canvas) {
  48. this.initCSS2Renderer(css2Canvas);
  49. }
  50. // this.setTestPlane();
  51. this.rayCaster = new THREE.Raycaster();
  52. // this.createStats();
  53. // this.removeSawtooth();
  54. }
  55. createStats() {
  56. this.stats = Stats();
  57. this.stats?.setMode(0);
  58. this.stats.domElement.style = 'position: absolute; top: 300px';
  59. this.canvasContainer?.appendChild(this.stats.domElement);
  60. }
  61. initScene() {
  62. this.scene = new THREE.Scene();
  63. // const axesHelper = new THREE.AxesHelper(100);
  64. // this.scene?.add(axesHelper);
  65. // const size = 1000;
  66. // const divisions = 10;
  67. // const gridHelper = new THREE.GridHelper(size, divisions);
  68. // this.scene?.add(gridHelper);
  69. }
  70. initLight() {
  71. // const light = new THREE.AmbientLight(0xffffff, 1);
  72. // light.position.set(0, 1000, 1000);
  73. // (this.scene as THREE.Scene).add(light);
  74. }
  75. initCamera() {
  76. // this.camera = new THREE.PerspectiveCamera(50, this.canvasContainer.clientWidth / this.canvasContainer.clientHeight, 0.0000001, 1000);
  77. if (!window['$camera']) {
  78. throw new Error('threejs摄像头初始化异常!');
  79. } else {
  80. this.camera = window['$camera'] as THREE.PerspectiveCamera;
  81. this.camera.layers.enableAll();
  82. if (this.canvasContainer) this.camera.aspect = this.canvasContainer.clientWidth / this.canvasContainer.clientHeight;
  83. this.camera.near = 0.0000001;
  84. this.camera.far = 1000;
  85. }
  86. //
  87. // const helper = new THREE.CameraHelper(this.camera);
  88. // this.scene?.add(helper);
  89. // gui.add(this.camera.position, 'x', 0.00001, 10000);
  90. // gui.add(this.camera.position, 'y', 0.00001, 10000);
  91. // gui.add(this.camera.position, 'z', 0.00001, 10000);
  92. // gui.add(this.camera, 'near', 0.01, 1).step(0.01);
  93. // gui.add(this.camera, 'far', 10, 100000);
  94. // gui.add(this.camera, 'fov', 0, 180);
  95. }
  96. initRenderer() {
  97. if (!window['$renderer']) {
  98. throw new Error('threejs渲染器初始化异常!');
  99. } else {
  100. this.renderer = window['$renderer'];
  101. if (this.canvasContainer) {
  102. this.renderer.toneMappingExposure = 1.0;
  103. this.renderer.toneMapping = THREE.ACESFilmicToneMapping;
  104. // const gl = this.renderer?.getContext('webgl');
  105. // gl && gl.getExtension('WEBGL_lose_context')?.restoreContext();
  106. // this.renderer?.forceContextRestore()
  107. this.renderer?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  108. this.canvasContainer.appendChild(this.renderer.domElement);
  109. }
  110. }
  111. }
  112. initCSS3Renderer(cssCanvas) {
  113. this.CSSCanvasContainer = document.querySelector(cssCanvas);
  114. if (this.CSSCanvasContainer) {
  115. this.css3dRender = new CSS3DRenderer() as CSS3DRenderer;
  116. this.css3dRender.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  117. this.CSSCanvasContainer?.appendChild(this.css3dRender.domElement);
  118. this.css3dRender.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  119. // this.css3dRender.domElement.style.pointerEvents = 'none';
  120. // this.orbitControls = new OrbitControls(this.camera as THREE.Camera, this.css3dRender?.domElement) as OrbitControls;
  121. // this.orbitControls.update();
  122. }
  123. }
  124. initCSS2Renderer(cssCanvas) {
  125. this.CSSCanvasContainer = document.querySelector(cssCanvas);
  126. if (this.CSSCanvasContainer) {
  127. this.css2dRender = new CSS2DRenderer();
  128. this.css2dRender.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  129. this.CSSCanvasContainer?.appendChild(this.css2dRender.domElement);
  130. this.css2dRender.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  131. // this.orbitControls = new OrbitControls(this.camera as THREE.Camera, this.css2dRender?.domElement) as OrbitControls;
  132. // this.orbitControls.update();
  133. // this.css2dRender.domElement.style.pointerEvents = 'none';
  134. }
  135. }
  136. initControles() {
  137. if (!window['$orbitControls']) {
  138. throw new Error('threejs控制器初始化异常!');
  139. } else {
  140. this.orbitControls = window['$orbitControls'];
  141. this.orbitControls.panSpeed = 1;
  142. this.orbitControls.rotateSpeed = 1;
  143. this.orbitControls.maxPolarAngle = Math.PI;
  144. this.orbitControls.minPolarAngle = 0;
  145. }
  146. // this.orbitControls = new OrbitControls(this.camera as THREE.Camera, this.renderer?.domElement) as OrbitControls;
  147. // this.orbitControls.update();
  148. // this.orbitControls.minDistance = 1;
  149. // this.orbitControls.maxDistance = 100;
  150. // this.orbitControls.maxDistance = true;
  151. }
  152. setGLTFModel(modalNames, group = null) {
  153. window['startTime'] = new Date().getTime();
  154. const modelStore = useModelStore();
  155. return new Promise(async (resolve, reject) => {
  156. try {
  157. const gltfLoader = new GLTFLoader();
  158. const dracoLoader = new DRACOLoader();
  159. dracoLoader.setDecoderPath('/model/draco/gltf/');
  160. dracoLoader.setDecoderConfig({ type: 'js' }); //使用兼容性强的draco_decoder.js解码器
  161. dracoLoader.preload();
  162. gltfLoader.setDRACOLoader(dracoLoader);
  163. const db = window['CustomDB'];
  164. const resolvePromise: Promise<any>[] = [];
  165. const modalNameArr = Object.prototype.toString.call(modalNames) === '[object Array]' ? modalNames : [modalNames];
  166. const len = modalNameArr.length;
  167. for (let i = 0; i < len; i++) {
  168. resolvePromise[i] = new Promise(async (childResolve, reject) => {
  169. try {
  170. // 解析模型
  171. const modalNameStr = modalNameArr[i];
  172. const data = modelStore.modelArr.get(modalNameStr) || null;
  173. let modalValue;
  174. if (!data) {
  175. const modalArr = await db.modal.where('modalName').equals(modalNameStr).toArray();
  176. if (modalArr.length > 0) {
  177. modalValue = modalArr[0].modalVal;
  178. }
  179. } else {
  180. modalValue = data.modalVal;
  181. }
  182. if (modalValue) {
  183. gltfLoader.parse(
  184. modalValue,
  185. '/model/glft/',
  186. (gltf) => {
  187. const object = gltf.scene;
  188. // setModalCenter(object);
  189. object.traverse((obj) => {
  190. if (obj instanceof THREE.Mesh) {
  191. obj.material.emissiveIntensity = 1;
  192. obj.material.emissiveMap = obj.material.map;
  193. obj.material.blending = THREE.CustomBlending;
  194. if (obj.material.opacity < 1) {
  195. obj.material.transparent = true;
  196. }
  197. if (obj.material.map) {
  198. obj.material.map.colorSpace = THREE.SRGBColorSpace;
  199. obj.material.map.flipY = false;
  200. obj.material.map.anisotropy = 1;
  201. }
  202. if (obj.material.emissiveMap) {
  203. obj.material.emissiveMap.colorSpace = THREE.SRGBColorSpace;
  204. obj.material.emissiveMap.flipY = false;
  205. }
  206. if (obj.material.map || obj.material.emissiveMap) {
  207. obj.material.needsUpdate = true;
  208. }
  209. // if (envMap) {
  210. // obj.material.envMap = envMap;
  211. // obj.material.envMapIntensity = 1;
  212. // }
  213. // obj.renderOrder = 1;
  214. }
  215. });
  216. object.animations = gltf.animations;
  217. object.name = modalNameStr;
  218. group?.add(object);
  219. childResolve(object);
  220. },
  221. (err) => {
  222. console.log(err);
  223. }
  224. );
  225. } else {
  226. // 开启线程下载
  227. console.log('需要开启线程下载模型资源。。。。。');
  228. const result = (await getList({ fileName: modalNameStr })) || [];
  229. const file = result['records'][0];
  230. if (file && file.path) {
  231. gltfLoader.load(`${baseApiUrl}/sys/common/static/${file.path}`, async (glft) => {
  232. if (glft) {
  233. const object = glft.scene;
  234. object.name = modalNameStr;
  235. if (glft.animations.length > 0) {
  236. object.animations = glft.animations;
  237. }
  238. group?.add(object);
  239. childResolve(object);
  240. const modalArr = await db.modal.where('modalName').equals(modalNameStr).toArray();
  241. if (modalArr.length < 1) {
  242. saveModel(modalNameStr, file.path, file.version);
  243. }
  244. }
  245. });
  246. } else {
  247. childResolve(null);
  248. }
  249. }
  250. } catch (error) {
  251. console.log(error);
  252. reject();
  253. }
  254. });
  255. }
  256. Promise.all(resolvePromise).then((objects) => {
  257. dracoLoader.dispose();
  258. resolve(objects);
  259. });
  260. } catch (error) {
  261. reject('加载模型出错');
  262. }
  263. });
  264. }
  265. // setFBXModel(modalNames, group = null) {
  266. // window['startTime'] = new Date().getTime();
  267. // const modelStore = useModelStore();
  268. // return new Promise(async (resolve, reject) => {
  269. // try {
  270. // const fbxLoader = new FBXLoader();
  271. // fbxLoader.setPath('/model/fbx/');
  272. // const db = window['CustomDB'];
  273. // const resolvePromise: Promise<any>[] = [];
  274. // let modalNameArr = Object.prototype.toString.call(modalNames) === '[object Array]' ? modalNames : [modalNames];
  275. // let len = modalNameArr.length;
  276. // for (let i = 0; i < len; i++) {
  277. // resolvePromise[i] = new Promise(async (childResolve, reject) => {
  278. // try {
  279. // // 解析模型
  280. // let modalValue;
  281. // const modalNameStr = modalNameArr[i];
  282. // let data = modelStore.modelArr.get(modalNameStr) || null;
  283. // if (!data) {
  284. // const modalArr = await db.modal.where('modalName').equals(modalNameStr).toArray();
  285. // if (modalArr.length > 0) modalValue = modalArr[0].modalVal;
  286. // } else {
  287. // modalValue = data.modalVal;
  288. // }
  289. // if (modalValue) {
  290. // const object = fbxLoader.parse(modalValue, '/model/fbx/');
  291. // // const object = fbx.scene;
  292. // // setModalCenter(object);
  293. // if (object) {
  294. // object.traverse((obj) => {
  295. // if (obj instanceof THREE.Mesh) {
  296. // obj.material.emissiveIntensity = 1;
  297. // obj.material.emissiveMap = obj.material.map;
  298. // obj.material.blending = THREE.CustomBlending;
  299. // if (obj.material.opacity < 1) {
  300. // obj.material.transparent = true;
  301. // }
  302. // if (obj.material.map) {
  303. // obj.material.map.encoding = THREE.sRGBEncoding;
  304. // obj.material.map.flipY = false;
  305. // obj.material.map.anisotropy = 1;
  306. // }
  307. // if (obj.material.emissiveMap) {
  308. // obj.material.emissiveMap.encoding = THREE.sRGBEncoding;
  309. // obj.material.emissiveMap.flipY = false;
  310. // }
  311. // if (obj.material.map || obj.material.emissiveMap) {
  312. // obj.material.needsUpdate = true;
  313. // }
  314. // // if (envMap) {
  315. // // obj.material.envMap = envMap;
  316. // // obj.material.envMapIntensity = 1;
  317. // // }
  318. // // obj.renderOrder = 1;
  319. // }
  320. // });
  321. // object.animations = object.animations;
  322. // object.name = modalNameStr;
  323. // group?.add(object);
  324. // childResolve(object);
  325. // }
  326. // }
  327. // } catch (error) {
  328. // console.log(error);
  329. // reject();
  330. // }
  331. // });
  332. // }
  333. // Promise.all(resolvePromise).then((objects) => {
  334. // resolve(objects);
  335. // });
  336. // } catch (error) {
  337. // reject('加载模型出错');
  338. // }
  339. // });
  340. // }
  341. setTestPlane() {
  342. const plane = new THREE.Mesh(new THREE.PlaneGeometry(10, 10), new THREE.MeshPhongMaterial({ color: 0x999999, specular: 0x101010 }));
  343. plane.rotation.x = -Math.PI / 2;
  344. plane.position.y = 0.03;
  345. plane.receiveShadow = true;
  346. this.scene?.add(plane);
  347. }
  348. removeSawtooth() {
  349. this.composer = new EffectComposer(this.renderer as THREE.WebGLRenderer);
  350. const FXAAShaderPass = new ShaderPass(FXAAShader);
  351. FXAAShaderPass.uniforms['resolution'].value.set(1 / this.canvasContainer.clientWidth, 1 / this.canvasContainer.clientHeight);
  352. FXAAShaderPass.renderToScreen = true;
  353. this.composer.addPass(FXAAShaderPass);
  354. }
  355. /* 场景环境背景 */
  356. setEnvMap(hdr) {
  357. if (!this.scene) return;
  358. // (this.scene as THREE.Scene).environment
  359. new RGBELoader().setPath('/model/hdr/').load(hdr + '.hdr', (texture) => {
  360. texture.colorSpace = THREE.SRGBColorSpace;
  361. texture.mapping = THREE.EquirectangularReflectionMapping;
  362. if (this.scene) (this.scene as THREE.Scene).environment = texture;
  363. texture.dispose();
  364. });
  365. }
  366. render() {
  367. this.startAnimation();
  368. this.orbitControls?.update();
  369. this.camera?.updateMatrixWorld();
  370. // this.composer?.render();
  371. this.css3dRender?.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  372. this.css2dRender?.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  373. this.renderer?.render(this.scene as THREE.Object3D, this.camera as THREE.Camera);
  374. }
  375. timeRender() {
  376. //设置为可渲染状态
  377. this.renderEnabled = true;
  378. //清除上次的延迟器
  379. if (this.timeOut) {
  380. clearTimeout(this.timeOut);
  381. }
  382. this.timeOut = setTimeout(() => {
  383. this.renderEnabled = false;
  384. }, 3000);
  385. }
  386. /* 漫游 */
  387. startMY() {}
  388. /* 初始动画 */
  389. startAnimation() {}
  390. renderAnimationScene() {}
  391. animate() {
  392. if (this.isRender) {
  393. this.animationId = requestAnimationFrame(this.animate.bind(this));
  394. const T = this.clock?.getDelta() || 0;
  395. this.timeS = this.timeS + T;
  396. if (this.timeS > this.renderT) {
  397. this.renderAnimationScene();
  398. if (this.renderEnabled) {
  399. this.render();
  400. }
  401. this.stats?.update();
  402. this.timeS = 0;
  403. }
  404. // this.stats?.update();
  405. // // TWEEN.update();
  406. // // this.renderAnimationScene();
  407. // if (this.renderEnabled) {
  408. // this.render();
  409. // }
  410. }
  411. }
  412. resizeRenderer() {
  413. // 更新相机比例
  414. if (this.camera) {
  415. (this.camera as THREE.PerspectiveCamera).aspect = this.canvasContainer.clientWidth / this.canvasContainer.clientHeight;
  416. // 刷新相机矩阵
  417. this.camera?.updateProjectionMatrix();
  418. }
  419. // 设置场景尺寸
  420. this.renderer?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  421. this.css3dRender?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  422. this.css2dRender?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  423. }
  424. wheelRenderer(e: WheelEvent) {
  425. const timeScale = e.deltaY > 0 ? 1 : -1;
  426. this.animationAction?.setEffectiveTimeScale(timeScale);
  427. (this.animationAction as THREE.AnimationAction).paused = false;
  428. this.animationAction?.play();
  429. if (this.timeoutId) {
  430. clearTimeout(this.timeoutId);
  431. }
  432. this.timeoutId = setTimeout(() => {
  433. this.animationAction?.halt(0.5);
  434. }, 500);
  435. }
  436. clearMesh(item) {
  437. item.geometry?.dispose();
  438. if (item.material) {
  439. const material = item.material;
  440. for (const key of Object.keys(material)) {
  441. const value = material[key];
  442. // if (value && typeof value === 'object' && 'minFilter' in value) {
  443. // // this.textureMap.set(value.uuid, value);
  444. // value.dispose();
  445. // }
  446. if (value && typeof value === 'object' && value['dispose'] && typeof value['dispose'] === 'function') {
  447. value.dispose();
  448. }
  449. }
  450. material.dispose();
  451. }
  452. if (item.texture) {
  453. item.texture.dispose();
  454. }
  455. }
  456. clearGroup(group) {
  457. const removeObj = (obj) => {
  458. if (obj && obj?.children && obj?.children.length > 0) {
  459. for (let i = obj?.children.length - 1; i >= 0; i--) {
  460. const item = obj?.children[i];
  461. if (item && item.children && item.children.length > 0) {
  462. removeObj(item);
  463. item?.clear();
  464. } else {
  465. if (item) {
  466. if (item.parent) item.parent.remove(item);
  467. this.clearMesh(item);
  468. item.clear();
  469. }
  470. }
  471. }
  472. }
  473. };
  474. removeObj(group);
  475. }
  476. clearScene() {
  477. this.clearGroup(this.scene);
  478. // console.log('场景纹理数量----------->', this.textureMap.size);
  479. this.textureMap.forEach((texture) => {
  480. texture?.dispose();
  481. });
  482. this.textureMap.clear();
  483. }
  484. destroy() {
  485. TWEEN.getAll().forEach((item) => {
  486. item.stop();
  487. });
  488. TWEEN.removeAll();
  489. this.isRender = false;
  490. cancelAnimationFrame(this.animationId);
  491. this.clearScene();
  492. window.removeEventListener('resize', this.resizeRenderer);
  493. // this.orbitControls?.dispose();
  494. // this.scene?.environment?.dispose();
  495. this.scene?.clear();
  496. this.renderer?.dispose();
  497. this.renderer?.getRenderTarget()?.dispose();
  498. if (this.renderer && this.canvasContainer) this.canvasContainer.innerHTML = '';
  499. if (this.CSSCanvasContainer && this.css3dRender) this.CSSCanvasContainer.innerHTML = '';
  500. // if (this.renderer) this.renderer.domElement = null;
  501. this.renderer?.clear();
  502. if (this.css3dRender) this.css3dRender.domElement = null;
  503. if (this.css2dRender) this.css2dRender.domElement = null;
  504. if (this.canvasContainer) this.canvasContainer.innerHTML = '';
  505. if (this.CSSCanvasContainer) this.CSSCanvasContainer.innerHTML = '';
  506. this.camera = null;
  507. this.orbitControls = null;
  508. this.renderer = null;
  509. this.stats = null;
  510. this.scene = null;
  511. this.css3dRender = null;
  512. this.css2dRender = null;
  513. THREE.Cache.clear();
  514. console.log('场景销毁后信息----------->', window['$renderer']?.info, this.scene);
  515. }
  516. }
  517. export default UseThree;