Skip to content

Commit

Permalink
Simplified light implementation a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristian Sons authored and ariyapour committed Nov 9, 2015
1 parent 32bfd63 commit 8f4d7f4
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 88 deletions.
123 changes: 63 additions & 60 deletions src/renderer/renderer/adapter/light.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var TransformableAdapter = require("./transformable.js");
var Events = require("../../../interface/notification.js");
var Resource = require("../../../base/resourcemanager.js").Resource;
var LightConfiguration = require("../scene/light-configuration.js");

var DEFAULT_LIGHT_MODEL = "urn:xml3d:light:directional";
/**
* Adapter for <light>
* @constructor
Expand All @@ -13,73 +15,74 @@ var LightRenderAdapter = function (factory, node) {
this.dataAdapter = Resource.getAdapter(node, "data");
this.createRenderNode();
};
XML3D.createClass(LightRenderAdapter, TransformableAdapter);

LightRenderAdapter.prototype.createRenderNode = function () {
var parentAdapter = this.getParentRenderAdapter();
var parentNode = parentAdapter.getRenderNode && parentAdapter.getRenderNode();
this.renderNode = this.factory.getScene().createRenderLight({
light: {
model: this.node.getAttribute("model") || "urn:xml3d:light:directional",
data: this.dataAdapter.getXflowNode()
},
parent: parentNode,
visible: !this.node.visible ? false : undefined,
localIntensity: this.node.intensity
});
};
XML3D.createClass(LightRenderAdapter, TransformableAdapter, {

createRenderNode: function () {
var parentAdapter = this.getParentRenderAdapter();
var parentNode = parentAdapter.getRenderNode && parentAdapter.getRenderNode();
this.renderNode = this.factory.getScene().createRenderLight({
configuration: this.createLightConfiguration(),
parent: parentNode,
visible: !this.node.visible ? false : undefined
});
},

LightRenderAdapter.prototype.notifyChanged = function (evt) {
switch (evt.type) {
notifyChanged: function (evt) {
switch (evt.type) {
case Events.NODE_REMOVED:
return;
case Events.THIS_REMOVED:
this.dispose();
return;
case Events.ADAPTER_HANDLE_CHANGED:
break;
case Events.VALUE_MODIFIED:
this.valueModified(evt.mutation);
break;
case Events.ADAPTER_VALUE_CHANGED:
this.renderNode.setLightType(this.getLightModel(), evt.adapter.getDataNode());
}
};
return;
case Events.THIS_REMOVED:
this.dispose();
return;
case Events.ADAPTER_HANDLE_CHANGED:
break;
case Events.VALUE_MODIFIED:
this.valueModified(evt.mutation);
break;
case Events.ADAPTER_VALUE_CHANGED:
this.renderNode.setLightType(this.getLightModel(), evt.adapter.getDataNode());
}
},

LightRenderAdapter.prototype.valueModified = function (mutation) {
var newValue = mutation.target.getAttribute(mutation.attributeName);
switch (mutation.attributeName) {
case "visible":
this.renderNode.setLocalVisible(newValue && (newValue.toLowerCase() !== "false"));
break;
case "intensity": // TODO(ksons): remove in 5.1
XML3D.debug.logWarning("The <light> attribute intensity is deprecated in XML3D 5.0.", this.node);
break;
case "model":
this.renderNode.remove();
this.createRenderNode();
break;
}
};
valueModified: function (mutation) {
var newValue = mutation.target.getAttribute(mutation.attributeName);
switch (mutation.attributeName) {
case "visible":
this.renderNode.setLocalVisible(newValue && (newValue.toLowerCase() !== "false"));
break;
case "intensity": // TODO(ksons): remove in 5.1
XML3D.debug.logWarning("The <light> attribute intensity is deprecated in XML3D 5.0.", this.node);
break;
case "model":
this.renderNode.remove();
this.createRenderNode();
break;
}
},

createLightConfiguration: function () {
var model = this.node.hasAttribute("model") ? this.node.getAttribute("model") : DEFAULT_LIGHT_MODEL;
var opt = {
name: this.node.id
};
return new LightConfiguration(model, this.dataAdapter.getXflowNode(), opt);
},

LightRenderAdapter.prototype.getLightModel = function () {
return ;
};
dispose: function () {
this.getRenderNode().remove();
this.clearAdapterHandles();
},

LightRenderAdapter.prototype.dispose = function () {
this.getRenderNode().remove();
this.clearAdapterHandles();
};

/**
* @return {XML3DMatrix}
*/
LightRenderAdapter.prototype.getWorldMatrix = function () {
var m = new window.XML3DMatrix();
this.renderNode.getWorldMatrix(m._data);
return m;
};
/**
* @return {XML3DMatrix}
*/
getWorldMatrix: function () {
var m = new window.XML3DMatrix();
this.renderNode.getWorldMatrix(m._data);
return m;
}
});

