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

Lights can be instantiated by multiple nodes #402

Merged
merged 1 commit into from
May 17, 2022
Merged
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
38 changes: 18 additions & 20 deletions source/Renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ class gltfRenderer
if (this.visibleLights.length === 0 && !state.renderingParameters.useIBL &&
state.renderingParameters.useDirectionalLightsWithDisabledIBL)
{
this.visibleLights.push(this.lightKey);
this.visibleLights.push(this.lightFill);
this.visibleLights.push([null, this.lightKey]);
this.visibleLights.push([null, this.lightFill]);
}

mat4.multiply(this.viewProjectionMatrix, this.projMatrix, this.viewMatrix);
Expand Down Expand Up @@ -585,21 +585,20 @@ class gltfRenderer
}
}

// returns all lights that are relevant for rendering or the default light if there are none
/// Compute a list of lights instantiated by one or more nodes as a list of node-light tuples.
getVisibleLights(gltf, scene)
{
let lights = [];
for (let light of gltf.lights)
{
if (light.node !== undefined)
{
if (scene.includesNode(gltf, light.node))
{
lights.push(light);
}
const nodeLights = [];
for (const nodeIndex of scene.nodes) {
const node = gltf.nodes[nodeIndex];
const lightIndex = node.extensions?.KHR_lights_punctual?.light;
if (lightIndex === undefined) {
continue;
}
const light = gltf.lights[lightIndex];
nodeLights.push([node, light]);
}
return lights;
return nodeLights;
}

updateSkin(state, node)
Expand Down Expand Up @@ -649,7 +648,7 @@ class gltfRenderer
if (state.renderingParameters.usePunctual)
{
fragDefines.push("USE_PUNCTUAL 1");
fragDefines.push("LIGHT_COUNT " + this.visibleLights.length);
fragDefines.push(`LIGHT_COUNT ${this.visibleLights.length}`);
}

if (state.renderingParameters.useIBL && state.environment)
Expand Down Expand Up @@ -732,15 +731,14 @@ class gltfRenderer

applyLights(gltf)
{
let uniformLights = [];
for (let light of this.visibleLights)
const uniforms = [];
for (const [node, light] of this.visibleLights)
{
uniformLights.push(light.toUniform(gltf));
uniforms.push(light.toUniform(node));
}

if (uniformLights.length > 0)
if (uniforms.length > 0)
{
this.shader.updateUniform("u_Lights", uniformLights);
this.shader.updateUniform("u_Lights", uniforms);
}
}

Expand Down
76 changes: 24 additions & 52 deletions source/gltf/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class gltfLight extends GltfObject
innerConeAngle = 0,
outerConeAngle = Math.PI / 4,
range = -1,
name = undefined,
node = undefined)
name = undefined)
{
super();
this.type = type;
Expand All @@ -23,37 +22,14 @@ class gltfLight extends GltfObject
this.outerConeAngle = outerConeAngle;
this.range = range;
this.name = name;
// non gltf
this.node = node;

//Can be used to overwrite direction from node
this.direction = undefined;
}

initGl(gltf, webGlContext)
{
super.initGl(gltf, webGlContext);

for (let i = 0; i < gltf.nodes.length; i++)
{
const nodeExtensions = gltf.nodes[i].extensions;
if (nodeExtensions === undefined)
{
continue;
}

const lightsExtension = nodeExtensions.KHR_lights_punctual;
if (lightsExtension === undefined)
{
continue;
}

const lightIndex = lightsExtension.light;
if (gltf.lights[lightIndex] === this)
{
this.node = i;
break;
}
}
}

fromJson(jsonLight)
Expand All @@ -66,36 +42,32 @@ class gltfLight extends GltfObject
}
}

toUniform(gltf)
toUniform(node)
{
const uLight = new UniformLight();
const matrix = node?.worldTransform ?? mat4.identity;

if (this.node !== undefined)
// To extract a correct rotation, the scaling component must be eliminated.
var scale = vec3.fromValues(1, 1, 1);
mat4.getScaling(scale, matrix);
const mn = mat4.create();
for(const col of [0, 1, 2])
{
const matrix = gltf.nodes[this.node].worldTransform;

var scale = vec3.fromValues(1, 1, 1);
mat4.getScaling(scale, matrix);

// To extract a correct rotation, the scaling component must be eliminated.
const mn = mat4.create();
for(const col of [0, 1, 2])
{
mn[col] = matrix[col] / scale[0];
mn[col + 4] = matrix[col + 4] / scale[1];
mn[col + 8] = matrix[col + 8] / scale[2];
}
var rotation = quat.create();
mat4.getRotation(rotation, mn);
quat.normalize(rotation, rotation);

const alongNegativeZ = vec3.fromValues(0, 0, -1);
vec3.transformQuat(uLight.direction, alongNegativeZ, rotation);

var translation = vec3.fromValues(0, 0, 0);
mat4.getTranslation(translation, matrix);
uLight.position = translation;
mn[col] = matrix[col] / scale[0];
mn[col + 4] = matrix[col + 4] / scale[1];
mn[col + 8] = matrix[col + 8] / scale[2];
}
var rotation = quat.create();
mat4.getRotation(rotation, mn);
quat.normalize(rotation, rotation);

const uLight = new UniformLight();

const alongNegativeZ = vec3.fromValues(0, 0, -1);
vec3.transformQuat(uLight.direction, alongNegativeZ, rotation);

var translation = vec3.fromValues(0, 0, 0);
mat4.getTranslation(translation, matrix);
uLight.position = translation;

if (this.direction !== undefined)
{
Expand Down