Skip to content

Commit

Permalink
wip: trying to get camera scale to update properly
Browse files Browse the repository at this point in the history
  • Loading branch information
marwie authored and hybridherbst committed Aug 28, 2023
1 parent 1b04cd9 commit af9c43c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/webgl_lights_pointlights.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@

camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 100;
setInterval(()=>{
const scale = Math.random() * 10 + .5;
camera.scale.set(scale, scale, scale)
}, 500)

scene = new THREE.Scene();

Expand Down
6 changes: 5 additions & 1 deletion examples/webgl_lights_spotlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 7, 4, 1 );
setInterval(()=>{
const scale = Math.random() * 3 + .5;
camera.scale.set(scale, scale, scale)
}, 500)

const controls = new OrbitControls( camera, renderer.domElement );
controls.minDistance = 2;
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,7 @@ class WebGLRenderer {

uniforms.ambientLightColor.value = lights.state.ambient;
uniforms.lightProbe.value = lights.state.probe;
uniforms.cameraScale.value = lights.state.cameraScale;
uniforms.directionalLights.value = lights.state.directional;
uniforms.directionalLightShadows.value = lights.state.directionalShadow;
uniforms.spotLights.value = lights.state.spot;
Expand Down Expand Up @@ -1975,6 +1976,7 @@ class WebGLRenderer {

uniforms.ambientLightColor.needsUpdate = value;
uniforms.lightProbe.needsUpdate = value;
uniforms.cameraScale.needsUpdate = value;

uniforms.directionalLights.needsUpdate = value;
uniforms.directionalLightShadows.needsUpdate = value;
Expand Down
5 changes: 3 additions & 2 deletions src/renderers/shaders/ShaderChunk/lights_pars_begin.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default /* glsl */`
uniform bool receiveShadow;
uniform vec3 ambientLightColor;
uniform vec3 lightProbe[ 9 ];
uniform float cameraScale;
// get the irradiance (radiance convolved with cosine lobe) at the point 'normal' on the unit sphere
// source: https://graphics.stanford.edu/papers/envmap/envmap.pdf
Expand Down Expand Up @@ -123,7 +124,7 @@ float getSpotAttenuation( const in float coneCosine, const in float penumbraCosi
light.direction = normalize( lVector );
float lightDistance = length( lVector );
float lightDistance = length( lVector ) * cameraScale;
light.color = pointLight.color;
light.color *= getDistanceAttenuation( lightDistance, pointLight.distance, pointLight.decay );
Expand Down Expand Up @@ -161,7 +162,7 @@ float getSpotAttenuation( const in float coneCosine, const in float penumbraCosi
if ( spotAttenuation > 0.0 ) {
float lightDistance = length( lVector );
float lightDistance = length( lVector ) * cameraScale;
light.color = spotLight.color * spotAttenuation;
light.color *= getDistanceAttenuation( lightDistance, spotLight.distance, spotLight.decay );
Expand Down
2 changes: 2 additions & 0 deletions src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ const UniformsLib = {

lightProbe: { value: [] },

cameraScale: { value: 1 },

directionalLights: { value: [], properties: {
direction: {},
color: {}
Expand Down
7 changes: 7 additions & 0 deletions src/renderers/webgl/WebGLLights.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ function WebGLLights( extensions, capabilities ) {
const distance = light.distance;

const shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null;
state.cameraScale = 0.0;

if ( light.isAmbientLight ) {

Expand Down Expand Up @@ -472,6 +473,9 @@ function WebGLLights( extensions, capabilities ) {

}

// TODO how can we properly set the version here?
state.version = nextVersion ++;

}

function setupView( lights, camera ) {
Expand All @@ -482,6 +486,9 @@ function WebGLLights( extensions, capabilities ) {
let rectAreaLength = 0;
let hemiLength = 0;

camera.getWorldScale(vector3);

Check failure on line 489 in src/renderers/webgl/WebGLLights.js

View workflow job for this annotation

GitHub Actions / Lint testing

There must be a space after this paren

Check failure on line 489 in src/renderers/webgl/WebGLLights.js

View workflow job for this annotation

GitHub Actions / Lint testing

There must be a space before this paren
state.cameraScale = vector3.x;

const viewMatrix = camera.matrixWorldInverse;

for ( let i = 0, l = lights.length; i < l; i ++ ) {
Expand Down

0 comments on commit af9c43c

Please sign in to comment.