// Export
module.exports = LightRenderAdapter;
Expand Down
2 changes: 0 additions & 2 deletions src/renderer/renderer/lights/light-models.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ function transformPose(light, position, direction) {
}

function transformDefault(target, offset, light) {
var color = target["intensity"].subarray(offset * 3, offset * 3 + 3);
XML3D.math.vec3.scale(color, color, light.localIntensity);
target["on"][offset] = light.visible;
}

Expand Down
36 changes: 36 additions & 0 deletions src/renderer/renderer/scene/light-configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var uniqueObjectId = require("../../webgl/base/utils.js").getUniqueCounter();
/**
* Connects a light model with a data node containing light paramters
* and a render light to evaluate the world position of the light.
* The LightConfiguration is immutable
*
* @param model The light model
* @param {Xflow.DataNode} dataNode The light parameters of this node
* @param {RenderLight} light The light in the transformation hierarchy
* @param {{}} opt
* @constructor
*/
var LightConfiguration = function(model, dataNode, opt) {
opt = opt || {};

this.id = uniqueObjectId();

/**
* @type {{type: string}}
*/
this.model = model;

/**
* Data Node of the renderObject
* @type {Xflow.DataNode}
*/
this.dataNode = dataNode;

/**
* A name for debug purposes
* @type {string|null}
*/
this.name = opt.name || null;
};

