|
| 1 | +import * as THREE from "three"; |
| 2 | +import { OrbitControls } from "three/addons/controls/OrbitControls.js"; |
| 3 | +import { STLLoader } from "three/addons/loaders/STLLoader.js"; |
| 4 | +import { EffectComposer } from "three/addons/postprocessing/EffectComposer.js"; |
| 5 | +import { RenderPass } from "three/addons/postprocessing/RenderPass.js"; |
| 6 | +import { UnrealBloomPass } from "three/addons/postprocessing/UnrealBloomPass.js"; |
| 7 | +import GUI from "https://cdn.jsdelivr.net/npm/lil-gui@0.21/+esm"; |
| 8 | + |
| 9 | +const loader = new STLLoader(); |
| 10 | +const container = document.getElementById("viewer"); |
| 11 | +const deg2rad = (degrees) => degrees * (Math.PI / 180); |
| 12 | + |
| 13 | +let renderer, camera, scene, controls, spotLight; |
| 14 | +let composer, bloomPass; |
| 15 | + |
| 16 | +function setupRenderer() { |
| 17 | + renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true }); |
| 18 | + renderer.setSize(container.clientWidth, container.clientHeight); |
| 19 | + renderer.setClearColor(0xffffff, 0); |
| 20 | + |
| 21 | + renderer.physicallyCorrectLights = true; |
| 22 | + renderer.outputColorSpace = THREE.SRGBColorSpace; |
| 23 | + renderer.toneMapping = THREE.ACESFilmicToneMapping; |
| 24 | + renderer.toneMappingExposure = 1.0; |
| 25 | + |
| 26 | + container.appendChild(renderer.domElement); |
| 27 | +} |
| 28 | + |
| 29 | +function setupCameraAndControls({ x, y, z }) { |
| 30 | + camera = new THREE.PerspectiveCamera( |
| 31 | + 60, |
| 32 | + container.clientWidth / container.clientHeight, |
| 33 | + 0.1, |
| 34 | + 1000 |
| 35 | + ); |
| 36 | + camera.position.set(x, y, z); |
| 37 | + controls = new OrbitControls(camera, renderer.domElement); |
| 38 | +} |
| 39 | + |
| 40 | +function setupScene({ x, y, z }) { |
| 41 | + scene = new THREE.Scene(); |
| 42 | + |
| 43 | + scene.add(new THREE.AmbientLight(0x888888)); |
| 44 | + |
| 45 | + spotLight = new THREE.DirectionalLight(0xffffff, 1); |
| 46 | + spotLight.position.set(x, y, z); |
| 47 | + scene.add(spotLight); |
| 48 | +} |
| 49 | + |
| 50 | +function setupPostProcessing() { |
| 51 | + composer = new EffectComposer(renderer); |
| 52 | + |
| 53 | + const renderPass = new RenderPass(scene, camera); |
| 54 | + composer.addPass(renderPass); |
| 55 | + |
| 56 | + bloomPass = new UnrealBloomPass( |
| 57 | + new THREE.Vector2(container.clientWidth, container.clientHeight), |
| 58 | + 1.2, |
| 59 | + 0.4, |
| 60 | + 0.85 |
| 61 | + ); |
| 62 | + |
| 63 | + composer.addPass(bloomPass); |
| 64 | +} |
| 65 | + |
| 66 | +function loadSTL(url) { |
| 67 | + const material = new THREE.MeshStandardMaterial({ |
| 68 | + color: 0x5588ff, |
| 69 | + emissive: 0x3366ff, |
| 70 | + emissiveIntensity: 0.6, |
| 71 | + metalness: 0.3, |
| 72 | + roughness: 0.2 |
| 73 | + }); |
| 74 | + |
| 75 | + loader.load(url, (geometry) => { |
| 76 | + const center = new THREE.Vector3(); |
| 77 | + geometry.computeBoundingBox(); |
| 78 | + geometry.boundingBox.getCenter(center).negate(); |
| 79 | + geometry.translate(center.x, center.y, center.z); |
| 80 | + |
| 81 | + const mesh = new THREE.Mesh(geometry, material); |
| 82 | + mesh.rotateX(deg2rad(-90)); |
| 83 | + scene.add(mesh); |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +function setupResizeHandler() { |
| 88 | + window.addEventListener("resize", () => { |
| 89 | + const w = container.clientWidth; |
| 90 | + const h = container.clientHeight; |
| 91 | + |
| 92 | + renderer.setSize(w, h); |
| 93 | + composer.setSize(w, h); |
| 94 | + |
| 95 | + camera.aspect = w / h; |
| 96 | + camera.updateProjectionMatrix(); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +function animate() { |
| 101 | + requestAnimationFrame(animate); |
| 102 | + controls.update(); |
| 103 | + composer.render(); |
| 104 | +} |
| 105 | + |
| 106 | +function setupGUI(state) { |
| 107 | + const gui = new GUI(); |
| 108 | + |
| 109 | + const light = gui.addFolder("Light"); |
| 110 | + light.add(state.lightPos, "x", -500, 500).onChange(v => { |
| 111 | + spotLight.position.x = v; |
| 112 | + }); |
| 113 | + light.add(state.lightPos, "y", -500, 500).onChange(v => { |
| 114 | + spotLight.position.y = v; |
| 115 | + }); |
| 116 | + light.add(state.lightPos, "z", -500, 500).onChange(v => { |
| 117 | + spotLight.position.z = v; |
| 118 | + }); |
| 119 | + |
| 120 | + const bloom = gui.addFolder("Bloom"); |
| 121 | + bloom.add(bloomPass, "strength", 0, 3, 0.01); |
| 122 | + bloom.add(bloomPass, "radius", 0, 1, 0.01); |
| 123 | + bloom.add(bloomPass, "threshold", 0, 1, 0.01); |
| 124 | +} |
| 125 | + |
| 126 | +function init() { |
| 127 | + const initialState = { |
| 128 | + lightPos: { x: 20, y: 50, z: 200 }, |
| 129 | + cameraPos: { x: 45, y: 45, z: 45 } |
| 130 | + }; |
| 131 | + |
| 132 | + setupRenderer(); |
| 133 | + setupScene(initialState.lightPos); |
| 134 | + setupCameraAndControls(initialState.cameraPos); |
| 135 | + setupPostProcessing(); |
| 136 | + setupGUI(initialState); |
| 137 | + loadSTL(window.STL_URL); |
| 138 | + setupResizeHandler(); |
| 139 | + animate(); |
| 140 | +} |
| 141 | + |
| 142 | +init(); |
0 commit comments