Skip to content

Commit 7629cd8

Browse files
committed
Index tweaks
1 parent d15657f commit 7629cd8

File tree

1 file changed

+112
-9
lines changed

1 file changed

+112
-9
lines changed

README.markdown

+112-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ articulated and pickable objects as required for high-detail visualisation appli
1414
## Downloads
1515
Hotlink to these binaries and they'll dynamically load SceneJS plugins on-demand from this repository as
1616
required. That's OK for playing around, but for production you'll probably want to serve the plugins yourself -
17-
see [Plugin System](#plugin-system) below for more info.
17+
see [Plugin System](#plugin-system) below for how to do that.
1818
* **[scenejs.js](http://xeolabs.github.com/scenejs/build/latest/scenejs.js)**
1919
* **[scenejs.min.js](http://xeolabs.github.com/scenejs/build/latest/scenejs.min.js)**
2020

@@ -37,6 +37,12 @@ Scene picking helper
3737
As mentioned above, SceneJS now uses plugins for things like primitives (box, teapot, text etc.) and fancy
3838
texture loading (video etc).
3939

40+
By default, SceneJS is hardwired to automatically download plugins from the [plugins directory](build/latest/plugins)
41+
in this repository. This means you can just hotlink to the SceneJS core library downloads and they will download the
42+
plugins automatically as you need them. That's nice for sharing SceneJS examples on jsFiddle etc, but in production
43+
you would want to [serve them youself](#serving-plugins-yourself).
44+
45+
### Geometry Plugins
4046
Plugins are used from within node definitions, as shown in this example for geometry:
4147

4248
```javascript
@@ -54,8 +60,8 @@ var myGeometry = myNode.addNode({
5460

5561
This geometry node will create its sphere geometry with the help of the ["sphere" plugin](./build/latest/plugins/geometry/sphere.js).
5662

57-
Essentially, the plugin's code looks like the listing below. The plugin provides geometry factory objects, each with
58-
a ```configure``` method to configure the sphere shape and a ```subscribe``` method to collect created geometry data.
63+
Essentially, the plugin's code looks like the listing below. The plugin provides geometry factory objects (called "sources"), each with
64+
a ```configure``` method to configure the sphere shape and a ```subscribe``` method to collect the generated geometry data.
5965

6066
```javascript
6167
SceneJS.Plugins.addPlugin(
@@ -100,7 +106,7 @@ SceneJS.Plugins.addPlugin(
100106
```
101107

102108
Then you can reconfigure the geometry at any time using setter methods on the node as shown below. Note however that we
103-
can't reconfigure the plugin type we're using for our node.
109+
can't reconfigure the plugin ```type```.
104110

105111
```javascript
106112
// Reconfigure our sphere like this - make it bigger and smoother:
@@ -111,13 +117,110 @@ myGeometry.set({ source:{ latitudeBands : 60, longitudeBands : 60, radius : 3 }
111117
myGeometry.set("source", { latitudeBands : 60, longitudeBands : 60, radius : 3 });
112118
```
113119

114-
By default, SceneJS is hardwired to automatically download plugins from [a directory in this repository](build/latest/plugins). This means you can
115-
just hotlink to the SceneJS core library downloads and they will download the plugins automatically as you need them. That's
116-
nice for putting SceneJS examples on code sharing sites like jsFiddle.
120+
### Texture Plugins
121+
122+
Texture ```layers``` can load their textures via plugins, as shown on this example ```texture``` node:
123+
124+
```javascript
125+
var myTexture = myNode.addNode({
126+
type:"texture",
127+
id: "myTexture",
128+
layers: [
129+
{
130+
source:{
131+
type:"image",
132+
src: "someImage.jpg"
133+
}
134+
}
135+
],
136+
nodes: [
137+
//...
138+
]
139+
});
140+
```
141+
142+
This layer uses the [image plugin](build/latest/plugins/texture/image.js), which simply fetches an image file and
143+
tweaks its dimensions to be a power-of-two (as currently required by WebGL):
144+
145+
```javascript
146+
SceneJS.Plugins.addPlugin(
147+
148+
"texture",
149+
"image",
150+
151+
new (function () {
152+
153+
this.getSource = function (params) {
154+
155+
var gl = params.gl;
156+
var texture = gl.createTexture();
157+
var publish;
158+
159+
return {
160+
subscribe:function (fn) {
161+
publish = fn;
162+
},
163+
configure:function (cfg) {
164+
if (!cfg.src) {
165+
throw "Parameter expected: 'src'";
166+
}
167+
var image = new Image();
168+
image.crossOrigin = "anonymous";
169+
image.onload = function () {
170+
gl.bindTexture(gl.TEXTURE_2D, texture);
171+
var potImage = ensureImageSizePowerOfTwo(image); // WebGL hates NPOT images
172+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, potImage);
173+
if (publish) {
174+
publish(texture);
175+
}
176+
};
177+
image.src = cfg.src;
178+
}
179+
};
180+
};
181+
182+
function ensureImageSizePowerOfTwo(image) {
183+
if (!isPowerOfTwo(image.width) || !isPowerOfTwo(image.height)) {
184+
var canvas = document.createElement("canvas");
185+
canvas.width = nextHighestPowerOfTwo(image.width);
186+
canvas.height = nextHighestPowerOfTwo(image.height);
187+
var ctx = canvas.getContext("2d");
188+
ctx.drawImage(image,
189+
0, 0, image.width, image.height,
190+
0, 0, canvas.width, canvas.height);
191+
image = canvas;
192+
}
193+
return image;
194+
}
195+
196+
function isPowerOfTwo(x) {
197+
return (x & (x - 1)) == 0;
198+
}
199+
200+
function nextHighestPowerOfTwo(x) {
201+
--x;
202+
for (var i = 1; i < 32; i <<= 1) {
203+
x = x | x >> i;
204+
}
205+
return x + 1;
206+
}
207+
208+
})());
209+
```
210+
211+
Then you can reconfigure the texture at any time through the node:
212+
213+
```javascript
214+
// Load a different image:
215+
myGeometry.setLayers({
216+
"0": {
217+
src: "someOtherImage.jpg"
218+
}
219+
});
117220

118221
### Serving plugins yourself
119-
If you'd rather serve the plugins yourself, instead of relying on the availability of this repository, then grab a copy of the
120-
plugins and configure SceneJS to load them from your location:
222+
If you'd rather serve the plugins yourself, instead of relying on the availability of this repository, then copy the
223+
[plugins directory](build/latest/plugins) to your server and configure SceneJS to load them from there:
121224
122225
```javascript
123226
SceneJS.configure({

0 commit comments

Comments
 (0)