Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[work in progess] introduce probe light #16199

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
513 changes: 486 additions & 27 deletions build/three.js

Large diffs are not rendered by default.

506 changes: 482 additions & 24 deletions build/three.module.js

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions examples/webgl_lights_probe.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lights - light probe</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
background-color: #fff;
color: #111;
margin: 0px;
overflow: hidden;
font-family: Monospace;
font-size: 13px;
}

#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
text-align: center;
}

a {
color: #0080ff;
text-decoration: none;
}

a:hover {
color: #f00;
}

#footer { width: 100%; margin: 2em auto; text-align: center; position: absolute; bottom: 0 }
strong { color: red }
</style>
</head>
<body>

<div id="container"></div>

<script src="../build/three.js"></script>

<script src="js/WebGL.js"></script>
<script src="js/libs/stats.min.js"></script>

<script>

if ( WEBGL.isWebGLAvailable() === false ) {

document.body.appendChild( WEBGL.getWebGLErrorMessage() );

}

var camera, scene, renderer, sphere;
var stats;

var clock = new THREE.Clock();

init();
animate();

function init() {

var container = document.getElementById( 'container' );

camera = new THREE.PerspectiveCamera( 30, window.innerWidth / window.innerHeight, 1, 5000 );
camera.position.set( 0, 0, 100 );

scene = new THREE.Scene();
scene.background = new THREE.Color(0);

// LIGHTS

var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
directionalLight.position.set( 1, 1, 1 );
directionalLight.target.position.set( 0, 0, 0 );
// scene.add( directionalLight );

var eucalyptusGroove = new THREE.LightProbe([
new THREE.Color( 0.3783264, 0.4260425, 0.4504587),
new THREE.Color( 0.2887813, 0.3586803, 0.4147053),
new THREE.Color( 0.0379030, 0.0295216, 0.0098567),
new THREE.Color(-0.1033028, -0.1031690, -0.0884924),
new THREE.Color(-0.0621750, -0.0554432, -0.0396779),
new THREE.Color( 0.0077820, -0.0148312, -0.0471301),
new THREE.Color(-0.0935561, -0.1254260, -0.1525629),
new THREE.Color(-0.0572703, -0.0502192, -0.0363410),
new THREE.Color( 0.0203348, -0.0044201, -0.0452180)
], 5.0);
eucalyptusGroove.position.set(25, 0, 0);
scene.add( eucalyptusGroove );

var office = new THREE.LightProbe([
new THREE.Color( 0.734601661990457511, 0.612049556826795960, 0.587005355037254706),
new THREE.Color( -0.507141321038506510, -0.417991745488756006, -0.429936331491808599 ),
new THREE.Color( 0.103560827244967943, 0.139260824989792281, 0.167449796591380679),
new THREE.Color( -0.033367479602039923, -0.253806971220720712, -0.413597597903080705),
new THREE.Color( -0.037424878066217721, 0.215960645848757793, 0.409441988391145206),
new THREE.Color( -0.103091702447255268, -0.141888444384140638, -0.169683072900887466),
new THREE.Color( -0.074329112149643245, -0.063088727238028067, -0.073822959245573444),
new THREE.Color( 0.010118787386155416, -0.113230203385412967, -0.185359511038784813),
new THREE.Color( 0.097220336682391037, 0.114504507536907302, 0.102246952020174295),
], 5.0);
office.position.set(-25, 0, 0);
scene.add( office );

// MODEL

var geometry = new THREE.SphereGeometry( 5, 32, 32 );
var material = new THREE.MeshStandardMaterial( {color: 0xffffff} );
sphere = new THREE.Mesh( geometry, material );
scene.add( sphere );

// RENDERER

renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

renderer.gammaInput = true;
renderer.gammaOutput = true;

// STATS

stats = new Stats();
container.appendChild( stats.dom );

//

window.addEventListener( 'resize', onWindowResize, false );

}

function onWindowResize() {

camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();

renderer.setSize( window.innerWidth, window.innerHeight );

}

//

function animate() {

requestAnimationFrame( animate );

render();
stats.update();

}

function render() {

var delta = clock.getDelta();

sphere.position.set(Math.sin(clock.elapsedTime) * 25.0, 0, 0);

renderer.render( scene, camera );

}

</script>
</body>
</html>
1 change: 1 addition & 0 deletions src/Three.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export { HemisphereLight } from './lights/HemisphereLight.js';
export { DirectionalLightShadow } from './lights/DirectionalLightShadow.js';
export { DirectionalLight } from './lights/DirectionalLight.js';
export { AmbientLight } from './lights/AmbientLight.js';
export { LightProbe } from './lights/LightProbe.js';
export { LightShadow } from './lights/LightShadow.js';
export { Light } from './lights/Light.js';
export { StereoCamera } from './cameras/StereoCamera.js';
Expand Down
37 changes: 37 additions & 0 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,32 @@ function WebGLRenderer( parameters ) {

}

