Skip to content

Commit d57991b

Browse files
committed
improve custom aspect example
1 parent 95ff03d commit d57991b

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

examples/webgpu_lights_spotlight.html

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<script type="module">
2727

2828
import * as THREE from 'three';
29-
import { Fn, vec2, length, abs, max, min, div, mul, clamp, acos } from 'three/tsl';
29+
import { Fn, float, vec2, vec3, length, uniform, abs, max, min, sub, div, mul, saturate, acos } from 'three/tsl';
3030

3131
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
3232

@@ -41,6 +41,8 @@
4141

4242
function init() {
4343

44+
// Renderer
45+
4446
renderer = new THREE.WebGPURenderer( { antialias: true } );
4547
renderer.setPixelRatio( window.devicePixelRatio );
4648
renderer.setSize( window.innerWidth, window.innerHeight );
@@ -58,15 +60,16 @@
5860
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
5961
camera.position.set( 7, 4, 1 );
6062

63+
// Controls
64+
6165
const controls = new OrbitControls( camera, renderer.domElement );
6266
controls.minDistance = 2;
6367
controls.maxDistance = 10;
6468
controls.maxPolarAngle = Math.PI / 2;
6569
controls.target.set( 0, 1, 0 );
6670
controls.update();
6771

68-
const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.15 );
69-
scene.add( ambient );
72+
// Textures
7073

7174
const loader = new THREE.TextureLoader().setPath( 'textures/' );
7275
const filenames = [ 'disturb.jpg', 'colors.png', 'uv_grid_opengl.jpg' ];
@@ -87,8 +90,15 @@
8790

8891
}
8992

93+
// Lights
94+
95+
const ambient = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d, 0.15 );
96+
scene.add( ambient );
97+
9098
const boxAttenuationFn = Fn( ( [ lightNode ], builder ) => {
9199

100+
const light = lightNode.light;
101+
92102
const sdBox = Fn( ( [ p, b ] ) => {
93103

94104
const d = vec2( abs( p ).sub( b ) ).toVar();
@@ -97,13 +107,13 @@
97107

98108
} );
99109

100-
const penumbraCos = lightNode.penumbraCosNode;
110+
const penumbraCos = uniform( 'float' ).onRenderUpdate( () => Math.cos( light.angle * ( 1 - Math.min( light.penumbra, .999 ) ) ) );
101111
const spotLightCoord = lightNode.getSpotLightCoord( builder );
102112
const coord = spotLightCoord.xyz.div( spotLightCoord.w );
103113

104114
const boxDist = sdBox( coord.xy.sub( vec2( 0.5 ) ), vec2( 0.5 ) );
105-
const angleFactor = div( 1.0, acos( penumbraCos ).sub( 1.0 ) );
106-
const attenuation = clamp( mul( 2.0, boxDist ).mul( angleFactor ), 0.0, 1.0 );
115+
const angleFactor = div( -1.0, sub( 1.0, acos( penumbraCos ) ).sub( 1.0 ) );
116+
const attenuation = saturate( boxDist.mul( - 2.0 ).mul( angleFactor ) );
107117

108118
return attenuation;
109119

@@ -140,7 +150,7 @@
140150
mesh.receiveShadow = true;
141151
scene.add( mesh );
142152

143-
//
153+
// Models
144154

145155
new PLYLoader().load( 'models/ply/binary/Lucy100k.ply', function ( geometry ) {
146156

@@ -246,8 +256,12 @@
246256

247257
spotLight.attenuationNode = val ? boxAttenuationFn : null;
248258

259+
aspectGUI.setValue( 1 ).enable( val );
260+
249261
} );
250262

263+
const aspectGUI = gui.add( spotLight.shadow, 'aspect', 0, 1 ).enable( false );
264+
251265
gui.open();
252266

253267
}

0 commit comments

Comments
 (0)