Skip to content

Commit 7d72e71

Browse files
mrdoobsunag
andauthored
Examples: Improve webgpu_lights_ies_spotlight (#30243)
* Examples: Added sphere to webgpu_lights_ies_spotlight. * update example --------- Co-authored-by: sunag <[email protected]>
1 parent 4c1941f commit 7d72e71

File tree

2 files changed

+64
-7
lines changed

2 files changed

+64
-7
lines changed
23.6 KB
Loading

examples/webgpu_lights_ies_spotlight.html

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
import { IESLoader } from 'three/addons/loaders/IESLoader.js';
3333

34+
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
35+
3436
let renderer, scene, camera;
3537
let lights;
3638

@@ -53,63 +55,89 @@
5355
//
5456

5557
const spotLight = new THREE.IESSpotLight( 0xff0000, 500 );
56-
spotLight.position.set( 6.5, 1.5, 6.5 );
58+
spotLight.position.set( 6.5, 3, 6.5 );
5759
spotLight.angle = Math.PI / 8;
5860
spotLight.penumbra = 0.7;
5961
spotLight.distance = 20;
62+
spotLight.castShadow = true;
6063
spotLight.iesMap = iesTexture1;
64+
spotLight.userData.helper = new THREE.SpotLightHelper( spotLight );
6165
scene.add( spotLight );
66+
scene.add( spotLight.target );
67+
scene.add( spotLight.userData.helper );
6268

6369
//
6470

6571
const spotLight2 = new THREE.IESSpotLight( 0x00ff00, 500 );
66-
spotLight2.position.set( 6.5, 1.5, - 6.5 );
72+
spotLight2.position.set( - 6.5, 3, 6.5 );
6773
spotLight2.angle = Math.PI / 8;
6874
spotLight2.penumbra = 0.7;
6975
spotLight2.distance = 20;
76+
spotLight2.castShadow = true;
7077
spotLight2.iesMap = iesTexture2;
78+
spotLight2.userData.helper = new THREE.SpotLightHelper( spotLight2 );
7179
scene.add( spotLight2 );
80+
scene.add( spotLight2.target );
81+
scene.add( spotLight2.userData.helper );
7282

7383
//
7484

7585
const spotLight3 = new THREE.IESSpotLight( 0x0000ff, 500 );
76-
spotLight3.position.set( - 6.5, 1.5, - 6.5 );
86+
spotLight3.position.set( - 6.5, 3, - 6.5 );
7787
spotLight3.angle = Math.PI / 8;
7888
spotLight3.penumbra = 0.7;
7989
spotLight3.distance = 20;
90+
spotLight3.castShadow = true;
8091
spotLight3.iesMap = iesTexture3;
92+
spotLight3.userData.helper = new THREE.SpotLightHelper( spotLight3 );
8193
scene.add( spotLight3 );
94+
scene.add( spotLight3.target );
95+
scene.add( spotLight3.userData.helper );
8296

8397
//
8498

8599
const spotLight4 = new THREE.IESSpotLight( 0xffffff, 500 );
86-
spotLight4.position.set( - 6.5, 1.5, 6.5 );
100+
spotLight4.position.set( 6.5, 3, - 6.5 );
87101
spotLight4.angle = Math.PI / 8;
88102
spotLight4.penumbra = 0.7;
89103
spotLight4.distance = 20;
104+
spotLight4.castShadow = true;
90105
spotLight4.iesMap = iesTexture4;
106+
spotLight4.userData.helper = new THREE.SpotLightHelper( spotLight4 );
91107
scene.add( spotLight4 );
108+
scene.add( spotLight4.target );
109+
scene.add( spotLight4.userData.helper );
92110

93111
//
94112

95113
lights = [ spotLight, spotLight2, spotLight3, spotLight4 ];
96114

97115
//
98116

99-
const material = new THREE.MeshPhongMaterial( { color: 0x808080/*, dithering: true*/ } );
117+
const material = new THREE.MeshPhongMaterial( { color: 0x999999/*, dithering: true*/ } );
100118

101119
const geometry = new THREE.PlaneGeometry( 200, 200 );
102120

103121
const mesh = new THREE.Mesh( geometry, material );
104122
mesh.rotation.x = - Math.PI * 0.5;
123+
mesh.receiveShadow = true;
105124
scene.add( mesh );
106125

126+
const geometry2 = new THREE.BoxGeometry( 2, 2, 2 );
127+
//const geometry2 = new THREE.IcosahedronGeometry( 1, 5 );
128+
129+
const mesh2 = new THREE.Mesh( geometry2, material );
130+
mesh2.position.y = 1;
131+
mesh2.castShadow = true;
132+
scene.add( mesh2 );
133+
107134
//
108135

109136
renderer = new THREE.WebGPURenderer( { antialias: true } );
110137
renderer.setPixelRatio( window.devicePixelRatio );
111138
renderer.setSize( window.innerWidth, window.innerHeight );
112139
renderer.setAnimationLoop( render );
140+
renderer.shadowMap.enabled = true;
113141
document.body.appendChild( renderer.domElement );
114142

115143
camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, .1, 100 );
@@ -122,6 +150,28 @@
122150

123151
//
124152

153+
function setHelperVisible( value ) {
154+
155+
for ( let i = 0; i < lights.length; i ++ ) {
156+
157+
lights[ i ].userData.helper.visible = value;
158+
159+
}
160+
161+
}
162+
163+
setHelperVisible( false );
164+
165+
//
166+
167+
const gui = new GUI();
168+
169+
gui.add( { helper: false }, 'helper' ).onChange( ( v ) => setHelperVisible( v ) );
170+
171+
gui.open();
172+
173+
//
174+
125175
window.addEventListener( 'resize', onWindowResize );
126176

127177
}
@@ -137,11 +187,18 @@
137187

138188
function render( time ) {
139189

140-
time = ( time / 1000 ) * 2.0;
190+
time = time / 1000;
141191

142192
for ( let i = 0; i < lights.length; i ++ ) {
143193

144-
lights[ i ].position.y = Math.sin( time + i ) + 0.97;
194+
const t = ( Math.sin( ( time + i ) * ( Math.PI / 2 ) ) + 1 ) / 2;
195+
196+
const x = THREE.MathUtils.lerp( lights[ i ].position.x, 0, t );
197+
const z = THREE.MathUtils.lerp( lights[ i ].position.z, 0, t );
198+
199+
lights[ i ].target.position.x = x;
200+
lights[ i ].target.position.z = z;
201+
if ( lights[ i ].userData.helper ) lights[ i ].userData.helper.update();
145202

146203
}
147204

0 commit comments

Comments
 (0)