useThree.ts 13 KB

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