@@ -14,7 +14,7 @@ articulated and pickable objects as required for high-detail visualisation appli
14
14
## Downloads
15
15
Hotlink to these binaries and they'll dynamically load SceneJS plugins on-demand from this repository as
16
16
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 .
18
18
* ** [ scenejs.js] ( http://xeolabs.github.com/scenejs/build/latest/scenejs.js ) **
19
19
* ** [ scenejs.min.js] ( http://xeolabs.github.com/scenejs/build/latest/scenejs.min.js ) **
20
20
@@ -37,6 +37,12 @@ Scene picking helper
37
37
As mentioned above, SceneJS now uses plugins for things like primitives (box, teapot, text etc.) and fancy
38
38
texture loading (video etc).
39
39
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
40
46
Plugins are used from within node definitions, as shown in this example for geometry:
41
47
42
48
``` javascript
@@ -54,8 +60,8 @@ var myGeometry = myNode.addNode({
54
60
55
61
This geometry node will create its sphere geometry with the help of the [ "sphere" plugin] ( ./build/latest/plugins/geometry/sphere.js ) .
56
62
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.
59
65
60
66
``` javascript
61
67
SceneJS .Plugins .addPlugin (
@@ -100,7 +106,7 @@ SceneJS.Plugins.addPlugin(
100
106
```
101
107
102
108
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 ``` .
104
110
105
111
``` javascript
106
112
// Reconfigure our sphere like this - make it bigger and smoother:
@@ -111,13 +117,110 @@ myGeometry.set({ source:{ latitudeBands : 60, longitudeBands : 60, radius : 3 }
111
117
myGeometry .set (" source" , { latitudeBands : 60 , longitudeBands : 60 , radius : 3 });
112
118
```
113
119
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
+ });
117
220
118
221
### 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 :
121
224
122
225
```javascript
123
226
SceneJS.configure({
0 commit comments