jsc3d-js-rails 1.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4500 @@
1
+ /**
2
+ @preserve Copyright (c) 2011~2013 Humu <humu2009@gmail.com>
3
+ jsc3d is freely distributable under the terms of the MIT license.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+ **/
23
+
24
+
25
+ /**
26
+ @namespace JSC3D
27
+ */
28
+ var JSC3D = JSC3D || {};
29
+
30
+
31
+ /**
32
+ @class Viewer
33
+
34
+ Viewer is the main class of JSC3D. It provides presentation of and interaction with a simple static 3D scene
35
+ which can either be given as the url of the scene file, or be manually constructed and passed in. It
36
+ also provides some settings to adjust the mode and quality of the rendering.<br /><br />
37
+
38
+ Viewer should be constructed with an existing canvas object where to perform the rendering.<br /><br />
39
+
40
+ Viewer provides 3 way to specify the scene:<br />
41
+ 1. Use setParameter() method before initilization and set 'SceneUrl' parameter with a valid url
42
+ that describes where to load the scene. <br />
43
+ 2. Use replaceSceneFromUrl() method, passing in a valid url to load/replace scene at runtime.<br />
44
+ 3. Use replaceScene() method, passing in a manually constructed scene object to replace the current one
45
+ at runtime.<br />
46
+ */
47
+ JSC3D.Viewer = function(canvas, parameters) {
48
+ if(parameters)
49
+ this.params = {
50
+ SceneUrl: parameters.SceneUrl || '',
51
+ InitRotationX: parameters.InitRotationX || 0,
52
+ InitRotationY: parameters.InitRotationY || 0,
53
+ InitRotationZ: parameters.InitRotationZ || 0,
54
+ ModelColor: parameters.ModelColor || '#caa618',
55
+ BackgroundColor1: parameters.BackgroundColor1 || '#ffffff',
56
+ BackgroundColor2: parameters.BackgroundColor2 || '#383840',
57
+ RenderMode: parameters.RenderMode || 'flat',
58
+ Definition: parameters.Definition || 'standard',
59
+ MipMapping: parameters.MipMapping || 'off',
60
+ SphereMapUrl: parameters.SphereMapUrl || ''
61
+ };
62
+ else
63
+ this.params = {
64
+ SceneUrl: '',
65
+ InitRotationX: 0,
66
+ InitRotationY: 0,
67
+ InitRotationZ: 0,
68
+ ModelColor: '#caa618',
69
+ BackgroundColor1: '#ffffff',
70
+ BackgroundColor2: '#383840',
71
+ RenderMode: 'flat',
72
+ Definition: 'standard',
73
+ MipMapping: 'off',
74
+ SphereMapUrl: ''
75
+ };
76
+
77
+ this.canvas = canvas;
78
+ this.ctx = null;
79
+ this.canvasData = null;
80
+ this.bkgColorBuffer = null;
81
+ this.colorBuffer = null;
82
+ this.zBuffer = null;
83
+ this.selectionBuffer = null;
84
+ this.frameWidth = canvas.width;
85
+ this.frameHeight = canvas.height;
86
+ this.scene = null;
87
+ this.defaultMaterial = null;
88
+ this.sphereMap = null;
89
+ this.isLoaded = false;
90
+ this.isFailed = false;
91
+ this.errorMsg = '';
92
+ this.initRotX = 0;
93
+ this.initRotY = 0;
94
+ this.initRotZ = 0;
95
+ this.zoomFactor = 1;
96
+ this.rotMatrix = new JSC3D.Matrix3x4;
97
+ this.transformMatrix = new JSC3D.Matrix3x4;
98
+ this.sceneUrl = '';
99
+ this.modelColor = 0xcaa618;
100
+ this.bkgColor1 = 0xffffff;
101
+ this.bkgColor2 = 0x383840;
102
+ this.renderMode = 'flat';
103
+ this.definition = 'standard';
104
+ this.isMipMappingOn = false;
105
+ this.sphereMapUrl = '';
106
+ this.buttonStates = {};
107
+ this.keyStates = {};
108
+ this.mouseX = 0;
109
+ this.mouseY = 0;
110
+ this.onmousedown = null;
111
+ this.onmouseup = null;
112
+ this.onmousemove = null;
113
+ this.beforeupdate = null;
114
+ this.afterupdate = null;
115
+ this.mouseUsage = 'default';
116
+ this.isDefaultInputHandlerEnabled = true;
117
+
118
+ // setup input handlers
119
+ var self = this;
120
+ canvas.attachEvent('onmousedown', function(e){self.mouseDownHandler(e);});
121
+ canvas.attachEvent('onmouseup', function(e){self.mouseUpHandler(e);});
122
+ canvas.attachEvent('onmousemove', function(e){self.mouseMoveHandler(e);});
123
+ document.attachEvent('onkeydown', function(e){self.keyDownHandler(e);});
124
+ document.attachEvent('onkeyup', function(e){self.keyUpHandler(e);});
125
+ };
126
+
127
+ /**
128
+ Set the initial value for a parameter to parameterize viewer before initialization.<br />
129
+ Available parameters are:<br />
130
+ '<b>SceneUrl</b>': url string that describes where to load the scene, default: '';<br />
131
+ '<b>InitRotationX</b>': initial rotation angle around x-axis for the whole scene, default: 0;<br />
132
+ '<b>InitRotationY</b>': initial rotation angle around y-axis for the whole scene, default: 0;<br />
133
+ '<b>InitRotationZ</b>': initial rotation angle around z-axis for the whole scene, default: 0;<br />
134
+ '<b>ModelColor</b>': fallback color for all meshes, default: '#caa618';<br />
135
+ '<b>BackgroundColor1</b>': color at the top of the background, default: '#ffffff';<br />
136
+ '<b>BackgroundColor2</b>': color at the bottom of the background, default: '#383840';<br />
137
+ '<b>RenderMode</b>': render mode, default: 'flat';<br />
138
+ '<b>Definition</b>': quality level of rendering, default: 'standard';<br />
139
+ '<b>MipMapping</b>': turn on/off mip-mapping, default: 'off';<br />
140
+ '<b>SphereMapUrl</b>': url string that describes where to load the image used for sphere mapping, default: ''.<br />
141
+ @param {string} name name of the parameter to set.
142
+ @param value new value for the parameter.
143
+ */
144
+ JSC3D.Viewer.prototype.setParameter = function(name, value) {
145
+ this.params[name] = value;
146
+ };
147
+
148
+ /**
149
+ Initialize viewer for rendering and interactions.
150
+ */
151
+ JSC3D.Viewer.prototype.init = function() {
152
+ this.sceneUrl = this.params['SceneUrl'];
153
+ this.initRotX = parseFloat(this.params['InitRotationX']);
154
+ this.initRotY = parseFloat(this.params['InitRotationY']);
155
+ this.initRotZ = parseFloat(this.params['InitRotationZ']);
156
+ this.modelColor = parseInt('0x' + this.params['ModelColor'].substring(1));
157
+ this.bkgColor1 = parseInt('0x' + this.params['BackgroundColor1'].substring(1));
158
+ this.bkgColor2 = parseInt('0x' + this.params['BackgroundColor2'].substring(1));
159
+ this.renderMode = this.params['RenderMode'].toLowerCase();
160
+ this.definition = this.params['Definition'].toLowerCase();
161
+ this.isMipMappingOn = this.params['MipMapping'].toLowerCase() == 'on';
162
+ this.sphereMapUrl = this.params['SphereMapUrl'];
163
+
164
+ try {
165
+ this.ctx = this.canvas.getContext('2d');
166
+ this.canvasData = this.ctx.getImageData(0, 0, this.canvas.width, this.canvas.height);
167
+ }
168
+ catch(e) {
169
+ this.ctx = null;
170
+ this.canvasData = null;
171
+ }
172
+
173
+ if(this.canvas.width <= 2 || this.canvas.height <= 2)
174
+ this.definition = 'standard';
175
+
176
+ switch(this.definition) {
177
+ case 'low':
178
+ this.frameWidth = ~~((this.canvas.width + 1) / 2);
179
+ this.frameHeight = ~~((this.canvas.height + 1) / 2);
180
+ break;
181
+ case 'high':
182
+ this.frameWidth = this.canvas.width * 2;
183
+ this.frameHeight = this.canvas.height * 2;
184
+ break;
185
+ case 'standard':
186
+ default:
187
+ this.frameWidth = this.canvas.width;
188
+ this.frameHeight = this.canvas.height;
189
+ break;
190
+ }
191
+
192
+ this.zoomFactor = 1;
193
+ this.rotMatrix.identity();
194
+ this.transformMatrix.identity();
195
+ this.isLoaded = false;
196
+ this.isFailed = false;
197
+ this.errorMsg = '';
198
+ this.scene = null;
199
+ // allocate memory storage for frame buffers
200
+ this.colorBuffer = new Array(this.frameWidth * this.frameHeight);
201
+ this.zBuffer = new Array(this.frameWidth * this.frameHeight);
202
+ this.selectionBuffer = new Array(this.frameWidth * this.frameHeight);
203
+ this.bkgColorBuffer = new Array(this.frameWidth * this.frameHeight);
204
+ this.generateBackground();
205
+ // create a default material for rendring of meshes that don't have one
206
+ this.defaultMaterial = new JSC3D.Material;
207
+ this.defaultMaterial.ambientColor = 0;
208
+ this.defaultMaterial.diffuseColor = this.modelColor;
209
+ this.defaultMaterial.transparency = 0;
210
+ this.defaultMaterial.simulateSpecular = true;
211
+ this.drawBackground();
212
+
213
+ // load scene if any
214
+ this.loadScene();
215
+
216
+ // load sphere mapping image if any
217
+ this.setSphereMapFromUrl(this.sphereMapUrl);
218
+ };
219
+
220
+ /**
221
+ Ask viewer to render a new frame or just repaint last frame.
222
+ @param {boolean} repaintOnly true to repaint last frame; false(default) to render a new frame.
223
+ */
224
+ JSC3D.Viewer.prototype.update = function(repaintOnly) {
225
+ if(this.isFailed) {
226
+ this.reportError(this.errorMsg);
227
+ return;
228
+ }
229
+
230
+ if(this.beforeupdate != null && (typeof this.beforeupdate) == 'function')
231
+ this.beforeupdate();
232
+
233
+ if(this.scene) {
234
+ if(!repaintOnly && this.colorBuffer != null) {
235
+ this.beginScene();
236
+ this.render();
237
+ this.endScene();
238
+ }
239
+
240
+ this.paint();
241
+ }
242
+ else {
243
+ this.drawBackground();
244
+ }
245
+
246
+ if(this.afterupdate != null && (typeof this.afterupdate) == 'function')
247
+ this.afterupdate();
248
+ };
249
+
250
+ /**
251
+ Rotate the scene with given angles around Cardinal axes.
252
+ @param {float} rotX rotation angle around x-axis in degrees.
253
+ @param {float} rotY rotation angle around y-axis in degrees.
254
+ @param {float} rotZ rotation angle around z-axis in degrees.
255
+ */
256
+ JSC3D.Viewer.prototype.rotate = function(rotX, rotY, rotZ) {
257
+ this.rotMatrix.rotateAboutXAxis(rotX);
258
+ this.rotMatrix.rotateAboutYAxis(rotY);
259
+ this.rotMatrix.rotateAboutZAxis(rotZ);
260
+ };
261
+
262
+ /**
263
+ Set render mode.<br />
264
+ Available render modes are:<br />
265
+ '<b>point</b>': render meshes as point clouds;<br />
266
+ '<b>wireframe</b>': render meshes as wireframe;<br />
267
+ '<b>flat</b>': render meshes as solid objects using flat shading;<br />
268
+ '<b>smooth</b>': render meshes as solid objects using smooth shading;<br />
269
+ '<b>texture</b>': render meshes as solid textured objects, no lighting will be apllied;<br />
270
+ '<b>textureflat</b>': render meshes as solid textured objects, lighting will be calculated per face;<br />
271
+ '<b>texturesmooth</b>': render meshes as solid textured objects, lighting will be calculated per vertex and interpolated.<br />
272
+ @param {string} mode new render mode.
273
+ */
274
+ JSC3D.Viewer.prototype.setRenderMode = function(mode) {
275
+ this.params['RenderMode'] = mode;
276
+ this.renderMode = mode;
277
+ };
278
+
279
+ /**
280
+ Set quality level of rendering.<br />
281
+ Available quality levels are:<br />
282
+ '<b>low</b>': low-quality rendering will be applied, with highest performance;<br />
283
+ '<b>standard</b>': normal-quality rendering will be applied, with modest performace;<br />
284
+ '<b>high</b>': high-quality rendering will be applied, with lowest performace.<br />
285
+ @params {string} definition new quality level.
286
+ */
287
+ JSC3D.Viewer.prototype.setDefinition = function(definition) {
288
+ if(this.canvas.width <= 2 || this.canvas.height <= 2)
289
+ definition = 'standard';
290
+
291
+ if(definition == this.definition)
292
+ return;
293
+
294
+ this.params['Definition'] = definition;
295
+ this.definition = definition;
296
+
297
+ var oldFrameWidth = this.frameWidth;
298
+
299
+ switch(this.definition) {
300
+ case 'low':
301
+ this.frameWidth = ~~((this.canvas.width + 1) / 2);
302
+ this.frameHeight = ~~((this.canvas.height + 1) / 2);
303
+ break;
304
+ case 'high':
305
+ this.frameWidth = this.canvas.width * 2;
306
+ this.frameHeight = this.canvas.height * 2;
307
+ break;
308
+ case 'standard':
309
+ default:
310
+ this.frameWidth = this.canvas.width;
311
+ this.frameHeight = this.canvas.height;
312
+ break;
313
+ }
314
+
315
+ var newSize = this.frameWidth * this.frameHeight;
316
+ if(this.colorBuffer.length < newSize)
317
+ this.colorBuffer = new Array(newSize);
318
+
319
+ if(this.zBuffer.length < newSize)
320
+ this.zBuffer = new Array(newSize);
321
+
322
+ if(this.selectionBuffer.length < newSize)
323
+ this.selectionBuffer = new Array(newSize);
324
+
325
+ if(this.bkgColorBuffer.length < newSize)
326
+ this.bkgColorBuffer = new Array(newSize);
327
+
328
+ this.generateBackground();
329
+
330
+ // zoom factor should be adjusted,
331
+ // otherwise there would be an abrupt zoom-in or zoom-out on next frame
332
+ this.zoomFactor *= this.frameWidth / oldFrameWidth;
333
+ };
334
+
335
+ /**
336
+ Specify a new image from the given url which will be used for applying sphere mapping.
337
+ @param {string} sphereMapUrl url string that describes where to load the image.
338
+ */
339
+ JSC3D.Viewer.prototype.setSphereMapFromUrl = function(sphereMapUrl) {
340
+ if(sphereMapUrl == '') {
341
+ this.sphereMap = null;
342
+ return;
343
+ }
344
+
345
+ this.params['SphereMapUrl'] = sphereMapUrl;
346
+ this.sphereMapUrl = sphereMapUrl;
347
+
348
+ var self = this;
349
+ var newSphereMap = new JSC3D.Texture;
350
+
351
+ newSphereMap.onready = function() {
352
+ self.sphereMap = newSphereMap;
353
+ self.update();
354
+ };
355
+
356
+ newSphereMap.createFromUrl(this.sphereMapUrl);
357
+ };
358
+
359
+ /**
360
+ Enable/Disable the default mouse and key event handling routines.
361
+ @param {boolean} enabled true to enable the default handler; false to disable them.
362
+ */
363
+ JSC3D.Viewer.prototype.enableDefaultInputHandler = function(enabled) {
364
+ this.isDefaultInputHandlerEnabled = enabled;
365
+ };
366
+
367
+ /**
368
+ Set control of mouse pointer.
369
+ Available options are:<br />
370
+ '<b>default</b>': default mouse control will be used;<br />
371
+ '<b>free</b>': this tells {JSC3D.Viewer} a user-defined mouse control will be adopted.
372
+ This is often used together with viewer.enableDefaultInputHandler(false)
373
+ and viewer.onmousedown, viewer.onmouseup and/or viewer.onmousemove overridden.<br />
374
+ '<b>rotate</b>': mouse will be used to rotate the scene;<br />
375
+ '<b>zoom</b>': mouse will be used to do zooming.<br />
376
+ @param {String} usage control of mouse pointer to be set.
377
+ */
378
+ JSC3D.Viewer.prototype.setMouseUsage = function(usage) {
379
+ this.mouseUsage = usage;
380
+ };
381
+
382
+ /**
383
+ Load a new scene from the given url to replace the current scene.
384
+ @param {string} sceneUrl url string that describes where to load the new scene.
385
+ */
386
+ JSC3D.Viewer.prototype.replaceSceneFromUrl = function(sceneUrl) {
387
+ this.params['SceneUrl'] = sceneUrl;
388
+ this.sceneUrl = sceneUrl;
389
+ this.isFailed = this.isLoaded = false;
390
+ this.loadScene();
391
+ };
392
+
393
+ /**
394
+ Replace the current scene with a given scene.
395
+ @param {JSC3D.Scene} scene the given scene.
396
+ */
397
+ JSC3D.Viewer.prototype.replaceScene = function(scene) {
398
+ this.params['SceneUrl'] = '';
399
+ this.sceneUrl = '';
400
+ this.isFailed = false;
401
+ this.isLoaded = true;
402
+ this.errorMsg = '';
403
+ this.setupScene(scene);
404
+ };
405
+
406
+ /**
407
+ Reset the current scene to its initial state.
408
+ */
409
+ JSC3D.Viewer.prototype.resetScene = function() {
410
+ var d = (!this.scene || this.scene.isEmpty()) ? 0 : this.scene.aabb.lengthOfDiagonal();
411
+ this.zoomFactor = (d == 0) ? 1 : (this.frameWidth < this.frameHeight ? this.frameWidth : this.frameHeight) / d;
412
+ this.rotMatrix.identity();
413
+ this.rotMatrix.rotateAboutXAxis(this.initRotX);
414
+ this.rotMatrix.rotateAboutYAxis(this.initRotY);
415
+ this.rotMatrix.rotateAboutZAxis(this.initRotZ);
416
+ };
417
+
418
+ /**
419
+ Get the current scene.
420
+ @returns {JSC3D.Scene} the current scene.
421
+ */
422
+ JSC3D.Viewer.prototype.getScene = function() {
423
+ return this.scene;
424
+ };
425
+
426
+ /**
427
+ Query information at a given position on the canvas.
428
+ @param {float} clientX client x coordinate on the current page.
429
+ @param {float} clientY client y coordinate on the current page.
430
+ @returns {JSC3D.PickInfo} a PickInfo object which hold the result.
431
+ */
432
+ JSC3D.Viewer.prototype.pick = function(clientX, clientY) {
433
+ var pickInfo = new JSC3D.PickInfo;
434
+
435
+ var canvasRect = this.canvas.getBoundingClientRect();
436
+ var canvasX = clientX - canvasRect.left;
437
+ var canvasY = clientY - canvasRect.top;
438
+
439
+ var frameX = canvasX;
440
+ var frameY = canvasY;
441
+ if( this.selectionBuffer != null &&
442
+ canvasX >= 0 && canvasX < this.canvas.width &&
443
+ canvasY >= 0 && canvasY < this.canvas.height ) {
444
+ switch(this.definition) {
445
+ case 'low':
446
+ frameX = ~~(frameX / 2);
447
+ frameY = ~~(frameY / 2);
448
+ break;
449
+ case 'high':
450
+ frameX *= 2;
451
+ frameY *= 2;
452
+ break;
453
+ case 'standard':
454
+ default:
455
+ break;
456
+ }
457
+
458
+ var pickedId = this.selectionBuffer[frameY * this.frameWidth + frameX];
459
+ if(pickedId > 0) {
460
+ var meshes = this.scene.getChildren();
461
+ for(var i=0; i<meshes.length; i++) {
462
+ if(meshes[i].internalId == pickedId) {
463
+ pickInfo.mesh = meshes[i];
464
+ break;
465
+ }
466
+ }
467
+ }
468
+ }
469
+
470
+ pickInfo.canvasX = canvasX;
471
+ pickInfo.canvasY = canvasY;
472
+ if(pickInfo.mesh)
473
+ pickInfo.depth = this.zBuffer[frameY * this.frameWidth + frameX];
474
+
475
+ return pickInfo;
476
+ };
477
+
478
+ /**
479
+ Paint onto canvas.
480
+ @private
481
+ */
482
+ JSC3D.Viewer.prototype.paint = function() {
483
+ if(!this.canvasData)
484
+ return;
485
+
486
+ this.ctx.putImageData(this.canvasData, 0, 0);
487
+ };
488
+
489
+ /**
490
+ The mouse-down event handling routine.
491
+ @private
492
+ */
493
+ JSC3D.Viewer.prototype.mouseDownHandler = function(e) {
494
+ if(this.onmousedown) {
495
+ var info = this.pick(e.clientX, e.clientY);
496
+ this.onmousedown(info.canvasX, info.canvasY, e.button, info.depth, info.mesh);
497
+ }
498
+
499
+ if(!this.isDefaultInputHandlerEnabled)
500
+ return;
501
+
502
+ this.buttonStates[e.button] = true;
503
+ this.mouseX = e.clientX;
504
+ this.mouseY = e.clientY;
505
+ };
506
+
507
+ /**
508
+ The mouse-up event handling routine.
509
+ @private
510
+ */
511
+ JSC3D.Viewer.prototype.mouseUpHandler = function(e) {
512
+ if(this.onmouseup) {
513
+ var info = this.pick(e.clientX, e.clientY);
514
+ this.onmouseup(info.canvasX, info.canvasY, e.button, info.depth, info.mesh);
515
+ }
516
+
517
+ if(!this.isDefaultInputHandlerEnabled)
518
+ return;
519
+
520
+ var isDragging = this.buttonStates[1] == true;
521
+ var isShiftDown = this.keyStates[16] == true;
522
+ if(isDragging) {
523
+ if(((isShiftDown && this.mouseUsage == 'default') || this.mouseUsage == 'zoom') && e.clientY != this.mouseY) {
524
+ this.zoomFactor *= this.mouseY < e.clientY ?
525
+ Math.pow(1.11, ~~(1 + (e.clientY - this.mouseY) * 8 / this.canvas.height)) :
526
+ Math.pow(0.9, ~~(1 + (this.mouseY - e.clientY) * 8 / this.canvas.height));
527
+ }
528
+ else if(this.mouseUsage == 'default' || this.mouseUsage == 'rotate') {
529
+ var rotX = (e.clientY - this.mouseY) * 180 / this.canvas.width;
530
+ var rotY = (e.clientX - this.mouseX) * 180 / this.canvas.height;
531
+ this.rotMatrix.rotateAboutXAxis(rotX);
532
+ this.rotMatrix.rotateAboutYAxis(rotY);
533
+ }
534
+ this.mouseX = e.clientX;
535
+ this.mouseY = e.clientY;
536
+ this.update();
537
+ }
538
+
539
+ this.buttonStates[e.button] = false;
540
+ };
541
+
542
+ /**
543
+ The mouse-move event handling routine.
544
+ @private
545
+ */
546
+ JSC3D.Viewer.prototype.mouseMoveHandler = function(e) {
547
+ if(this.onmousemove) {
548
+ var info = this.pick(e.clientX, e.clientY);
549
+ this.onmousemove(info.canvasX, info.canvasY, e.button, info.depth, info.mesh);
550
+ }
551
+
552
+ if(!this.isDefaultInputHandlerEnabled)
553
+ return;
554
+
555
+ /*
556
+ var isDragging = this.buttonStates[1] == true;
557
+ var isShiftDown = this.keyStates[16] == true;
558
+ if(isDragging) {
559
+ if(isShiftDown) {
560
+ this.zoomFactor *= this.mouseY <= e.clientY ? 1.11 : 0.9;
561
+ }
562
+ else {
563
+ var rotX = (e.clientY - this.mouseY) * 360 / this.canvas.width;
564
+ var rotY = (e.clientX - this.mouseX) * 360 / this.canvas.height;
565
+ this.rotMatrix.rotateAboutXAxis(rotX);
566
+ this.rotMatrix.rotateAboutYAxis(rotY);
567
+ }
568
+ this.mouseX = e.clientX;
569
+ this.mouseY = e.clientY;
570
+ this.update();
571
+ }
572
+ */
573
+ };
574
+
575
+ /**
576
+ The key-down event handling routine.
577
+ @private
578
+ */
579
+ JSC3D.Viewer.prototype.keyDownHandler = function(e) {
580
+ this.keyStates[e.keyCode] = true;
581
+ };
582
+
583
+ /**
584
+ The key-up event handling routine.
585
+ @private
586
+ */
587
+ JSC3D.Viewer.prototype.keyUpHandler = function(e) {
588
+ this.keyStates[e.keyCode] = false;
589
+ };
590
+
591
+ /**
592
+ Internally load a scene.
593
+ @private
594
+ */
595
+ JSC3D.Viewer.prototype.loadScene = function() {
596
+ this.scene = null;
597
+ this.isLoaded = false;
598
+
599
+ if(this.sceneUrl == '')
600
+ return false;
601
+
602
+ var lastSlashAt = this.sceneUrl.lastIndexOf('/');
603
+ if(lastSlashAt == -1)
604
+ lastSlashAt = this.sceneUrl.lastIndexOf('\\');
605
+
606
+ var fileName = this.sceneUrl.substring(lastSlashAt + 1);
607
+ var lastDotAt = fileName.lastIndexOf('.');
608
+ if(lastDotAt == -1)
609
+ return false;
610
+
611
+ var fileExtName = fileName.substring(lastDotAt + 1);
612
+ var loader = JSC3D.LoaderSelector.getLoader(fileExtName);
613
+ if(!loader)
614
+ return false;
615
+
616
+ var self = this;
617
+
618
+ loader.onload = function(scene) {
619
+ self.setupScene(scene);
620
+ };
621
+
622
+ loader.onerror = function(errorMsg) {
623
+ self.scene = null;
624
+ self.isLoaded = false;
625
+ self.isFailed = true;
626
+ self.errorMsg = errorMsg;
627
+ self.update();
628
+ };
629
+
630
+ loader.onprogress = function(task, prog) {
631
+ self.reportProgress(task, prog);
632
+ };
633
+
634
+ loader.onresource = function(resource) {
635
+ if((resource instanceof JSC3D.Texture) && self.isMipMappingOn && !resource.hasMipmap())
636
+ resource.generateMipmaps();
637
+ self.update();
638
+ };
639
+
640
+ loader.loadFromUrl(this.sceneUrl);
641
+
642
+ return true;
643
+ };
644
+
645
+ /**
646
+ Prepare for rendering of a new scene.
647
+ @private
648
+ */
649
+ JSC3D.Viewer.prototype.setupScene = function(scene) {
650
+ scene.init();
651
+ if(!scene.isEmpty()) {
652
+ var d = scene.aabb.lengthOfDiagonal();
653
+ var w = this.frameWidth;
654
+ var h = this.frameHeight;
655
+ this.zoomFactor = (d == 0) ? 1 : (w < h ? w : h) / d;
656
+ }
657
+
658
+ this.rotMatrix.identity();
659
+ this.rotMatrix.rotateAboutXAxis(this.initRotX);
660
+ this.rotMatrix.rotateAboutYAxis(this.initRotY);
661
+ this.rotMatrix.rotateAboutZAxis(this.initRotZ);
662
+ this.scene = scene;
663
+ this.isLoaded = true;
664
+ this.isFailed = false;
665
+ this.errorMsg = '';
666
+ this.update();
667
+ };
668
+
669
+ /**
670
+ Show progress and some informations about current time-cosuming task.
671
+ @param {string} task text information about current task.
672
+ @param {float} progress progress of current task. this should be a number between 0 and 1.
673
+ */
674
+ JSC3D.Viewer.prototype.reportProgress = function(task, progress) {
675
+ if(!this.ctx)
676
+ return;
677
+
678
+ this.drawBackground();
679
+
680
+ this.ctx.save();
681
+
682
+ var r = 255 - ((this.bkgColor1 & 0xff0000) >> 16);
683
+ var g = 255 - ((this.bkgColor1 & 0xff00) >> 8);
684
+ var b = 255 - (this.bkgColor1 & 0xff);
685
+ var style = '#' + r.toString(16) + g.toString(16) + b.toString(16);
686
+ this.ctx.strokeStyle = style;
687
+ this.ctx.fillStyle = style;
688
+
689
+ var barX = 40;
690
+ var barY = this.canvas.height * 0.38;
691
+ var barWidth = this.canvas.width - barX * 2;
692
+ var barHeight = 20;
693
+ this.ctx.strokeRect(barX, barY, barWidth, barHeight);
694
+ this.ctx.fillRect(barX+2, barY+2, (barWidth-4)*progress, barHeight-4);
695
+
696
+ this.ctx.font = '12px Courier New';
697
+ this.ctx.textAlign = 'left';
698
+ this.ctx.fillText(task, barX, barY-4, barWidth);
699
+
700
+ this.ctx.restore();
701
+ };
702
+
703
+ /**
704
+ Show informations about a fatal error.
705
+ @param {string} message text information about this error.
706
+ */
707
+ JSC3D.Viewer.prototype.reportError = function(message) {
708
+ if(!this.ctx)
709
+ return;
710
+
711
+ this.drawBackground();
712
+
713
+ this.ctx.save();
714
+
715
+ var msgX = 40;
716
+ var msgY = this.canvas.height * 0.38 - 4;
717
+ var r = 255 - ((this.bkgColor1 & 0xff0000) >> 16);
718
+ var g = 255 - ((this.bkgColor1 & 0xff00) >> 8);
719
+ var b = 255 - (this.bkgColor1 & 0xff);
720
+ var style = '#' + r.toString(16) + g.toString(16) + b.toString(16);
721
+ this.ctx.fillStyle = style;
722
+ this.ctx.font = '16px Courier New';
723
+ this.ctx.textAlign = 'left';
724
+ this.ctx.fillText(message, msgX, msgY);
725
+
726
+ this.ctx.restore();
727
+ };
728
+
729
+ /**
730
+ Fill the background color buffer.
731
+ @private
732
+ */
733
+ JSC3D.Viewer.prototype.generateBackground = function() {
734
+ var w = this.frameWidth;
735
+ var h = this.frameHeight;
736
+ var pixels = this.bkgColorBuffer;
737
+
738
+ var r1 = (this.bkgColor1 & 0xff0000) >> 16;
739
+ var g1 = (this.bkgColor1 & 0xff00) >> 8;
740
+ var b1 = this.bkgColor1 & 0xff;
741
+ var r2 = (this.bkgColor2 & 0xff0000) >> 16;
742
+ var g2 = (this.bkgColor2 & 0xff00) >> 8;
743
+ var b2 = this.bkgColor2 & 0xff;
744
+
745
+ var pix = 0;
746
+ for(var i=0; i<h; i++) {
747
+ var r = (r1 + i * (r2 - r1) / h) & 0xff;
748
+ var g = (g1 + i * (g2 - g1) / h) & 0xff;
749
+ var b = (b1 + i * (b2 - b1) / h) & 0xff;
750
+
751
+ for(var j=0; j<w; j++) {
752
+ pixels[pix++] = r << 16 | g << 8 | b;
753
+ }
754
+ }
755
+ };
756
+
757
+ /**
758
+ Draw background onto canvas.
759
+ @private
760
+ */
761
+ JSC3D.Viewer.prototype.drawBackground = function() {
762
+ if(!this.canvasData)
763
+ return;
764
+
765
+ this.beginScene();
766
+ this.endScene();
767
+
768
+ this.paint();
769
+ };
770
+
771
+ /**
772
+ Begin to render a new frame.
773
+ @private
774
+ */
775
+ JSC3D.Viewer.prototype.beginScene = function() {
776
+ var cbuf = this.colorBuffer;
777
+ var zbuf = this.zBuffer;
778
+ var sbuf = this.selectionBuffer;
779
+ var bbuf = this.bkgColorBuffer;
780
+ var size = this.frameWidth * this.frameHeight;
781
+ var MIN_Z = -Number.MAX_VALUE;
782
+
783
+ for(var i=0; i<size; i++) {
784
+ cbuf[i] = bbuf[i];
785
+ zbuf[i] = MIN_Z;
786
+ sbuf[i] = 0;
787
+ }
788
+ };
789
+
790
+ /**
791
+ End for rendering of a frame.
792
+ @private
793
+ */
794
+ JSC3D.Viewer.prototype.endScene = function() {
795
+ var data = this.canvasData.data;
796
+ var width = this.canvas.width;
797
+ var height = this.canvas.height;
798
+ var cbuf = this.colorBuffer;
799
+ var cwidth = this.frameWidth;
800
+ var cheight = this.frameHeight;
801
+ var csize = cwidth * cheight;
802
+
803
+ switch(this.definition) {
804
+ case 'low':
805
+ var halfWidth = width >> 1;
806
+ var surplus = cwidth - halfWidth;
807
+ var src = 0, dest = 0;
808
+ for(var i=0; i<height; i++) {
809
+ for(var j=0; j<width; j++) {
810
+ var color = cbuf[src];
811
+ data[dest] = (color & 0xff0000) >> 16;
812
+ data[dest + 1] = (color & 0xff00) >> 8;
813
+ data[dest + 2] = color & 0xff;
814
+ data[dest + 3] = 0xff;
815
+ src += (j & 1);
816
+ dest += 4;
817
+ }
818
+ src += (i & 1) ? surplus : -halfWidth;
819
+ }
820
+ break;
821
+ case 'high':
822
+ var src = 0, dest = 0;
823
+ for(var i=0; i<height; i++) {
824
+ for(var j=0; j<width; j++) {
825
+ var color0 = cbuf[src];
826
+ var color1 = cbuf[src + 1];
827
+ var color2 = cbuf[src + cwidth];
828
+ var color3 = cbuf[src + cwidth + 1];
829
+ data[dest] = ((color0 & 0xff0000) + (color1 & 0xff0000) + (color2 & 0xff0000) + (color3 & 0xff0000)) >> 18;
830
+ data[dest + 1] = ((color0 & 0xff00) + (color1 & 0xff00) + (color2 & 0xff00) + (color3 & 0xff00)) >> 10;
831
+ data[dest + 2] = ((color0 & 0xff) + (color1 & 0xff) + (color2 & 0xff) + (color3 & 0xff)) >> 2;
832
+ data[dest + 3] = 0xff;
833
+ src += 2;
834
+ dest += 4;
835
+ }
836
+ src += cwidth;
837
+ }
838
+ break;
839
+ case 'standard':
840
+ default:
841
+ for(var src=0, dest=0; src<csize; src++, dest+=4) {
842
+ var color = cbuf[src];
843
+ data[dest] = (color & 0xff0000) >> 16;
844
+ data[dest + 1] = (color & 0xff00) >> 8;
845
+ data[dest + 2] = color & 0xff;
846
+ data[dest + 3] = 0xff;
847
+ }
848
+ break;
849
+ }
850
+ };
851
+
852
+ /**
853
+ Render a new frame.
854
+ @private
855
+ */
856
+ JSC3D.Viewer.prototype.render = function() {
857
+ if(this.scene.isEmpty())
858
+ return;
859
+
860
+ var aabb = this.scene.aabb;
861
+
862
+ // calculate transformation matrix
863
+ this.transformMatrix.identity();
864
+ this.transformMatrix.translate(-(aabb.minX+aabb.maxX)/2, -(aabb.minY+aabb.maxY)/2, -(aabb.minZ+aabb.maxZ)/2);
865
+ this.transformMatrix.multiply(this.rotMatrix);
866
+ this.transformMatrix.scale(this.zoomFactor, -this.zoomFactor, this.zoomFactor);
867
+ this.transformMatrix.translate(this.frameWidth/2, this.frameHeight/2, 0);
868
+
869
+ // sort, transform and render the scene
870
+ var renderList = this.sortScene(this.transformMatrix);
871
+ for(var i=0; i<renderList.length; i++) {
872
+ var mesh = renderList[i];
873
+
874
+ if(!mesh.isTrivial()) {
875
+ JSC3D.Math3D.transformVectors(this.transformMatrix, mesh.vertexBuffer, mesh.transformedVertexBuffer);
876
+
877
+ if(mesh.visible) {
878
+ switch(this.renderMode) {
879
+ case 'point':
880
+ this.renderPoint(mesh);
881
+ break;
882
+ case 'wireframe':
883
+ this.renderWireframe(mesh);
884
+ break;
885
+ case 'flat':
886
+ this.renderSolidFlat(mesh);
887
+ break;
888
+ case 'smooth':
889
+ this.renderSolidSmooth(mesh);
890
+ break;
891
+ case 'texture':
892
+ if(mesh.hasTexture())
893
+ this.renderSolidTexture(mesh);
894
+ else
895
+ this.renderSolidFlat(mesh);
896
+ break;
897
+ case 'textureflat':
898
+ if(mesh.hasTexture())
899
+ this.renderTextureFlat(mesh);
900
+ else
901
+ this.renderSolidFlat(mesh);
902
+ break;
903
+ case 'texturesmooth':
904
+ if(mesh.isEnvironmentCast && this.sphereMap != null && this.sphereMap.hasData())
905
+ this.renderSolidSphereMapped(mesh);
906
+ else if(mesh.hasTexture())
907
+ this.renderTextureSmooth(mesh);
908
+ else
909
+ this.renderSolidSmooth(mesh);
910
+ break;
911
+ default:
912
+ this.renderSolidFlat(mesh);
913
+ break;
914
+ }
915
+ }
916
+ }
917
+ }
918
+ };
919
+
920
+ /**
921
+ Sort meshes inside the scene into a render list. The sorting criterion is a mixture of trnasparency and depth.
922
+ This routine is necessary to ensure a correct rendering order.
923
+ @private
924
+ */
925
+ JSC3D.Viewer.prototype.sortScene = function(mat) {
926
+ var renderList = [];
927
+
928
+ var meshes = this.scene.getChildren();
929
+ for(var i=0; i<meshes.length; i++) {
930
+ var mesh = meshes[i];
931
+ if(!mesh.isTrivial()) {
932
+ renderList.push(mesh);
933
+ var meshCenter = mesh.aabb.center();
934
+ JSC3D.Math3D.transformVectors(mat, meshCenter, meshCenter);
935
+ var meshMaterial = mesh.material ? mesh.material : this.defaultMaterial;
936
+ mesh.sortKey = {
937
+ depth: meshCenter[2],
938
+ isTransparnt: (meshMaterial.transparency > 0) || (mesh.hasTexture() ? mesh.texture.hasTransparency : false)
939
+ };
940
+ }
941
+ }
942
+
943
+ renderList.sort(
944
+ function(mesh0, mesh1) {
945
+ // opaque meshes should always be prior to transparent ones to be rendered
946
+ if(!mesh0.sortKey.isTransparnt && mesh1.sortKey.isTransparnt)
947
+ return -1;
948
+
949
+ // opaque meshes should always be prior to transparent ones to be rendered
950
+ if(mesh0.sortKey.isTransparnt && !mesh1.sortKey.isTransparnt)
951
+ return 1;
952
+
953
+ // transparent meshes should be rendered from far to near
954
+ if(mesh0.sortKey.isTransparnt)
955
+ return mesh0.sortKey.depth - mesh1.sortKey.depth;
956
+
957
+ // opaque meshes should be rendered form near to far
958
+ return mesh1.sortKey.depth - mesh0.sortKey.depth;
959
+ } );
960
+
961
+ return renderList;
962
+ };
963
+
964
+ /**
965
+ Render the given mesh as points.
966
+ @private
967
+ */
968
+ JSC3D.Viewer.prototype.renderPoint = function(mesh) {
969
+ var w = this.frameWidth;
970
+ var h = this.frameHeight;
971
+ var xbound = w - 1;
972
+ var ybound = h - 1;
973
+ var ibuf = mesh.indexBuffer;
974
+ var vbuf = mesh.transformedVertexBuffer;
975
+ var nbuf = mesh.transformedVertexNormalZBuffer;
976
+ var cbuf = this.colorBuffer;
977
+ var zbuf = this.zBuffer;
978
+ var sbuf = this.selectionBuffer;
979
+ var numOfVertices = vbuf.length / 3;
980
+ var id = mesh.internalId;
981
+ var color = mesh.material ? mesh.material.diffuseColor : this.defaultMaterial.diffuseColor;
982
+
983
+ if(!nbuf || nbuf.length < numOfVertices) {
984
+ mesh.transformedVertexNormalZBuffer = new Array(numOfVertices);
985
+ nbuf = mesh.transformedVertexNormalZBuffer;
986
+ }
987
+
988
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.vertexNormalBuffer, nbuf);
989
+
990
+ for(var i=0, j=0; i<numOfVertices; i++, j+=3) {
991
+ var xformedNz = nbuf[i];
992
+ if(mesh.isDoubleSided)
993
+ xformedNz = xformedNz > 0 ? xformedNz : -xformedNz;
994
+ if(xformedNz > 0) {
995
+ var x = ~~(vbuf[j] + 0.5);
996
+ var y = ~~(vbuf[j + 1] + 0.5);
997
+ var z = vbuf[j + 2];
998
+ if(x >=0 && x < xbound && y >=0 && y < ybound) {
999
+ var pix = y * w + x;
1000
+ if(z > zbuf[pix]) {
1001
+ zbuf[pix] = z;
1002
+ cbuf[pix] = color;
1003
+ sbuf[pix] = id;
1004
+ }
1005
+ pix++;
1006
+ if(z > zbuf[pix]) {
1007
+ zbuf[pix] = z;
1008
+ cbuf[pix] = color;
1009
+ sbuf[pix] = id;
1010
+ }
1011
+ pix += xbound;
1012
+ if(z > zbuf[pix]) {
1013
+ zbuf[pix] = z;
1014
+ cbuf[pix] = color;
1015
+ sbuf[pix] = id;
1016
+ }
1017
+ pix++;
1018
+ if(z > zbuf[pix]) {
1019
+ zbuf[pix] = z;
1020
+ cbuf[pix] = color;
1021
+ sbuf[pix] = id;
1022
+ }
1023
+ }
1024
+ }
1025
+ }
1026
+ };
1027
+
1028
+ /**
1029
+ Render the given mesh as wireframe.
1030
+ @private
1031
+ */
1032
+ JSC3D.Viewer.prototype.renderWireframe = function(mesh) {
1033
+ var w = this.frameWidth;
1034
+ var h = this.frameHeight;
1035
+ var xbound = w - 1;
1036
+ var ybound = h - 1;
1037
+ var ibuf = mesh.indexBuffer;
1038
+ var vbuf = mesh.transformedVertexBuffer;
1039
+ var nbuf = mesh.transformedFaceNormalZBuffer;
1040
+ var cbuf = this.colorBuffer;
1041
+ var zbuf = this.zBuffer;
1042
+ var sbuf = this.selectionBuffer;
1043
+ var numOfFaces = mesh.faceCount;
1044
+ var id = mesh.internalId;
1045
+ var color = mesh.material ? mesh.material.diffuseColor : this.defaultMaterial.diffuseColor;
1046
+
1047
+ if(!nbuf || nbuf.length < numOfFaces) {
1048
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
1049
+ nbuf = mesh.transformedFaceNormalZBuffer;
1050
+ }
1051
+
1052
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, nbuf);
1053
+
1054
+ var i = 0, j = 0;
1055
+ while(i < numOfFaces) {
1056
+ var xformedNz = nbuf[i++];
1057
+ if(mesh.isDoubleSided)
1058
+ xformedNz = xformedNz > 0 ? xformedNz : -xformedNz;
1059
+ if(xformedNz < 0) {
1060
+ do {
1061
+ } while (ibuf[j++] != -1);
1062
+ }
1063
+ else {
1064
+ var vStart, v0, v1;
1065
+ v0 = ibuf[j++] * 3;
1066
+ v1 = ibuf[j++] * 3;
1067
+ vStart = v0;
1068
+
1069
+ var isClosed = false;
1070
+ while(!isClosed) {
1071
+ var x0 = ~~(vbuf[v0] + 0.5);
1072
+ var y0 = ~~(vbuf[v0 + 1] + 0.5);
1073
+ var z0 = vbuf[v0 + 2];
1074
+ var x1 = ~~(vbuf[v1] + 0.5);
1075
+ var y1 = ~~(vbuf[v1 + 1] + 0.5);
1076
+ var z1 = vbuf[v1 + 2];
1077
+
1078
+ var dx = x1 - x0;
1079
+ var dy = y1 - y0;
1080
+ var dz = z1 - z0;
1081
+
1082
+ var dd;
1083
+ var xInc, yInc, zInc;
1084
+ if(Math.abs(dx) > Math.abs(dy)) {
1085
+ dd = dx;
1086
+ xInc = dx > 0 ? 1 : -1;
1087
+ yInc = dx != 0 ? xInc * dy / dx : 0;
1088
+ zInc = dx != 0 ? xInc * dz / dx : 0;
1089
+ }
1090
+ else {
1091
+ dd = dy;
1092
+ yInc = dy > 0 ? 1 : -1;
1093
+ xInc = dy != 0 ? yInc * dx / dy : 0;
1094
+ zInc = dy != 0 ? yInc * dz / dy : 0;
1095
+ }
1096
+
1097
+ var x = x0;
1098
+ var y = y0;
1099
+ var z = z0;
1100
+
1101
+ if(dd < 0) {
1102
+ x = x1;
1103
+ y = y1;
1104
+ z = z1;
1105
+ dd = -dd;
1106
+ xInc = -xInc;
1107
+ yInc = -yInc;
1108
+ zInc = -zInc;
1109
+ }
1110
+
1111
+ for(var k=0; k<dd; k++) {
1112
+ if(x >=0 && x < xbound && y >=0 && y < ybound) {
1113
+ var pix = (~~y) * w + (~~x);
1114
+ if(z > zbuf[pix]) {
1115
+ zbuf[pix] = z;
1116
+ cbuf[pix] = color;
1117
+ sbuf[pix] = id;
1118
+ }
1119
+ }
1120
+
1121
+ x += xInc;
1122
+ y += yInc;
1123
+ z += zInc;
1124
+ }
1125
+
1126
+ if(v1 == vStart) {
1127
+ isClosed = true;
1128
+ }
1129
+ else {
1130
+ v0 = v1;
1131
+
1132
+ if(ibuf[j] != -1) {
1133
+ v1 = ibuf[j++] * 3;
1134
+ }
1135
+ else {
1136
+ v1 = vStart;
1137
+ }
1138
+ }
1139
+ }
1140
+
1141
+ j++;
1142
+ }
1143
+ }
1144
+ };
1145
+
1146
+ /**
1147
+ Render the given mesh as solid object, using flat shading.
1148
+ @private
1149
+ */
1150
+ JSC3D.Viewer.prototype.renderSolidFlat = function(mesh) {
1151
+ var w = this.frameWidth;
1152
+ var h = this.frameHeight;
1153
+ var ibuf = mesh.indexBuffer;
1154
+ var vbuf = mesh.transformedVertexBuffer;
1155
+ var nbuf = mesh.transformedFaceNormalZBuffer;
1156
+ var cbuf = this.colorBuffer;
1157
+ var zbuf = this.zBuffer;
1158
+ var sbuf = this.selectionBuffer;
1159
+ var numOfFaces = mesh.faceCount;
1160
+ var id = mesh.internalId;
1161
+ var material = mesh.material ? mesh.material : this.defaultMaterial;
1162
+ var palette = material.getPalette();
1163
+ var isOpaque = material.transparency == 0;
1164
+ var trans = material.transparency * 255;
1165
+ var opaci = 255 - trans;
1166
+
1167
+ // skip this mesh if it is completely transparent
1168
+ if(material.transparency == 1)
1169
+ return;
1170
+
1171
+ if(!nbuf || nbuf.length < numOfFaces) {
1172
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
1173
+ nbuf = mesh.transformedFaceNormalZBuffer;
1174
+ }
1175
+
1176
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, nbuf);
1177
+
1178
+ var Xs = new Array(3);
1179
+ var Ys = new Array(3);
1180
+ var Zs = new Array(3);
1181
+ var i = 0, j = 0;
1182
+ while(i < numOfFaces) {
1183
+ var xformedNz = nbuf[i++];
1184
+ if(mesh.isDoubleSided)
1185
+ xformedNz = xformedNz > 0 ? xformedNz : -xformedNz;
1186
+ if(xformedNz < 0) {
1187
+ do {
1188
+ } while (ibuf[j++] != -1);
1189
+ }
1190
+ else {
1191
+ var color = palette[~~(xformedNz * 255)];
1192
+
1193
+ var v0, v1, v2;
1194
+ v0 = ibuf[j++] * 3;
1195
+ v1 = ibuf[j++] * 3;
1196
+
1197
+ do {
1198
+ v2 = ibuf[j++] * 3;
1199
+
1200
+ Xs[0] = ~~(vbuf[v0] + 0.5);
1201
+ Ys[0] = ~~(vbuf[v0 + 1] + 0.5);
1202
+ Zs[0] = vbuf[v0 + 2];
1203
+ Xs[1] = ~~(vbuf[v1] + 0.5);
1204
+ Ys[1] = ~~(vbuf[v1 + 1] + 0.5);
1205
+ Zs[1] = vbuf[v1 + 2];
1206
+ Xs[2] = ~~(vbuf[v2] + 0.5);
1207
+ Ys[2] = ~~(vbuf[v2 + 1] + 0.5);
1208
+ Zs[2] = vbuf[v2 + 2];
1209
+
1210
+ var high = Ys[0] < Ys[1] ? 0 : 1;
1211
+ high = Ys[high] < Ys[2] ? high : 2;
1212
+ var low = Ys[0] > Ys[1] ? 0 : 1;
1213
+ low = Ys[low] > Ys[2] ? low : 2;
1214
+ var mid = 3 - low - high;
1215
+
1216
+ if(high != low) {
1217
+ var x0 = Xs[low];
1218
+ var z0 = Zs[low];
1219
+ var dy0 = Ys[low] - Ys[high];
1220
+ dy0 = dy0 != 0 ? dy0 : 1;
1221
+ var xStep0 = (Xs[low] - Xs[high]) / dy0;
1222
+ var zStep0 = (Zs[low] - Zs[high]) / dy0;
1223
+
1224
+ var x1 = Xs[low];
1225
+ var z1 = Zs[low];
1226
+ var dy1 = Ys[low] - Ys[mid];
1227
+ dy1 = dy1 != 0 ? dy1 : 1;
1228
+ var xStep1 = (Xs[low] - Xs[mid]) / dy1;
1229
+ var zStep1 = (Zs[low] - Zs[mid]) / dy1;
1230
+
1231
+ var x2 = Xs[mid];
1232
+ var z2 = Zs[mid];
1233
+ var dy2 = Ys[mid] - Ys[high];
1234
+ dy2 = dy2 != 0 ? dy2 : 1;
1235
+ var xStep2 = (Xs[mid] - Xs[high]) / dy2;
1236
+ var zStep2 = (Zs[mid] - Zs[high]) / dy2;
1237
+
1238
+ var linebase = Ys[low] * w;
1239
+ for(var y=Ys[low]; y>Ys[high]; y--) {
1240
+ if(y >=0 && y < h) {
1241
+ var xLeft = ~~x0;
1242
+ var zLeft = z0;
1243
+ var xRight, zRight;
1244
+ if(y > Ys[mid]) {
1245
+ xRight = ~~x1;
1246
+ zRight = z1;
1247
+ }
1248
+ else {
1249
+ xRight = ~~x2;
1250
+ zRight = z2;
1251
+ }
1252
+
1253
+ if(xLeft > xRight) {
1254
+ var temp;
1255
+ temp = xLeft;
1256
+ xLeft = xRight;
1257
+ xRight = temp;
1258
+ temp = zLeft;
1259
+ zLeft = zRight;
1260
+ zRight = temp;
1261
+ }
1262
+
1263
+ if(xLeft < 0)
1264
+ xLeft = 0;
1265
+ if(xRight >= w)
1266
+ xRight = w - 1;
1267
+
1268
+ var zInc = (xLeft != xRight) ? ((zRight - zLeft) / (xRight - xLeft)) : 1;
1269
+ var pix = linebase + xLeft;
1270
+ if(isOpaque) {
1271
+ for(var x=xLeft, z=zLeft; x<=xRight; x++, z+=zInc) {
1272
+ if(z > zbuf[pix]) {
1273
+ zbuf[pix] = z;
1274
+ cbuf[pix] = color;
1275
+ sbuf[pix] = id;
1276
+ }
1277
+ pix++;
1278
+ }
1279
+ }
1280
+ else {
1281
+ for(var x=xLeft, z=zLeft; x<xRight; x++, z+=zInc) {
1282
+ if(z > zbuf[pix]) {
1283
+ var foreColor = color;
1284
+ var backColor = cbuf[pix];
1285
+ var rr = ((backColor & 0xff0000) * trans + (foreColor & 0xff0000) * opaci) >> 8;
1286
+ var gg = ((backColor & 0xff00) * trans + (foreColor & 0xff00) * opaci) >> 8;
1287
+ var bb = ((backColor & 0xff) * trans + (foreColor & 0xff) * opaci) >> 8;
1288
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
1289
+ sbuf[pix] = id;
1290
+ }
1291
+ pix++;
1292
+ }
1293
+ }
1294
+ }
1295
+
1296
+ // step up to next scanline
1297
+ //
1298
+ x0 -= xStep0;
1299
+ z0 -= zStep0;
1300
+ if(y > Ys[mid]) {
1301
+ x1 -= xStep1;
1302
+ z1 -= zStep1;
1303
+ }
1304
+ else {
1305
+ x2 -= xStep2;
1306
+ z2 -= zStep2;
1307
+ }
1308
+ linebase -= w;
1309
+ }
1310
+ }
1311
+
1312
+ v1 = v2;
1313
+ } while (ibuf[j] != -1);
1314
+
1315
+ j++;
1316
+ }
1317
+ }
1318
+ };
1319
+
1320
+ /**
1321
+ Render the given mesh as solid object, using smooth shading.
1322
+ @private
1323
+ */
1324
+ JSC3D.Viewer.prototype.renderSolidSmooth = function(mesh) {
1325
+ var w = this.frameWidth;
1326
+ var h = this.frameHeight;
1327
+ var ibuf = mesh.indexBuffer;
1328
+ var vbuf = mesh.transformedVertexBuffer;
1329
+ var vnbuf = mesh.transformedVertexNormalZBuffer;
1330
+ var fnbuf = mesh.transformedFaceNormalZBuffer;
1331
+ var cbuf = this.colorBuffer;
1332
+ var zbuf = this.zBuffer;
1333
+ var sbuf = this.selectionBuffer;
1334
+ var numOfFaces = mesh.faceCount;
1335
+ var numOfVertices = vbuf.length / 3;
1336
+ var id = mesh.internalId;
1337
+ var material = mesh.material ? mesh.material : this.defaultMaterial;
1338
+ var palette = material.getPalette();
1339
+ var isOpaque = material.transparency == 0;
1340
+ var trans = material.transparency * 255;
1341
+ var opaci = 255 - trans;
1342
+
1343
+ // skip this mesh if it is completely transparent
1344
+ if(material.transparency == 1)
1345
+ return;
1346
+
1347
+ if(!vnbuf || vnbuf.length < numOfVertices) {
1348
+ mesh.transformedVertexNormalZBuffer = new Array(numOfVertices);
1349
+ vnbuf = mesh.transformedVertexNormalZBuffer;
1350
+ }
1351
+
1352
+ if(!fnbuf || fnbuf.length < numOfFaces) {
1353
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
1354
+ fnbuf = mesh.transformedFaceNormalZBuffer;
1355
+ }
1356
+
1357
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.vertexNormalBuffer, vnbuf);
1358
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, fnbuf);
1359
+
1360
+ var isDoubleSided = mesh.isDoubleSided;
1361
+
1362
+ var Xs = new Array(3);
1363
+ var Ys = new Array(3);
1364
+ var Zs = new Array(3);
1365
+ var Ns = new Array(3);
1366
+ var i = 0, j = 0;
1367
+ while(i < numOfFaces) {
1368
+ var xformedFNz = fnbuf[i++];
1369
+ if(isDoubleSided)
1370
+ xformedFNz = xformedFNz > 0 ? xformedFNz : -xformedFNz;
1371
+ if(xformedFNz < 0) {
1372
+ do {
1373
+ } while (ibuf[j++] != -1);
1374
+ }
1375
+ else {
1376
+ var i0, i1, i2;
1377
+ var v0, v1, v2;
1378
+ i0 = ibuf[j++];
1379
+ v0 = i0 * 3;
1380
+ i1 = ibuf[j++];
1381
+ v1 = i1 * 3;
1382
+
1383
+ do {
1384
+ i2 = ibuf[j++];
1385
+ v2 = i2 * 3;
1386
+
1387
+ Xs[0] = ~~(vbuf[v0] + 0.5);
1388
+ Ys[0] = ~~(vbuf[v0 + 1] + 0.5);
1389
+ Zs[0] = vbuf[v0 + 2];
1390
+ Xs[1] = ~~(vbuf[v1] + 0.5);
1391
+ Ys[1] = ~~(vbuf[v1 + 1] + 0.5);
1392
+ Zs[1] = vbuf[v1 + 2];
1393
+ Xs[2] = ~~(vbuf[v2] + 0.5);
1394
+ Ys[2] = ~~(vbuf[v2 + 1] + 0.5);
1395
+ Zs[2] = vbuf[v2 + 2];
1396
+
1397
+ Ns[0] = vnbuf[i0];
1398
+ Ns[1] = vnbuf[i1];
1399
+ Ns[2] = vnbuf[i2];
1400
+ if(isDoubleSided) {
1401
+ if(Ns[0] < 0)
1402
+ Ns[0] = -Ns[0];
1403
+ if(Ns[1] < 0)
1404
+ Ns[1] = -Ns[1];
1405
+ if(Ns[2] < 0)
1406
+ Ns[2] = -Ns[2];
1407
+ }
1408
+
1409
+ var high = Ys[0] < Ys[1] ? 0 : 1;
1410
+ high = Ys[high] < Ys[2] ? high : 2;
1411
+ var low = Ys[0] > Ys[1] ? 0 : 1;
1412
+ low = Ys[low] > Ys[2] ? low : 2;
1413
+ var mid = 3 - low - high;
1414
+
1415
+ if(high != low) {
1416
+ var x0 = Xs[low];
1417
+ var z0 = Zs[low];
1418
+ var n0 = Ns[low] * 255;
1419
+ var dy0 = Ys[low] - Ys[high];
1420
+ dy0 = dy0 != 0 ? dy0 : 1;
1421
+ var xStep0 = (Xs[low] - Xs[high]) / dy0;
1422
+ var zStep0 = (Zs[low] - Zs[high]) / dy0;
1423
+ var nStep0 = (Ns[low] - Ns[high]) * 255 / dy0;
1424
+
1425
+ var x1 = Xs[low];
1426
+ var z1 = Zs[low];
1427
+ var n1 = Ns[low] * 255;
1428
+ var dy1 = Ys[low] - Ys[mid];
1429
+ dy1 = dy1 != 0 ? dy1 : 1;
1430
+ var xStep1 = (Xs[low] - Xs[mid]) / dy1;
1431
+ var zStep1 = (Zs[low] - Zs[mid]) / dy1;
1432
+ var nStep1 = (Ns[low] - Ns[mid]) * 255 / dy1;
1433
+
1434
+ var x2 = Xs[mid];
1435
+ var z2 = Zs[mid];
1436
+ var n2 = Ns[mid] * 255;
1437
+ var dy2 = Ys[mid] - Ys[high];
1438
+ dy2 = dy2 != 0 ? dy2 : 1;
1439
+ var xStep2 = (Xs[mid] - Xs[high]) / dy2;
1440
+ var zStep2 = (Zs[mid] - Zs[high]) / dy2;
1441
+ var nStep2 = (Ns[mid] - Ns[high]) * 255 / dy2;
1442
+
1443
+ var linebase = Ys[low] * w;
1444
+ for(var y=Ys[low]; y>Ys[high]; y--) {
1445
+ if(y >=0 && y < h) {
1446
+ var xLeft = ~~x0;
1447
+ var zLeft = z0;
1448
+ var nLeft = n0;
1449
+ var xRight, zRight, nRight;
1450
+ if(y > Ys[mid]) {
1451
+ xRight = ~~x1;
1452
+ zRight = z1;
1453
+ nRight = n1;
1454
+ }
1455
+ else {
1456
+ xRight = ~~x2;
1457
+ zRight = z2;
1458
+ nRight = n2;
1459
+ }
1460
+
1461
+ if(xLeft > xRight) {
1462
+ var temp;
1463
+ temp = xLeft;
1464
+ xLeft = xRight;
1465
+ xRight = temp;
1466
+ temp = zLeft;
1467
+ zLeft = zRight;
1468
+ zRight = temp;
1469
+ temp = nLeft;
1470
+ nLeft = nRight;
1471
+ nRight = temp;
1472
+ }
1473
+
1474
+ var zInc = (xLeft != xRight) ? ((zRight - zLeft) / (xRight - xLeft)) : 1;
1475
+ var nInc = (xLeft != xRight) ? ((nRight - nLeft) / (xRight - xLeft)) : 1;
1476
+ if(xLeft < 0) {
1477
+ zLeft -= xLeft * zInc;
1478
+ nLeft -= xLeft * nInc;
1479
+ xLeft = 0;
1480
+ }
1481
+ if(xRight >= w) {
1482
+ xRight = w - 1;
1483
+ }
1484
+ var pix = linebase + xLeft;
1485
+ if(isOpaque) {
1486
+ for(var x=xLeft, z=zLeft, n=nLeft; x<=xRight; x++, z+=zInc, n+=nInc) {
1487
+ if(z > zbuf[pix]) {
1488
+ zbuf[pix] = z;
1489
+ cbuf[pix] = palette[n > 0 ? (~~n) : 0];
1490
+ sbuf[pix] = id;
1491
+ }
1492
+ pix++;
1493
+ }
1494
+ }
1495
+ else {
1496
+ for(var x=xLeft, z=zLeft, n=nLeft; x<xRight; x++, z+=zInc, n+=nInc) {
1497
+ if(z > zbuf[pix]) {
1498
+ var foreColor = palette[n > 0 ? (~~n) : 0];
1499
+ var backColor = cbuf[pix];
1500
+ var rr = ((backColor & 0xff0000) * trans + (foreColor & 0xff0000) * opaci) >> 8;
1501
+ var gg = ((backColor & 0xff00) * trans + (foreColor & 0xff00) * opaci) >> 8;
1502
+ var bb = ((backColor & 0xff) * trans + (foreColor & 0xff) * opaci) >> 8;
1503
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
1504
+ sbuf[pix] = id;
1505
+ }
1506
+ pix++;
1507
+ }
1508
+ }
1509
+ }
1510
+
1511
+ // step up to next scanline
1512
+ //
1513
+ x0 -= xStep0;
1514
+ z0 -= zStep0;
1515
+ n0 -= nStep0;
1516
+ if(y > Ys[mid]) {
1517
+ x1 -= xStep1;
1518
+ z1 -= zStep1;
1519
+ n1 -= nStep1;
1520
+ }
1521
+ else {
1522
+ x2 -= xStep2;
1523
+ z2 -= zStep2;
1524
+ n2 -= nStep2;
1525
+ }
1526
+ linebase -= w;
1527
+ }
1528
+ }
1529
+
1530
+ v1 = v2;
1531
+ i1 = i2;
1532
+ } while (ibuf[j] != -1);
1533
+
1534
+ j++;
1535
+ }
1536
+ }
1537
+ };
1538
+
1539
+ /**
1540
+ Render the given mesh as textured object, with no lightings.
1541
+ @private
1542
+ */
1543
+ JSC3D.Viewer.prototype.renderSolidTexture = function(mesh) {
1544
+ var w = this.frameWidth;
1545
+ var h = this.frameHeight;
1546
+ var ibuf = mesh.indexBuffer;
1547
+ var vbuf = mesh.transformedVertexBuffer;
1548
+ var nbuf = mesh.transformedFaceNormalZBuffer;
1549
+ var cbuf = this.colorBuffer;
1550
+ var zbuf = this.zBuffer;
1551
+ var sbuf = this.selectionBuffer;
1552
+ var numOfFaces = mesh.faceCount;
1553
+ var id = mesh.internalId;
1554
+ var texture = mesh.texture;
1555
+ var isOpaque = !texture.hasTransparency;
1556
+ var tbuf = mesh.texCoordBuffer;
1557
+ var tibuf = mesh.texCoordIndexBuffer;
1558
+ var tdata = texture.data;
1559
+ var tdim = texture.width;
1560
+ var tbound = tdim - 1;
1561
+ var mipmaps = texture.hasMipmap() ? texture.mipmaps : null;
1562
+ var mipentries = mipmaps ? texture.mipentries : null;
1563
+
1564
+ if(!nbuf || nbuf.length < numOfFaces) {
1565
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
1566
+ nbuf = mesh.transformedFaceNormalZBuffer;
1567
+ }
1568
+
1569
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, nbuf);
1570
+
1571
+ var Xs = new Array(3);
1572
+ var Ys = new Array(3);
1573
+ var Zs = new Array(3);
1574
+ var THs = new Array(3);
1575
+ var TVs = new Array(3);
1576
+ var i = 0, j = 0;
1577
+ while(i < numOfFaces) {
1578
+ var xformedNz = nbuf[i++];
1579
+ if(mesh.isDoubleSided)
1580
+ xformedNz = xformedNz > 0 ? xformedNz : -xformedNz;
1581
+ if(xformedNz < 0) {
1582
+ do {
1583
+ } while (ibuf[j++] != -1);
1584
+ }
1585
+ else {
1586
+ var v0, v1, v2;
1587
+ var t0, t1, t2;
1588
+ v0 = ibuf[j] * 3;
1589
+ t0 = tibuf[j] * 2;
1590
+ j++;
1591
+ v1 = ibuf[j] * 3;
1592
+ t1 = tibuf[j] * 2;
1593
+ j++;
1594
+
1595
+ // select an appropriate mip-map level for texturing
1596
+ //
1597
+ if(mipmaps) {
1598
+ v2 = ibuf[j] * 3;
1599
+ t2 = tibuf[j] * 2;
1600
+
1601
+ tdim = texture.width;
1602
+
1603
+ Xs[0] = vbuf[v0];
1604
+ Ys[0] = vbuf[v0 + 1];
1605
+ Xs[1] = vbuf[v1];
1606
+ Ys[1] = vbuf[v1 + 1];
1607
+ Xs[2] = vbuf[v2];
1608
+ Ys[2] = vbuf[v2 + 1];
1609
+
1610
+ THs[0] = tbuf[t0] * tdim;
1611
+ TVs[0] = tbuf[t0 + 1] * tdim;
1612
+ THs[1] = tbuf[t1] * tdim;
1613
+ TVs[1] = tbuf[t1 + 1] * tdim;
1614
+ THs[2] = tbuf[t2] * tdim;
1615
+ TVs[2] = tbuf[t2 + 1] * tdim;
1616
+
1617
+ var faceArea = (Xs[1] - Xs[0]) * (Ys[2] - Ys[0]) - (Ys[1] - Ys[0]) * (Xs[2] - Xs[0]);
1618
+ if(faceArea < 0)
1619
+ faceArea = -faceArea;
1620
+ faceArea += 1;
1621
+ var texArea = (THs[1] - THs[0]) * (TVs[2] - TVs[0]) - (TVs[1] - TVs[0]) * (THs[2] - THs[0]);
1622
+ if(texArea < 0)
1623
+ texArea = -texArea;
1624
+ var mipRatio = texArea / faceArea;
1625
+
1626
+ var level = 0;
1627
+ if(mipRatio < mipentries[1])
1628
+ level = 0;
1629
+ else if(mipRatio >= mipentries[mipentries.length - 1]) {
1630
+ level = mipentries.length - 1;
1631
+ tdim = 1;
1632
+ }
1633
+ else {
1634
+ while(mipRatio >= mipentries[level+1]) {
1635
+ level++;
1636
+ tdim /= 2;
1637
+ }
1638
+ }
1639
+
1640
+ tdata = mipmaps[level];
1641
+ tbound = tdim - 1;
1642
+ }
1643
+
1644
+ do {
1645
+ v2 = ibuf[j] * 3;
1646
+ t2 = tibuf[j] * 2;
1647
+ j++;
1648
+
1649
+ Xs[0] = ~~(vbuf[v0] + 0.5);
1650
+ Ys[0] = ~~(vbuf[v0 + 1] + 0.5);
1651
+ Zs[0] = vbuf[v0 + 2];
1652
+ Xs[1] = ~~(vbuf[v1] + 0.5);
1653
+ Ys[1] = ~~(vbuf[v1 + 1] + 0.5);
1654
+ Zs[1] = vbuf[v1 + 2];
1655
+ Xs[2] = ~~(vbuf[v2] + 0.5);
1656
+ Ys[2] = ~~(vbuf[v2 + 1] + 0.5);
1657
+ Zs[2] = vbuf[v2 + 2];
1658
+
1659
+ THs[0] = tbuf[t0] * tdim;
1660
+ TVs[0] = tbuf[t0 + 1] * tdim;
1661
+ THs[1] = tbuf[t1] * tdim;
1662
+ TVs[1] = tbuf[t1 + 1] * tdim;
1663
+ THs[2] = tbuf[t2] * tdim;
1664
+ TVs[2] = tbuf[t2 + 1] * tdim;
1665
+
1666
+ var high = Ys[0] < Ys[1] ? 0 : 1;
1667
+ high = Ys[high] < Ys[2] ? high : 2;
1668
+ var low = Ys[0] > Ys[1] ? 0 : 1;
1669
+ low = Ys[low] > Ys[2] ? low : 2;
1670
+ var mid = 3 - low - high;
1671
+
1672
+ if(high != low) {
1673
+ var x0 = Xs[low];
1674
+ var z0 = Zs[low];
1675
+ var th0 = THs[low];
1676
+ var tv0 = TVs[low];
1677
+ var dy0 = Ys[low] - Ys[high];
1678
+ dy0 = dy0 != 0 ? dy0 : 1;
1679
+ var xStep0 = (Xs[low] - Xs[high]) / dy0;
1680
+ var zStep0 = (Zs[low] - Zs[high]) / dy0;
1681
+ var thStep0 = (THs[low] - THs[high]) / dy0;
1682
+ var tvStep0 = (TVs[low] - TVs[high]) / dy0;
1683
+
1684
+ var x1 = Xs[low];
1685
+ var z1 = Zs[low];
1686
+ var th1 = THs[low];
1687
+ var tv1 = TVs[low];
1688
+ var dy1 = Ys[low] - Ys[mid];
1689
+ dy1 = dy1 != 0 ? dy1 : 1;
1690
+ var xStep1 = (Xs[low] - Xs[mid]) / dy1;
1691
+ var zStep1 = (Zs[low] - Zs[mid]) / dy1;
1692
+ var thStep1 = (THs[low] - THs[mid]) / dy1;
1693
+ var tvStep1 = (TVs[low] - TVs[mid]) / dy1;
1694
+
1695
+ var x2 = Xs[mid];
1696
+ var z2 = Zs[mid];
1697
+ var th2 = THs[mid];
1698
+ var tv2 = TVs[mid];
1699
+ var dy2 = Ys[mid] - Ys[high];
1700
+ dy2 = dy2 != 0 ? dy2 : 1;
1701
+ var xStep2 = (Xs[mid] - Xs[high]) / dy2;
1702
+ var zStep2 = (Zs[mid] - Zs[high]) / dy2;
1703
+ var thStep2 = (THs[mid] - THs[high]) / dy2;
1704
+ var tvStep2 = (TVs[mid] - TVs[high]) / dy2;
1705
+
1706
+ var linebase = Ys[low] * w;
1707
+ for(var y=Ys[low]; y>Ys[high]; y--) {
1708
+ if(y >=0 && y < h) {
1709
+ var xLeft = ~~x0;
1710
+ var zLeft = z0;
1711
+ var thLeft = th0;
1712
+ var tvLeft = tv0;
1713
+ var xRight, zRight, thRight, tvRight;
1714
+ if(y > Ys[mid]) {
1715
+ xRight = ~~x1;
1716
+ zRight = z1;
1717
+ thRight = th1;
1718
+ tvRight = tv1;
1719
+ }
1720
+ else {
1721
+ xRight = ~~x2;
1722
+ zRight = z2;
1723
+ thRight = th2;
1724
+ tvRight = tv2;
1725
+ }
1726
+
1727
+ if(xLeft > xRight) {
1728
+ var temp;
1729
+ temp = xLeft;
1730
+ xLeft = xRight;
1731
+ xRight = temp;
1732
+ temp = zLeft;
1733
+ zLeft = zRight;
1734
+ zRight = temp;
1735
+ temp = thLeft;
1736
+ thLeft = thRight;
1737
+ thRight = temp;
1738
+ temp = tvLeft;
1739
+ tvLeft = tvRight;
1740
+ tvRight = temp;
1741
+ }
1742
+
1743
+ var zInc = (xLeft != xRight) ? ((zRight - zLeft) / (xRight - xLeft)) : 1;
1744
+ var thInc = (xLeft != xRight) ? ((thRight - thLeft) / (xRight - xLeft)) : 1;
1745
+ var tvInc = (xLeft != xRight) ? ((tvRight - tvLeft) / (xRight - xLeft)) : 1;
1746
+
1747
+ if(xLeft < 0) {
1748
+ zLeft -= xLeft * zInc;
1749
+ thLeft -= xLeft * thInc;
1750
+ tvLeft -= xLeft * tvInc;
1751
+ xLeft = 0;
1752
+ }
1753
+ if(xRight >= w)
1754
+ xRight = w - 1;
1755
+
1756
+ var pix = linebase + xLeft;
1757
+ if(isOpaque) {
1758
+ for(var x=xLeft, z=zLeft, th=thLeft, tv=tvLeft; x<=xRight; x++, z+=zInc, th+=thInc, tv+=tvInc) {
1759
+ if(z > zbuf[pix]) {
1760
+ zbuf[pix] = z;
1761
+ cbuf[pix] = tdata[(tv & tbound) * tdim + (th & tbound)];
1762
+ sbuf[pix] = id;
1763
+ }
1764
+ pix++;
1765
+ }
1766
+ }
1767
+ else {
1768
+ for(var x=xLeft, z=zLeft, th=thLeft, tv=tvLeft; x<xRight; x++, z+=zInc, th+=thInc, tv+=tvInc) {
1769
+ if(z > zbuf[pix]) {
1770
+ var foreColor = tdata[(tv & tbound) * tdim + (th & tbound)];
1771
+ var backColor = cbuf[pix];
1772
+ var opaci = (foreColor >> 24) & 0xff;
1773
+ var trans = 255 - opaci;
1774
+ var rr = ((backColor & 0xff0000) * trans + (foreColor & 0xff0000) * opaci) >> 8;
1775
+ var gg = ((backColor & 0xff00) * trans + (foreColor & 0xff00) * opaci) >> 8;
1776
+ var bb = ((backColor & 0xff) * trans + (foreColor & 0xff) * opaci) >> 8;
1777
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
1778
+ sbuf[pix] = id;
1779
+ }
1780
+ pix++;
1781
+ }
1782
+ }
1783
+ }
1784
+
1785
+ // step up to next scanline
1786
+ //
1787
+ x0 -= xStep0;
1788
+ z0 -= zStep0;
1789
+ th0 -= thStep0;
1790
+ tv0 -= tvStep0;
1791
+ if(y > Ys[mid]) {
1792
+ x1 -= xStep1;
1793
+ z1 -= zStep1;
1794
+ th1 -= thStep1;
1795
+ tv1 -= tvStep1;
1796
+ }
1797
+ else {
1798
+ x2 -= xStep2;
1799
+ z2 -= zStep2;
1800
+ th2 -= thStep2;
1801
+ tv2 -= tvStep2;
1802
+ }
1803
+ linebase -= w;
1804
+ }
1805
+ }
1806
+
1807
+ v1 = v2;
1808
+ t1 = t2;
1809
+ } while (ibuf[j] != -1);
1810
+
1811
+ j++;
1812
+ }
1813
+ }
1814
+ };
1815
+
1816
+ /**
1817
+ Render the given mesh as textured object. Lighting will be calculated per face.
1818
+ @private
1819
+ */
1820
+ JSC3D.Viewer.prototype.renderTextureFlat = function(mesh) {
1821
+ var w = this.frameWidth;
1822
+ var h = this.frameHeight;
1823
+ var ibuf = mesh.indexBuffer;
1824
+ var vbuf = mesh.transformedVertexBuffer;
1825
+ var nbuf = mesh.transformedFaceNormalZBuffer;
1826
+ var cbuf = this.colorBuffer;
1827
+ var zbuf = this.zBuffer;
1828
+ var sbuf = this.selectionBuffer;
1829
+ var numOfFaces = mesh.faceCount;
1830
+ var id = mesh.internalId;
1831
+ var material = mesh.material ? mesh.material : this.defaultMaterial;
1832
+ var palette = material.getPalette();
1833
+ var texture = mesh.texture;
1834
+ var isOpaque = (material.transparency == 0) && !texture.hasTransparency;
1835
+ var matOpacity = ~~((1 - material.transparency) * 255);
1836
+ var tbuf = mesh.texCoordBuffer;
1837
+ var tibuf = mesh.texCoordIndexBuffer;
1838
+ var tdata = texture.data;
1839
+ var tdim = texture.width;
1840
+ var tbound = tdim - 1;
1841
+ var mipmaps = texture.hasMipmap() ? texture.mipmaps : null;
1842
+ var mipentries = mipmaps ? texture.mipentries : null;
1843
+
1844
+ // skip this mesh if it is completely transparent
1845
+ if(material.transparency == 1)
1846
+ return;
1847
+
1848
+ if(!nbuf || nbuf.length < numOfFaces) {
1849
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
1850
+ nbuf = mesh.transformedFaceNormalZBuffer;
1851
+ }
1852
+
1853
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, nbuf);
1854
+
1855
+ var Xs = new Array(3);
1856
+ var Ys = new Array(3);
1857
+ var Zs = new Array(3);
1858
+ var THs = new Array(3);
1859
+ var TVs = new Array(3);
1860
+ var i = 0, j = 0;
1861
+ while(i < numOfFaces) {
1862
+ var xformedNz = nbuf[i++];
1863
+ if(mesh.isDoubleSided)
1864
+ xformedNz = xformedNz > 0 ? xformedNz : -xformedNz;
1865
+ if(xformedNz < 0) {
1866
+ do {
1867
+ } while (ibuf[j++] != -1);
1868
+ }
1869
+ else {
1870
+ var color = palette[~~(xformedNz * 255)];
1871
+
1872
+ var v0, v1, v2;
1873
+ var t0, t1, t2;
1874
+ v0 = ibuf[j] * 3;
1875
+ t0 = tibuf[j] * 2;
1876
+ j++;
1877
+ v1 = ibuf[j] * 3;
1878
+ t1 = tibuf[j] * 2;
1879
+ j++;
1880
+
1881
+ if(mipmaps) {
1882
+ v2 = ibuf[j] * 3;
1883
+ t2 = tibuf[j] * 2;
1884
+
1885
+ tdim = texture.width;
1886
+
1887
+ Xs[0] = vbuf[v0];
1888
+ Ys[0] = vbuf[v0 + 1];
1889
+ Xs[1] = vbuf[v1];
1890
+ Ys[1] = vbuf[v1 + 1];
1891
+ Xs[2] = vbuf[v2];
1892
+ Ys[2] = vbuf[v2 + 1];
1893
+
1894
+ THs[0] = tbuf[t0] * tdim;
1895
+ TVs[0] = tbuf[t0 + 1] * tdim;
1896
+ THs[1] = tbuf[t1] * tdim;
1897
+ TVs[1] = tbuf[t1 + 1] * tdim;
1898
+ THs[2] = tbuf[t2] * tdim;
1899
+ TVs[2] = tbuf[t2 + 1] * tdim;
1900
+
1901
+ var faceArea = (Xs[1] - Xs[0]) * (Ys[2] - Ys[0]) - (Ys[1] - Ys[0]) * (Xs[2] - Xs[0]);
1902
+ if(faceArea < 0)
1903
+ faceArea = -faceArea;
1904
+ faceArea += 1;
1905
+ var texArea = (THs[1] - THs[0]) * (TVs[2] - TVs[0]) - (TVs[1] - TVs[0]) * (THs[2] - THs[0]);
1906
+ if(texArea < 0)
1907
+ texArea = -texArea;
1908
+ var mipRatio = texArea / faceArea;
1909
+
1910
+ var level = 0;
1911
+ if(mipRatio < mipentries[1])
1912
+ level = 0;
1913
+ else if(mipRatio >= mipentries[mipentries.length - 1]) {
1914
+ level = mipentries.length - 1;
1915
+ tdim = 1;
1916
+ }
1917
+ else {
1918
+ while(mipRatio >= mipentries[level+1]) {
1919
+ level++;
1920
+ tdim /= 2;
1921
+ }
1922
+ }
1923
+
1924
+ tdata = mipmaps[level];
1925
+ tbound = tdim - 1;
1926
+ }
1927
+
1928
+ do {
1929
+ v2 = ibuf[j] * 3;
1930
+ t2 = tibuf[j] * 2;
1931
+ j++;
1932
+
1933
+ Xs[0] = ~~(vbuf[v0] + 0.5);
1934
+ Ys[0] = ~~(vbuf[v0 + 1] + 0.5);
1935
+ Zs[0] = vbuf[v0 + 2];
1936
+ Xs[1] = ~~(vbuf[v1] + 0.5);
1937
+ Ys[1] = ~~(vbuf[v1 + 1] + 0.5);
1938
+ Zs[1] = vbuf[v1 + 2];
1939
+ Xs[2] = ~~(vbuf[v2] + 0.5);
1940
+ Ys[2] = ~~(vbuf[v2 + 1] + 0.5);
1941
+ Zs[2] = vbuf[v2 + 2];
1942
+
1943
+ THs[0] = tbuf[t0] * tdim;
1944
+ TVs[0] = tbuf[t0 + 1] * tdim;
1945
+ THs[1] = tbuf[t1] * tdim;
1946
+ TVs[1] = tbuf[t1 + 1] * tdim;
1947
+ THs[2] = tbuf[t2] * tdim;
1948
+ TVs[2] = tbuf[t2 + 1] * tdim;
1949
+
1950
+ var high = Ys[0] < Ys[1] ? 0 : 1;
1951
+ high = Ys[high] < Ys[2] ? high : 2;
1952
+ var low = Ys[0] > Ys[1] ? 0 : 1;
1953
+ low = Ys[low] > Ys[2] ? low : 2;
1954
+ var mid = 3 - low - high;
1955
+
1956
+ if(high != low) {
1957
+ var x0 = Xs[low];
1958
+ var z0 = Zs[low];
1959
+ var th0 = THs[low];
1960
+ var tv0 = TVs[low];
1961
+ var dy0 = Ys[low] - Ys[high];
1962
+ dy0 = dy0 != 0 ? dy0 : 1;
1963
+ var xStep0 = (Xs[low] - Xs[high]) / dy0;
1964
+ var zStep0 = (Zs[low] - Zs[high]) / dy0;
1965
+ var thStep0 = (THs[low] - THs[high]) / dy0;
1966
+ var tvStep0 = (TVs[low] - TVs[high]) / dy0;
1967
+
1968
+ var x1 = Xs[low];
1969
+ var z1 = Zs[low];
1970
+ var th1 = THs[low];
1971
+ var tv1 = TVs[low];
1972
+ var dy1 = Ys[low] - Ys[mid];
1973
+ dy1 = dy1 != 0 ? dy1 : 1;
1974
+ var xStep1 = (Xs[low] - Xs[mid]) / dy1;
1975
+ var zStep1 = (Zs[low] - Zs[mid]) / dy1;
1976
+ var thStep1 = (THs[low] - THs[mid]) / dy1;
1977
+ var tvStep1 = (TVs[low] - TVs[mid]) / dy1;
1978
+
1979
+ var x2 = Xs[mid];
1980
+ var z2 = Zs[mid];
1981
+ var th2 = THs[mid];
1982
+ var tv2 = TVs[mid];
1983
+ var dy2 = Ys[mid] - Ys[high];
1984
+ dy2 = dy2 != 0 ? dy2 : 1;
1985
+ var xStep2 = (Xs[mid] - Xs[high]) / dy2;
1986
+ var zStep2 = (Zs[mid] - Zs[high]) / dy2;
1987
+ var thStep2 = (THs[mid] - THs[high]) / dy2;
1988
+ var tvStep2 = (TVs[mid] - TVs[high]) / dy2;
1989
+
1990
+ var linebase = Ys[low] * w;
1991
+ for(var y=Ys[low]; y>Ys[high]; y--) {
1992
+ if(y >=0 && y < h) {
1993
+ var xLeft = ~~x0;
1994
+ var zLeft = z0;
1995
+ var thLeft = th0;
1996
+ var tvLeft = tv0;
1997
+ var xRight, zRight, thRight, tvRight;
1998
+ if(y > Ys[mid]) {
1999
+ xRight = ~~x1;
2000
+ zRight = z1;
2001
+ thRight = th1;
2002
+ tvRight = tv1;
2003
+ }
2004
+ else {
2005
+ xRight = ~~x2;
2006
+ zRight = z2;
2007
+ thRight = th2;
2008
+ tvRight = tv2;
2009
+ }
2010
+
2011
+ if(xLeft > xRight) {
2012
+ var temp;
2013
+ temp = xLeft;
2014
+ xLeft = xRight;
2015
+ xRight = temp;
2016
+ temp = zLeft;
2017
+ zLeft = zRight;
2018
+ zRight = temp;
2019
+ temp = thLeft;
2020
+ thLeft = thRight;
2021
+ thRight = temp;
2022
+ temp = tvLeft;
2023
+ tvLeft = tvRight;
2024
+ tvRight = temp;
2025
+ }
2026
+
2027
+ var zInc = (xLeft != xRight) ? ((zRight - zLeft) / (xRight - xLeft)) : 1;
2028
+ var thInc = (xLeft != xRight) ? ((thRight - thLeft) / (xRight - xLeft)) : 1;
2029
+ var tvInc = (xLeft != xRight) ? ((tvRight - tvLeft) / (xRight - xLeft)) : 1;
2030
+
2031
+ if(xLeft < 0) {
2032
+ zLeft -= xLeft * zInc;
2033
+ thLeft -= xLeft * thInc;
2034
+ tvLeft -= xLeft * tvInc;
2035
+ xLeft = 0;
2036
+ }
2037
+ if(xRight >= w)
2038
+ xRight = w - 1;
2039
+
2040
+ var pix = linebase + xLeft;
2041
+ if(isOpaque) {
2042
+ for(var x=xLeft, z=zLeft, th=thLeft, tv=tvLeft; x<=xRight; x++, z+=zInc, th+=thInc, tv+=tvInc) {
2043
+ if(z > zbuf[pix]) {
2044
+ zbuf[pix] = z;
2045
+ var texel = tdata[(tv & tbound) * tdim + (th & tbound)];
2046
+ var rr = (((color & 0xff0000) >> 16) * ((texel & 0xff0000) >> 8));
2047
+ var gg = (((color & 0xff00) >> 8) * ((texel & 0xff00) >> 8));
2048
+ var bb = ((color & 0xff) * (texel & 0xff)) >> 8;
2049
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
2050
+ sbuf[pix] = id;
2051
+ }
2052
+ pix++;
2053
+ }
2054
+ }
2055
+ else {
2056
+ for(var x=xLeft, z=zLeft, th=thLeft, tv=tvLeft; x<xRight; x++, z+=zInc, th+=thInc, tv+=tvInc) {
2057
+ if(z > zbuf[pix]) {
2058
+ var foreColor = tdata[(tv & tbound) * tdim + (th & tbound)];
2059
+ var backColor = cbuf[pix];
2060
+ var opaci = (((foreColor >> 24) & 0xff) * (matOpacity & 0xff)) >> 8;
2061
+ var rr = (((color & 0xff0000) >> 16) * ((foreColor & 0xff0000) >> 8));
2062
+ var gg = (((color & 0xff00) >> 8) * ((foreColor & 0xff00) >> 8));
2063
+ var bb = ((color & 0xff) * (foreColor & 0xff)) >> 8;
2064
+ if(opaci > 250) {
2065
+ zbuf[pix] = z;
2066
+ }
2067
+ else {
2068
+ var trans = 255 - opaci;
2069
+ rr = (rr * opaci + (backColor & 0xff0000) * trans) >> 8;
2070
+ gg = (gg * opaci + (backColor & 0xff00) * trans) >> 8;
2071
+ bb = (bb * opaci + (backColor & 0xff) * trans) >> 8;
2072
+ }
2073
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
2074
+ sbuf[pix] = id;
2075
+ }
2076
+ pix++;
2077
+ }
2078
+ }
2079
+ }
2080
+
2081
+ // step up to next scanline
2082
+ //
2083
+ x0 -= xStep0;
2084
+ z0 -= zStep0;
2085
+ th0 -= thStep0;
2086
+ tv0 -= tvStep0;
2087
+ if(y > Ys[mid]) {
2088
+ x1 -= xStep1;
2089
+ z1 -= zStep1;
2090
+ th1 -= thStep1;
2091
+ tv1 -= tvStep1;
2092
+ }
2093
+ else {
2094
+ x2 -= xStep2;
2095
+ z2 -= zStep2;
2096
+ th2 -= thStep2;
2097
+ tv2 -= tvStep2;
2098
+ }
2099
+ linebase -= w;
2100
+ }
2101
+ }
2102
+
2103
+ v1 = v2;
2104
+ t1 = t2;
2105
+ } while (ibuf[j] != -1);
2106
+
2107
+ j++;
2108
+ }
2109
+ }
2110
+ };
2111
+
2112
+ /**
2113
+ Render the given mesh as textured object. Lighting will be calculated per vertex and then inerpolated between and inside scanlines.
2114
+ @private
2115
+ */
2116
+ JSC3D.Viewer.prototype.renderTextureSmooth = function(mesh) {
2117
+ var w = this.frameWidth;
2118
+ var h = this.frameHeight;
2119
+ var ibuf = mesh.indexBuffer;
2120
+ var vbuf = mesh.transformedVertexBuffer;
2121
+ var vnbuf = mesh.transformedVertexNormalZBuffer;
2122
+ var fnbuf = mesh.transformedFaceNormalZBuffer;
2123
+ var cbuf = this.colorBuffer;
2124
+ var zbuf = this.zBuffer;
2125
+ var sbuf = this.selectionBuffer;
2126
+ var numOfFaces = mesh.faceCount;
2127
+ var id = mesh.internalId;
2128
+ var numOfVertices = vbuf.length / 3;
2129
+ var material = mesh.material ? mesh.material : this.defaultMaterial;
2130
+ var palette = material.getPalette();
2131
+ var texture = mesh.texture;
2132
+ var isOpaque = (material.transparency == 0) && !texture.hasTransparency;
2133
+ var matOpacity = ~~((1 - material.transparency) * 255);
2134
+ var tbuf = mesh.texCoordBuffer;
2135
+ var tibuf = mesh.texCoordIndexBuffer;
2136
+ var tdata = texture.data;
2137
+ var tdim = texture.width;
2138
+ var tbound = tdim - 1;
2139
+ var mipmaps = texture.hasMipmap() ? texture.mipmaps : null;
2140
+ var mipentries = mipmaps ? texture.mipentries : null;
2141
+
2142
+ // skip this mesh if it is completely transparent
2143
+ if(material.transparency == 1)
2144
+ return;
2145
+
2146
+ if(!vnbuf || vnbuf.length < numOfVertices) {
2147
+ mesh.transformedVertexNormalZBuffer = new Array(numOfVertices);
2148
+ vnbuf = mesh.transformedVertexNormalZBuffer;
2149
+ }
2150
+
2151
+ if(!fnbuf || fnbuf.length < numOfFaces) {
2152
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
2153
+ fnbuf = mesh.transformedFaceNormalZBuffer;
2154
+ }
2155
+
2156
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.vertexNormalBuffer, vnbuf);
2157
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, fnbuf);
2158
+
2159
+ var isDoubleSided = mesh.isDoubleSided;
2160
+
2161
+ var Xs = new Array(3);
2162
+ var Ys = new Array(3);
2163
+ var Zs = new Array(3);
2164
+ var Ns = new Array(3);
2165
+ var THs = new Array(3);
2166
+ var TVs = new Array(3);
2167
+ var i = 0, j = 0;
2168
+ while(i < numOfFaces) {
2169
+ var xformedFNz = fnbuf[i++];
2170
+ if(isDoubleSided)
2171
+ xformedFNz = xformedFNz > 0 ? xformedFNz : -xformedFNz;
2172
+ if(xformedFNz < 0) {
2173
+ do {
2174
+ } while (ibuf[j++] != -1);
2175
+ }
2176
+ else {
2177
+ var i0, i1, i2;
2178
+ var v0, v1, v2;
2179
+ var t0, t1, t2;
2180
+ i0 = ibuf[j];
2181
+ v0 = i0 * 3;
2182
+ t0 = tibuf[j] * 2;
2183
+ j++;
2184
+ i1 = ibuf[j];
2185
+ v1 = i1 * 3;
2186
+ t1 = tibuf[j] * 2;
2187
+ j++;
2188
+
2189
+ if(mipmaps) {
2190
+ v2 = ibuf[j] * 3;
2191
+ t2 = tibuf[j] * 2;
2192
+
2193
+ tdim = texture.width;
2194
+
2195
+ Xs[0] = vbuf[v0];
2196
+ Ys[0] = vbuf[v0 + 1];
2197
+ Xs[1] = vbuf[v1];
2198
+ Ys[1] = vbuf[v1 + 1];
2199
+ Xs[2] = vbuf[v2];
2200
+ Ys[2] = vbuf[v2 + 1];
2201
+
2202
+ THs[0] = tbuf[t0] * tdim;
2203
+ TVs[0] = tbuf[t0 + 1] * tdim;
2204
+ THs[1] = tbuf[t1] * tdim;
2205
+ TVs[1] = tbuf[t1 + 1] * tdim;
2206
+ THs[2] = tbuf[t2] * tdim;
2207
+ TVs[2] = tbuf[t2 + 1] * tdim;
2208
+
2209
+ var faceArea = (Xs[1] - Xs[0]) * (Ys[2] - Ys[0]) - (Ys[1] - Ys[0]) * (Xs[2] - Xs[0]);
2210
+ if(faceArea < 0)
2211
+ faceArea = -faceArea;
2212
+ faceArea += 1;
2213
+ var texArea = (THs[1] - THs[0]) * (TVs[2] - TVs[0]) - (TVs[1] - TVs[0]) * (THs[2] - THs[0]);
2214
+ if(texArea < 0)
2215
+ texArea = -texArea;
2216
+ var mipRatio = texArea / faceArea;
2217
+
2218
+ var level = 0;
2219
+ if(mipRatio < mipentries[1])
2220
+ level = 0;
2221
+ else if(mipRatio >= mipentries[mipentries.length - 1]) {
2222
+ level = mipentries.length - 1;
2223
+ tdim = 1;
2224
+ }
2225
+ else {
2226
+ while(mipRatio >= mipentries[level+1]) {
2227
+ level++;
2228
+ tdim /= 2;
2229
+ }
2230
+ }
2231
+
2232
+ tdata = mipmaps[level];
2233
+ tbound = tdim - 1;
2234
+ }
2235
+
2236
+ do {
2237
+ i2 = ibuf[j];
2238
+ v2 = i2 * 3;
2239
+ t2 = tibuf[j] * 2;
2240
+ j++;
2241
+
2242
+ Xs[0] = ~~(vbuf[v0] + 0.5);
2243
+ Ys[0] = ~~(vbuf[v0 + 1] + 0.5);
2244
+ Zs[0] = vbuf[v0 + 2];
2245
+ Xs[1] = ~~(vbuf[v1] + 0.5);
2246
+ Ys[1] = ~~(vbuf[v1 + 1] + 0.5);
2247
+ Zs[1] = vbuf[v1 + 2];
2248
+ Xs[2] = ~~(vbuf[v2] + 0.5);
2249
+ Ys[2] = ~~(vbuf[v2 + 1] + 0.5);
2250
+ Zs[2] = vbuf[v2 + 2];
2251
+
2252
+ THs[0] = tbuf[t0] * tdim;
2253
+ TVs[0] = tbuf[t0 + 1] * tdim;
2254
+ THs[1] = tbuf[t1] * tdim;
2255
+ TVs[1] = tbuf[t1 + 1] * tdim;
2256
+ THs[2] = tbuf[t2] * tdim;
2257
+ TVs[2] = tbuf[t2 + 1] * tdim;
2258
+
2259
+ Ns[0] = vnbuf[i0];
2260
+ Ns[1] = vnbuf[i1];
2261
+ Ns[2] = vnbuf[i2];
2262
+ if(isDoubleSided) {
2263
+ if(Ns[0] < 0)
2264
+ Ns[0] = -Ns[0];
2265
+ if(Ns[1] < 0)
2266
+ Ns[1] = -Ns[1];
2267
+ if(Ns[2] < 0)
2268
+ Ns[2] = -Ns[2];
2269
+ }
2270
+
2271
+ var high = Ys[0] < Ys[1] ? 0 : 1;
2272
+ high = Ys[high] < Ys[2] ? high : 2;
2273
+ var low = Ys[0] > Ys[1] ? 0 : 1;
2274
+ low = Ys[low] > Ys[2] ? low : 2;
2275
+ var mid = 3 - low - high;
2276
+
2277
+ if(high != low) {
2278
+ var x0 = Xs[low];
2279
+ var z0 = Zs[low];
2280
+ var th0 = THs[low];
2281
+ var tv0 = TVs[low];
2282
+ var n0 = Ns[low] * 255;
2283
+ var dy0 = Ys[low] - Ys[high];
2284
+ dy0 = dy0 != 0 ? dy0 : 1;
2285
+ var xStep0 = (Xs[low] - Xs[high]) / dy0;
2286
+ var zStep0 = (Zs[low] - Zs[high]) / dy0;
2287
+ var thStep0 = (THs[low] - THs[high]) / dy0;
2288
+ var tvStep0 = (TVs[low] - TVs[high]) / dy0;
2289
+ var nStep0 = (Ns[low] - Ns[high]) * 255 / dy0;
2290
+
2291
+ var x1 = Xs[low];
2292
+ var z1 = Zs[low];
2293
+ var th1 = THs[low];
2294
+ var tv1 = TVs[low];
2295
+ var n1 = Ns[low] * 255;
2296
+ var dy1 = Ys[low] - Ys[mid];
2297
+ dy1 = dy1 != 0 ? dy1 : 1;
2298
+ var xStep1 = (Xs[low] - Xs[mid]) / dy1;
2299
+ var zStep1 = (Zs[low] - Zs[mid]) / dy1;
2300
+ var thStep1 = (THs[low] - THs[mid]) / dy1;
2301
+ var tvStep1 = (TVs[low] - TVs[mid]) / dy1;
2302
+ var nStep1 = (Ns[low] - Ns[mid]) * 255 / dy1;
2303
+
2304
+ var x2 = Xs[mid];
2305
+ var z2 = Zs[mid];
2306
+ var th2 = THs[mid];
2307
+ var tv2 = TVs[mid];
2308
+ var n2 = Ns[mid] * 255;
2309
+ var dy2 = Ys[mid] - Ys[high];
2310
+ dy2 = dy2 != 0 ? dy2 : 1;
2311
+ var xStep2 = (Xs[mid] - Xs[high]) / dy2;
2312
+ var zStep2 = (Zs[mid] - Zs[high]) / dy2;
2313
+ var thStep2 = (THs[mid] - THs[high]) / dy2;
2314
+ var tvStep2 = (TVs[mid] - TVs[high]) / dy2;
2315
+ var nStep2 = (Ns[mid] - Ns[high]) * 255 / dy2;
2316
+
2317
+ var linebase = Ys[low] * w;
2318
+ for(var y=Ys[low]; y>Ys[high]; y--) {
2319
+ if(y >=0 && y < h) {
2320
+ var xLeft = ~~x0;
2321
+ var zLeft = z0;
2322
+ var thLeft = th0;
2323
+ var tvLeft = tv0;
2324
+ var nLeft = n0;
2325
+ var xRight, zRight, thRight, tvRight, nRight;
2326
+ if(y > Ys[mid]) {
2327
+ xRight = ~~x1;
2328
+ zRight = z1;
2329
+ thRight = th1;
2330
+ tvRight = tv1;
2331
+ nRight = n1;
2332
+ }
2333
+ else {
2334
+ xRight = ~~x2;
2335
+ zRight = z2;
2336
+ thRight = th2;
2337
+ tvRight = tv2;
2338
+ nRight = n2;
2339
+ }
2340
+
2341
+ if(xLeft > xRight) {
2342
+ var temp;
2343
+ temp = xLeft;
2344
+ xLeft = xRight;
2345
+ xRight = temp;
2346
+ temp = zLeft;
2347
+ zLeft = zRight;
2348
+ zRight = temp;
2349
+ temp = thLeft;
2350
+ thLeft = thRight;
2351
+ thRight = temp;
2352
+ temp = tvLeft;
2353
+ tvLeft = tvRight;
2354
+ tvRight = temp;
2355
+ temp = nLeft;
2356
+ nLeft = nRight;
2357
+ nRight = temp;
2358
+ }
2359
+
2360
+ var zInc = (xLeft != xRight) ? ((zRight - zLeft) / (xRight - xLeft)) : 1;
2361
+ var thInc = (xLeft != xRight) ? ((thRight - thLeft) / (xRight - xLeft)) : 1;
2362
+ var tvInc = (xLeft != xRight) ? ((tvRight - tvLeft) / (xRight - xLeft)) : 1;
2363
+ var nInc = (xLeft != xRight) ? ((nRight - nLeft) / (xRight - xLeft)) : 0;
2364
+
2365
+ if(xLeft < 0) {
2366
+ zLeft -= xLeft * zInc;
2367
+ thLeft -= xLeft * thInc;
2368
+ tvLeft -= xLeft * tvInc;
2369
+ nLeft -= xLeft * nInc;
2370
+ xLeft = 0;
2371
+ }
2372
+ if(xRight >= w)
2373
+ xRight = w - 1;
2374
+
2375
+ var pix = linebase + xLeft;
2376
+ if(isOpaque) {
2377
+ for(var x=xLeft, z=zLeft, n=nLeft, th=thLeft, tv=tvLeft; x<=xRight; x++, z+=zInc, n+=nInc, th+=thInc, tv+=tvInc) {
2378
+ if(z > zbuf[pix]) {
2379
+ zbuf[pix] = z;
2380
+ var color = palette[n > 0 ? (~~n) : 0];
2381
+ var texel = tdata[(tv & tbound) * tdim + (th & tbound)];
2382
+ var rr = (((color & 0xff0000) >> 16) * ((texel & 0xff0000) >> 8));
2383
+ var gg = (((color & 0xff00) >> 8) * ((texel & 0xff00) >> 8));
2384
+ var bb = ((color & 0xff) * (texel & 0xff)) >> 8;
2385
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
2386
+ sbuf[pix] = id;
2387
+ }
2388
+ pix++;
2389
+ }
2390
+ }
2391
+ else {
2392
+ for(var x=xLeft, z=zLeft, n=nLeft, th=thLeft, tv=tvLeft; x<xRight; x++, z+=zInc, n+=nInc, th+=thInc, tv+=tvInc) {
2393
+ if(z > zbuf[pix]) {
2394
+ var color = palette[n > 0 ? (~~n) : 0];
2395
+ var foreColor = tdata[(tv & tbound) * tdim + (th & tbound)];
2396
+ var backColor = cbuf[pix];
2397
+ var opaci = (((foreColor >> 24) & 0xff) * (matOpacity & 0xff)) >> 8;
2398
+ var rr = (((color & 0xff0000) >> 16) * ((foreColor & 0xff0000) >> 8));
2399
+ var gg = (((color & 0xff00) >> 8) * ((foreColor & 0xff00) >> 8));
2400
+ var bb = ((color & 0xff) * (foreColor & 0xff)) >> 8;
2401
+ if(opaci > 250) {
2402
+ zbuf[pix] = z;
2403
+ }
2404
+ else {
2405
+ var trans = 255 - opaci;
2406
+ rr = (rr * opaci + (backColor & 0xff0000) * trans) >> 8;
2407
+ gg = (gg * opaci + (backColor & 0xff00) * trans) >> 8;
2408
+ bb = (bb * opaci + (backColor & 0xff) * trans) >> 8;
2409
+ }
2410
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
2411
+ sbuf[pix] = id;
2412
+ }
2413
+ pix++;
2414
+ }
2415
+ }
2416
+ }
2417
+
2418
+ // step up to next scanline
2419
+ //
2420
+ x0 -= xStep0;
2421
+ z0 -= zStep0;
2422
+ th0 -= thStep0;
2423
+ tv0 -= tvStep0;
2424
+ n0 -= nStep0;
2425
+ if(y > Ys[mid]) {
2426
+ x1 -= xStep1;
2427
+ z1 -= zStep1;
2428
+ th1 -= thStep1;
2429
+ tv1 -= tvStep1;
2430
+ n1 -= nStep1;
2431
+ }
2432
+ else {
2433
+ x2 -= xStep2;
2434
+ z2 -= zStep2;
2435
+ th2 -= thStep2;
2436
+ tv2 -= tvStep2;
2437
+ n2 -= nStep2;
2438
+ }
2439
+ linebase -= w;
2440
+ }
2441
+ }
2442
+
2443
+ i1 = i2;
2444
+ v1 = v2;
2445
+ t1 = t2;
2446
+ } while (ibuf[j] != -1);
2447
+
2448
+ j++;
2449
+ }
2450
+ }
2451
+ };
2452
+
2453
+ /**
2454
+ Render the given mesh as solid object with sphere mapping. Lighting will be calculated per vertex and then inerpolated between and inside scanlines.
2455
+ @private
2456
+ */
2457
+ JSC3D.Viewer.prototype.renderSolidSphereMapped = function(mesh) {
2458
+ var w = this.frameWidth;
2459
+ var h = this.frameHeight;
2460
+ var ibuf = mesh.indexBuffer;
2461
+ var vbuf = mesh.transformedVertexBuffer;
2462
+ var vnbuf = mesh.transformedVertexNormalBuffer;
2463
+ var fnbuf = mesh.transformedFaceNormalZBuffer;
2464
+ var cbuf = this.colorBuffer;
2465
+ var zbuf = this.zBuffer;
2466
+ var sbuf = this.selectionBuffer;
2467
+ var numOfFaces = mesh.faceCount;
2468
+ var numOfVertices = vbuf.length / 3;
2469
+ var id = mesh.internalId;
2470
+ var material = mesh.material ? mesh.material : this.defaultMaterial;
2471
+ var palette = material.getPalette();
2472
+ var sphereMap = this.sphereMap;
2473
+ var sdata = sphereMap.data;
2474
+ var sdim = sphereMap.width;
2475
+ var sbound = sdim - 1;
2476
+ var isOpaque = material.transparency == 0;
2477
+ var trans = material.transparency * 255;
2478
+ var opaci = 255 - trans;
2479
+
2480
+ // skip this mesh if it is completely transparent
2481
+ if(material.transparency == 1)
2482
+ return;
2483
+
2484
+ if(!vnbuf || vnbuf.length < numOfVertices * 3) {
2485
+ mesh.transformedVertexNormalBuffer = new Array(numOfVertices * 3);
2486
+ vnbuf = mesh.transformedVertexNormalBuffer;
2487
+ }
2488
+
2489
+ if(!fnbuf || fnbuf.length < numOfFaces) {
2490
+ mesh.transformedFaceNormalZBuffer = new Array(numOfFaces);
2491
+ fnbuf = mesh.transformedFaceNormalZBuffer;
2492
+ }
2493
+
2494
+ JSC3D.Math3D.transformVectors(this.rotMatrix, mesh.vertexNormalBuffer, vnbuf);
2495
+ JSC3D.Math3D.transformVectorZs(this.rotMatrix, mesh.faceNormalBuffer, fnbuf);
2496
+
2497
+ var isDoubleSided = mesh.isDoubleSided;
2498
+
2499
+ var Xs = new Array(3);
2500
+ var Ys = new Array(3);
2501
+ var Zs = new Array(3);
2502
+ var NXs = new Array(3);
2503
+ var NYs = new Array(3);
2504
+ var NZs = new Array(3);
2505
+ var i = 0, j = 0;
2506
+ while(i < numOfFaces) {
2507
+ var xformedFNz = fnbuf[i++];
2508
+ if(isDoubleSided)
2509
+ xformedFNz = xformedFNz > 0 ? xformedFNz : -xformedFNz;
2510
+ if(xformedFNz < 0) {
2511
+ do {
2512
+ } while (ibuf[j++] != -1);
2513
+ }
2514
+ else {
2515
+ var v0, v1, v2;
2516
+ v0 = ibuf[j++] * 3;
2517
+ v1 = ibuf[j++] * 3;
2518
+
2519
+ do {
2520
+ v2 = ibuf[j++] * 3;
2521
+
2522
+ Xs[0] = ~~(vbuf[v0] + 0.5);
2523
+ Ys[0] = ~~(vbuf[v0 + 1] + 0.5);
2524
+ Zs[0] = vbuf[v0 + 2];
2525
+ Xs[1] = ~~(vbuf[v1] + 0.5);
2526
+ Ys[1] = ~~(vbuf[v1 + 1] + 0.5);
2527
+ Zs[1] = vbuf[v1 + 2];
2528
+ Xs[2] = ~~(vbuf[v2] + 0.5);
2529
+ Ys[2] = ~~(vbuf[v2 + 1] + 0.5);
2530
+ Zs[2] = vbuf[v2 + 2];
2531
+
2532
+ NXs[0] = vnbuf[v0];
2533
+ NYs[0] = vnbuf[v0 + 1];
2534
+ NZs[0] = vnbuf[v0 + 2];
2535
+ NXs[1] = vnbuf[v1];
2536
+ NYs[1] = vnbuf[v1 + 1];
2537
+ NZs[1] = vnbuf[v1 + 2];
2538
+ NXs[2] = vnbuf[v2];
2539
+ NYs[2] = vnbuf[v2 + 1];
2540
+ NZs[2] = vnbuf[v2 + 2];
2541
+ if(isDoubleSided) {
2542
+ if(NZs[0] < 0)
2543
+ NZs[0] = -NZs[0];
2544
+ if(NZs[1] < 0)
2545
+ NZs[1] = -NZs[1];
2546
+ if(NZs[2] < 0)
2547
+ NZs[2] = -NZs[2];
2548
+ }
2549
+
2550
+ var high = Ys[0] < Ys[1] ? 0 : 1;
2551
+ high = Ys[high] < Ys[2] ? high : 2;
2552
+ var low = Ys[0] > Ys[1] ? 0 : 1;
2553
+ low = Ys[low] > Ys[2] ? low : 2;
2554
+ var mid = 3 - low - high;
2555
+
2556
+ if(high != low) {
2557
+ var x0 = Xs[low];
2558
+ var z0 = Zs[low];
2559
+ var n0 = NZs[low] * 255;
2560
+ var sh0 = ((NXs[low] / 2 + 0.5) * sdim) & sbound;
2561
+ var sv0 = ((0.5 - NYs[low] / 2) * sdim) & sbound;
2562
+ var dy0 = Ys[low] - Ys[high];
2563
+ dy0 = dy0 != 0 ? dy0 : 1;
2564
+ var xStep0 = (Xs[low] - Xs[high]) / dy0;
2565
+ var zStep0 = (Zs[low] - Zs[high]) / dy0;
2566
+ var nStep0 = (NZs[low] - NZs[high]) * 255 / dy0;
2567
+ var shStep0 = (((NXs[low] - NXs[high]) / 2) * sdim) / dy0;
2568
+ var svStep0 = (((NYs[high] - NYs[low]) / 2) * sdim) / dy0;
2569
+
2570
+ var x1 = Xs[low];
2571
+ var z1 = Zs[low];
2572
+ var n1 = NZs[low] * 255;
2573
+ var sh1 = ((NXs[low] / 2 + 0.5) * sdim) & sbound;
2574
+ var sv1 = ((0.5 - NYs[low] / 2) * sdim) & sbound;
2575
+ var dy1 = Ys[low] - Ys[mid];
2576
+ dy1 = dy1 != 0 ? dy1 : 1;
2577
+ var xStep1 = (Xs[low] - Xs[mid]) / dy1;
2578
+ var zStep1 = (Zs[low] - Zs[mid]) / dy1;
2579
+ var nStep1 = (NZs[low] - NZs[mid]) * 255 / dy1;
2580
+ var shStep1 = (((NXs[low] - NXs[mid]) / 2) * sdim) / dy1;
2581
+ var svStep1 = (((NYs[mid] - NYs[low]) / 2) * sdim) / dy1;
2582
+
2583
+ var x2 = Xs[mid];
2584
+ var z2 = Zs[mid];
2585
+ var n2 = NZs[mid] * 255;
2586
+ var sh2 = ((NXs[mid] / 2 + 0.5) * sdim) & sbound;
2587
+ var sv2 = ((0.5 - NYs[mid] / 2) * sdim) & sbound;
2588
+ var dy2 = Ys[mid] - Ys[high];
2589
+ dy2 = dy2 != 0 ? dy2 : 1;
2590
+ var xStep2 = (Xs[mid] - Xs[high]) / dy2;
2591
+ var zStep2 = (Zs[mid] - Zs[high]) / dy2;
2592
+ var nStep2 = (NZs[mid] - NZs[high]) * 255 / dy2;
2593
+ var shStep2 = (((NXs[mid] - NXs[high]) / 2) * sdim) / dy2;
2594
+ var svStep2 = (((NYs[high] - NYs[mid]) / 2) * sdim) / dy2;
2595
+
2596
+ var linebase = Ys[low] * w;
2597
+ for(var y=Ys[low]; y>Ys[high]; y--) {
2598
+ if(y >=0 && y < h) {
2599
+ var xLeft = ~~x0;
2600
+ var zLeft = z0;
2601
+ var nLeft = n0;
2602
+ var shLeft = sh0;
2603
+ var svLeft = sv0;
2604
+ var xRight, zRight, nRight, shRight, svRight;
2605
+ if(y > Ys[mid]) {
2606
+ xRight = ~~x1;
2607
+ zRight = z1;
2608
+ nRight = n1;
2609
+ shRight = sh1;
2610
+ svRight = sv1;
2611
+ }
2612
+ else {
2613
+ xRight = ~~x2;
2614
+ zRight = z2;
2615
+ nRight = n2;
2616
+ shRight = sh2;
2617
+ svRight = sv2;
2618
+ }
2619
+
2620
+ if(xLeft > xRight) {
2621
+ var temp;
2622
+ temp = xLeft;
2623
+ xLeft = xRight;
2624
+ xRight = temp;
2625
+ temp = zLeft;
2626
+ zLeft = zRight;
2627
+ zRight = temp;
2628
+ temp = nLeft;
2629
+ nLeft = nRight;
2630
+ nRight = temp;
2631
+ temp = shLeft;
2632
+ shLeft = shRight;
2633
+ shRight = temp;
2634
+ temp = svLeft;
2635
+ svLeft = svRight;
2636
+ svRight = temp;
2637
+ }
2638
+
2639
+ var zInc = (xLeft != xRight) ? ((zRight - zLeft) / (xRight - xLeft)) : 1;
2640
+ var nInc = (xLeft != xRight) ? ((nRight - nLeft) / (xRight - xLeft)) : 1;
2641
+ var shInc = (xLeft != xRight) ? ((shRight - shLeft) / (xRight - xLeft)) : 1;
2642
+ var svInc = (xLeft != xRight) ? ((svRight - svLeft) / (xRight - xLeft)) : 1;
2643
+ if(xLeft < 0) {
2644
+ zLeft -= xLeft * zInc;
2645
+ nLeft -= xLeft * nInc;
2646
+ shLeft -= shLeft * shInc;
2647
+ svLeft -= svLeft * svInc;
2648
+ xLeft = 0;
2649
+ }
2650
+ if(xRight >= w) {
2651
+ xRight = w - 1;
2652
+ }
2653
+ var pix = linebase + xLeft;
2654
+ if(isOpaque) {
2655
+ for(var x=xLeft, z=zLeft, n=nLeft, sh=shLeft, sv=svLeft; x<=xRight; x++, z+=zInc, n+=nInc, sh+=shInc, sv+=svInc) {
2656
+ if(z > zbuf[pix]) {
2657
+ zbuf[pix] = z;
2658
+ var color = palette[n > 0 ? (~~n) : 0];
2659
+ var stexel = sdata[(sv & sbound) * sdim + (sh & sbound)];
2660
+ var rr = (((color & 0xff0000) >> 16) * ((stexel & 0xff0000) >> 8));
2661
+ var gg = (((color & 0xff00) >> 8) * ((stexel & 0xff00) >> 8));
2662
+ var bb = ((color & 0xff) * (stexel & 0xff)) >> 8;
2663
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
2664
+ sbuf[pix] = id;
2665
+ }
2666
+ pix++;
2667
+ }
2668
+ }
2669
+ else {
2670
+ for(var x=xLeft, z=zLeft, n=nLeft, sh=shLeft, sv=svLeft; x<xRight; x++, z+=zInc, n+=nInc, sh+=shInc, sv+=svInc) {
2671
+ if(z > zbuf[pix]) {
2672
+ var color = palette[n > 0 ? (~~n) : 0];
2673
+ var foreColor = sdata[(sv & sbound) * sdim + (sh & sbound)];
2674
+ var backColor = cbuf[pix];
2675
+ var rr = (((color & 0xff0000) >> 16) * ((foreColor & 0xff0000) >> 8));
2676
+ var gg = (((color & 0xff00) >> 8) * ((foreColor & 0xff00) >> 8));
2677
+ var bb = ((color & 0xff) * (foreColor & 0xff)) >> 8;
2678
+ rr = (rr * opaci + (backColor & 0xff0000) * trans) >> 8;
2679
+ gg = (gg * opaci + (backColor & 0xff00) * trans) >> 8;
2680
+ bb = (bb * opaci + (backColor & 0xff) * trans) >> 8;
2681
+ cbuf[pix] = (rr & 0xff0000) | (gg & 0xff00) | (bb & 0xff);
2682
+ sbuf[pix] = id;
2683
+ }
2684
+ pix++;
2685
+ }
2686
+ }
2687
+ }
2688
+
2689
+ // step up to next scanline
2690
+ //
2691
+ x0 -= xStep0;
2692
+ z0 -= zStep0;
2693
+ n0 -= nStep0;
2694
+ sh0 -= shStep0;
2695
+ sv0 -= svStep0;
2696
+ if(y > Ys[mid]) {
2697
+ x1 -= xStep1;
2698
+ z1 -= zStep1;
2699
+ n1 -= nStep1;
2700
+ sh1 -= shStep1;
2701
+ sv1 -= svStep1;
2702
+ }
2703
+ else {
2704
+ x2 -= xStep2;
2705
+ z2 -= zStep2;
2706
+ n2 -= nStep2;
2707
+ sh2 -= shStep2;
2708
+ sv2 -= svStep2;
2709
+ }
2710
+ linebase -= w;
2711
+ }
2712
+ }
2713
+
2714
+ v1 = v2;
2715
+ } while (ibuf[j] != -1);
2716
+
2717
+ j++;
2718
+ }
2719
+ }
2720
+ };
2721
+
2722
+ JSC3D.Viewer.prototype.params = null;
2723
+ JSC3D.Viewer.prototype.canvas = null;
2724
+ JSC3D.Viewer.prototype.ctx = null;
2725
+ JSC3D.Viewer.prototype.canvasData = null;
2726
+ JSC3D.Viewer.prototype.bkgColorBuffer = null;
2727
+ JSC3D.Viewer.prototype.colorBuffer = null;
2728
+ JSC3D.Viewer.prototype.zBuffer = null;
2729
+ JSC3D.Viewer.prototype.selectionBuffer = null;
2730
+ JSC3D.Viewer.prototype.frameWidth = 0;
2731
+ JSC3D.Viewer.prototype.frameHeight = 0;
2732
+ JSC3D.Viewer.prototype.scene = null;
2733
+ JSC3D.Viewer.prototype.defaultMaterial = null;
2734
+ JSC3D.Viewer.prototype.sphereMap = null;
2735
+ JSC3D.Viewer.prototype.isLoaded = false;
2736
+ JSC3D.Viewer.prototype.isFailed = false;
2737
+ JSC3D.Viewer.prototype.errorMsg = '';
2738
+ JSC3D.Viewer.prototype.initRotX = 0;
2739
+ JSC3D.Viewer.prototype.initRotY = 0;
2740
+ JSC3D.Viewer.prototype.initRotZ = 0;
2741
+ JSC3D.Viewer.prototype.zoomFactor = 1;
2742
+ JSC3D.Viewer.prototype.rotMatrix = null;
2743
+ JSC3D.Viewer.prototype.transformMatrix = null;
2744
+ JSC3D.Viewer.prototype.sceneUrl = '';
2745
+ JSC3D.Viewer.prototype.modelColor = 0xcaa618;
2746
+ JSC3D.Viewer.prototype.bkgColor1 = 0xffffff;
2747
+ JSC3D.Viewer.prototype.bkgColor2 = 0xffff80;
2748
+ JSC3D.Viewer.prototype.renderMode = 'flat';
2749
+ JSC3D.Viewer.prototype.definition = 'standard';
2750
+ JSC3D.Viewer.prototype.isMipMappingOn = false;
2751
+ JSC3D.Viewer.prototype.sphereMapUrl = '';
2752
+ JSC3D.Viewer.prototype.buttonStates = null;
2753
+ JSC3D.Viewer.prototype.keyStates = null;
2754
+ JSC3D.Viewer.prototype.mouseX = 0;
2755
+ JSC3D.Viewer.prototype.mouseY = 0;
2756
+ JSC3D.Viewer.prototype.onmousedown = null;
2757
+ JSC3D.Viewer.prototype.onmouseup = null;
2758
+ JSC3D.Viewer.prototype.onmousemove = null;
2759
+ JSC3D.Viewer.prototype.beforeupdate = null;
2760
+ JSC3D.Viewer.prototype.afterupdate = null;
2761
+ JSC3D.Viewer.prototype.mouseUsage = 'default';
2762
+ JSC3D.Viewer.prototype.isDefaultInputHandlerEnabled = false;
2763
+
2764
+
2765
+ /**
2766
+ @class PickInfo
2767
+
2768
+ PickInfo is used as the return value of JSC3D.Viewer's pick() method, holding picking values at a given position
2769
+ on the canvas.
2770
+ */
2771
+ JSC3D.PickInfo = function() {
2772
+ this.canvasX = 0;
2773
+ this.canvasY = 0;
2774
+ this.depth = -Infinity;
2775
+ this.mesh = null;
2776
+ };
2777
+
2778
+
2779
+ /**
2780
+ @class Scene
2781
+
2782
+ This class implements scene that contains a group of meshes that forms the world.
2783
+ */
2784
+ JSC3D.Scene = function(name) {
2785
+ this.name = name || '';
2786
+ this.aabb = null;
2787
+ this.children = [];
2788
+ this.maxChildId = 1;
2789
+ };
2790
+
2791
+ /**
2792
+ Initialize the scene.
2793
+ */
2794
+ JSC3D.Scene.prototype.init = function() {
2795
+ if(this.isEmpty())
2796
+ return;
2797
+
2798
+ for(var i=0; i<this.children.length; i++)
2799
+ this.children[i].init();
2800
+
2801
+ if(!this.aabb) {
2802
+ this.aabb = new JSC3D.AABB;
2803
+ this.calcAABB();
2804
+ }
2805
+ };
2806
+
2807
+ /**
2808
+ See if the scene is empty.
2809
+ @returns {boolean} true if it contains no meshes; false if it has any.
2810
+ */
2811
+ JSC3D.Scene.prototype.isEmpty = function() {
2812
+ return (this.children.length == 0);
2813
+ };
2814
+
2815
+ /**
2816
+ Add a mesh to the scene.
2817
+ @param {JSC3D.Mesh} mesh the mesh to be added.
2818
+ */
2819
+ JSC3D.Scene.prototype.addChild = function(mesh) {
2820
+ mesh.internalId = this.maxChildId++;
2821
+ this.children.push(mesh);
2822
+ };
2823
+
2824
+ /**
2825
+ Remove a mesh from the scene.
2826
+ @param {JSC3D.Mesh} mesh the mesh to be added.
2827
+ */
2828
+ JSC3D.Scene.prototype.removeChild = function(mesh) {
2829
+ for(var i=0; i<this.children.length; i++) {
2830
+ if(this.children[i] == mesh) {
2831
+ this.children.splice(i, 1);
2832
+ break;
2833
+ }
2834
+ }
2835
+ };
2836
+
2837
+ /**
2838
+ Get all meshes in the scene.
2839
+ @returns {Array} meshes as an array.
2840
+ */
2841
+ JSC3D.Scene.prototype.getChildren = function() {
2842
+ return this.children.slice(0);
2843
+ };
2844
+
2845
+ /**
2846
+ Traverse meshes in the scene, calling a given function on each of them.
2847
+ @param {Function} operator a function that will be called on each mesh.
2848
+ */
2849
+ JSC3D.Scene.prototype.forEachChild = function(operator) {
2850
+ if((typeof operator) != 'function')
2851
+ return;
2852
+
2853
+ for(var i=0; i<this.children.length; i++) {
2854
+ if(operator.call(null, this.children[i]))
2855
+ break;
2856
+ }
2857
+ };
2858
+
2859
+ /**
2860
+ Calculate AABB of the scene.
2861
+ @private
2862
+ */
2863
+ JSC3D.Scene.prototype.calcAABB = function() {
2864
+ this.aabb.minX = this.aabb.minY = this.aabb.minZ = Number.MAX_VALUE;
2865
+ this.aabb.maxX = this.aabb.maxY = this.aabb.maxZ = -Number.MAX_VALUE;
2866
+ for(var i=0; i<this.children.length; i++) {
2867
+ var child = this.children[i];
2868
+ if(!child.isTrivial()) {
2869
+ var minX = child.aabb.minX;
2870
+ var minY = child.aabb.minY;
2871
+ var minZ = child.aabb.minZ;
2872
+ var maxX = child.aabb.maxX;
2873
+ var maxY = child.aabb.maxY;
2874
+ var maxZ = child.aabb.maxZ;
2875
+ if(this.aabb.minX > minX)
2876
+ this.aabb.minX = minX;
2877
+ if(this.aabb.minY > minY)
2878
+ this.aabb.minY = minY;
2879
+ if(this.aabb.minZ > minZ)
2880
+ this.aabb.minZ = minZ;
2881
+ if(this.aabb.maxX < maxX)
2882
+ this.aabb.maxX = maxX;
2883
+ if(this.aabb.maxY < maxY)
2884
+ this.aabb.maxY = maxY;
2885
+ if(this.aabb.maxZ < maxZ)
2886
+ this.aabb.maxZ = maxZ;
2887
+ }
2888
+ }
2889
+ };
2890
+
2891
+ JSC3D.Scene.prototype.name = '';
2892
+ JSC3D.Scene.prototype.aabb = null;
2893
+ JSC3D.Scene.prototype.children = null;
2894
+ JSC3D.Scene.prototype.maxChildId = 1;
2895
+
2896
+
2897
+ /**
2898
+ @class Mesh
2899
+
2900
+ This class implements mesh that is used as an expression of 3D object and the basic primitive for rendering. <br />
2901
+ A mesh basically consists of a sequence of faces, and optioanlly a material, a texture mapping and other attributes and metadata.<br />
2902
+ A face consists of 3 or more coplanary vertex that should be descript in counter-clockwise order.<br />
2903
+ A texture mapping includes a valid texture object with a sequence of texture coordinats specified per vertex.<br />
2904
+ */
2905
+ JSC3D.Mesh = function(name, visible, material, texture, isDoubleSided, isEnvironmentCast, coordBuffer, indexBuffer, texCoordBuffer, texCoordIndexBuffer) {
2906
+ this.name = name || '';
2907
+ this.metadata = '';
2908
+ this.visible = (visible != undefined) ? visible : true;
2909
+ this.aabb = null;
2910
+ this.vertexBuffer = coordBuffer || null;
2911
+ this.indexBuffer = indexBuffer || null;
2912
+ this.vertexNormalBuffer = null;
2913
+ this.faceNormalBuffer = null;
2914
+ this.material = material || null;
2915
+ this.texture = texture || null;
2916
+ this.faceCount = 0;
2917
+ this.isDoubleSided = isDoubleSided || false;
2918
+ this.isEnvironmentCast = isEnvironmentCast || false;
2919
+ this.internalId = 0;
2920
+ this.texCoordBuffer = texCoordBuffer || null;
2921
+ this.texCoordIndexBuffer = texCoordIndexBuffer || null;
2922
+ this.transformedVertexBuffer = null;
2923
+ this.transformedVertexNormalZBuffer = null;
2924
+ this.transformedFaceNormalZBuffer = null;
2925
+ this.transformedVertexNormalBuffer = null;
2926
+ };
2927
+
2928
+ /**
2929
+ Initialize the mesh.
2930
+ */
2931
+ JSC3D.Mesh.prototype.init = function() {
2932
+ if(this.isTrivial()) {
2933
+ return;
2934
+ }
2935
+
2936
+ if(this.faceCount == 0) {
2937
+ this.calcFaceCount();
2938
+ if(this.faceCount == 0)
2939
+ return;
2940
+ }
2941
+
2942
+ if(!this.aabb) {
2943
+ this.aabb = new JSC3D.AABB;
2944
+ this.calcAABB();
2945
+ }
2946
+
2947
+ if(!this.faceNormalBuffer) {
2948
+ this.faceNormalBuffer = new Array(this.faceCount * 3);
2949
+ this.calcFaceNormals();
2950
+ }
2951
+
2952
+ if(!this.vertexNormalBuffer) {
2953
+ this.vertexNormalBuffer = new Array(this.vertexBuffer.length);
2954
+ this.calcVertexNormals();
2955
+ }
2956
+
2957
+ this.normalizeFaceNormals();
2958
+
2959
+ this.transformedVertexBuffer = new Array(this.vertexBuffer.length);
2960
+ };
2961
+
2962
+ /**
2963
+ See if the mesh is a trivial mesh. A trivial mesh should be omited in any calculations and rendering.
2964
+ @returns {boolean} true if it is trivial; false if not.
2965
+ */
2966
+ JSC3D.Mesh.prototype.isTrivial = function() {
2967
+ return ( !this.vertexBuffer || this.vertexBuffer.length < 3 ||
2968
+ !this.indexBuffer || this.indexBuffer.length < 3 );
2969
+ };
2970
+
2971
+ /**
2972
+ Set material for the mesh.
2973
+ @param {JSC3D.Material} material the material object.
2974
+ */
2975
+ JSC3D.Mesh.prototype.setMaterial = function(material) {
2976
+ this.material = material;
2977
+ };
2978
+
2979
+ /**
2980
+ Set texture for the mesh.
2981
+ @param {JSC3D.Texture} texture the texture object.
2982
+ */
2983
+ JSC3D.Mesh.prototype.setTexture = function(texture) {
2984
+ this.texture = texture;
2985
+ };
2986
+
2987
+ /**
2988
+ See if the mesh has valid texture mapping.
2989
+ @returns {boolean} true if it has valid texture mapping; false if not.
2990
+ */
2991
+ JSC3D.Mesh.prototype.hasTexture = function() {
2992
+ return ( (this.texCoordBuffer != null) && (this.texCoordBuffer.length >= 2) &&
2993
+ (this.texCoordIndexBuffer != null) && (this.texCoordIndexBuffer.length >= 3) &&
2994
+ (this.texCoordIndexBuffer.length >= this.indexBuffer.length) &&
2995
+ (this.texture != null) && this.texture.hasData() );
2996
+ };
2997
+
2998
+ /**
2999
+ Calculate count of faces.
3000
+ @private
3001
+ */
3002
+ JSC3D.Mesh.prototype.calcFaceCount = function() {
3003
+ this.faceCount = 0;
3004
+
3005
+ var ibuf = this.indexBuffer;
3006
+ if(ibuf[ibuf.length - 1] != -1)
3007
+ ibuf.push(-1);
3008
+
3009
+ for(var i=0; i<ibuf.length; i++) {
3010
+ if(ibuf[i] == -1)
3011
+ this.faceCount++;
3012
+ }
3013
+ };
3014
+
3015
+ /**
3016
+ Calculate AABB of the mesh.
3017
+ @private
3018
+ */
3019
+ JSC3D.Mesh.prototype.calcAABB = function() {
3020
+ var minX = minY = minZ = Number.MAX_VALUE;
3021
+ var maxX = maxY = maxZ = -Number.MAX_VALUE;
3022
+
3023
+ var vbuf = this.vertexBuffer;
3024
+ for(var i=0; i<vbuf.length; i+=3) {
3025
+ var x = vbuf[i];
3026
+ var y = vbuf[i + 1];
3027
+ var z = vbuf[i + 2];
3028
+
3029
+ if(x < minX)
3030
+ minX = x;
3031
+ if(x > maxX)
3032
+ maxX = x;
3033
+ if(y < minY)
3034
+ minY = y;
3035
+ if(y > maxY)
3036
+ maxY = y;
3037
+ if(z < minZ)
3038
+ minZ = z;
3039
+ if(z > maxZ)
3040
+ maxZ = z;
3041
+ }
3042
+
3043
+ this.aabb.minX = minX;
3044
+ this.aabb.minY = minY;
3045
+ this.aabb.minZ = minZ;
3046
+ this.aabb.maxX = maxX;
3047
+ this.aabb.maxY = maxY;
3048
+ this.aabb.maxZ = maxZ;
3049
+ };
3050
+
3051
+ /**
3052
+ Calculate per face normals. The reault remain un-normalized for later vertex normal calculations.
3053
+ @private
3054
+ */
3055
+ JSC3D.Mesh.prototype.calcFaceNormals = function() {
3056
+ var vbuf = this.vertexBuffer;
3057
+ var ibuf = this.indexBuffer;
3058
+ var nbuf = this.faceNormalBuffer;
3059
+ var i = 0, j = 0;
3060
+ while(i < ibuf.length) {
3061
+ var index = ibuf[i++] * 3;
3062
+ var x0 = vbuf[index];
3063
+ var y0 = vbuf[index + 1];
3064
+ var z0 = vbuf[index + 2];
3065
+
3066
+ index = ibuf[i++] * 3;
3067
+ var x1 = vbuf[index];
3068
+ var y1 = vbuf[index + 1];
3069
+ var z1 = vbuf[index + 2];
3070
+
3071
+ index = ibuf[i++] * 3;
3072
+ var x2 = vbuf[index];
3073
+ var y2 = vbuf[index + 1];
3074
+ var z2 = vbuf[index + 2];
3075
+
3076
+ var dx1 = x1 - x0;
3077
+ var dy1 = y1 - y0;
3078
+ var dz1 = z1 - z0;
3079
+ var dx2 = x2 - x0;
3080
+ var dy2 = y2 - y0;
3081
+ var dz2 = z2 - z0;
3082
+
3083
+ var nx = dy1 * dz2 - dz1 * dy2;
3084
+ var ny = dz1 * dx2 - dx1 * dz2;
3085
+ var nz = dx1 * dy2 - dy1 * dx2;
3086
+
3087
+ nbuf[j++] = nx;
3088
+ nbuf[j++] = ny;
3089
+ nbuf[j++] = nz;
3090
+
3091
+ do {
3092
+ } while (ibuf[i++] != -1);
3093
+ }
3094
+ };
3095
+
3096
+ /**
3097
+ Calculate per vertex normals.
3098
+ @private
3099
+ */
3100
+ JSC3D.Mesh.prototype.calcVertexNormals = function() {
3101
+ if(!this.faceNormalBuffer) {
3102
+ this.faceNormalBuffer = new Array(this.faceCount * 3);
3103
+ this.calcFaceNormals();
3104
+ }
3105
+
3106
+ var vbuf = this.vertexBuffer;
3107
+ var ibuf = this.indexBuffer;
3108
+ var fnbuf = this.faceNormalBuffer;
3109
+ var vnbuf = this.vertexNormalBuffer;
3110
+ for(var i=0; i<vnbuf.length; i++) {
3111
+ vnbuf[i] = 0;
3112
+ }
3113
+
3114
+ var numOfVertices = vbuf.length / 3;
3115
+
3116
+ var i = 0, j = 0, k = 0;
3117
+ while(i < ibuf.length) {
3118
+ k = ibuf[i++];
3119
+ if(k == -1) {
3120
+ j += 3;
3121
+ }
3122
+ else {
3123
+ var index = k * 3;
3124
+ vnbuf[index ] += fnbuf[j];
3125
+ vnbuf[index + 1] += fnbuf[j + 1];
3126
+ vnbuf[index + 2] += fnbuf[j + 2];
3127
+ }
3128
+ }
3129
+
3130
+ for(var i=0, j=0; i<vnbuf.length; i+=3, j++) {
3131
+ var nx = vnbuf[i];
3132
+ var ny = vnbuf[i + 1];
3133
+ var nz = vnbuf[i + 2];
3134
+ var len = Math.sqrt(nx * nx + ny * ny + nz * nz);
3135
+ if(len > 0) {
3136
+ nx /= len;
3137
+ ny /= len;
3138
+ nz /= len;
3139
+ }
3140
+
3141
+ vnbuf[i ] = nx;
3142
+ vnbuf[i + 1] = ny;
3143
+ vnbuf[i + 2] = nz;
3144
+ }
3145
+ };
3146
+
3147
+ /**
3148
+ Normalize face normals.
3149
+ @private
3150
+ */
3151
+ JSC3D.Mesh.prototype.normalizeFaceNormals = function() {
3152
+ var nbuf = this.faceNormalBuffer;
3153
+
3154
+ for(var i=0; i<nbuf.length; i+=3) {
3155
+ var nx = nbuf[i];
3156
+ var ny = nbuf[i + 1];
3157
+ var nz = nbuf[i + 2];
3158
+ var len = Math.sqrt(nx * nx + ny * ny + nz * nz);
3159
+ if(len > 0) {
3160
+ nx /= len;
3161
+ ny /= len;
3162
+ nz /= len;
3163
+ }
3164
+
3165
+ nbuf[i ] = nx;
3166
+ nbuf[i + 1] = ny;
3167
+ nbuf[i + 2] = nz;
3168
+ }
3169
+ };
3170
+
3171
+ JSC3D.Mesh.prototype.checkValid = function() {
3172
+ //TODO: not implemented yet
3173
+ };
3174
+
3175
+ JSC3D.Mesh.prototype.name = '';
3176
+ JSC3D.Mesh.prototype.metadata = '';
3177
+ JSC3D.Mesh.prototype.visible = false;
3178
+ JSC3D.Mesh.prototype.aabb = null;
3179
+ JSC3D.Mesh.prototype.vertexBuffer = null;
3180
+ JSC3D.Mesh.prototype.indexBuffer = null;
3181
+ JSC3D.Mesh.prototype.vertexNormalBuffer = null;
3182
+ JSC3D.Mesh.prototype.faceNormalBuffer = null;
3183
+ JSC3D.Mesh.prototype.texCoordBuffer = null;
3184
+ JSC3D.Mesh.prototype.texCoordIndexBuffer = null;
3185
+ JSC3D.Mesh.prototype.material = null;
3186
+ JSC3D.Mesh.prototype.texture = null;
3187
+ JSC3D.Mesh.prototype.faceCount = 0;
3188
+ JSC3D.Mesh.prototype.isDoubleSided = false;
3189
+ JSC3D.Mesh.prototype.isEnvironmentCast = false;
3190
+ JSC3D.Mesh.prototype.internalId = 0;
3191
+ JSC3D.Mesh.prototype.transformedVertexBuffer = null;
3192
+ JSC3D.Mesh.prototype.transformedVertexNormalZBuffer = null;
3193
+ JSC3D.Mesh.prototype.transformedFaceNormalZBuffer = null;
3194
+ JSC3D.Mesh.prototype.transformedVertexNormalBuffer = null;
3195
+
3196
+
3197
+ /**
3198
+ @class Material
3199
+
3200
+ This class implements material which describes the feel and look of a mesh.
3201
+ */
3202
+ JSC3D.Material = function(name, ambientColor, diffuseColor, transparency, simulateSpecular) {
3203
+ this.name = name || '';
3204
+ this.ambientColor = ambientColor || 0;
3205
+ this.diffuseColor = diffuseColor || 0x7f7f7f;
3206
+ this.transparency = transparency || 0;
3207
+ this.simulateSpecular = simulateSpecular || false;
3208
+ this.palette = null;
3209
+ };
3210
+
3211
+ /**
3212
+ Get the palette of the material used for shadings.
3213
+ @return {Array} palette of the material as an array.
3214
+ */
3215
+ JSC3D.Material.prototype.getPalette = function() {
3216
+ if(!this.palette) {
3217
+ this.palette = new Array(256);
3218
+ this.generatePalette();
3219
+ }
3220
+
3221
+ return this.palette;
3222
+ };
3223
+
3224
+ /**
3225
+ @private
3226
+ */
3227
+ JSC3D.Material.prototype.generatePalette = function() {
3228
+ var ambientR = (this.ambientColor & 0xff0000) >> 16;
3229
+ var ambientG = (this.ambientColor & 0xff00) >> 8;
3230
+ var ambientB = this.ambientColor & 0xff;
3231
+ var diffuseR = (this.diffuseColor & 0xff0000) >> 16;
3232
+ var diffuseG = (this.diffuseColor & 0xff00) >> 8;
3233
+ var diffuseB = this.diffuseColor & 0xff;
3234
+
3235
+ if(this.simulateSpecular) {
3236
+ var i = 0;
3237
+ while(i < 204) {
3238
+ var r = ambientR + i * diffuseR / 204;
3239
+ var g = ambientG + i * diffuseG / 204;
3240
+ var b = ambientB + i * diffuseB / 204;
3241
+ if(r > 255)
3242
+ r = 255;
3243
+ if(g > 255)
3244
+ g = 255;
3245
+ if(b > 255)
3246
+ b = 255;
3247
+
3248
+ this.palette[i++] = r << 16 | g << 8 | b;
3249
+ }
3250
+
3251
+ while(i < 256) {
3252
+ var r = ambientR + diffuseR + (i - 204) * (255 - diffuseR) / 82;
3253
+ var g = ambientG + diffuseG + (i - 204) * (255 - diffuseG) / 82;
3254
+ var b = ambientB + diffuseB + (i - 204) * (255 - diffuseB) / 82;
3255
+ if(r > 255)
3256
+ r = 255;
3257
+ if(g > 255)
3258
+ g = 255;
3259
+ if(b > 255)
3260
+ b = 255;
3261
+
3262
+ this.palette[i++] = r << 16 | g << 8 | b;
3263
+ }
3264
+ }
3265
+ else {
3266
+ var i = 0;
3267
+ while(i < 256) {
3268
+ var r = ambientR + i * diffuseR / 256;
3269
+ var g = ambientG + i * diffuseG / 256;
3270
+ var b = ambientB + i * diffuseB / 256;
3271
+ if(r > 255)
3272
+ r = 255;
3273
+ if(g > 255)
3274
+ g = 255;
3275
+ if(b > 255)
3276
+ b = 255;
3277
+
3278
+ this.palette[i++] = r << 16 | g << 8 | b;
3279
+ }
3280
+ }
3281
+ };
3282
+
3283
+ JSC3D.Material.prototype.name = '';
3284
+ JSC3D.Material.prototype.ambientColor = 0;
3285
+ JSC3D.Material.prototype.diffuseColor = 0x7f7f7f;
3286
+ JSC3D.Material.prototype.transparency = 0;
3287
+ JSC3D.Material.prototype.simulateSpecular = false;
3288
+ JSC3D.Material.prototype.palette = null;
3289
+
3290
+
3291
+ /**
3292
+ @class Texture
3293
+
3294
+ This class implements texture which describes the surface details for a mesh.
3295
+ */
3296
+ JSC3D.Texture = function(name, onready) {
3297
+ this.name = name || '';
3298
+ this.width = 0;
3299
+ this.height = 0;
3300
+ this.data = null;
3301
+ this.mipmaps = null;
3302
+ this.mipentries = null;
3303
+ this.hasTransparency = false;
3304
+ this.srcUrl = '';
3305
+ this.onready = (onready && typeof(onready) == 'function') ? onready : null;
3306
+ };
3307
+
3308
+ /**
3309
+ Load an image and extract texture data from it.
3310
+ @param {string} imageUrl where to load the image.
3311
+ @param {boolean} useMipmap set true to generate mip-maps; false(default) not to generate mip-maps.
3312
+ */
3313
+ JSC3D.Texture.prototype.createFromUrl = function(imageUrl, useMipmap) {
3314
+ var self = this;
3315
+ var img = new Image;
3316
+
3317
+ img.onload = function() {
3318
+ self.data = null;
3319
+ self.mipmaps = null;
3320
+ self.mipentries = null;
3321
+ self.width = 0;
3322
+ self.height = 0;
3323
+ self.hasTransparency = false;
3324
+ self.srcUrl = '';
3325
+ self.createFromImage(this, useMipmap);
3326
+ };
3327
+
3328
+ img.onerror = function() {
3329
+ self.data = null;
3330
+ self.mipmaps = null;
3331
+ self.mipentries = null;
3332
+ self.width = 0;
3333
+ self.height = 0;
3334
+ self.hasTransparency = false;
3335
+ self.srcUrl = '';
3336
+ };
3337
+
3338
+ img.src = imageUrl;
3339
+ };
3340
+
3341
+ /**
3342
+ Extract texture data from an exsisting image.
3343
+ @param {Image} image image as datasource of the texture.
3344
+ @param {boolean} useMipmap set true to generate mip-maps; false(default) not to generate mip-maps.
3345
+ */
3346
+ JSC3D.Texture.prototype.createFromImage = function(image, useMipmap) {
3347
+ if(image.width <=0 || image.height <=0)
3348
+ return;
3349
+
3350
+ if(JSC3D.Texture.cv_is_locked) {
3351
+ var self = this;
3352
+ setTimeout( function() {
3353
+ self.createFromImage(image);
3354
+ }, JSC3D.Texture.cv_lock_cycle );
3355
+
3356
+ return;
3357
+ }
3358
+
3359
+ var cv = JSC3D.Texture.cv;
3360
+ if(!cv) {
3361
+ try {
3362
+ cv = document.createElement('canvas');
3363
+ JSC3D.Texture.cv = cv;
3364
+ }
3365
+ catch(e) {
3366
+ return;
3367
+ }
3368
+ }
3369
+
3370
+ var dim = image.width > image.height ? image.width : image.height;
3371
+ if(dim <= 32)
3372
+ dim = 32;
3373
+ else if(dim <= 64)
3374
+ dim = 64;
3375
+ else if(dim <= 128)
3376
+ dim = 128;
3377
+ //else if(dim <= 256)
3378
+ // dim = 256;
3379
+ //else
3380
+ // dim = 512;
3381
+ else
3382
+ dim = 256;
3383
+
3384
+ try {
3385
+ JSC3D.Texture.cv_is_locked = true;
3386
+
3387
+ var self = this;
3388
+
3389
+ if(cv.width != dim || cv.height != dim)
3390
+ cv.width = cv.height = dim;
3391
+
3392
+ setTimeout( function() {
3393
+ var ctx = cv.getContext('2d');
3394
+ ctx.drawImage(image, 0, 0, dim, dim);
3395
+
3396
+ setTimeout( function() {
3397
+ var imgData = ctx.getImageData(0, 0, dim, dim);
3398
+ var data = imgData.data;
3399
+
3400
+ var size = data.length / 4;
3401
+ self.data = new Array(size);
3402
+ var alpha;
3403
+ for(var i=0, j=0; i<size; i++, j+=4) {
3404
+ alpha = data[j + 3];
3405
+ self.data[i] = alpha << 24 | data[j] << 16 | data[j+1] << 8 | data[j+2];
3406
+ if(alpha < 250)
3407
+ self.hasTransparency = true;
3408
+ }
3409
+
3410
+ self.width = dim;
3411
+ self.height = dim;
3412
+
3413
+ cv.width = 1;
3414
+ cv.height = 1;
3415
+
3416
+ JSC3D.Texture.cv_is_locked = false;
3417
+
3418
+ self.mipmaps = null;
3419
+ if(useMipmap)
3420
+ self.generateMipmaps();
3421
+
3422
+ self.srcUrl = image.src;
3423
+
3424
+ if(self.onready != null && (typeof self.onready) == 'function')
3425
+ self.onready();
3426
+
3427
+ }, JSC3D.Texture.cv_lock_cycle );
3428
+
3429
+ }, JSC3D.Texture.cv_lock_cycle );
3430
+ }
3431
+ catch(e) {
3432
+ JSC3D.Texture.cv_is_locked = false;
3433
+ return;
3434
+ }
3435
+ };
3436
+
3437
+ /**
3438
+ See if this texture contains texel data.
3439
+ @returns {boolean} true if it has texel data; false if not.
3440
+ */
3441
+ JSC3D.Texture.prototype.hasData = function() {
3442
+ return (this.data != null);
3443
+ };
3444
+
3445
+ /**
3446
+ Generate mip-maps for the texture.
3447
+ */
3448
+ JSC3D.Texture.prototype.generateMipmaps = function() {
3449
+ if(this.width <= 1 || this.data == null || this.mipmaps != null)
3450
+ return;
3451
+
3452
+ this.mipmaps = [this.data];
3453
+ this.mipentries = [1];
3454
+
3455
+ var numOfMipLevels = 1 + ~~(0.1 + Math.log(this.width) * Math.LOG2E);
3456
+ var dim = this.width >> 1;
3457
+ for(var level=1; level<numOfMipLevels; level++) {
3458
+ var map = new Array(dim * dim);
3459
+ var uppermap = this.mipmaps[level - 1];
3460
+ var upperdim = dim << 1;
3461
+
3462
+ var src = 0, dest = 0;
3463
+ for(var i=0; i<dim; i++) {
3464
+ for(var j=0; j<dim; j++) {
3465
+ var texel0 = uppermap[src];
3466
+ var texel1 = uppermap[src + 1];
3467
+ var texel2 = uppermap[src + upperdim];
3468
+ var texel3 = uppermap[src + upperdim + 1];
3469
+ var a = ( ((texel0 & 0xff000000) >>> 2) + ((texel1 & 0xff000000) >>> 2) + ((texel2 & 0xff000000) >>> 2) + ((texel3 & 0xff000000) >>> 2) ) & 0xff000000;
3470
+ var r = ( ((texel0 & 0xff0000) + (texel1 & 0xff0000) + (texel2 & 0xff0000) + (texel3 & 0xff0000)) >> 2 ) & 0xff0000;
3471
+ var g = ( ((texel0 & 0xff00) + (texel1 & 0xff00) + (texel2 & 0xff00) + (texel3 & 0xff00)) >> 2 ) & 0xff00;
3472
+ var b = ( ((texel0 & 0xff) + (texel1 & 0xff) + (texel2 & 0xff) + (texel3 & 0xff)) >> 2 ) & 0xff;
3473
+ map[dest] = a + r + g + b;
3474
+ src += 2;
3475
+ dest++;
3476
+ }
3477
+ src += upperdim;
3478
+ }
3479
+
3480
+ this.mipmaps.push(map);
3481
+ this.mipentries.push(Math.pow(4, level));
3482
+ dim = dim >> 1;
3483
+ }
3484
+ };
3485
+
3486
+ /**
3487
+ See if this texture has mip-maps.
3488
+ @returns {boolean} true if it has mip-maps; false if not.
3489
+ */
3490
+ JSC3D.Texture.prototype.hasMipmap = function() {
3491
+ return (this.mipmaps != null);
3492
+ };
3493
+
3494
+ JSC3D.Texture.prototype.name = '';
3495
+ JSC3D.Texture.prototype.data = null;
3496
+ JSC3D.Texture.prototype.mipmaps = null;
3497
+ JSC3D.Texture.prototype.mipentries = null;
3498
+ JSC3D.Texture.prototype.width = 0;
3499
+ JSC3D.Texture.prototype.height = 0;
3500
+ JSC3D.Texture.prototype.hasTransparency = false;
3501
+ JSC3D.Texture.prototype.srcUrl = '';
3502
+ JSC3D.Texture.prototype.onready = null;
3503
+ JSC3D.Texture.cv = null;
3504
+ JSC3D.Texture.cv_is_locked = false;
3505
+ JSC3D.Texture.cv_lock_cycle = 100; // milliseconds
3506
+
3507
+
3508
+ /**
3509
+ @class AABB
3510
+
3511
+ This class implements the Axis-Aligned Bounding Box to measure spacial enclosure.
3512
+ */
3513
+ JSC3D.AABB = function() {
3514
+ this.minX = this.maxX = 0;
3515
+ this.minY = this.maxY = 0;
3516
+ this.minZ = this.maxZ = 0;
3517
+ };
3518
+
3519
+ /**
3520
+ Get the center coordinates of the AABB.
3521
+ @returns {Array} center coordinates as an array.
3522
+ */
3523
+ JSC3D.AABB.prototype.center = function() {
3524
+ return [(this.minX + this.maxX) / 2, (this.minY + this.maxY) / 2, (this.minZ + this.maxZ) / 2];
3525
+ };
3526
+
3527
+ /**
3528
+ Get the length of the diagonal of the AABB.
3529
+ @returns {float} length of the diagonal.
3530
+ */
3531
+ JSC3D.AABB.prototype.lengthOfDiagonal = function() {
3532
+ var xx = this.maxX - this.minX;
3533
+ var yy = this.maxY - this.minY;
3534
+ var zz = this.maxZ - this.minZ;
3535
+ return Math.sqrt(xx * xx + yy * yy + zz * zz);
3536
+ };
3537
+
3538
+
3539
+ /**
3540
+ @class Matrix3x4
3541
+
3542
+ This class implements 3x4 matrix and mass operations for 3D transformations.
3543
+ */
3544
+ JSC3D.Matrix3x4 = function() {
3545
+ this.m00 = 1; this.m01 = 0; this.m02 = 0; this.m03 = 0;
3546
+ this.m10 = 0; this.m11 = 1; this.m12 = 0; this.m13 = 0;
3547
+ this.m20 = 0; this.m21 = 0; this.m22 = 1; this.m23 = 0;
3548
+ };
3549
+
3550
+ /**
3551
+ Make the matrix an identical matrix.
3552
+ */
3553
+ JSC3D.Matrix3x4.prototype.identity = function() {
3554
+ this.m00 = 1; this.m01 = 0; this.m02 = 0; this.m03 = 0;
3555
+ this.m10 = 0; this.m11 = 1; this.m12 = 0; this.m13 = 0;
3556
+ this.m20 = 0; this.m21 = 0; this.m22 = 1; this.m23 = 0;
3557
+ };
3558
+
3559
+ /**
3560
+ Scale the matrix using scaling factors on each axial directions.
3561
+ @param {float} sx scaling factors on x-axis.
3562
+ @param {float} sy scaling factors on y-axis.
3563
+ @param {float} sz scaling factors on z-axis.
3564
+ */
3565
+ JSC3D.Matrix3x4.prototype.scale = function(sx, sy, sz) {
3566
+ this.m00 *= sx; this.m01 *= sx; this.m02 *= sx; this.m03 *= sx;
3567
+ this.m10 *= sy; this.m11 *= sy; this.m12 *= sy; this.m13 *= sy;
3568
+ this.m20 *= sz; this.m21 *= sz; this.m22 *= sz; this.m23 *= sz;
3569
+ };
3570
+
3571
+ /**
3572
+ Translate the matrix using translations on each axial directions.
3573
+ @param {float} tx translations on x-axis.
3574
+ @param {float} ty translations on y-axis.
3575
+ @param {float} tz translations on z-axis.
3576
+ */
3577
+ JSC3D.Matrix3x4.prototype.translate = function(tx, ty, tz) {
3578
+ this.m03 += tx;
3579
+ this.m13 += ty;
3580
+ this.m23 += tz;
3581
+ };
3582
+
3583
+ /**
3584
+ Rotate the matrix an arbitrary angle about the x-axis.
3585
+ @param {float} angle rotation angle in degrees.
3586
+ */
3587
+ JSC3D.Matrix3x4.prototype.rotateAboutXAxis = function(angle) {
3588
+ if(angle != 0) {
3589
+ angle *= Math.PI / 180;
3590
+ var cosA = Math.cos(angle);
3591
+ var sinA = Math.sin(angle);
3592
+
3593
+ var m10 = cosA * this.m10 - sinA * this.m20;
3594
+ var m11 = cosA * this.m11 - sinA * this.m21;
3595
+ var m12 = cosA * this.m12 - sinA * this.m22;
3596
+ var m13 = cosA * this.m13 - sinA * this.m23;
3597
+ var m20 = cosA * this.m20 + sinA * this.m10;
3598
+ var m21 = cosA * this.m21 + sinA * this.m11;
3599
+ var m22 = cosA * this.m22 + sinA * this.m12;
3600
+ var m23 = cosA * this.m23 + sinA * this.m13;
3601
+
3602
+ this.m10 = m10; this.m11 = m11; this.m12 = m12; this.m13 = m13;
3603
+ this.m20 = m20; this.m21 = m21; this.m22 = m22; this.m23 = m23;
3604
+ }
3605
+ };
3606
+
3607
+ /**
3608
+ Rotate the matrix an arbitrary angle about the y-axis.
3609
+ @param {float} angle rotation angle in degrees.
3610
+ */
3611
+ JSC3D.Matrix3x4.prototype.rotateAboutYAxis = function(angle) {
3612
+ if(angle != 0) {
3613
+ angle *= Math.PI / 180;
3614
+ var cosA = Math.cos(angle);
3615
+ var sinA = Math.sin(angle);
3616
+
3617
+ var m00 = cosA * this.m00 + sinA * this.m20;
3618
+ var m01 = cosA * this.m01 + sinA * this.m21;
3619
+ var m02 = cosA * this.m02 + sinA * this.m22;
3620
+ var m03 = cosA * this.m03 + sinA * this.m23;
3621
+ var m20 = cosA * this.m20 - sinA * this.m00;
3622
+ var m21 = cosA * this.m21 - sinA * this.m01;
3623
+ var m22 = cosA * this.m22 - sinA * this.m02;
3624
+ var m23 = cosA * this.m23 - sinA * this.m03;
3625
+
3626
+ this.m00 = m00; this.m01 = m01; this.m02 = m02; this.m03 = m03;
3627
+ this.m20 = m20; this.m21 = m21; this.m22 = m22; this.m23 = m23;
3628
+ }
3629
+ };
3630
+
3631
+ /**
3632
+ Rotate the matrix an arbitrary angle about the z-axis.
3633
+ @param {float} angle rotation angle in degrees.
3634
+ */
3635
+ JSC3D.Matrix3x4.prototype.rotateAboutZAxis = function(angle) {
3636
+ if(angle != 0) {
3637
+ angle *= Math.PI / 180;
3638
+ var cosA = Math.cos(angle);
3639
+ var sinA = Math.sin(angle);
3640
+
3641
+ var m10 = cosA * this.m10 + sinA * this.m00;
3642
+ var m11 = cosA * this.m11 + sinA * this.m01;
3643
+ var m12 = cosA * this.m12 + sinA * this.m02;
3644
+ var m13 = cosA * this.m13 + sinA * this.m03;
3645
+ var m00 = cosA * this.m00 - sinA * this.m10;
3646
+ var m01 = cosA * this.m01 - sinA * this.m11;
3647
+ var m02 = cosA * this.m02 - sinA * this.m12;
3648
+ var m03 = cosA * this.m03 - sinA * this.m13;
3649
+
3650
+ this.m00 = m00; this.m01 = m01; this.m02 = m02; this.m03 = m03;
3651
+ this.m10 = m10; this.m11 = m11; this.m12 = m12; this.m13 = m13;
3652
+ }
3653
+ };
3654
+
3655
+ /**
3656
+ Multiply the matrix by another matrix.
3657
+ @param {JSC3D.Matrix3x4} mult another matrix to be multiplied on this.
3658
+ */
3659
+ JSC3D.Matrix3x4.prototype.multiply = function(mult) {
3660
+ var m00 = mult.m00 * this.m00 + mult.m01 * this.m10 + mult.m02 * this.m20;
3661
+ var m01 = mult.m00 * this.m01 + mult.m01 * this.m11 + mult.m02 * this.m21;
3662
+ var m02 = mult.m00 * this.m02 + mult.m01 * this.m12 + mult.m02 * this.m22;
3663
+ var m03 = mult.m00 * this.m03 + mult.m01 * this.m13 + mult.m02 * this.m23 + mult.m03;
3664
+ var m10 = mult.m10 * this.m00 + mult.m11 * this.m10 + mult.m12 * this.m20;
3665
+ var m11 = mult.m10 * this.m01 + mult.m11 * this.m11 + mult.m12 * this.m21;
3666
+ var m12 = mult.m10 * this.m02 + mult.m11 * this.m12 + mult.m12 * this.m22;
3667
+ var m13 = mult.m10 * this.m03 + mult.m11 * this.m13 + mult.m12 * this.m23 + mult.m13;
3668
+ var m20 = mult.m20 * this.m00 + mult.m21 * this.m10 + mult.m22 * this.m20;
3669
+ var m21 = mult.m20 * this.m01 + mult.m21 * this.m11 + mult.m22 * this.m21;
3670
+ var m22 = mult.m20 * this.m02 + mult.m21 * this.m12 + mult.m22 * this.m22;
3671
+ var m23 = mult.m20 * this.m03 + mult.m21 * this.m13 + mult.m22 * this.m23 + mult.m23;
3672
+
3673
+ this.m00 = m00; this.m01 = m01; this.m02 = m02; this.m03 = m03;
3674
+ this.m10 = m10; this.m11 = m11; this.m12 = m12; this.m13 = m13;
3675
+ this.m20 = m20; this.m21 = m21; this.m22 = m22; this.m23 = m23;
3676
+ };
3677
+
3678
+
3679
+ /**
3680
+ @class Math3D
3681
+
3682
+ This class provides some utility methods for 3D mathematics.
3683
+ */
3684
+ JSC3D.Math3D = {
3685
+
3686
+ /**
3687
+ Transform vectors using the given matrix.
3688
+ @param {JSC3D.Matrix3x4} mat the transformation matrix.
3689
+ @param {Array} vecs a batch of vectors to be transform.
3690
+ @param {Array} xfvecs holds the transformed vetors.
3691
+ */
3692
+ transformVectors: function(mat, vecs, xfvecs) {
3693
+ for(var i=0; i<vecs.length; i+=3) {
3694
+ var x = vecs[i];
3695
+ var y = vecs[i + 1];
3696
+ var z = vecs[i + 2];
3697
+ xfvecs[i] = mat.m00 * x + mat.m01 * y + mat.m02 * z + mat.m03;
3698
+ xfvecs[i + 1] = mat.m10 * x + mat.m11 * y + mat.m12 * z + mat.m13;
3699
+ xfvecs[i + 2] = mat.m20 * x + mat.m21 * y + mat.m22 * z + mat.m23;
3700
+ }
3701
+ },
3702
+
3703
+ /**
3704
+ Transform vectors' z components using the given matrix.
3705
+ @param {JSC3D.Matrix3x4} mat the transformation matrix.
3706
+ @param {Array} vecs a batch of vectors to be transform.
3707
+ @param {Array} xfveczs holds the transformed z components of the input vectors.
3708
+ */
3709
+ transformVectorZs: function(mat, vecs, xfveczs) {
3710
+ var num = vecs.length / 3;
3711
+ var i = 0, j = 0
3712
+ while(i < num) {
3713
+ xfveczs[i] = mat.m20 * vecs[j] + mat.m21 * vecs[j + 1] + mat.m22 * vecs[j + 2] + mat.m23;
3714
+ i++;
3715
+ j += 3;
3716
+ }
3717
+ }
3718
+ };
3719
+
3720
+
3721
+ /**
3722
+ @class LoaderSelector
3723
+ */
3724
+ JSC3D.LoaderSelector = {
3725
+
3726
+ /**
3727
+ Register a scene loader for a specific file format, using the file extesion name for lookup.
3728
+ @param {string} fileExtName extension name for the specific file format.
3729
+ @param {Function} loaderCtor constructor of the loader class.
3730
+ */
3731
+ registerLoader: function(fileExtName, loaderCtor) {
3732
+ if((typeof loaderCtor) == 'function') {
3733
+ JSC3D.LoaderSelector.loaderTable[fileExtName] = loaderCtor;
3734
+ }
3735
+ },
3736
+
3737
+ /**
3738
+ Get the proper loader for a target file format using the file extension name.
3739
+ @param {string} fileExtName file extension name for the specific format.
3740
+ @returns {object} loader object for the specific format; null if not found.
3741
+ */
3742
+ getLoader: function(fileExtName) {
3743
+ var loaderCtor = JSC3D.LoaderSelector.loaderTable[fileExtName.toLowerCase()];
3744
+ if(!loaderCtor)
3745
+ return null;
3746
+
3747
+ var loaderInst;
3748
+ try {
3749
+ loaderInst = new loaderCtor();
3750
+ }
3751
+ catch(e) {
3752
+ loaderInst = null;
3753
+ }
3754
+
3755
+ return loaderInst;
3756
+ },
3757
+
3758
+ loaderTable: {}
3759
+ };
3760
+
3761
+
3762
+ /**
3763
+ @class ObjLoader
3764
+
3765
+ This class implements a scene loader from a wavefront obj file.
3766
+ */
3767
+ JSC3D.ObjLoader = function(onload, onerror, onprogress, onresource) {
3768
+ this.onload = (onload && typeof(onload) == 'function') ? onload : null;
3769
+ this.onerror = (onerror && typeof(onerror) == 'function') ? onerror : null;
3770
+ this.onprogress = (onprogress && typeof(onprogress) == 'function') ? onprogress : null;
3771
+ this.onresource = (onresource && typeof(onresource) == 'function') ? onresource : null;
3772
+ this.requestCount = 0;
3773
+ };
3774
+
3775
+ /**
3776
+ Load scene from a given obj file.
3777
+ @param {string} urlName a string that specifies where to fetch the obj file.
3778
+ */
3779
+ JSC3D.ObjLoader.prototype.loadFromUrl = function(urlName) {
3780
+ var urlPath = '';
3781
+ var fileName = urlName;
3782
+
3783
+ var lastSlashAt = urlName.lastIndexOf('/');
3784
+ if(lastSlashAt == -1)
3785
+ lastSlashAt = urlName.lastIndexOf('\\');
3786
+ if(lastSlashAt != -1) {
3787
+ urlPath = urlName.substring(0, lastSlashAt+1);
3788
+ fileName = urlName.substring(lastSlashAt+1);
3789
+ }
3790
+
3791
+ this.requestCount = 0;
3792
+ this.loadObjFile(urlPath, fileName);
3793
+ };
3794
+
3795
+ /**
3796
+ Load scene from the obj file using the given url path and file name.
3797
+ @private
3798
+ */
3799
+ JSC3D.ObjLoader.prototype.loadObjFile = function(urlPath, fileName) {
3800
+ var urlName = urlPath + fileName;
3801
+ var self = this;
3802
+ var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('MSXML2.XMLHTTP.3.0');
3803
+ xhr.open('GET', urlName, true);
3804
+
3805
+ xhr.onreadystatechange = function() {
3806
+ if(this.readyState == 4) {
3807
+ if(this.status == 200 || this.status == 0) {
3808
+ if(self.onload) {
3809
+ if(self.onprogress)
3810
+ self.onprogress('Loading obj file ...', 1);
3811
+ var scene = new JSC3D.Scene;
3812
+ var mtllibs = self.parseObj(scene, this.responseText);
3813
+ if(mtllibs.length > 0) {
3814
+ for(var i=0; i<mtllibs.length; i++)
3815
+ self.loadMtlFile(scene, urlPath, mtllibs[i]);
3816
+ }
3817
+ if(--self.requestCount == 0)
3818
+ self.onload(scene);
3819
+ }
3820
+ }
3821
+ else if(self.onerror) {
3822
+ self.requestCount--;
3823
+ self.onerror('Failed to load obj file \'' + urlName + '\'.');
3824
+ }
3825
+ }
3826
+ };
3827
+
3828
+ if(this.onprogress && xhr.onprogress) {
3829
+ this.onprogress('Loading obj file ...', 0);
3830
+ xhr.onprogress = function(event) {
3831
+ self.onprogress('Loading obj file ...', event.position / event.totalSize);
3832
+ };
3833
+ }
3834
+
3835
+ this.requestCount++;
3836
+ xhr.send();
3837
+ };
3838
+
3839
+ /**
3840
+ Load materials and textures from an mtl file and set them to corresponding meshes.
3841
+ @private
3842
+ */
3843
+ JSC3D.ObjLoader.prototype.loadMtlFile = function(scene, urlPath, fileName) {
3844
+ var urlName = urlPath + fileName;
3845
+ var self = this;
3846
+ var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('MSXML2.XMLHTTP.3.0');
3847
+ xhr.open('GET', urlName, true);
3848
+
3849
+ xhr.onreadystatechange = function() {
3850
+ if(this.readyState == 4) {
3851
+ if(this.status == 200 || this.status == 0) {
3852
+ if(self.onprogress)
3853
+ self.onprogress('Loading mtl file ...', 1);
3854
+ var mtls = self.parseMtl(this.responseText);
3855
+ var textures = {};
3856
+ var meshes = scene.getChildren();
3857
+ for(var i=0; i<meshes.length; i++) {
3858
+ var mesh = meshes[i];
3859
+ if(mesh.mtl != null && mesh.mtllib != null && mesh.mtllib == fileName) {
3860
+ var mtl = mtls[mesh.mtl];
3861
+ if(mtl != null) {
3862
+ if(mtl.material != null)
3863
+ mesh.setMaterial(mtl.material);
3864
+ if(mtl.textureFileName != '') {
3865
+ if(!textures[mtl.textureFileName])
3866
+ textures[mtl.textureFileName] = [mesh];
3867
+ else
3868
+ textures[mtl.textureFileName].push(mesh);
3869
+ }
3870
+ }
3871
+ }
3872
+ }
3873
+ for(var textureFileName in textures)
3874
+ self.setupTexture(textures[textureFileName], urlPath + textureFileName);
3875
+ }
3876
+ else {
3877
+ //TODO: when failed to load an mtl file ...
3878
+ }
3879
+ if(--self.requestCount == 0)
3880
+ self.onload(scene);
3881
+ }
3882
+ };
3883
+
3884
+ if(this.onprogress && xhr.onprogress) {
3885
+ this.onprogress('Loading mtl file ...', 0);
3886
+ xhr.onprogress = function(event) {
3887
+ self.onprogress('Loading mtl file ...', event.position / event.totalSize);
3888
+ };
3889
+ }
3890
+
3891
+ this.requestCount++;
3892
+ xhr.send();
3893
+ };
3894
+
3895
+ /**
3896
+ Parse contents of the obj file, generating the scene and returning all required mtllibs.
3897
+ @private
3898
+ */
3899
+ JSC3D.ObjLoader.prototype.parseObj = function(scene, data) {
3900
+ var meshes = {};
3901
+ var mtllibs = [];
3902
+ var namePrefix = 'obj-';
3903
+ var meshIndex = 0;
3904
+ var curMesh = null;
3905
+ var curMtllibName = '';
3906
+ var curMtlName = '';
3907
+
3908
+ var tempVertexBuffer = []; // temporary buffer as container for all vertices
3909
+ var tempTexCoordBuffer = []; // temporary buffer as container for all vertex texture coords
3910
+
3911
+ // create a default mesh to hold all faces that are not associated with any mtl.
3912
+ var defaultMeshName = namePrefix + meshIndex++;
3913
+ var defaultMesh = new JSC3D.Mesh;
3914
+ defaultMesh.name = defaultMeshName;
3915
+ defaultMesh.indexBuffer = [];
3916
+ meshes['nomtl'] = defaultMesh;
3917
+ curMesh = defaultMesh;
3918
+
3919
+ var lines = data.split(/[ \t]*\r?\n[ \t]*/);
3920
+ for(var i=0; i<lines.length; i++) {
3921
+ var line = lines[i];
3922
+ var tokens = line.split(/[ \t]+/);
3923
+ if(tokens.length > 0) {
3924
+ var keyword = tokens[0];
3925
+ switch(keyword) {
3926
+ case 'v':
3927
+ if(tokens.length > 3) {
3928
+ for(var j=1; j<4; j++) {
3929
+ tempVertexBuffer.push( parseFloat(tokens[j]) );
3930
+ }
3931
+ }
3932
+ break;
3933
+ case 'vn':
3934
+ // ignore vertex normals
3935
+ break;
3936
+ case 'vt':
3937
+ if(tokens.length > 2) {
3938
+ tempTexCoordBuffer.push( parseFloat(tokens[1]) );
3939
+ tempTexCoordBuffer.push( 1 - parseFloat(tokens[2]) );
3940
+ }
3941
+ break;
3942
+ case 'f':
3943
+ if(tokens.length > 3) {
3944
+ for(var j=1; j<tokens.length; j++) {
3945
+ var refs = tokens[j].split('/');
3946
+ curMesh.indexBuffer.push( parseInt(refs[0]) - 1 );
3947
+ if(refs.length > 1 && refs[1] != '') {
3948
+ if(!curMesh.texCoordIndexBuffer)
3949
+ curMesh.texCoordIndexBuffer = [];
3950
+ curMesh.texCoordIndexBuffer.push( parseInt(refs[1]) - 1 );
3951
+ }
3952
+ }
3953
+ curMesh.indexBuffer.push(-1); // mark the end of vertex index sequence for the face
3954
+ if(curMesh.texCoordIndexBuffer)
3955
+ curMesh.texCoordIndexBuffer.push(-1); // mark the end of vertex tex coord index sequence for the face
3956
+ }
3957
+ break;
3958
+ case 'mtllib':
3959
+ if(tokens.length > 1) {
3960
+ curMtllibName = tokens[1];
3961
+ mtllibs.push(curMtllibName);
3962
+ }
3963
+ else
3964
+ curMtllibName = '';
3965
+ break;
3966
+ case 'usemtl':
3967
+ if(tokens.length > 1 && tokens[1] != '' && curMtllibName != '') {
3968
+ curMtlName = tokens[1];
3969
+ var meshid = curMtllibName + '-' + curMtlName;
3970
+ var mesh = meshes[meshid];
3971
+ if(!mesh) {
3972
+ // create a new mesh to hold faces using the same mtl
3973
+ mesh = new JSC3D.Mesh;
3974
+ mesh.name = namePrefix + meshIndex++;
3975
+ mesh.indexBuffer = [];
3976
+ mesh.mtllib = curMtllibName;
3977
+ mesh.mtl = curMtlName;
3978
+ meshes[meshid] = mesh;
3979
+ }
3980
+ curMesh = mesh;
3981
+ }
3982
+ else {
3983
+ curMtlName = '';
3984
+ curMesh = defaultMesh;
3985
+ }
3986
+ break;
3987
+ case '#':
3988
+ // ignore comments
3989
+ default:
3990
+ break;
3991
+ }
3992
+ }
3993
+ }
3994
+
3995
+ var viBuffer = tempVertexBuffer.length >= 3 ? (new Array(tempVertexBuffer.length / 3)) : null;
3996
+ var tiBuffer = tempTexCoordBuffer.length >= 2 ? (new Array(tempTexCoordBuffer.length / 2)) : null;
3997
+
3998
+ for(var id in meshes) {
3999
+ var mesh = meshes[id];
4000
+
4001
+ // split vertices into the mesh, the indices are also re-calculated
4002
+ if(tempVertexBuffer.length >= 3 && mesh.indexBuffer.length > 0) {
4003
+ for(var i=0; i<viBuffer.length; i++)
4004
+ viBuffer[i] = -1;
4005
+
4006
+ mesh.vertexBuffer = [];
4007
+ var oldVI = 0, newVI = 0;
4008
+ for(var i=0; i<mesh.indexBuffer.length; i++) {
4009
+ oldVI = mesh.indexBuffer[i];
4010
+ if(oldVI != -1) {
4011
+ if(viBuffer[oldVI] == -1) {
4012
+ var v = oldVI * 3;
4013
+ mesh.vertexBuffer.push(tempVertexBuffer[v]);
4014
+ mesh.vertexBuffer.push(tempVertexBuffer[v + 1]);
4015
+ mesh.vertexBuffer.push(tempVertexBuffer[v + 2]);
4016
+ mesh.indexBuffer[i] = newVI;
4017
+ viBuffer[oldVI] = newVI;
4018
+ newVI++;
4019
+ }
4020
+ else {
4021
+ mesh.indexBuffer[i] = viBuffer[oldVI];
4022
+ }
4023
+ }
4024
+ }
4025
+ }
4026
+
4027
+ // split vertex texture coords into the mesh, the indices for tex coords are re-calculated as well
4028
+ if(tempTexCoordBuffer.length >= 2 && mesh.texCoordIndexBuffer != null && mesh.texCoordIndexBuffer.length > 0) {
4029
+ for(var i=0; i<tiBuffer.length; i++)
4030
+ tiBuffer[i] = -1;
4031
+
4032
+ mesh.texCoordBuffer = [];
4033
+ var oldTI = 0, newTI = 0;
4034
+ for(var i=0; i<mesh.texCoordIndexBuffer.length; i++) {
4035
+ oldTI = mesh.texCoordIndexBuffer[i];
4036
+ if(oldTI != -1) {
4037
+ if(tiBuffer[oldTI] == -1) {
4038
+ var t = oldTI * 2;
4039
+ mesh.texCoordBuffer.push(tempTexCoordBuffer[t]);
4040
+ mesh.texCoordBuffer.push(tempTexCoordBuffer[t + 1]);
4041
+ mesh.texCoordIndexBuffer[i] = newTI;
4042
+ tiBuffer[oldTI] = newTI;
4043
+ newTI++;
4044
+ }
4045
+ else {
4046
+ mesh.texCoordIndexBuffer[i] = tiBuffer[oldTI];
4047
+ }
4048
+ }
4049
+ }
4050
+ }
4051
+
4052
+ // add mesh to scene
4053
+ if(!mesh.isTrivial())
4054
+ scene.addChild(mesh);
4055
+ }
4056
+
4057
+ return mtllibs;
4058
+ };
4059
+
4060
+ /**
4061
+ Parse contents of an mtl file, returning all materials and textures defined in it.
4062
+ @private
4063
+ */
4064
+ JSC3D.ObjLoader.prototype.parseMtl = function(data) {
4065
+ var mtls = {};
4066
+ var curMtlName = '';
4067
+
4068
+ var lines = data.split(/[ \t]*\r?\n[ \t]*/);
4069
+ for(var i=0; i<lines.length; i++) {
4070
+ var line = lines[i];
4071
+ var tokens = line.split(/[ \t]+/);
4072
+ if(tokens.length > 0) {
4073
+ var keyword = tokens[0];
4074
+ switch(keyword) {
4075
+ case 'newmtl':
4076
+ curMtlName = tokens[1];
4077
+ var mtl = {};
4078
+ mtl.material = new JSC3D.Material;
4079
+ mtl.textureFileName = '';
4080
+ mtls[curMtlName] = mtl;
4081
+ break;
4082
+ case 'Ka':
4083
+ /*
4084
+ if(tokens.length == 4 && !isNaN(tokens[1])) {
4085
+ var ambientR = (parseFloat(tokens[1]) * 255) & 0xff;
4086
+ var ambientG = (parseFloat(tokens[2]) * 255) & 0xff;
4087
+ var ambientB = (parseFloat(tokens[3]) * 255) & 0xff;
4088
+ var mtl = mtls[curMtlName];
4089
+ if(mtl != null)
4090
+ mtl.material.ambientColor = (ambientR << 16) | (ambientG << 8) | ambientB;
4091
+ }
4092
+ */
4093
+ break;
4094
+ case 'Kd':
4095
+ if(tokens.length == 4 && !isNaN(tokens[1])) {
4096
+ var diffuseR = (parseFloat(tokens[1]) * 255) & 0xff;
4097
+ var diffuseG = (parseFloat(tokens[2]) * 255) & 0xff;
4098
+ var diffuseB = (parseFloat(tokens[3]) * 255) & 0xff;
4099
+ var mtl = mtls[curMtlName];
4100
+ if(mtl != null)
4101
+ mtl.material.diffuseColor = (diffuseR << 16) | (diffuseG << 8) | diffuseB;
4102
+ }
4103
+ break;
4104
+ case 'Ks':
4105
+ // ignore specular reflectivity definition
4106
+ break;
4107
+ case 'd':
4108
+ if(tokens.length == 2 && !isNaN(tokens[1])) {
4109
+ var opacity = parseFloat(tokens[1]);
4110
+ var mtl = mtls[curMtlName];
4111
+ if(mtl != null)
4112
+ mtl.material.transparency = 1 - opacity;
4113
+ }
4114
+ break;
4115
+ case 'illum':
4116
+ /*
4117
+ if(tokens.length == 2 && tokens[1] == '2') {
4118
+ var mtl = mtls[curMtlName];
4119
+ if(mtl != null)
4120
+ mtl.material.simulateSpecular = true;
4121
+ }
4122
+ */
4123
+ break;
4124
+ case 'map_Kd':
4125
+ if(tokens.length == 2) {
4126
+ var texFileName = tokens[1];
4127
+ var mtl = mtls[curMtlName];
4128
+ if(mtl != null)
4129
+ mtl.textureFileName = texFileName;
4130
+ }
4131
+ break;
4132
+ case '#':
4133
+ // ignore any comments
4134
+ default:
4135
+ break;
4136
+ }
4137
+ }
4138
+ }
4139
+
4140
+ return mtls;
4141
+ };
4142
+
4143
+ /**
4144
+ Asynchronously load a texture from a given url and set it to corresponding meshes when done.
4145
+ @private
4146
+ */
4147
+ JSC3D.ObjLoader.prototype.setupTexture = function(meshList, textureUrlName) {
4148
+ var self = this;
4149
+ var texture = new JSC3D.Texture;
4150
+
4151
+ texture.onready = function() {
4152
+ for(var i=0; i<meshList.length; i++)
4153
+ meshList[i].setTexture(this);
4154
+ if(self.onresource)
4155
+ self.onresource(this);
4156
+ };
4157
+
4158
+ texture.createFromUrl(textureUrlName);
4159
+ };
4160
+
4161
+ JSC3D.ObjLoader.prototype.onload = null;
4162
+ JSC3D.ObjLoader.prototype.onerror = null;
4163
+ JSC3D.ObjLoader.prototype.onprogress = null;
4164
+ JSC3D.ObjLoader.prototype.onresource = null;
4165
+ JSC3D.ObjLoader.prototype.requestCount = 0;
4166
+
4167
+ JSC3D.LoaderSelector.registerLoader('obj', JSC3D.ObjLoader);
4168
+
4169
+
4170
+ /**
4171
+ @class StlLoader
4172
+
4173
+ This class implements a scene loader from an STL file. Both binary and ASCII STL files are supported.
4174
+ */
4175
+ JSC3D.StlLoader = function(onload, onerror, onprogress, onresource) {
4176
+ this.onload = (onload && typeof(onload) == 'function') ? onload : null;
4177
+ this.onerror = (onerror && typeof(onerror) == 'function') ? onerror : null;
4178
+ this.onprogress = (onprogress && typeof(onprogress) == 'function') ? onprogress : null;
4179
+ this.onresource = (onresource && typeof(onresource) == 'function') ? onresource : null;
4180
+ this.decimalPrecision = 3;
4181
+ };
4182
+
4183
+ /**
4184
+ Load scene from a given STL file.
4185
+ @param {string} urlName a string that specifies where to fetch the STL file.
4186
+ */
4187
+ JSC3D.StlLoader.prototype.loadFromUrl = function(urlName) {
4188
+ /*
4189
+ we assume this is an ASCII STL file and load it in that manner.
4190
+ if it is not, we load the same file a second time and treat it as a binary stream.
4191
+ */
4192
+
4193
+ var self = this;
4194
+ var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('MSXML2.XMLHTTP.3.0');
4195
+ xhr.open('GET', urlName, true);
4196
+
4197
+ xhr.onreadystatechange = function() {
4198
+ if(this.readyState == 4) {
4199
+ if(this.status == 200 || this.status == 0) {
4200
+ if(self.onload) {
4201
+ if(self.onprogress)
4202
+ self.onprogress('Loading stl file ...', 1);
4203
+ if(this.responseText.substring(0, 5).toLowerCase() == 'solid') {
4204
+ var scene = new JSC3D.Scene;
4205
+ self.parseStlAscii(scene, this.responseText);
4206
+ self.onload(scene);
4207
+ }
4208
+ else {
4209
+ // this is not an ASCII STL file, reload it as a binary STL file
4210
+ self.loadBinaryFromUrl(urlName);
4211
+ }
4212
+ }
4213
+ }
4214
+ else if(self.onerror) {
4215
+ self.onerror('Failed to load stl file \'' + urlName + '\'.');
4216
+ }
4217
+ }
4218
+ };
4219
+
4220
+ if(this.onprogress && xhr.onprogress) {
4221
+ this.onprogress('Loading stl file ...', 0);
4222
+ xhr.onprogress = function(event) {
4223
+ self.onprogress('Loading stl file ...', event.position / event.totalSize);
4224
+ };
4225
+ }
4226
+
4227
+ xhr.send();
4228
+ };
4229
+
4230
+ /**
4231
+ Load scene from a given binary STL file.
4232
+ @private
4233
+ */
4234
+ JSC3D.StlLoader.prototype.loadBinaryFromUrl = function(urlName) {
4235
+ var self = this;
4236
+ var xhr = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('MSXML2.XMLHTTP.3.0');
4237
+ xhr.open('GET', urlName, true);
4238
+ xhr.setRequestHeader("Accept-Charset", "x-user-defined");
4239
+
4240
+ xhr.onreadystatechange = function() {
4241
+ if(this.readyState == 4) {
4242
+ if(this.status == 200 || this.status == 0) {
4243
+ if(self.onload) {
4244
+ if(self.onprogress)
4245
+ self.onprogress('Loading binary stl file ...', 1);
4246
+ var scene = new JSC3D.Scene;
4247
+ // As javascript cannot access contents of the XHR's responseBody property which hold the raw binary data
4248
+ // returned by the request, we need to extract those data to a javascript array before we can parse them.
4249
+ try {
4250
+ self.parseStlBinary(scene, (new VBArray(this.responseBody)).toArray());
4251
+ } catch(e) {}
4252
+ self.onload(scene);
4253
+ }
4254
+ }
4255
+ else if(self.onerror) {
4256
+ self.onerror('Failed to load binary stl file \'' + urlName + '\'.');
4257
+ }
4258
+ }
4259
+ };
4260
+
4261
+ if(this.onprogress && xhr.onprogress) {
4262
+ this.onprogress('Loading binary stl file ...', 0);
4263
+ xhr.onprogress = function(event) {
4264
+ self.onprogress('Loading binary stl file ...', event.position / event.totalSize);
4265
+ };
4266
+ }
4267
+
4268
+ xhr.send();
4269
+ };
4270
+
4271
+ /**
4272
+ Set decimal precision that defines the threshold to detect and weld vertices that coincide.
4273
+ @param {number} precision the decimal preciison.
4274
+ */
4275
+ JSC3D.StlLoader.prototype.setDecimalPrecision = function(precision) {
4276
+ this.decimalPrecision = precision;
4277
+ };
4278
+
4279
+ /**
4280
+ Parse contents of an ASCII STL file and generate the scene.
4281
+ Submitted by Triffid Hunter.
4282
+ @private
4283
+ */
4284
+ JSC3D.StlLoader.prototype.parseStlAscii = function(scene, data) {
4285
+ var mesh = new JSC3D.Mesh;
4286
+ mesh.vertexBuffer = [];
4287
+ mesh.indexBuffer = [];
4288
+ mesh.faceNormalBuffer = [];
4289
+
4290
+ var facePattern = 'facet\\s+normal\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+' +
4291
+ 'outer\\s+loop\\s+' +
4292
+ 'vertex\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+' +
4293
+ 'vertex\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+' +
4294
+ 'vertex\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+([-+]?\\b(?:[0-9]*\\.)?[0-9]+(?:[eE][-+]?[0-9]+)?\\b)\\s+' +
4295
+ 'endloop\\s+' +
4296
+ 'endfacet';
4297
+ var faceRegExp = new RegExp(facePattern, 'ig');
4298
+ var matches = data.match(faceRegExp);
4299
+
4300
+ if(matches) {
4301
+ var numOfFaces = matches.length;
4302
+
4303
+ mesh.faceCount = numOfFaces;
4304
+ var v2i = {};
4305
+
4306
+ // reset regexp for vertex extraction
4307
+ faceRegExp.lastIndex = 0;
4308
+ faceRegExp.global = false;
4309
+
4310
+ // read faces
4311
+ for (var r = faceRegExp.exec(data); r != null; r = faceRegExp.exec(data)) {
4312
+ mesh.faceNormalBuffer.push(parseFloat(r[1]), parseFloat(r[2]), parseFloat(r[3]));
4313
+
4314
+ for (var i = 0; i < 3; i++) {
4315
+ var x = parseFloat(r[4 + (i * 3)]);
4316
+ var y = parseFloat(r[5 + (i * 3)]);
4317
+ var z = parseFloat(r[6 + (i * 3)]);
4318
+
4319
+ // weld vertices by the given decimal precision
4320
+ var vertKey = x.toFixed(this.decimalPrecision) + '-' + y.toFixed(this.decimalPrecision) + '-' + z.toFixed(this.decimalPrecision);
4321
+ var vi = v2i[vertKey];
4322
+ if(vi == undefined) {
4323
+ vi = mesh.vertexBuffer.length / 3;
4324
+ v2i[vertKey] = vi;
4325
+ mesh.vertexBuffer.push(x);
4326
+ mesh.vertexBuffer.push(y);
4327
+ mesh.vertexBuffer.push(z);
4328
+ }
4329
+ mesh.indexBuffer.push(vi);
4330
+ }
4331
+
4332
+ // mark the end of the indices of a face
4333
+ mesh.indexBuffer.push(-1);
4334
+ }
4335
+ }
4336
+
4337
+ // add mesh to scene
4338
+ if(!mesh.isTrivial())
4339
+ scene.addChild(mesh);
4340
+ };
4341
+
4342
+ /**
4343
+ Parse contents of a binary STL file and generate the scene.
4344
+ @private
4345
+ */
4346
+ JSC3D.StlLoader.prototype.parseStlBinary = function(scene, data) {
4347
+ var UINT16_BYTES = 2;
4348
+ var UINT32_BYTES = 4;
4349
+ var FLOAT_BYTES = 4;
4350
+ var HEADER_BYTES = 80;
4351
+ var FACE_COUNT_BYTES = UINT32_BYTES;
4352
+ var FACE_NORMAL_BYTES = FLOAT_BYTES * 3;
4353
+ var FACE_VERTICES = 3;
4354
+ var VERTEX_BYTES = FLOAT_BYTES * 3;
4355
+ var ATTRIB_BYTE_COUNT_BYTES = UINT16_BYTES;
4356
+
4357
+ // 84 is the minimun length of a valid binary stl file
4358
+ if(data.length < HEADER_BYTES + FACE_COUNT_BYTES)
4359
+ return;
4360
+
4361
+ var cur = 0;
4362
+
4363
+ // skip 80-byte's stl file header
4364
+ cur += HEADER_BYTES;
4365
+
4366
+ // read face count
4367
+ var numOfFaces = this.readUInt32LittleEndian(data, cur);
4368
+ cur += UINT32_BYTES;
4369
+
4370
+ var expectedLen = HEADER_BYTES + FACE_COUNT_BYTES +
4371
+ (FACE_NORMAL_BYTES + VERTEX_BYTES * FACE_VERTICES + ATTRIB_BYTE_COUNT_BYTES) * numOfFaces;
4372
+
4373
+ // file is not complete
4374
+ if(data.length < expectedLen)
4375
+ return;
4376
+
4377
+ var mesh = new JSC3D.Mesh;
4378
+ mesh.faceCount = numOfFaces;
4379
+ mesh.vertexBuffer = [];
4380
+ mesh.indexBuffer = [];
4381
+ mesh.faceNormalBuffer = [];
4382
+ var v2i = {};
4383
+
4384
+ // read faces
4385
+ for(var i=0; i<numOfFaces; i++) {
4386
+ // read normal vector of a face
4387
+ mesh.faceNormalBuffer.push(this.readFloatLittleEndian(data, cur));
4388
+ cur += FLOAT_BYTES;
4389
+ mesh.faceNormalBuffer.push(this.readFloatLittleEndian(data, cur));
4390
+ cur += FLOAT_BYTES;
4391
+ mesh.faceNormalBuffer.push(this.readFloatLittleEndian(data, cur));
4392
+ cur += FLOAT_BYTES;
4393
+
4394
+ // read all 3 vertices of a face
4395
+ for(var j=0; j<FACE_VERTICES; j++) {
4396
+ // read coords of a vertex
4397
+ var x, y, z;
4398
+ x = this.readFloatLittleEndian(data, cur);
4399
+ cur += FLOAT_BYTES;
4400
+ y = this.readFloatLittleEndian(data, cur);
4401
+ cur += FLOAT_BYTES;
4402
+ z = this.readFloatLittleEndian(data, cur);
4403
+ cur += FLOAT_BYTES;
4404
+
4405
+ // weld vertices by the given decimal precision
4406
+ var vertKey = x.toFixed(this.decimalPrecision) + '-' + y.toFixed(this.decimalPrecision) + '-' + z.toFixed(this.decimalPrecision);
4407
+ var vi = v2i[vertKey];
4408
+ if(vi != undefined) {
4409
+ mesh.indexBuffer.push(vi);
4410
+ }
4411
+ else {
4412
+ vi = mesh.vertexBuffer.length / 3;
4413
+ v2i[vertKey] = vi;
4414
+ mesh.vertexBuffer.push(x);
4415
+ mesh.vertexBuffer.push(y);
4416
+ mesh.vertexBuffer.push(z);
4417
+ mesh.indexBuffer.push(vi);
4418
+ }
4419
+ }
4420
+
4421
+ // mark the end of the indices of a face
4422
+ mesh.indexBuffer.push(-1);
4423
+
4424
+ // skip 2-bytes' 'attribute byte count' field, since we do not deal with any additional attribs
4425
+ cur += ATTRIB_BYTE_COUNT_BYTES;
4426
+
4427
+ }
4428
+
4429
+ // add mesh to scene
4430
+ if(!mesh.isTrivial())
4431
+ scene.addChild(mesh);
4432
+ };
4433
+
4434
+ /**
4435
+ @private
4436
+ */
4437
+ JSC3D.StlLoader.prototype.readUInt32LittleEndian = function(data, start) {
4438
+ var rv = 0, f = 1;
4439
+ for (var i=0; i<4; i++) {
4440
+ rv += (data[start + i] * f);
4441
+ f *= 256;
4442
+ }
4443
+
4444
+ return rv;
4445
+ };
4446
+
4447
+ /**
4448
+ @private
4449
+ */
4450
+ JSC3D.StlLoader.prototype.readFloatLittleEndian = function(data, start) {
4451
+ var mLen = 23;
4452
+ var eLen = 8; // 4 * 8 - 23 - 1
4453
+ var eMax = 255; // (1 << eLen) - 1;
4454
+ var eBias = 127; // eMax >> 1;
4455
+
4456
+ var i = 3;
4457
+ var d = -1;
4458
+ var s = data[start + i];
4459
+ i += d;
4460
+ var bits = -7;
4461
+ var e = s & ((1 << (-bits)) - 1);
4462
+ s >>= -bits;
4463
+ bits += eLen
4464
+ while(bits > 0) {
4465
+ e = e * 256 + data[start + i];
4466
+ i += d;
4467
+ bits -= 8;
4468
+ }
4469
+
4470
+ var m = e & ((1 << (-bits)) - 1);
4471
+ e >>= -bits;
4472
+ bits += mLen;
4473
+ while(bits > 0) {
4474
+ m = m * 256 + data[start + i];
4475
+ i += d;
4476
+ bits -= 8;
4477
+ }
4478
+
4479
+ switch(e) {
4480
+ case 0: // 0 or denormalized number
4481
+ e = 1 - eBias;
4482
+ break;
4483
+ case eMax: // NaN or +/-Infinity
4484
+ return m ? NaN : ((s ? -1 : 1) * Infinity);
4485
+ default: // normalized number
4486
+ m = m + Math.pow(2, mLen);
4487
+ e = e - eBias;
4488
+ break;
4489
+ }
4490
+
4491
+ return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
4492
+ };
4493
+
4494
+ JSC3D.StlLoader.prototype.onload = null;
4495
+ JSC3D.StlLoader.prototype.onerror = null;
4496
+ JSC3D.StlLoader.prototype.onprogress = null;
4497
+ JSC3D.StlLoader.prototype.onresource = null;
4498
+ JSC3D.StlLoader.prototype.decimalPrecision = 3;
4499
+
4500
+ JSC3D.LoaderSelector.registerLoader('stl', JSC3D.StlLoader);