import * as THREE from 'three' import Stats from 'three/addons/libs/stats.module.js'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' import TWEEN from '@tweenjs/tween.js'; import { EffectComposer } from 'three/examples/jsm/postprocessing/EffectComposer'; import { RenderPass } from 'three/examples/jsm/postprocessing/RenderPass'; import { OutlinePass } from 'three/examples/jsm/postprocessing/OutlinePass'; import { ShaderPass } from 'three/examples/jsm/postprocessing/ShaderPass'; import { FXAAShader } from 'three/examples/jsm/shaders/FXAAShader'; import { FBXLoader } from 'three/examples/jsm/loaders/FBXLoader' const help = false; export default class ShelfThree { constructor(dom) { this.dom = dom; this.objects = []; } startup = () => { this.scene = this.initScene(); this.camera = this.initCamera(); this.renderer = this.initRenderer(); this.controls = this.initControls(); this.stats = this.initStats(); this.composer = this.initComposer(); this.windowResize(); this.initRaycaster(); initLight(this.scene); this.animate(); } animate = () => { this.animationFrame = requestAnimationFrame(this.animate); this.render(); TWEEN.update(); } render = () => { // this.renderer.render(this.scene, this.camera); this.composer.render(); // use special effects this.stats.update(); // Rotate the scene this.controls.update() this.controls.autoRotateSpeed = 2; } getFullWidth = () => { return this.dom?.offsetWidth; } getFullHeight = () => { return this.dom?.offsetHeight; } addObject = (object) => { this.scene.add(object); this.objects.push(object); } initScene = () => { const scene = new THREE.Scene(); // scene.background = new THREE.Color(0xf8f8f8); scene.background = new THREE.Color(0x7a7a7a); scene.fog = new THREE.Fog(scene.background, 1, 5000); if (help) { scene.add(new THREE.AxesHelper(1000)); } return scene; } initCamera = () => { const camera = new THREE.PerspectiveCamera(70, this.getFullWidth() / this.getFullHeight(), 1, 60000); camera.position.set(-400, 400, 400); this.scene.add(camera); return camera; } initRenderer = () => { const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setClearAlpha(0); // renderer.useLegacyLights = false; // 旧版光源 重要 renderer.shadowMap.enabled = true; // this.renderer.shadowMap.type = THREE.BasicShadowMap; // 最低阴影 // renderer.toneMapping = THREE.ReinhardToneMapping; renderer.toneMapping = THREE.ACESFilmicToneMapping; renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(this.getFullWidth(), this.getFullHeight()); this.dom?.appendChild(renderer.domElement); return renderer; } initComposer = () => { const composer = new EffectComposer(this.renderer); const renderPass = new RenderPass(this.scene, this.camera); composer.addPass(renderPass); // pixel ratio const pixelRatio = this.renderer.getPixelRatio(); const newWidth = Math.floor(this.getFullWidth() * pixelRatio) || 1; const newHeight = Math.floor(this.getFullHeight() * pixelRatio) || 1; composer.setSize(newWidth, newHeight); // antialias const effectFXAA = new ShaderPass(FXAAShader); effectFXAA.uniforms['resolution'].value.set(1 / newWidth, 1 / newHeight); composer.addPass(effectFXAA); // outline this.outlinePass = new OutlinePass(new THREE.Vector2(this.getFullWidth(), this.getFullHeight()), this.scene, this.camera); this.outlinePass.visibleEdgeColor.set('#ffffff'); // outline color this.outlinePass.edgeStrength = 10; // outline weight composer.addPass(this.outlinePass); return composer; } initControls = () => { const controls = new OrbitControls(this.camera, this.renderer.domElement); // controls.enableRotate = false; // 禁用整体旋转功能 controls.enablePan = false; // 允许平移 controls.enableDamping = true; // 开启阻尼 controls.dampingFactor = 0.08; // 阻尼因子 controls.enableZoom = true; // 允许缩放 // controls.minDistance = 1; // 最小缩放距离 // controls.maxDistance = 30; // 最大缩放距离 controls.minPolarAngle = 0; // 最小俯仰角度(向下看) controls.minPolarAngle = Math.PI / 2.1; controls.maxPolarAngle = Math.PI / 2.1; // 最大俯仰角度(向上看) controls.rotateSpeed = 0.6; // 视角移动速度减慢 controls.autoRotate = true; // 自动旋转 controls.target = new THREE.Vector3(0, 250, 0) // 屏幕中心点 平替 camera的lookAt return controls; } initStats = () => { const stats = new Stats(); this.dom?.appendChild(stats.dom); if (!help) { stats.domElement.style.display = 'none'; } return stats; } windowResize = () => { this.resizeHandler = () => { this.camera.aspect = this.getFullWidth() / this.getFullHeight(); this.camera.updateProjectionMatrix(); this.renderer.setSize(this.getFullWidth(), this.getFullHeight()); this.renderer.setPixelRatio(window.devicePixelRatio); }; window.addEventListener('resize', this.resizeHandler, false); } initRaycaster = () => { const raycaster = new THREE.Raycaster(); const mouse = new THREE.Vector2(); this.handleClickEvent = (event) => { let x, y; if (event.changedTouches) { x = event.changedTouches[0].pageX; y = event.changedTouches[0].pageY; } else { x = event.clientX; y = event.clientY; } let rect = this.dom.getBoundingClientRect(); mouse.x = ((x - rect.left) / rect.width) * 2 - 1; mouse.y = -((y - rect.top) / rect.height) * 2 + 1; event.preventDefault(); raycaster.setFromCamera(mouse, this.camera); let intersects = raycaster.intersectObjects(this.objects, true); if (intersects.length === 0) { return; } let objName = intersects[0].object.name; this.objects.forEach(obj => { if (obj.name === objName) { this.outlinePass.selectedObjects = [obj]; } }) if (objName && this.handleClick) { this.handleClick(objName, x, y); } } this.dom?.addEventListener("click", this.handleClickEvent, false); } generateMesh = (fn) => { const loader = new FBXLoader(); fn(loader, this.addObject); } setNewSelectedMesh = (objName) => { for (const mesh of this.objects) { if (mesh.name === objName) { this.outlinePass.selectedObjects = [mesh]; break; } } } rePerspective = (maxHeight, normalHeight) => { const height = Math.max(maxHeight, normalHeight) const cameraPosScale = 0.7; this.camera.position.set( -height * cameraPosScale, height * cameraPosScale, height * cameraPosScale ); this.controls.target = new THREE.Vector3(0, maxHeight * 0.53, 0); } destroy = () => { cancelAnimationFrame(this.animationFrame); window.removeEventListener('resize', this.resizeHandler); if (this.scene) { while (this.scene.children.length > 0) { this.removeEntity(this.scene.children[0]); } this.scene = null; } if (this.renderer) { this.renderer.dispose(); this.renderer.forceContextLoss(); this.renderer.context = null; this.renderer.domElement = null; this.renderer = null; } if (this.composer) { this.composer.passes.forEach(pass => { if (pass.dispose) { pass.dispose(); } }); this.composer = null; } if (this.controls) { this.controls.dispose(); this.controls = null; } this.dom?.removeEventListener("click", this.handleClickEvent); this.camera = null; this.objects = []; while (this.dom?.firstChild) { this.dom.removeChild(this.dom.firstChild); } } removeEntity = (object) => { if (object.material) object.material.dispose(); if (object.geometry) object.geometry.dispose(); if (object.texture) object.texture.dispose(); this.scene.remove(object); } }; const initLight = (scene) => { const hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 6); hemiLight.color.setHSL(0.6, 1, 0.6); hemiLight.groundColor.setHSL(0.095, 1, 0.75); hemiLight.position.set(0, 1500, 0); scene.add(hemiLight); const dirLight = new THREE.DirectionalLight(0xffffff, 2.5); dirLight.color.setHSL(0.1, 1, 0.95); dirLight.position.set(-10, 10, 10); dirLight.position.multiplyScalar(30); scene.add(dirLight); const dirLight1 = new THREE.DirectionalLight(0xffffff, 2.5); dirLight1.color.setHSL(0.1, 1, 0.95); dirLight1.position.set(10, 10, -10); dirLight1.position.multiplyScalar(30); scene.add(dirLight1); }