Skip to content

Commit

Permalink
Updated plugin API for textures, rebuild
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed May 21, 2013
1 parent 7d0bc5a commit 5bb3734
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 208 deletions.
1 change: 1 addition & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"src/core/scene/translate.js",
"src/core/scene/scale.js",
"src/core/scene/modelXFormStack.js",
// "src/core/scene/object.js",

"src/core/display/display.js",
"src/core/display/programSourceFactory.js",
Expand Down
39 changes: 8 additions & 31 deletions build/latest/plugins/texture/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,34 @@ SceneJS.Plugins.addPlugin(

new (function () {

var sourceService = this;

this.getSource = function (params) {

var gl = params.gl;
var configs = {};
var texture = gl.createTexture();
var updated;
var publish;

return {

getTexture:function () {
return texture;
},

onUpdate:function (fn) {
updated = fn;
subscribe:function (fn) {
publish = fn;
},

setConfigs:function (cfg) {

configure:function (cfg) {
if (!cfg.src) {
throw "Parameter expected: 'src'";
}

configs = cfg;

var image = new Image();
image.crossOrigin = "anonymous";

image.onload = function () {

gl.bindTexture(gl.TEXTURE_2D, texture);

var potImage = ensureImageSizePowerOfTwo(image); // WebGL hates NPOT images

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, potImage);

if (updated) {
updated();
if (publish) {
publish(texture);
}
};

image.src = configs.src;
},

getConfigs:function () {
return configs;
image.src = cfg.src;
},

destroy:function () {
destroy:function () { // TODO
}
};
};
Expand Down
30 changes: 6 additions & 24 deletions build/latest/plugins/texture/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,21 @@ SceneJS.Plugins.addPlugin(

this.getSource = function (params) {

var gl = params.gl;
var configs = {};

var updated;
var gl = params.gl;
var publish;
var video;

var texture = gl.createTexture();

return {

getTexture:function () {
return texture;
},

onUpdate:function (fn) {
updated = fn;
subscribe:function (fn) {
publish = fn;
},

setConfigs:function (cfg) {

if (cfg.src) {


var canvas = document.createElement("canvas");
document.getElementsByTagName("body")[0].appendChild(canvas);
var ctx = canvas.getContext("2d");
Expand Down Expand Up @@ -92,28 +84,18 @@ SceneJS.Plugins.addPlugin(
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.generateMipmap(gl.TEXTURE_2D);


if (updated) {
updated();
if (publish) {
publish(texture);
}
}

window.requestAnimationFrame(updateTexture);
};

window.requestAnimationFrame(updateTexture);// TODO: synch with render loop
}

configs = cfg;
},

getConfigs:function () {
return configs;
},

destroy:function () {

// TODO: destroy any existing video
}
};
Expand Down
99 changes: 3 additions & 96 deletions build/latest/scenejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11237,10 +11237,10 @@ new (function () {
source.subscribe(// Get notification whenever source updates the texture
(function () {
var loaded = false;
return function () {
return function (texture) {
if (!loaded) { // Texture first initialised - create layer
loaded = true;
self._setLayerTexture(gl, layer, source.getTexture());
self._setLayerTexture(gl, layer, texture);

} else { // Texture updated - layer already has the handle to it, so just signal a redraw
self._engine.display.imageDirty = true;
Expand Down Expand Up @@ -12387,99 +12387,7 @@ var SceneJS_modelXFormStack = new (function () {
};

})();
(function () {

// Supported sub-node types for a SceneJS.Object
// Params for types not listed here are ignored.
var types = [
"lookAt",
"lights",
"translate",
"rotate",
"scale",
"flags",
"material",
"layer",
"tag",
"shader",
"xform",
"matrix",
"clips",
"geometry"
];

/**
* @class Scene graph convenience node which defines an object complete with transform, material, geometry etc,
* @extends SceneJS.Node
*/
SceneJS.Object = SceneJS_NodeFactory.createNodeType("object");

SceneJS.Object.prototype._init = function (params) {

this._nodes = {};

var nodeArgs = [];
var geometry = null;
var type;
var attr;
for (var i = 0, len = types.length; i < len; i++) {
type = types[i];
if (params.hasOwnProperty(type)) {
attr = params[type];
if (attr) {
if (type == "geometry") {
geometry = attr; // Geometry must be leaf
} else {
nodeArgs.push([type, attr]);
}
}
}
}

if (geometry) {
nodeArgs.push(["geometry", geometry]);
}

nodeArgs[nodeArgs.length - 1].nodes = params.nodes;

params.nodes = [];

for (var i = 0, len = nodeArgs.length; i < len; i++) {
this._addNode(nodeArgs[i][0], nodeArgs[i][1]);
}
};

SceneJS.Object.prototype._addNode = function (type, params) {
this[type] =
this._leaf =
this._nodes[type] =
(this._leaf || this).addNode(
SceneJS._apply(params, {
type:type
}));
};

/**
* Sets attributes on nodes within this object
* @param params
*/
SceneJS.Object.prototype.set = function (params) {
var node;
for (var type in params) {
if (params.hasOwnProperty(type)) {
node = this._nodes[type];
if (node) {
node.set(params[type]);
}
}
}
};

SceneJS.Object.prototype._compile = function () {
this._compileNodes();
};

})();/**
/**
* @class Renders and picks a {@link SceneJS.Scene}
* @private
*
Expand Down Expand Up @@ -13768,7 +13676,6 @@ var SceneJS_ProgramSourceFactory = new (function () {
/* Vector from vertex to light, packaged with the pre-computed length of that vector
*/
src.push("varying vec4 SCENEJS_vViewLightVecAndDist" + i + ";"); // varying for fragment lighting
//i++;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/scene/texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ new (function () {
source.subscribe(// Get notification whenever source updates the texture
(function () {
var loaded = false;
return function () {
return function (texture) {
if (!loaded) { // Texture first initialised - create layer
loaded = true;
self._setLayerTexture(gl, layer, source.getTexture());
self._setLayerTexture(gl, layer, texture);

} else { // Texture updated - layer already has the handle to it, so just signal a redraw
self._engine.display.imageDirty = true;
Expand Down
39 changes: 8 additions & 31 deletions src/plugins/texture/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,57 +5,34 @@ SceneJS.Plugins.addPlugin(

new (function () {

var sourceService = this;

this.getSource = function (params) {

var gl = params.gl;
var configs = {};
var texture = gl.createTexture();
var updated;
var publish;

return {

getTexture:function () {
return texture;
},

onUpdate:function (fn) {
updated = fn;
subscribe:function (fn) {
publish = fn;
},

setConfigs:function (cfg) {

configure:function (cfg) {
if (!cfg.src) {
throw "Parameter expected: 'src'";
}

configs = cfg;

var image = new Image();
image.crossOrigin = "anonymous";

image.onload = function () {

gl.bindTexture(gl.TEXTURE_2D, texture);

var potImage = ensureImageSizePowerOfTwo(image); // WebGL hates NPOT images

gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, potImage);

if (updated) {
updated();
if (publish) {
publish(texture);
}
};

image.src = configs.src;
},

getConfigs:function () {
return configs;
image.src = cfg.src;
},

destroy:function () {
destroy:function () { // TODO
}
};
};
Expand Down
Loading

0 comments on commit 5bb3734

Please sign in to comment.