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 extension #92

Merged
merged 37 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
584c36a
created a base class for structs that can be uploaded to the shader a…
Nov 12, 2018
0956a72
fixed type of color in uniform light
Nov 12, 2018
1f448e4
activated light handling in the renderer
Nov 12, 2018
e2f9486
moved the default direction for directional light into the uniform li…
Nov 12, 2018
2becb31
enabled updateUniform to work with arrays and objects that inherit Un…
Nov 12, 2018
859dd72
the shader now iterates all lights
Nov 12, 2018
e2ec8f4
little cleanup
Nov 12, 2018
c6b2968
little cleanup
Nov 12, 2018
826d3ce
WIP: moving things out of the lights-loop that don't belong there
Nov 12, 2018
14e250f
re-enabled IBL with -n as light direction
Nov 12, 2018
66e2a11
extract shared variables to outer scope
Nov 12, 2018
5906476
explicitly excluding mix-code
Nov 12, 2018
58c549a
split PBRInfo into MaterialInfo and AngularInfo to reduce code duplic…
Nov 12, 2018
f3e98fa
cleanup
Nov 12, 2018
ce26c2a
now correctly ignoring lights that are not attached to any node
Nov 12, 2018
818de13
added empty methods for calculating point and spot lights
Nov 13, 2018
d0c8bfa
only rendering lights that are contained in the scene
Nov 13, 2018
7921790
fixed direction calculation in light
Nov 13, 2018
45b0ec4
multiply light color with intensity
Nov 13, 2018
03ee61d
implemented point lights
Nov 13, 2018
5a86933
added default values for spot light
Nov 13, 2018
d3fdc62
easier setting of default model
Nov 13, 2018
466e96a
fixed names and/or vector directions
Nov 13, 2018
6ad005e
enabled unlimited range for point lights when range is undefined
Nov 13, 2018
fa21005
cleanup
Nov 13, 2018
3359913
implemented skeleton for spot lights
Nov 13, 2018
2ca317d
implemented spotlights (outer cone only)
Nov 13, 2018
47a686c
implemented smoothing between inner and outer cone for spotlights
Nov 13, 2018
8d4d339
now providing the cosines for outer and inner cone directly
Nov 13, 2018
e1f3e3b
removed commented out code
Nov 14, 2018
827c54e
now setting light range to an explicit number by default so the shade…
Nov 14, 2018
e379543
ticked the feature in the features list
Nov 14, 2018
058def4
Merge branch 'reference-viewer' into feature/LightsExtension
Nov 14, 2018
d0f74d0
"punctual" lights are now optional and can be toggled in the UI
Nov 14, 2018
ef64da2
inverted the default light direction to fit the recent code changes
Nov 14, 2018
5bc227b
Merge branch 'feature/LightsExtension' of github.com:KhronosGroup/glT…
Nov 14, 2018
c2bee3a
Merge branch 'reference-viewer' into feature/LightsExtension
UX3D-nopper Nov 14, 2018
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Features
- [ ] Selection of tonemapping algorithms for IBL
- [x] Support for the KHR_materials_unlit extension
- [x] Support for the KHR_texture_transform extension
- [ ] Support for the KHR_lights_punctual extension
- [x] Support for the KHR_lights_punctual extension
- [ ] Educational GUI for inspecting BRDF components

