better shadow

This commit is contained in:
Adrien MALINGREY 2023-06-01 01:23:30 +02:00
parent ed87833848
commit da7869d31d

40
main.js
View File

@ -222,29 +222,27 @@ scene.add( ambientLight );
const sunLight = new THREE.DirectionalLight( 0xffffff, 0.3 ); const sunLight = new THREE.DirectionalLight( 0xffffff, 0.3 );
sunLight.castShadow = true; sunLight.castShadow = true;
sunLight.shadow.camera.near = 0.1; sunLight.shadow.camera.near = 20;
sunLight.shadow.camera.far = 500; sunLight.shadow.camera.far = 200;
sunLight.shadow.camera.right = 30; sunLight.shadow.camera.right = 10;
sunLight.shadow.camera.left = - 30; sunLight.shadow.camera.left = -10;
sunLight.shadow.camera.top = 30; sunLight.shadow.camera.top = 10;
sunLight.shadow.camera.bottom = - 30; sunLight.shadow.camera.bottom = -10;
sunLight.shadow.mapSize.width = 4096; sunLight.shadow.mapSize.width = 4096;
sunLight.shadow.mapSize.height = 4096; sunLight.shadow.mapSize.height = 4096;
sunLight.shadow.radius = 5; sunLight.target = camera
sunLight.target = ground
scene.add( sunLight ); scene.add( sunLight );
const torchLight = new THREE.SpotLight(0xffffe8, 1, mazeLength/2, .45, 1) const torchLight = new THREE.SpotLight(0xffffe8, 1, mazeLength/2, .45, 1)
torchLight.castShadow = true; torchLight.castShadow = true;
torchLight.shadow.camera.near = 0.1; torchLight.shadow.camera.near = 20;
torchLight.shadow.camera.far = 500; torchLight.shadow.camera.far = 200;
torchLight.shadow.camera.right = 30; torchLight.shadow.camera.right = 30;
torchLight.shadow.camera.left = -30; torchLight.shadow.camera.left = -30;
torchLight.shadow.camera.top = 30; torchLight.shadow.camera.top = 30;
torchLight.shadow.camera.bottom = -30; torchLight.shadow.camera.bottom = -30;
torchLight.shadow.mapSize.width = 4096; torchLight.shadow.mapSize.width = 512;
torchLight.shadow.mapSize.height = 4096; torchLight.shadow.mapSize.height = 512;
torchLight.shadow.radius = 4;
scene.add( torchLight ); scene.add( torchLight );
scene.add( torchLight.target ); scene.add( torchLight.target );
@ -292,14 +290,14 @@ function updateSun() {
const hour = ( 8 + time / 1440 ) % 24 const hour = ( 8 + time / 1440 ) % 24
const hourAngle = Math.PI * (1-hour/12) const hourAngle = Math.PI * (1-hour/12)
elevation = Math.asin( Math.sin(declination)*Math.sin(latitude) + Math.cos(declination)*Math.cos(latitude)*Math.cos(hourAngle) ) elevation = Math.asin( Math.sin(declination)*Math.sin(latitude) + Math.cos(declination)*Math.cos(latitude)*Math.cos(hourAngle) )
azimuth = Math.asin( Math.cos(declination)*Math.sin(hourAngle)/Math.cos(elevation) ) azimuth = 0.6 + Math.asin( Math.cos(declination)*Math.sin(hourAngle)/Math.cos(elevation) )
} }
const phi = Math.PI/2 - elevation const phi = Math.PI/2 - elevation
const theta = azimuth const theta = azimuth
sun.setFromSphericalCoords( 1, phi, theta ); sun.setFromSphericalCoords( 100, phi, theta );
sky.material.uniforms[ 'sunPosition' ].value.copy( sun ); sky.material.uniforms[ 'sunPosition' ].value.copy( sun );
water.material.uniforms[ 'sunDirection' ].value.copy( sun ).normalize(); water.material.uniforms[ 'sunDirection' ].value.copy( sun ).normalize();
@ -307,10 +305,6 @@ function updateSun() {
if ( elevation >= 0 ) { if ( elevation >= 0 ) {
sunLight.visible = true sunLight.visible = true
sunLight.position.setFromSphericalCoords(100, phi, theta)
sunLight.position.x += mazeLength/2
sunLight.position.z += mazeWidth/2
torchLight.visible = false torchLight.visible = false
} else { } else {
@ -603,6 +597,14 @@ function animate() {
raft.rotation.x = 0.06 * Math.cos( time ) raft.rotation.x = 0.06 * Math.cos( time )
raft.position.y = 0.05 * (0.5 * Math.sin( 1.2 * time ) + 0.5 * Math.sin( time )) raft.position.y = 0.05 * (0.5 * Math.sin( 1.2 * time ) + 0.5 * Math.sin( time ))
if ( sunLight.visible ) {
sunLight.position.copy( sun )
sunLight.position.x += camera.position.x
sunLight.position.z += camera.position.z
}
if ( torchLight.visible ) { if ( torchLight.visible ) {
torchLight.position.copy(camera.position) torchLight.position.copy(camera.position)