module.exports = LightConfiguration;
11 changes: 2 additions & 9 deletions src/renderer/renderer/scene/renderlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ function createLightModel(type, data, light) {
var RenderLight = function (scene, pageEntry, opt) {
RenderNode.call(this, NODE_TYPE.LIGHT, scene, pageEntry, opt);
opt = opt || {};
var light = opt.light || {};
this.userData = null;
this.localIntensity = opt.localIntensity !== undefined ? opt.localIntensity : 1.0;
this.setLightType(light.model, light.data);
var configuration = opt.configuration || {};
this.setLightType(configuration.model, configuration.dataNode);
};
RenderLight.ENTRY_SIZE = ENTRY_SIZE;

Expand Down Expand Up @@ -102,11 +100,6 @@ XML3D.extend(RenderLight.prototype, {
}
},

setLocalIntensity: function (intensity) {
this.localIntensity = intensity;
this.lightValueChanged();
},

remove: function () {
this.parent.removeChild(this);
this.scene.lights.remove(this);
Expand Down
18 changes: 8 additions & 10 deletions tests/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,18 @@ var SceneConstants = XML3DTestLib.SceneConstants;
var DataNode = XML3DTestLib.DataNode;
var ComputeRequest = XML3DTestLib.ComputeRequest;

test("Light attributes", 10, function () {
test("Light attributes", 9, function () {

var dataNode = new DataNode(false);
var lightModels = this.scene.lights._models;

var light = this.scene.createRenderLight({
light: {
configuration: {
model: "urn:xml3d:light:directional",
data: dataNode
}
});

equal(light.localIntensity, 1.0, "Local intensity default");
var result = new ComputeRequest(light.model.dataNode, ["intensity"]).getResult();
var actualVector = new XML3DVec3();
actualVector.set(result.getOutputData("intensity").getValue());
Expand All @@ -49,7 +48,7 @@ test("Light attributes", 10, function () {
equal(lightModels.directional.lightModels.length, 1, "Light without type is in directional container (default)");
});

test("Light callbacks", 9, function () {
test("Light callbacks", 8, function () {

var dataNode = new DataNode(false);

Expand Down Expand Up @@ -81,8 +80,7 @@ test("Light callbacks", 9, function () {
start();
});

stop(6);
light.setLocalIntensity(0.5);
stop(5);
group.setLocalVisible(false);
group.setLocalVisible(true);
light.setVisible(false);
Expand All @@ -98,17 +96,17 @@ test("Light removal: Issue #71", function () {
var group = this.scene.createRenderGroup();
this.scene.createRenderLight({ // SC 3: Add a new light to the scene
parent: group,
light: {
data: dataNode,
configuration: {
dataNode: dataNode,
model: "urn:xml3d:light:point"
}
});

group = this.scene.createRenderGroup();
var light = this.scene.createRenderLight({ // SC 3: Add a new light to the scene
parent: group,
light: {
data: dataNode,
configuration: {
dataNode: dataNode,
model: "urn:xml3d:light:point"
}
});
Expand Down
14 changes: 7 additions & 7 deletions tests/webgl-shading.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ test("Phong fragment shader", function() {
notEqual(fragment1.indexOf("HAS_DIFFUSETEXTURE 0"), -1, "HAS_DIFFUSETEXTURE set");

directives = [];
var point1 = scene.createRenderLight({light: { model: "urn:xml3d:light:point"}});
var point2 = scene.createRenderLight({light: { model: "urn:xml3d:light:point"}});
var point1 = scene.createRenderLight({configuration: { model: "urn:xml3d:light:point"}});
var point2 = scene.createRenderLight({configuration: { model: "urn:xml3d:light:point"}});

phong.addDirectives.call(phong, directives, scene.lights, materialParameters);
equal(directives.length, 10, "10 directives from phong material");
Expand All @@ -64,7 +64,7 @@ test("Phong fragment shader", function() {
// Remove the two point lights and add a directional light instead
point1.remove();
point2.remove();
var dir1 = scene.createRenderLight({light: { model: "urn:xml3d:light:directional"}});
var dir1 = scene.createRenderLight({configuration: { model: "urn:xml3d:light:directional"}});

phong.addDirectives.call(phong, directives, scene.lights, materialParameters);
var fragment3 = this.mergeDirectives(directives, this.addFragmentShaderHeader(phong.fragment));
Expand All @@ -88,7 +88,7 @@ test("Phong fragment shader", function() {
directives = [];
dir1.remove();
// Create 15 point lights
var pointLights = Array.apply(null, Array(15)).map(function() { return scene.createRenderLight({light: { model: "urn:xml3d:light:point"}}); });
var pointLights = Array.apply(null, Array(15)).map(function() { return scene.createRenderLight({configuration: { model: "urn:xml3d:light:point"}}); });

phong.addDirectives.call(phong, directives, scene.lights, {
specularTexture : {}
Expand All @@ -104,9 +104,9 @@ test("Phong fragment shader", function() {
notEqual(fragment5.indexOf("HAS_EMISSIVETEXTURE 0"), -1, "HAS_EMISSIVETEXTURE set");

pointLights.forEach(function(light, idx) { if (idx > 4) light.remove(); });
var dir1 = scene.createRenderLight({light: { model: "urn:xml3d:light:directional"}});
var dir2 = scene.createRenderLight({light: { model: "urn:xml3d:light:directional"}});
var dir3 = scene.createRenderLight({light: { model: "urn:xml3d:light:directional"}});
var dir1 = scene.createRenderLight({configuration: { model: "urn:xml3d:light:directional"}});
var dir2 = scene.createRenderLight({configuration: { model: "urn:xml3d:light:directional"}});
var dir3 = scene.createRenderLight({configuration: { model: "urn:xml3d:light:directional"}});

directives = [];
phong.addDirectives.call(phong, directives, scene.lights, {
Expand Down

0 comments on commit 8f4d7f4

Please sign in to comment.