If you would like to see this in action, [view the live demo](http://gltf.ux3d.io/).
Expand Down
41 changes: 27 additions & 14 deletions src/light.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
class gltfLight
{
constructor(type = "directional",
color = jsToGl([1, 1, 1]),
color = [1, 1, 1],
intensity = 1,
range = undefined,
innerConeAngle = 0.0,
outerConeAngle = Math.PI / 4.0,
range = -1.0, // if no range is defined in the json, this is the default the shader understands
name = undefined,
node = undefined)
{
this.type = type;
this.color = color;
this.intensity = intensity;
this.innerConeAngle = innerConeAngle;
this.outerConeAngle = outerConeAngle;
this.range = range;
this.name = name;
this.node = node; // non-standard
Expand All @@ -20,20 +24,26 @@ class gltfLight
fromKeys(this, jsonLight);
}

toUniform(transform)
toUniform(gltf)
{
let uLight = new UniformLight();
let rotation = mat3.create();
mat4.getRotation(rotation, transform);
mat3.multiply(uLight.direction, uLight.direction, rotation);

if (this.node !== undefined)
{
let transform = gltf.nodes[this.node].worldTransform;
let rotation = quat.create();
let alongNegativeZ = vec3.fromValues(0, 0, -1);
mat4.getRotation(rotation, transform);
vec3.transformQuat(uLight.direction, alongNegativeZ, rotation);
mat4.getTranslation(uLight.position, transform);
}

uLight.range = this.range;
This conversation was marked as resolved.
Show resolved Hide resolved
uLight.color = this.color;
uLight.color = jsToGl(this.color);
uLight.intensity = this.intensity;

mat4.getTranslation(uLight.position, transform);
uLight.innerConeAngle = this.innerConeAngle;
uLight.outerConeAngle = this.outerConeAngle;
uLight.innerConeCos = Math.cos(this.innerConeAngle);
uLight.outerConeCos = Math.cos(this.outerConeAngle);

switch(this.type)
{
Expand All @@ -57,20 +67,23 @@ const Type_Directional = 0;
const Type_Point = 1;
const Type_Spot = 2;

class UniformLight
class UniformLight extends UniformStruct
{
constructor()
{
this.direction = jsToGl([0, 0, -1]);
super();

const defaultDirection = vec3.fromValues(-0.7399, -0.6428, -0.1983);
this.direction = defaultDirection;
this.range = -1.0;
This conversation was marked as resolved.
Show resolved Hide resolved

this.color = jsToGl([1, 1, 1]);
this.intensity = 1.0;

this.position = jsToGl([0, 0, 0]);
this.innerConeAngle = 0.0;
this.innerConeCos = 0.0;

this.outerConeAngle = Math.PI / 4.0;
this.outerConeCos = Math.PI / 4.0;
this.type = Type_Directional;
this.padding = vec2.create();
}
Expand Down
49 changes: 26 additions & 23 deletions src/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ class gltfRenderer

LoadWebGLExtensions(requiredWebglExtensions);

this.lightCount = 0;
this.defaultLight = new gltfLight();
this.visibleLights = [];

this.viewMatrix = mat4.create();
this.projMatrix = mat4.create();
Expand Down Expand Up @@ -112,7 +111,7 @@ class gltfRenderer
this.currentCameraPosition = this.viewer.getCameraPosition();
}

this.lightCount = gltf.lights.length > 0 ? gltf.lights.length : 1;
this.visibleLights = this.getVisibleLights(gltf, scene);

mat4.multiply(this.viewProjMatrix, this.projMatrix, this.viewMatrix);

Expand All @@ -131,6 +130,23 @@ class gltfRenderer
}
}

// returns all lights that are relevant for rendering or the default light if there are none
getVisibleLights(gltf, scene)
{
let lights = [];
for (let light of gltf.lights)
{
if (light.node !== undefined)
{
if (scene.nodes.includes(light.node))
{
lights.push(light);
}
}
}
return lights.length > 0 ? lights : [ new gltfLight() ];
}

// same transform, recursive
drawNode(gltf, scene, nodeIndex, recursive)
{
Expand Down Expand Up @@ -176,7 +192,7 @@ class gltfRenderer

let fragDefines = material.getDefines().concat(primitive.getDefines());

fragDefines.push("LIGHT_COUNT " + this.lightCount);
fragDefines.push("LIGHT_COUNT " + this.visibleLights.length);

if (this.viewer.parameters.useIBL)
{
Expand All @@ -199,7 +215,10 @@ class gltfRenderer

gl.useProgram(this.shader.program);

// this.applyLights(gltf);
if (this.viewer.parameters.usePunctual)
{
this.applyLights(gltf);
}

// update model dependant matrices once per node
this.shader.updateUniform("u_MVPMatrix", mvpMatrix);
Expand Down Expand Up @@ -294,23 +313,9 @@ class gltfRenderer
applyLights(gltf)
{
let uniformLights = [];

function addLight(light)
{
const transform = gltf.nodes[light.node].worldTransform;
uniformLights.push(light.toUniform(transform));
}

if (gltf.lights.length > 0)
{
for (let l of gltf.lights)
{
addLight(l);
}
}
else
for (let light of this.visibleLights)
{
addLight(this.defaultLight);
uniformLights.push(light.toUniform(gltf));
}

this.shader.updateUniform("u_Lights", uniformLights);
Expand All @@ -329,8 +334,6 @@ class gltfRenderer
SetTexture(this.shader.getUniformLocation("u_SpecularEnvSampler"), gltf, specularEnvMap, texSlotOffset + 1);
SetTexture(this.shader.getUniformLocation("u_brdfLUT"), gltf, lut, texSlotOffset + 2);

this.shader.updateUniform("u_ScaleDiffBaseMR", jsToGl([0, 0, 0, 0]));
this.shader.updateUniform("u_ScaleFGDSpec", jsToGl([0, 0, 0, 0]));
this.shader.updateUniform("u_ScaleIBLAmbient", jsToGl([1, 1, 0, 0]));
}

Expand Down
39 changes: 38 additions & 1 deletion src/shader.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,46 @@ class gltfShader
}
}


updateUniform(objectName, object, log = true)
{
if (Array.isArray(object))
{
this.updateUniformArray(objectName, object, log);
}
else if (object instanceof UniformStruct)
{
this.updateUniformStruct(objectName, object, log);
}
else
{
this.updateUniformValue(objectName, object, log);
}
}

updateUniformArray(arrayName, array, log)
{
for (let i = 0; i < array.length; ++i)
{
let element = array[i];
let uniformName = arrayName + "[" + i + "]";
this.updateUniform(uniformName, element, log);
}
}

updateUniformStruct(structName, object, log)
{
let memberNames = Object.keys(object);
for (let memberName of memberNames)
{
let uniformName = structName + "." + memberName;
this.updateUniform(uniformName, object[memberName], log);
}
}

// upload the values of a uniform with the given name using type resolve to get correct function call
// vec3 => gl.uniform3f(value)
updateUniform(uniformName, value, log = true)
updateUniformValue(uniformName, value, log)
{
const uniform = this.uniforms.get(uniformName);

Expand Down
Loading