Skip to content

Commit 4106588

Browse files
committed
Improved chromatic aberration example.
1 parent f7e8be0 commit 4106588

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed
-366 KB
Loading

examples/webgpu_postprocessing_ca.html

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import * as THREE from 'three';
3030
import { pass, renderOutput, uniform } from 'three/tsl';
3131

32+
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
33+
import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
3234
import { chromaticAberration } from 'three/addons/tsl/display/ChromaticAberrationNode.js';
3335

3436
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
@@ -44,36 +46,36 @@
4446
};
4547

4648
let camera, scene, renderer, clock, mainGroup;
47-
let postProcessing;
49+
let controls, postProcessing;
4850

4951
init();
5052

5153
async function init() {
5254

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+
5361
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
5462
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();
5671

5772
scene = new THREE.Scene();
5873
scene.background = new THREE.Color( 0x0a0a0a );
5974

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;
6977

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();
7779

7880
// Create main group
7981
mainGroup = new THREE.Group();
@@ -87,12 +89,6 @@
8789
gridHelper.position.y = -10;
8890
scene.add( gridHelper );
8991

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-
9692
// post processing
9793
postProcessing = new THREE.PostProcessing( renderer );
9894
postProcessing.outputColorTransform = false;
@@ -135,10 +131,9 @@
135131

136132
const animationFolder = gui.addFolder( 'Animation' );
137133
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 ) => {
140135

141-
camera.position.z = value;
136+
controls.autoRotate = value;
142137

143138
} );
144139

@@ -295,6 +290,8 @@
295290

296291
const time = clock.getElapsedTime();
297292

293+
controls.update();
294+
298295
if ( params.animated ) {
299296

300297
// Animate individual shapes
@@ -328,15 +325,6 @@
328325

329326
}
330327

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-
340328
postProcessing.render();
341329
}
342330

0 commit comments

Comments
 (0)