|
29 | 29 | import * as THREE from 'three'; |
30 | 30 | import { pass, renderOutput, uniform } from 'three/tsl'; |
31 | 31 |
|
| 32 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 33 | + import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; |
32 | 34 | import { chromaticAberration } from 'three/addons/tsl/display/ChromaticAberrationNode.js'; |
33 | 35 |
|
34 | 36 | import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; |
|
44 | 46 | }; |
45 | 47 |
|
46 | 48 | let camera, scene, renderer, clock, mainGroup; |
47 | | - let postProcessing; |
| 49 | + let controls, postProcessing; |
48 | 50 |
|
49 | 51 | init(); |
50 | 52 |
|
51 | 53 | async function init() { |
52 | 54 |
|
| 55 | + renderer = new THREE.WebGPURenderer( { antialias: true } ); |
| 56 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 57 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 58 | + renderer.setAnimationLoop( animate ); |
| 59 | + document.body.appendChild( renderer.domElement ); |
| 60 | + |
53 | 61 | camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 ); |
54 | 62 | camera.position.set( 0, 15, params.cameraDistance ); |
55 | | - camera.lookAt( 0, 0, 0 ); |
| 63 | + |
| 64 | + controls = new OrbitControls( camera, renderer.domElement ); |
| 65 | + controls.enableDamping = true; |
| 66 | + controls.dampingFactor = 0.1; |
| 67 | + controls.autoRotate = true; |
| 68 | + controls.autoRotateSpeed = -0.1; |
| 69 | + controls.target.set( 0, 0.5, 0 ); |
| 70 | + controls.update(); |
56 | 71 |
|
57 | 72 | scene = new THREE.Scene(); |
58 | 73 | scene.background = new THREE.Color( 0x0a0a0a ); |
59 | 74 |
|
60 | | - clock = new THREE.Clock(); |
61 | | - |
62 | | - // Lighting |
63 | | - const ambientLight = new THREE.AmbientLight( 0xffffff, 0.3 ); |
64 | | - scene.add( ambientLight ); |
65 | | - |
66 | | - const dirLight1 = new THREE.DirectionalLight( 0xffffff, 1.5 ); |
67 | | - dirLight1.position.set( 10, 20, 10 ); |
68 | | - scene.add( dirLight1 ); |
| 75 | + const pmremGenerator = new THREE.PMREMGenerator( renderer ); |
| 76 | + scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture; |
69 | 77 |
|
70 | | - const dirLight2 = new THREE.DirectionalLight( 0x4080ff, 0.5 ); |
71 | | - dirLight2.position.set( -10, 20, -10 ); |
72 | | - scene.add( dirLight2 ); |
73 | | - |
74 | | - const pointLight = new THREE.PointLight( 0xff4080, 1, 50 ); |
75 | | - pointLight.position.set( 0, 10, 0 ); |
76 | | - scene.add( pointLight ); |
| 78 | + clock = new THREE.Clock(); |
77 | 79 |
|
78 | 80 | // Create main group |
79 | 81 | mainGroup = new THREE.Group(); |
|
87 | 89 | gridHelper.position.y = -10; |
88 | 90 | scene.add( gridHelper ); |
89 | 91 |
|
90 | | - renderer = new THREE.WebGPURenderer(); |
91 | | - renderer.setPixelRatio( window.devicePixelRatio ); |
92 | | - renderer.setSize( window.innerWidth, window.innerHeight ); |
93 | | - renderer.setAnimationLoop( animate ); |
94 | | - document.body.appendChild( renderer.domElement ); |
95 | | - |
96 | 92 | // post processing |
97 | 93 | postProcessing = new THREE.PostProcessing( renderer ); |
98 | 94 | postProcessing.outputColorTransform = false; |
|
135 | 131 |
|
136 | 132 | const animationFolder = gui.addFolder( 'Animation' ); |
137 | 133 | animationFolder.add( params, 'animated' ); |
138 | | - animationFolder.add( params, 'autoRotate' ); |
139 | | - animationFolder.add( params, 'cameraDistance', 20, 60 ).onChange( ( value ) => { |
| 134 | + animationFolder.add( params, 'autoRotate' ).onChange( ( value ) => { |
140 | 135 |
|
141 | | - camera.position.z = value; |
| 136 | + controls.autoRotate = value; |
142 | 137 |
|
143 | 138 | } ); |
144 | 139 |
|
|
295 | 290 |
|
296 | 291 | const time = clock.getElapsedTime(); |
297 | 292 |
|
| 293 | + controls.update(); |
| 294 | + |
298 | 295 | if ( params.animated ) { |
299 | 296 |
|
300 | 297 | // Animate individual shapes |
|
328 | 325 |
|
329 | 326 | } |
330 | 327 |
|
331 | | - if ( params.autoRotate ) { |
332 | | - |
333 | | - const radius = params.cameraDistance; |
334 | | - camera.position.x = Math.sin( time * 0.1 ) * radius; |
335 | | - camera.position.z = Math.cos( time * 0.1 ) * radius; |
336 | | - camera.lookAt( 0, 0, 0 ); |
337 | | - |
338 | | - } |
339 | | - |
340 | 328 | postProcessing.render(); |
341 | 329 | } |
342 | 330 |
|
|
0 commit comments