Skip to content

Commit

Permalink
Added support for custom node types
Browse files Browse the repository at this point in the history
  • Loading branch information
xeolabs committed Jun 12, 2013
1 parent c66297c commit 5652945
Show file tree
Hide file tree
Showing 17 changed files with 685 additions and 413 deletions.
103 changes: 102 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Hotlink to these binaries and they'll dynamically load SceneJS plugins on-demand
required. That's OK for playing around, but for production you'll probably want to serve the plugins yourself -
see [Plugin API](#plugin-api) below for how to do that.
* **[scenejs.js](http://xeolabs.github.com/scenejs/api/latest/scenejs.js)**
* **[scenejs.min.js](http://xeolabs.github.com/scenejs/api/latest/scenejs.min.js)**

Also hotlinkable are a bunch of helper utilities:
* **[OrbitControl](http://xeolabs.github.com/scenejs/api/latest/extras/orbitControl.js)** -
Expand Down Expand Up @@ -224,6 +223,108 @@ myGeometry.setLayers({
});
```

### Custom Node Types
Non-core node types are provided as plugins. This is a powerful extension mechanism that allows you to create your
own high-level scene components that just slot straight into the graph as nodes which you can access as usual via the JSON API.

Shown below is the [redTeapot plugin](build/latest/plugins/node/demos/redTeapot.js), which defines a scene node type that is a
red teapot which can be translated and scaled. This node type exposes setters to update the position and scale of the teapot.

```javascript
SceneJS.Types.addType("demos/redTeapot", {

// Constructor
init:function (params) {

this._translate = this.addNode({
type:"translate"
});

this._scale = this._translate.addNode({
type:"scale",
x:1, y:1, z:1,
nodes:[
{
type:"material",
color:{ r:1.0, g:0.6, b:0.6 },
nodes:[
{
type:"geometry",
source:{
type:"teapot"
}
}
]
}
]
});

if (params.pos) {
this.setPos(params.pos);
}

if (params.size) {
this.setSize(params.size);
}
},

// Setter for teapot position
setPos:function (pos) {
this._translate.setXYZ(pos);
},

// Setter for teapot scale
setSize:function (size) {
this._scale.setXYZ(size);
}
});
```

The scene below contains an instance of our "redTeapot" node. See how we just reference the type
with ```type:"demos/redTeapot"```, which causes SceneJS to dynamically load the plugin script shown above,
which installs the node type into SceneJS when it executes.

```javascript
var scene = SceneJS.createScene({
nodes:[
{
type:"rotate",
y: 1,
angle: 45,
nodes:[

// Instance our custom node type
// The optional size and pos attributes are fed into the node's setters
{
type:"demos/redTeapot",
id:"myRedTeapot",
size:{
x:0.4,
y:1.0,
z:0.5
},
pos:{
x:-1
}
}
]
}
]
});

// Subscribe to the redTeapot - the plugin which defines the redTeapot node
// may still be loading, so we get the node instance asynchronously

scene.on("nodes.myRedTeapot",
function(redTeapot) {

// Call a setter on our node (see the setter definitions in the plugin script above)
redTeapot.setSize({ x: 1.2 });
});
```



### Serving plugins yourself
If you'd rather serve the plugins yourself, instead of relying on the availability of this repository, then copy the
[plugins directory](build/latest/plugins) to your server and configure SceneJS to load them from there:
Expand Down
30 changes: 22 additions & 8 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@

"licenses/license-header.js",

// Integrated 3rd party libs

"src/core/webgl-debug-utils.js",

// Utilities

"src/core/map.js",
"src/core/scenejs.js",
"src/core/eventManager.js",
Expand All @@ -137,6 +141,8 @@
"src/core/webgl.js",
"src/core/status.js",

// Scene graph classes

"src/core/scene/nodeEvents.js",
"src/core/scene/core.js",
"src/core/scene/coreFactory.js",
Expand Down Expand Up @@ -166,7 +172,9 @@
"src/core/scene/translate.js",
"src/core/scene/scale.js",
"src/core/scene/modelXFormStack.js",
// "src/core/scene/object.js",
"src/core/nodeTypes.js",

// Display list classes

"src/core/display/display.js",
"src/core/display/programSourceFactory.js",
Expand All @@ -177,6 +185,8 @@
"src/core/display/object.js",
"src/core/display/renderContext.js",

// Display list state chunks

"src/core/display/chunks/chunk.js",
"src/core/display/chunks/chunkFactory.js",
"src/core/display/chunks/cameraChunk.js",
Expand Down Expand Up @@ -207,7 +217,6 @@

sys.print("Generating file list\n");


var getFileList = function (list, all) {

var addDepends = function (file, list) {
Expand Down Expand Up @@ -247,7 +256,9 @@
output.push(fs.readFileSync(fileList[i]));
}

var distDir = "api/latest";
var productionBuild = true;

var distDir = "api/" + (productionBuild ? "latest" : "dev");
var distPluginDir = distDir + "/plugins";
var distExtrasDir = distDir + "/extras";

Expand All @@ -260,19 +271,22 @@
function () {

// Deep-copy an existing directory

sys.print("Distributing plugins to: " + distPluginDir + "\n");
wrench.copyDirSyncRecursive("src/plugins", distPluginDir);

sys.print("Distributing extras to: " + distPluginDir + "\n");
// wrench.copyDirSyncRecursive("src/extras", distExtrasDir);

fs.writeFileSync(distExtrasDir + "/orbitControl.js", fs.readFileSync("src/extras/orbitControl.js"));
fs.writeFileSync(distExtrasDir + "/pickControl.js", fs.readFileSync("src/extras/pickControl.js"));


fs.writeFileSync(distExtrasDir + "/gui.js", fs.readFileSync("src/extras/gui/dat.gui.min.js") + fs.readFileSync("src/extras/gui/gui.js"));

if (fileList.length > 0) {
sys.print("Writing built library: scenejs.js\n");
output.push('SceneJS.configure({ pluginPath: "http://xeolabs.github.com/scenejs/api/latest/plugins" });');
// output.push('SceneJS.configure({ pluginPath: "/home/lindsay/xeolabs/projects/scenejs3.0/api/latest/plugins"});');
sys.print("Writing core library to: " + distDir + "/scenejs.js\n");

output.push('SceneJS.configure({ pluginPath: "http://xeolabs.github.com/scenejs/' + distDir + '/plugins" });');
// output.push('SceneJS.configure({ pluginPath: "/home/lindsay/xeolabs/projects/scenejs3.0/api/latest/plugins"});');
output = output.join("");
fs.writeFileSync(distDir + "/scenejs.js", output);

Expand Down
Loading

0 comments on commit 5652945

Please sign in to comment.