useThree.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. import * as THREE from 'three';
  2. import { RGBELoader } from 'three/examples/jsm/loaders/RGBELoader.js';
  3. // 导入轨道控制器
  4. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
  5. import { CSS3DRenderer } from 'three/examples/jsm/renderers/CSS3DRenderer.js';
  6. // import gsap from 'gsap';
  7. import ResourceTracker from '/@/utils/threejs/ResourceTracker.js';
  8. import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js';
  9. import { DRACOLoader } from 'three/examples/jsm/loaders/DRACOLoader.js';
  10. import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer.js';
  11. import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader.js';
  12. import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass.js';
  13. import { setModalCenter } from '/@/utils/threejs/util';
  14. import Stats from 'three/examples/jsm/libs/stats.module.js';
  15. import { useModelStore } from '/@/store/modules/threejs';
  16. // import * as dat from 'dat.gui';
  17. // const gui = new dat.GUI();
  18. // gui.domElement.style = 'position:absolute;top:10px;left:10px;z-index:99999999999999';
  19. class UseThree {
  20. stats: Stats | null = null; // 帧
  21. canvasContainer: HTMLCanvasElement | null; //canvas 容器
  22. CSSCanvasContainer: HTMLCanvasElement | null = null; // css canvas 容器
  23. camera: THREE.PerspectiveCamera | null = null; // 相机
  24. scene: THREE.Scene | null = null;
  25. renderer: THREE.WebGLRenderer | null = null;
  26. renderEnabled = true;
  27. css3dRender: CSS3DRenderer | null = null;
  28. orbitControls: OrbitControls | null = null;
  29. animationAction: THREE.AnimationAction | null = null;
  30. clock: THREE.Clock | null = new THREE.Clock(); // 计时器
  31. timeoutId: NodeJS.Timeout | null = null;
  32. animationId = 0;
  33. spriteText: THREE.Sprite | null = null;
  34. mouse = new THREE.Vector2();
  35. rayCaster: THREE.Raycaster | null = null;
  36. animations: THREE.AnimationClip[] = [];
  37. mixers: THREE.AnimationMixer[] = [];
  38. renderT = 1 / 60;
  39. timeS = 0;
  40. resourceTracker: ResourceTracker | null = null;
  41. track: any = null;
  42. composer; //后期
  43. timeOut: NodeJS.Timeout | null = null; //
  44. constructor(canvasSelector, cssCanvas?) {
  45. this.animationId = 0;
  46. this.canvasContainer = document.querySelector(canvasSelector);
  47. //初始化
  48. this.init(cssCanvas);
  49. // this.animate();
  50. window.addEventListener('resize', this.resizeRenderer.bind(this));
  51. // 添加滚动事件,鼠标滚动模型执行动画
  52. // window.addEventListener('wheel', this.wheelRenderer.bind(this));
  53. // this.canvasContainer?.appendChild(gui.domElement);
  54. }
  55. init(cssCanvas?) {
  56. // 初始化场景
  57. this.initScene();
  58. // 初始化环境光
  59. // this.initLight();
  60. // 初始化相机
  61. this.initCamera();
  62. //初始化渲染器
  63. this.initRenderer();
  64. // 初始化控制器
  65. this.initControles();
  66. if (cssCanvas) {
  67. this.initCSSRenderer(cssCanvas);
  68. }
  69. // this.setTestPlane();
  70. this.rayCaster = new THREE.Raycaster();
  71. this.createStats();
  72. // this.removeSawtooth();
  73. }
  74. createStats() {
  75. this.stats = Stats();
  76. this.stats?.setMode(0);
  77. this.stats.domElement.style = 'position: absolute; top: 300px';
  78. this.canvasContainer?.appendChild(this.stats.domElement);
  79. }
  80. initScene() {
  81. this.scene = new THREE.Scene();
  82. // const axesHelper = new THREE.AxesHelper(100);
  83. // this.scene?.add(axesHelper);
  84. // const size = 1000;
  85. // const divisions = 10;
  86. // const gridHelper = new THREE.GridHelper(size, divisions);
  87. // this.scene?.add(gridHelper);
  88. }
  89. initLight() {
  90. const light = new THREE.AmbientLight(0xffffff, 1);
  91. light.position.set(0, 1000, 1000);
  92. (this.scene as THREE.Scene).add(light);
  93. }
  94. initCamera() {
  95. this.camera = new THREE.PerspectiveCamera(50, this.canvasContainer.clientWidth / this.canvasContainer.clientHeight, 0.0000001, 1000);
  96. //
  97. // const helper = new THREE.CameraHelper(this.camera);
  98. // this.scene?.add(helper);
  99. // gui.add(this.camera.position, 'x', 0.00001, 10000);
  100. // gui.add(this.camera.position, 'y', 0.00001, 10000);
  101. // gui.add(this.camera.position, 'z', 0.00001, 10000);
  102. // gui.add(this.camera, 'near', 0.01, 1).step(0.01);
  103. // gui.add(this.camera, 'far', 10, 100000);
  104. // gui.add(this.camera, 'fov', 0, 180);
  105. }
  106. initRenderer() {
  107. this.renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true }) as THREE.WebGLRenderer;
  108. // 设置屏幕像素比
  109. this.renderer?.setPixelRatio(window.devicePixelRatio);
  110. // 设置渲染的尺寸
  111. this.renderer?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  112. // 色调映射
  113. this.renderer.toneMapping = THREE.ACESFilmicToneMapping;
  114. this.renderer.outputEncoding = THREE.sRGBEncoding;
  115. this.renderer.shadowMap.enabled = true;
  116. // 曝光程度
  117. this.renderer.toneMappingExposure = 1;
  118. // this.renderer.physicallyCorrectLights = true;
  119. // this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
  120. this.canvasContainer?.appendChild(this.renderer.domElement);
  121. }
  122. initCSSRenderer(cssCanvas) {
  123. this.CSSCanvasContainer = document.querySelector(cssCanvas);
  124. if (this.CSSCanvasContainer) {
  125. this.css3dRender = new CSS3DRenderer() as CSS3DRenderer;
  126. this.css3dRender.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  127. this.CSSCanvasContainer?.appendChild(this.css3dRender.domElement);
  128. this.css3dRender.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  129. this.orbitControls = new OrbitControls(this.camera as THREE.Camera, this.css3dRender?.domElement) as OrbitControls;
  130. this.orbitControls.update();
  131. }
  132. }
  133. initControles() {
  134. this.orbitControls = new OrbitControls(this.camera as THREE.Camera, this.renderer?.domElement) as OrbitControls;
  135. // this.orbitControls.enableDamping = true;
  136. }
  137. setModel(modalName) {
  138. const modelStore = useModelStore();
  139. return new Promise(async (resolve, reject) => {
  140. try {
  141. const a = new Date().getTime();
  142. let modalValue = modelStore.modelArr.get(modalName);
  143. if (!modalValue) {
  144. const db = window['CustomDB'];
  145. const modalArr = await db.modal.where('modalName').equals(modalName).toArray();
  146. modalValue = modalArr[0].modalVal;
  147. }
  148. if (modalValue) {
  149. console.log('模型下载速度', new Date().getTime() - a);
  150. const b = new Date().getTime();
  151. try {
  152. const gltfLoader = new GLTFLoader();
  153. const dracoLoader = new DRACOLoader();
  154. dracoLoader.setDecoderPath('/model/draco/gltf/');
  155. dracoLoader.setDecoderConfig({ type: 'js' }); //使用兼容性强的draco_decoder.js解码器
  156. dracoLoader.preload();
  157. gltfLoader.setDRACOLoader(dracoLoader);
  158. gltfLoader.setPath('/model/glft/');
  159. gltfLoader.parse(
  160. modalValue,
  161. '/model/glft/',
  162. (gltf) => {
  163. const object = gltf.scene;
  164. setModalCenter(object);
  165. object.traverse((obj) => {
  166. if (obj instanceof THREE.Mesh) {
  167. obj.material.emissiveIntensity = 1;
  168. obj.material.emissiveMap = obj.material.map;
  169. obj.material.blending = THREE.CustomBlending;
  170. if (obj.material.opacity < 1) {
  171. obj.material.transparent = true;
  172. }
  173. obj.renderOrder = 1;
  174. // if (obj.name !== 'buxiugangse') {
  175. // obj.receiveShadow = true;
  176. // }
  177. // obj.castShadow = true;
  178. }
  179. });
  180. object.name = modalName;
  181. console.log('模型渲染时间', object, new Date().getTime() - b);
  182. resolve(gltf);
  183. dracoLoader.dispose();
  184. },
  185. (err) => {
  186. console.log(err);
  187. }
  188. );
  189. } catch (error) {
  190. console.log(error);
  191. }
  192. }
  193. } catch (error) {
  194. reject('加载模型出错');
  195. }
  196. });
  197. }
  198. setTestPlane() {
  199. const plane = new THREE.Mesh(new THREE.PlaneGeometry(10, 10), new THREE.MeshPhongMaterial({ color: 0x999999, specular: 0x101010 }));
  200. plane.rotation.x = -Math.PI / 2;
  201. plane.position.y = 0.03;
  202. plane.receiveShadow = true;
  203. this.scene?.add(plane);
  204. }
  205. removeSawtooth() {
  206. this.composer = new EffectComposer(this.renderer as THREE.WebGLRenderer);
  207. const FXAAShaderPass = new ShaderPass(FXAAShader);
  208. FXAAShaderPass.uniforms['resolution'].value.set(1 / this.canvasContainer.clientWidth, 1 / this.canvasContainer.clientHeight);
  209. FXAAShaderPass.renderToScreen = true;
  210. this.composer.addPass(FXAAShaderPass);
  211. }
  212. /* 场景环境背景 */
  213. setEnvMap(hdr) {
  214. const pmremGenerator = new THREE.PMREMGenerator(this.renderer as THREE.WebGLRenderer); // 使用hdr作为背景色
  215. pmremGenerator.compileEquirectangularShader();
  216. new THREE.TextureLoader().setPath('/model/hdr/').load(hdr + '.jpeg', (texture) => {
  217. texture.mapping = THREE.EquirectangularReflectionMapping;
  218. const envMap = pmremGenerator.fromEquirectangular(texture).texture;
  219. (this.scene as THREE.Scene).environment = envMap;
  220. pmremGenerator.dispose();
  221. });
  222. }
  223. render() {
  224. this.startAnimation();
  225. this.orbitControls?.update();
  226. this.camera?.updateMatrixWorld();
  227. // this.composer?.render();
  228. this.css3dRender?.render(this.scene as THREE.Scene, this.camera as THREE.PerspectiveCamera);
  229. this.renderer?.render(this.scene as THREE.Object3D, this.camera as THREE.Camera);
  230. }
  231. timeRender() {
  232. //设置为可渲染状态
  233. this.renderEnabled = true;
  234. //清除上次的延迟器
  235. if (this.timeOut) {
  236. clearTimeout(this.timeOut);
  237. }
  238. this.timeOut = setTimeout(() => {
  239. this.renderEnabled = false;
  240. }, 3000);
  241. }
  242. /* 漫游 */
  243. startMY() {}
  244. /* 初始动画 */
  245. startAnimation() {}
  246. renderAnimationScene() {}
  247. animate() {
  248. if (this.animationId != -1) {
  249. this.animationId = requestAnimationFrame(this.animate.bind(this));
  250. // const T = this.clock?.getDelta() || 0;
  251. // this.timeS = this.timeS + T;
  252. // if (this.timeS > this.renderT) {
  253. // this.stats?.update();
  254. // // this.renderAnimationScene();
  255. // if (this.renderEnabled) {
  256. // this.render();
  257. // }
  258. // this.timeS = 0;
  259. // }
  260. this.stats?.update();
  261. // this.renderAnimationScene();
  262. if (this.renderEnabled) {
  263. this.render();
  264. }
  265. }
  266. }
  267. resizeRenderer() {
  268. // 更新相机比例
  269. (this.camera as THREE.PerspectiveCamera).aspect = this.canvasContainer.clientWidth / this.canvasContainer.clientHeight;
  270. // 刷新相机矩阵
  271. this.camera?.updateProjectionMatrix();
  272. // 设置场景尺寸
  273. this.renderer?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  274. this.css3dRender?.setSize(this.canvasContainer.clientWidth, this.canvasContainer.clientHeight);
  275. }
  276. wheelRenderer(e: WheelEvent) {
  277. const timeScale = e.deltaY > 0 ? 1 : -1;
  278. this.animationAction?.setEffectiveTimeScale(timeScale);
  279. (this.animationAction as THREE.AnimationAction).paused = false;
  280. this.animationAction?.play();
  281. if (this.timeoutId) {
  282. clearTimeout(this.timeoutId);
  283. }
  284. this.timeoutId = setTimeout(() => {
  285. this.animationAction?.halt(0.5);
  286. }, 500);
  287. }
  288. // deleteModal() {
  289. // try {
  290. // const gl = this.renderer?.domElement?.getContext('webgl');
  291. // gl && gl.getExtension('WEBGL_lose_context')?.loseContext();
  292. // const gl1 = this.css3dRender?.domElement?.getContext('webgl');
  293. // gl1 && gl1.getExtension('WEBGL_lose_context')?.loseContext();
  294. // this.renderer?.forceContextLoss();
  295. // this.renderer?.dispose();
  296. // if (this.renderer) this.renderer.domElement = null;
  297. // this.renderer = null;
  298. // this.resourceTracker && this.resourceTracker.dispose();
  299. // if (this.canvasContainer) this.canvasContainer.innerHTML = '';
  300. // this.canvasContainer = null;
  301. // this.orbitControls = null;
  302. // this.css3dRender = null;
  303. // this.camera = null;
  304. // this.clock = null;
  305. // cancelAnimationFrame(this.animationId);
  306. // this.animationId = -1;
  307. // this.scene = null;
  308. // this.mixers = [];
  309. // console.log('销毁场景', this.scene);
  310. // } catch (error) {
  311. // console.log(error);
  312. // }
  313. // THREE.Cache.clear();
  314. // console.log('3D环境已清理干净');
  315. // }
  316. clearGroup(group) {
  317. const clearCache = (item) => {
  318. item.geometry?.dispose();
  319. item.material?.dispose();
  320. };
  321. const removeObj = (obj) => {
  322. if (obj?.children && obj?.children.length > 0) {
  323. for (let i = obj?.children.length - 1; i >= 0; i--) {
  324. const item = obj?.children[i];
  325. if (item.children.length > 0) {
  326. removeObj(item);
  327. } else {
  328. clearCache(item);
  329. item.clear();
  330. }
  331. }
  332. }
  333. obj?.removeFromParent();
  334. obj?.clear();
  335. };
  336. removeObj(group);
  337. }
  338. deleteModal() {
  339. cancelAnimationFrame(this.animationId);
  340. this.animationId = -1;
  341. for (let i = this.scene?.children.length - 1; i > -1; i--) {
  342. const obj: any = this.scene?.children[i];
  343. if (obj.type === 'Group') {
  344. this.clearGroup(obj);
  345. } else if (obj.type === 'Mesh') {
  346. if (obj.material) {
  347. obj.material.dispose();
  348. } else if (obj.geometry) {
  349. obj.geometry.dispose();
  350. }
  351. obj.clear();
  352. }
  353. obj.removeFromParent();
  354. this.scene?.remove(obj);
  355. }
  356. this.renderer?.clear();
  357. this.renderer?.dispose();
  358. this.renderer?.forceContextLoss();
  359. const gl = this.renderer?.getContext('webgl');
  360. gl && gl.getExtension('WEBGL_lose_context')?.loseContext();
  361. this.canvasContainer?.removeChild(this.renderer?.domElement as Node);
  362. // const glCss = this.css3dRender?.getContext('webgl');
  363. // glCss && glCss.getExtension('WEBGL_lose_context')?.loseContext();
  364. this.CSSCanvasContainer?.removeChild(this.css3dRender?.domElement as Node);
  365. this.camera = null;
  366. this.orbitControls = null;
  367. if (this.renderer) this.renderer.domElement = null;
  368. if (this.css3dRender) this.css3dRender.domElement = null;
  369. if (this.canvasContainer) this.canvasContainer.innerHTML = '';
  370. if (this.CSSCanvasContainer) this.CSSCanvasContainer.innerHTML = '';
  371. this.resourceTracker && this.resourceTracker.dispose();
  372. this.scene?.environment?.dispose();
  373. !!this.scene?.clear ?? this.scene?.clear();
  374. this.stats = null;
  375. this.scene = null;
  376. this.css3dRender = null;
  377. this.renderer = null;
  378. THREE.Cache.clear();
  379. }
  380. }
  381. export default UseThree;