| | |
| | | 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; |
| | |
| | | this.renderer = this.initRenderer(); |
| | | this.controls = this.initControls(); |
| | | this.stats = this.initStats(); |
| | | this.composer = this.initComposer(); |
| | | this.initLight(); |
| | | this.initRaycaster(); |
| | | this.windowResize(); |
| | | |
| | | this.animate(); |
| | |
| | | } |
| | | |
| | | render = () => { |
| | | this.composer.render(); |
| | | this.renderer.render(this.scene, this.camera); |
| | | this.stats.update(); |
| | | this.controls.update(); |
| | | } |
| | |
| | | |
| | | initScene = () => { |
| | | const scene = new THREE.Scene(); |
| | | scene.background = new THREE.Color(0x7a7a7a); |
| | | scene.background = new THREE.Color(0x323232); |
| | | scene.fog = new THREE.Fog(scene.background, 1, 5000); |
| | | if (help) { |
| | | scene.add(new THREE.AxesHelper(1000)); |
| | |
| | | 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); |
| | | |
| | | 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); |
| | | |
| | | const effectFXAA = new ShaderPass(FXAAShader); |
| | | effectFXAA.uniforms['resolution'].value.set(1 / newWidth, 1 / newHeight); |
| | | composer.addPass(effectFXAA); |
| | | |
| | | this.outlinePass = new OutlinePass(new THREE.Vector2(this.getFullWidth(), this.getFullHeight()), this.scene, this.camera); |
| | | this.outlinePass.visibleEdgeColor.set('#ffffff'); |
| | | this.outlinePass.edgeStrength = 10; |
| | | composer.addPass(this.outlinePass); |
| | | |
| | | return composer; |
| | | } |
| | | |
| | | initControls = () => { |
| | |
| | | this.scene.add(dirLight1); |
| | | } |
| | | |
| | | initRaycaster = () => { |
| | | this.raycaster = new THREE.Raycaster(); |
| | | this.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(); |
| | | this.mouse.x = ((x - rect.left) / rect.width) * 2 - 1; |
| | | this.mouse.y = -((y - rect.top) / rect.height) * 2 + 1; |
| | | event.preventDefault(); |
| | | this.raycaster.setFromCamera(this.mouse, this.camera); |
| | | let intersects = this.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 obj of this.objects) { |
| | | if (obj.name === objName) { |
| | | this.outlinePass.selectedObjects = [obj]; |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | rePerspective = (maxHeight, normalHeight) => { |
| | |
| | | this.camera.aspect = this.getFullWidth() / this.getFullHeight(); |
| | | this.camera.updateProjectionMatrix(); |
| | | this.renderer.setSize(this.getFullWidth(), this.getFullHeight()); |
| | | this.composer.setSize(this.getFullWidth(), this.getFullHeight()); |
| | | }; |
| | | window.addEventListener('resize', this.resizeHandler, false); |
| | | } |
| | |
| | | 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) { |