useThree.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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, isBlender = false) {
  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. let object: THREE.Object3D = 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. if (isBlender) {
  217. object = object.children[0];
  218. }
  219. object.animations = gltf.animations;
  220. object.name = modalNameStr;
  221. group?.add(object);
  222. childResolve(object);
  223. },
  224. (err) => {
  225. console.log(err);
  226. }
  227. );
  228. } else {
  229. // 开启线程下载
  230. console.log('需要开启线程下载模型资源。。。。。');
  231. const result = (await getList({ fileName: modalNameStr })) || [];
  232. const file = result['records'][0];
  233. if (file && file.path) {
  234. gltfLoader.load(`${baseApiUrl}/sys/common/static/${file.path}`, async (glft) => {
  235. if (glft) {
  236. const object = glft.scene;
  237. object.name = modalNameStr;
  238. if (glft.animations.length > 0) {
  239. object.animations = glft.animations;
  240. }
  241. group?.add(object);
  242. childResolve(object);
  243. const modalArr = await db.modal.where('modalName').equals(modalNameStr).toArray();
  244. if (modalArr.length < 1) {
  245. saveModel(modalNameStr, file.path, file.version);
  246. }
  247. }
  248. });
  249. } else {
  250. childResolve(null);
  251. }
  252. }
  253. } catch (error) {
  254. console.log(error);
  255. reject();
  256. }
  257. });
  258. }
  259. Promise.all(resolvePromise).then((objects) => {
  260. dracoLoader.dispose();
  261. resolve(objects);
  262. });
  263. } catch (error) {
  264. reject('加载模型出错');
  265. }
  266. });
  267. }
  268. // setFBXModel(modalNames, group = null) {
  269. // window['startTime'] = new Date().getTime();
  270. // const modelStore = useModelStore();
  271. // return new Promise(async (resolve, reject) => {
  272. // try {
  273. // const fbxLoader = new FBXLoader();
  274. // fbxLoader.setPath('/model/fbx/');
  275. // const db = window['CustomDB'];
  276. // const resolvePromise: Promise<any>[] = [];
  277. // let modalNameArr = Object.prototype.toString.call(modalNames) === '[object Array]' ? modalNames : [modalNames];
  278. // let len = modalNameArr.length;
  279. // for (let i = 0; i < len; i++) {
  280. // resolvePromise[i] = new Promise(async (childResolve, reject) => {
  281. // try {
  282. // // 解析模型
  283. // let modalValue;
  284. // const modalNameStr = modalNameArr[i];
  285. // let data = modelStore.modelArr.get(modalNameStr) || null;
  286. // if (!data) {
  287. // const modalArr = await db.modal.where('modalName').equals(modalNameStr).toArray();
  288. // if (modalArr.length > 0) modalValue = modalArr[0].modalVal;
  289. // } else {
  290. // modalValue = data.modalVal;
  291. // }
  292. // if (modalValue) {
  293. // const object = fbxLoader.parse(modalValue, '/model/fbx/');
  294. // // const object = fbx.scene;
  295. // // setModalCenter(object);
  296. // if (object) {
  297. // object.traverse((obj) => {
  298. // if (obj instanceof THREE.Mesh) {
  299. // obj.material.emissiveIntensity = 1;
  300. // obj.material.emissiveMap = obj.material.map;
  301. // obj.material.blending = THREE.CustomBlending;
  302. // if (obj.material.opacity < 1) {
  303. // obj.material.transparent = true;
  304. // }
  305. // if (obj.material.map) {
  306. // obj.material.map.encoding = THREE.sRGBEncoding;
  307. // obj.material.map.flipY = false;
  308. // obj.material.map.anisotropy = 1;
  309. // }
  310. // if (obj.material.emissiveMap) {
  311. // obj.material.emissiveMap.encoding = THREE.sRGBEncoding;
  312. // obj.material.emissiveMap.flipY = false;
  313. // }
  314. // if (obj.material.map || obj.material.emissiveMap) {
  315. // obj.material.needsUpdate = true;
  316. // }
  317. // // if (envMap) {
  318. // // obj.material.envMap = envMap;
  319. // // obj.material.envMapIntensity = 1;
  320. // // }
  321. // // obj.renderOrder = 1;
  322. // }
  323. // });
  324. // object.animations = object.animations;
  325. // object.name = modalNameStr;
  326. // group?.add(object);
  327. // childResolve(object);
  328. // }
  329. // }
  330. // } catch (error) {
  331. // console.log(error);
  332. // reject();
  333. // }
  334. // });
  335. // }
  336. // Promise.all(resolvePromise).then((objects) => {
  337. // resolve(objects);
  338. // });
  339. // } catch (error) {
  340. // reject('加载模型出错');
  341. // }
  342. // });
  343. // }
  344. setTestPlane() {
  345. const plane = new THREE.Mesh(new THREE.PlaneGeometry(10, 10), new THREE.MeshPhongMaterial({ color: 0x999999, specular: 0x101010 }));
  346. plane.rotation.x = -Math.PI / 2;
  347. plane.position.y = 0.03;
  348. plane.receiveShadow = true;
  349. this.scene?.add(plane);
  350. }
  351. removeSawtooth() {
  352. this.composer = new EffectComposer(this.renderer as THREE.WebGLRenderer);
  353. const FXAAShaderPass = new ShaderPass(FXAAShader);
  354. FXAAShaderPass.uniforms['resolution'].value.set(1 / this.canvasContainer.clientWidth, 1 / this.canvasContainer.clientHeight);
  355. FXAAShaderPass.renderToScreen = true;
  356. this.composer.addPass(FXAAShaderPass);
  357. }
  358. /* 场景环境背景 */
  359. setEnvMap(hdr) {
  360. if (!this.scene) return;
  361. // (this.scene as THREE.Scene).environment
  362. new RGBELoader().setPath('/model/hdr/').load(hdr + '.hdr', (texture) => {
  363. texture.colorSpace = THREE.SRGBColorSpace;
  364. texture.mapping = THREE.EquirectangularReflectionMapping;
  365. if (this.scene) (this.scene as THREE.Scene).environment = texture;
  366. texture.dispose();
  367. });
  368. }
  369. render() {
  370. this.startAnimation();
  371. this.orbitControls?.update();
  372. this.camera?.updateMatrixWorld();
  373. // this.composer?.render();
  374. this.css3dRender?.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  375. this.css2dRender?.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  376. this.renderer?.render(this.scene as THREE.Object3D, this.camera as THREE.Camera);
  377. }
  378. timeRender() {
  379. //设置为可渲染状态
  380. this.renderEnabled = true;
  381. //清除上次的延迟器
  382. if (this.timeOut) {
  383. clearTimeout(this.timeOut);
  384. }
  385. this.timeOut = setTimeout(() => {
  386. this.renderEnabled = false;
  387. }, 3000);
  388. }
  389. /* 漫游 */
  390. startMY() {}
  391. /* 初始动画 */
  392. startAnimation() {}
  393. renderAnimationScene() {}
  394. animate() {
  395. if (this.isRender) {
  396. this.animationId = requestAnimationFrame(this.animate.bind(this));
  397. const T = this.clock?.getDelta() || 0;
  398. this.timeS = this.timeS + T;
  399. if (this.timeS > this.renderT) {
  400. this.renderAnimationScene();
  401. if (this.renderEnabled) {
  402. this.render();
  403. }
  404. this.stats?.update();
  405. this.timeS = 0;
  406. }
  407. // this.stats?.update();
  408. // // TWEEN.update();
  409. // // this.renderAnimationScene();
  410. // if (this.renderEnabled) {
  411. // this.render();
  412. // }
  413. }
  414. }
  415. resizeRenderer() {
  416. // 更新相机比例
  417. if (this.camera) {
  418. (this.camera as THREE.PerspectiveCamera).aspect = this.canvasContainer.clientWidth / this.canvasContainer.clientHeight;
  419. // 刷新相机矩阵
  420. this.camera?.updateProjectionMatrix();
  421. }
  422. // 设置场景尺寸
  423. this.renderer?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  424. this.css3dRender?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  425. this.css2dRender?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  426. }
  427. wheelRenderer(e: WheelEvent) {
  428. const timeScale = e.deltaY > 0 ? 1 : -1;
  429. this.animationAction?.setEffectiveTimeScale(timeScale);
  430. (this.animationAction as THREE.AnimationAction).paused = false;
  431. this.animationAction?.play();
  432. if (this.timeoutId) {
  433. clearTimeout(this.timeoutId);
  434. }
  435. this.timeoutId = setTimeout(() => {
  436. this.animationAction?.halt(0.5);
  437. }, 500);
  438. }
  439. clearMesh(item) {
  440. item.geometry?.dispose();
  441. if (item.material) {
  442. const material = item.material;
  443. for (const key of Object.keys(material)) {
  444. const value = material[key];
  445. // if (value && typeof value === 'object' && 'minFilter' in value) {
  446. // // this.textureMap.set(value.uuid, value);
  447. // value.dispose();
  448. // }
  449. if (value && typeof value === 'object' && value['dispose'] && typeof value['dispose'] === 'function') {
  450. value.dispose();
  451. }
  452. }
  453. material.dispose();
  454. }
  455. if (item.texture) {
  456. item.texture.dispose();
  457. }
  458. }
  459. clearGroup(group) {
  460. const removeObj = (obj) => {
  461. if (obj && obj?.children && obj?.children.length > 0) {
  462. for (let i = obj?.children.length - 1; i >= 0; i--) {
  463. const item = obj?.children[i];
  464. if (item && item.children && item.children.length > 0) {
  465. removeObj(item);
  466. item?.clear();
  467. } else {
  468. if (item) {
  469. if (item.parent) item.parent.remove(item);
  470. this.clearMesh(item);
  471. item.clear();
  472. }
  473. }
  474. }
  475. }
  476. };
  477. removeObj(group);
  478. }
  479. clearScene() {
  480. this.clearGroup(this.scene);
  481. // console.log('场景纹理数量----------->', this.textureMap.size);
  482. this.textureMap.forEach((texture) => {
  483. texture?.dispose();
  484. });
  485. this.textureMap.clear();
  486. }
  487. destroy() {
  488. TWEEN.getAll().forEach((item) => {
  489. item.stop();
  490. });
  491. TWEEN.removeAll();
  492. this.isRender = false;
  493. cancelAnimationFrame(this.animationId);
  494. this.clearScene();
  495. window.removeEventListener('resize', this.resizeRenderer);
  496. // this.orbitControls?.dispose();
  497. // this.scene?.environment?.dispose();
  498. this.scene?.clear();
  499. this.renderer?.dispose();
  500. this.renderer?.getRenderTarget()?.dispose();
  501. if (this.renderer && this.canvasContainer) this.canvasContainer.innerHTML = '';
  502. if (this.CSSCanvasContainer && this.css3dRender) this.CSSCanvasContainer.innerHTML = '';
  503. // if (this.renderer) this.renderer.domElement = null;
  504. this.renderer?.clear();
  505. if (this.css3dRender) this.css3dRender.domElement = null;
  506. if (this.css2dRender) this.css2dRender.domElement = null;
  507. if (this.canvasContainer) this.canvasContainer.innerHTML = '';
  508. if (this.CSSCanvasContainer) this.CSSCanvasContainer.innerHTML = '';
  509. this.camera = null;
  510. this.orbitControls = null;
  511. this.renderer = null;
  512. this.stats = null;
  513. this.scene = null;
  514. this.css3dRender = null;
  515. this.css2dRender = null;
  516. THREE.Cache.clear();
  517. console.log('场景销毁后信息----------->', window['$renderer']?.info, this.scene);
  518. }
  519. }
  520. export default UseThree;