function computeObjectSphericalHarmonics( object, lightProbes ) {

// This routine can certainly be improved to take into account a better interpolation
// scheme and visibility sets but for now nearest neighbour allows the SH
// project to continue

var closestProbe = null;
var closestDist = Number.MAX_SAFE_INTEGER;

for ( var lightIdx = 0; lightIdx < lightProbes.length; lightIdx++ ) {

if ( object.position.distanceTo( lightProbes[lightIdx].position ) < closestDist ) {

closestProbe = lightProbes[lightIdx];

}

}

return [closestProbe.coeff0, closestProbe.coeff1, closestProbe.coeff2,
closestProbe.coeff3, closestProbe.coeff4, closestProbe.coeff5,
closestProbe.coeff6, closestProbe.coeff7, closestProbe.coeff8
];

}

function renderObject( object, scene, camera, geometry, material, group ) {

object.onBeforeRender( _this, scene, camera, geometry, material, group );
Expand Down Expand Up @@ -1466,6 +1492,7 @@ function WebGLRenderer( parameters ) {
releaseMaterialProgramReference( material );

} else if ( lightsHash.stateID !== lightsStateHash.stateID ||
lightsHash.probeLength !== lightsStateHash.probeLength ||
lightsHash.directionalLength !== lightsStateHash.directionalLength ||
lightsHash.pointLength !== lightsStateHash.pointLength ||
lightsHash.spotLength !== lightsStateHash.spotLength ||
Expand All @@ -1474,6 +1501,7 @@ function WebGLRenderer( parameters ) {
lightsHash.shadowsLength !== lightsStateHash.shadowsLength ) {

lightsHash.stateID = lightsStateHash.stateID;
lightsHash.probeLength = lightsStateHash.probeLength;
lightsHash.directionalLength = lightsStateHash.directionalLength;
lightsHash.pointLength = lightsStateHash.pointLength;
lightsHash.spotLength = lightsStateHash.spotLength;
Expand Down Expand Up @@ -1587,6 +1615,7 @@ function WebGLRenderer( parameters ) {
}

lightsHash.stateID = lightsStateHash.stateID;
lightsHash.probeLength = lightsStateHash.probeLength;
lightsHash.directionalLength = lightsStateHash.directionalLength;
lightsHash.pointLength = lightsStateHash.pointLength;
lightsHash.spotLength = lightsStateHash.spotLength;
Expand All @@ -1598,7 +1627,10 @@ function WebGLRenderer( parameters ) {

// wire up the material to this renderer's lighting state

// var harmonics = computeObjectSphericalHarmonics( object, currentRenderState.state.lights.state.probe );

uniforms.ambientLightColor.value = lights.state.ambient;
// uniforms.sh.value = harmonics;
uniforms.directionalLights.value = lights.state.directional;
uniforms.spotLights.value = lights.state.spot;
uniforms.rectAreaLights.value = lights.state.rectArea;
Expand Down Expand Up @@ -1959,6 +1991,10 @@ function WebGLRenderer( parameters ) {

}

var harmonics = computeObjectSphericalHarmonics( object, currentRenderState.state.lights.state.probe );

p_uniforms.setValue( _gl, 'sh', harmonics );

// common matrices

p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix );
Expand Down Expand Up @@ -2403,6 +2439,7 @@ function WebGLRenderer( parameters ) {

uniforms.ambientLightColor.needsUpdate = value;

uniforms.sh.needsUpdate = value;
uniforms.directionalLights.needsUpdate = value;
uniforms.pointLights.needsUpdate = value;
uniforms.spotLights.needsUpdate = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ IncidentLight directLight;

vec3 irradiance = getAmbientLightIrradiance( ambientLightColor );

irradiance += shGetIrradianceAt(sh, geometry.normal);

#if ( NUM_HEMI_LIGHTS > 0 )

#pragma unroll_loop
Expand Down
29 changes: 29 additions & 0 deletions src/renderers/shaders/ShaderChunk/lights_pars_begin.glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ vec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {

}

uniform vec3 sh[9];

#define C1 0.429043
#define C2 0.511664
#define C3 0.743125
#define C4 0.886227
#define C5 0.247708

// normal is assumed to be unit length!
vec3 shGetIrradianceAt( vec3 sh[9], vec3 normal )
{
// band 0
vec3 result = sh[0] * C4;

// band 1
result += sh[1] * ( 2.0 * C2 * normal.y );
result += sh[2] * ( 2.0 * C2 * normal.z );
result += sh[3] * ( 2.0 * C2 * normal.x );

// band 2
result += sh[4] * ( 2.0 * C1 * normal.x*normal.y );
result += sh[5] * ( 2.0 * C1 * normal.y*normal.z );
result += sh[6] * ( C3 * normal.z*normal.z - C5 );
result += sh[7] * ( 2.0 * C1 * normal.x*normal.z );
result += sh[8] * ( C1 * ( normal.x*normal.x - normal.y*normal.y ) );

return result;
}

#if NUM_DIR_LIGHTS > 0

struct DirectionalLight {
Expand Down
13 changes: 13 additions & 0 deletions src/renderers/shaders/UniformsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ var UniformsLib = {

ambientLightColor: { value: [] },

sh: { value: [], properties: {
coeff0: {},
coeff1: {},
coeff2: {},
coeff3: {},
coeff4: {},
coeff5: {},
coeff6: {},
coeff7: {},
coeff8: {},
position: {},
} },

directionalLights: { value: [], properties: {
direction: {},
color: {},
Expand Down
Loading