jsc3d-js-rails 1.6.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/jsc3d-js-rails.gemspec +23 -0
- data/lib/jsc3d/js/rails.rb +10 -0
- data/lib/jsc3d/js/rails/version.rb +7 -0
- data/vendor/assets/javascripts/jsc3d.3ds.js +717 -0
- data/vendor/assets/javascripts/jsc3d.console.js +147 -0
- data/vendor/assets/javascripts/jsc3d.js +5474 -0
- data/vendor/assets/javascripts/jsc3d.touch.js +1459 -0
- data/vendor/assets/javascripts/jsc3d.webgl.js +1041 -0
- data/vendor/assets/javascripts/jsc3d_ie.js +4500 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9782cb77e095035e34592c06985348b2c002a2fd
|
4
|
+
data.tar.gz: 9a6a369afb4016bb8b88fa981a395e175d0c6a80
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: defe6f362fa2fb28faf9392ed9254a7e4d1ae85b2e2b27f741f78113d3f9f7caf7279df1b07bb7b92ccc800564bb049bc2b077b5e9cf62095a54ba8d17146d1a
|
7
|
+
data.tar.gz: b6118403c0f1dff325183fd794e7737d071efc4b749e64a2c42786d6b233485865cd079a934f4c85b41667b783fae626da6610433357bb0bc5a841dd55521c72
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Bogdan Frankovskiy
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Jsc3d::Js::Rails
|
2
|
+
|
3
|
+
This gem provides [js3d](https://code.google.com/p/jsc3d/) driver for your Rails 3+ application.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'jsc3d-js-rails'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install jsc3d-js-rails
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jsc3d/js/rails/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jsc3d-js-rails"
|
8
|
+
spec.version = Jsc3d::Js::Rails::VERSION
|
9
|
+
spec.authors = ["Bogdan Frankovskiy"]
|
10
|
+
spec.email = ["bfrankovskiy@gmail.com"]
|
11
|
+
spec.description = %q{This gem provides js3d driver for your Rails 3+ application.}
|
12
|
+
spec.summary = %q{Use js3d javascript library with Rails 3jdt}
|
13
|
+
spec.homepage = "https://code.google.com/p/jsc3d/"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "railties", ">= 3.0", "< 5.0"
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
@@ -0,0 +1,717 @@
|
|
1
|
+
/**
|
2
|
+
* @preserve Copyright (c) 2013 Vasile Dirla <vasile@dirla.ro>
|
3
|
+
* This file is part of jsc3d project, which is freely distributable under the
|
4
|
+
* terms of the MIT license.
|
5
|
+
*
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
11
|
+
* furnished to do so, subject to the following conditions:
|
12
|
+
*
|
13
|
+
* The above copyright notice and this permission notice shall be included in
|
14
|
+
* all copies or substantial portions of the Software.
|
15
|
+
*
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22
|
+
* THE SOFTWARE.
|
23
|
+
*/
|
24
|
+
|
25
|
+
|
26
|
+
/**
|
27
|
+
@namespace JSC3D
|
28
|
+
*/
|
29
|
+
var JSC3D = JSC3D || {};
|
30
|
+
|
31
|
+
|
32
|
+
/**
|
33
|
+
@class Autodesk3DSLoader
|
34
|
+
|
35
|
+
This class implements a scene loader from an 3DS file.
|
36
|
+
*/
|
37
|
+
JSC3D.Autodesk3DSLoader = function(onload, onerror, onprogress, onresource) {
|
38
|
+
this.onload = (onload && typeof(onload) == 'function') ? onload : null;
|
39
|
+
this.onerror = (onerror && typeof(onerror) == 'function') ? onerror : null;
|
40
|
+
this.onprogress = (onprogress && typeof(onprogress) == 'function') ? onprogress : null;
|
41
|
+
this.onresource = (onresource && typeof(onresource) == 'function') ? onresource : null;
|
42
|
+
this.request = null;
|
43
|
+
|
44
|
+
this._materials = {};
|
45
|
+
this._unfinalized_objects = {};
|
46
|
+
this._textures={};
|
47
|
+
|
48
|
+
this._cur_obj_end = 0;
|
49
|
+
this._cur_obj;
|
50
|
+
|
51
|
+
this._cur_mat_end = 0;
|
52
|
+
this._cur_mat;
|
53
|
+
|
54
|
+
this.totalFaces = 0;
|
55
|
+
};
|
56
|
+
|
57
|
+
/**
|
58
|
+
Load scene from a given 3DS file.
|
59
|
+
@param {String} urlName a string that specifies where to fetch the 3DS file.
|
60
|
+
*/
|
61
|
+
JSC3D.Autodesk3DSLoader.prototype.loadFromUrl = function(urlName) {
|
62
|
+
var self = this;
|
63
|
+
var xhr = new XMLHttpRequest;
|
64
|
+
xhr.open('GET', urlName, true);
|
65
|
+
if(JSC3D.PlatformInfo.browser == 'ie' && JSC3D.PlatformInfo.version >= '10')
|
66
|
+
xhr.responseType = 'blob'; // use blob method to deal with 3DS files for IE >= 10
|
67
|
+
else{
|
68
|
+
xhr.overrideMimeType('text/plain; charset=x-user-defined');
|
69
|
+
// xhr.responseType = "arraybuffer";
|
70
|
+
}
|
71
|
+
|
72
|
+
xhr.onreadystatechange = function() {
|
73
|
+
if(this.readyState == 4) {
|
74
|
+
if(this.status == 200 || this.status == 0) {
|
75
|
+
if(JSC3D.console)
|
76
|
+
JSC3D.console.logInfo('Finished loading 3DS file "' + urlName + '".');
|
77
|
+
if(self.onload) {
|
78
|
+
if(self.onprogress)
|
79
|
+
self.onprogress('Loading 3DS file ...', 1);
|
80
|
+
if(JSC3D.PlatformInfo.browser == 'ie' && JSC3D.PlatformInfo.version >= '10') {
|
81
|
+
// asynchronously decode blob to binary string
|
82
|
+
var blobReader = new FileReader;
|
83
|
+
blobReader.onload = function(event) {
|
84
|
+
var scene = new JSC3D.Scene;
|
85
|
+
self.parse3DS(scene, event.target.result);
|
86
|
+
self.onload(scene);
|
87
|
+
};
|
88
|
+
blobReader.readAsText(this.response, 'x-user-defined');
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
var scene = new JSC3D.Scene;
|
92
|
+
self.parse3DS(scene, this.responseText);
|
93
|
+
self.onload(scene);
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
else {
|
98
|
+
if(JSC3D.console)
|
99
|
+
JSC3D.console.logError('Failed to load 3DS file "' + urlName + '".');
|
100
|
+
if(self.onerror)
|
101
|
+
self.onerror('Failed to load 3DS file "' + urlName + '".');
|
102
|
+
}
|
103
|
+
self.request = null;
|
104
|
+
}
|
105
|
+
};
|
106
|
+
|
107
|
+
if(this.onprogress) {
|
108
|
+
this.onprogress('Loading 3DS file ...', 0);
|
109
|
+
xhr.onprogress = function(event) {
|
110
|
+
self.onprogress('Loading 3DS file ...', event.loaded / event.total);
|
111
|
+
};
|
112
|
+
}
|
113
|
+
|
114
|
+
this.request = xhr;
|
115
|
+
xhr.send();
|
116
|
+
};
|
117
|
+
|
118
|
+
/**
|
119
|
+
Abort current loading if it is not finished yet.
|
120
|
+
*/
|
121
|
+
JSC3D.Autodesk3DSLoader.prototype.abort = function() {
|
122
|
+
if(this.request) {
|
123
|
+
this.request.abort();
|
124
|
+
this.request = null;
|
125
|
+
}
|
126
|
+
};
|
127
|
+
|
128
|
+
JSC3D.Autodesk3DSLoader.prototype.parseMaterial= function (reader, endMaterial) {
|
129
|
+
var mat = {};
|
130
|
+
|
131
|
+
while (reader.tell() < endMaterial) {
|
132
|
+
var cid ;
|
133
|
+
var len;
|
134
|
+
var end;
|
135
|
+
|
136
|
+
cid = reader.readUInt16();
|
137
|
+
len = reader.readUInt32();
|
138
|
+
end = reader.tell() + (len-6);
|
139
|
+
|
140
|
+
switch (cid) {
|
141
|
+
case 0xA000: // Material name
|
142
|
+
mat.name = this.readNulTermString(reader);
|
143
|
+
break;
|
144
|
+
|
145
|
+
case 0xA010: // Ambient color
|
146
|
+
mat.ambientColor = this.readColor(reader);
|
147
|
+
break;
|
148
|
+
|
149
|
+
case 0xA020: // Diffuse color
|
150
|
+
mat.diffuseColor = this.readColor(reader);
|
151
|
+
break;
|
152
|
+
|
153
|
+
case 0xA030: // Specular color
|
154
|
+
mat.specularColor = this.readColor(reader);
|
155
|
+
break;
|
156
|
+
|
157
|
+
case 0xA050: // transparency
|
158
|
+
mat.transparency = this.readAmount(reader,end);
|
159
|
+
break;
|
160
|
+
|
161
|
+
case 0xA081: // Two-sided, existence indicates "true"
|
162
|
+
mat.twoSided = true;
|
163
|
+
break;
|
164
|
+
|
165
|
+
case 0xA200: // Main (color) texture
|
166
|
+
mat.colorMap = this.parseTexture(reader,end);
|
167
|
+
break;
|
168
|
+
|
169
|
+
case 0xA204: // Specular map
|
170
|
+
mat.specularMap = this.parseTexture(reader,end);
|
171
|
+
break;
|
172
|
+
|
173
|
+
default:
|
174
|
+
reader.seek(end);
|
175
|
+
break;
|
176
|
+
}
|
177
|
+
}
|
178
|
+
|
179
|
+
return mat;
|
180
|
+
};
|
181
|
+
|
182
|
+
JSC3D.Autodesk3DSLoader.prototype.readAmount = function(reader,end) {
|
183
|
+
var cid;
|
184
|
+
var len;
|
185
|
+
var amount = 0;
|
186
|
+
cid = reader.readUInt16();
|
187
|
+
len = reader.readUInt32();
|
188
|
+
|
189
|
+
switch (cid) {
|
190
|
+
case 0x0030: // Floats
|
191
|
+
amount = reader.readUInt16();
|
192
|
+
break;
|
193
|
+
default:
|
194
|
+
break;
|
195
|
+
}
|
196
|
+
|
197
|
+
reader.seek(end);
|
198
|
+
return amount;
|
199
|
+
};
|
200
|
+
|
201
|
+
JSC3D.Autodesk3DSLoader.prototype.readColor = function(reader) {
|
202
|
+
var cid;
|
203
|
+
var len;
|
204
|
+
var r, g, b;
|
205
|
+
|
206
|
+
cid = reader.readUInt16();
|
207
|
+
len = reader.readUInt32();
|
208
|
+
|
209
|
+
switch (cid) {
|
210
|
+
case 0x0010: // Floats
|
211
|
+
r = reader.readFloat32() * 255;
|
212
|
+
g = reader.readFloat32() * 255;
|
213
|
+
b = reader.readFloat32() * 255;
|
214
|
+
break;
|
215
|
+
case 0x0011: // 24-bit color
|
216
|
+
r = reader.readUInt8();
|
217
|
+
g = reader.readUInt8();
|
218
|
+
b = reader.readUInt8();
|
219
|
+
break;
|
220
|
+
default:
|
221
|
+
reader.skip(len-6);
|
222
|
+
break;
|
223
|
+
}
|
224
|
+
|
225
|
+
return (r<<16) | (g<<8) | b;
|
226
|
+
};
|
227
|
+
|
228
|
+
JSC3D.Autodesk3DSLoader.prototype.parseTexture = function(reader, endTexture) {
|
229
|
+
var tex ={};
|
230
|
+
|
231
|
+
while (reader.tell() < endTexture) {
|
232
|
+
var cid;
|
233
|
+
var len;
|
234
|
+
|
235
|
+
cid = reader.readUInt16();
|
236
|
+
len = reader.readUInt32();
|
237
|
+
|
238
|
+
switch (cid) {
|
239
|
+
case 0xA300:
|
240
|
+
tex.url = this.readNulTermString(reader);
|
241
|
+
break;
|
242
|
+
default:
|
243
|
+
// Skip this unknown texture sub-chunk
|
244
|
+
reader.skip(len-6);
|
245
|
+
break;
|
246
|
+
}
|
247
|
+
}
|
248
|
+
|
249
|
+
this._textures[tex.url] = tex;
|
250
|
+
//TODO: vasea
|
251
|
+
// this.addDependency(tex.url);
|
252
|
+
|
253
|
+
return tex;
|
254
|
+
};
|
255
|
+
|
256
|
+
JSC3D.Autodesk3DSLoader.prototype.readNulTermString= function (reader) {
|
257
|
+
var chr;
|
258
|
+
var str = '';
|
259
|
+
|
260
|
+
while ((chr = reader.readUInt8()) > 0) {
|
261
|
+
str += String.fromCharCode(chr);
|
262
|
+
}
|
263
|
+
|
264
|
+
return str;
|
265
|
+
};
|
266
|
+
|
267
|
+
JSC3D.Autodesk3DSLoader.prototype.parseVertexList = function (reader) {
|
268
|
+
var i;
|
269
|
+
var len;
|
270
|
+
var count;
|
271
|
+
|
272
|
+
count = reader.readUInt16();
|
273
|
+
this._cur_obj.verts = new Array(count*3);
|
274
|
+
|
275
|
+
i = 0;
|
276
|
+
len = this._cur_obj.verts.length;
|
277
|
+
while (i<len) {
|
278
|
+
var x, y, z;
|
279
|
+
|
280
|
+
x = reader.readFloat32();
|
281
|
+
y = reader.readFloat32();
|
282
|
+
z = reader.readFloat32();
|
283
|
+
|
284
|
+
this._cur_obj.verts[i++] = x;
|
285
|
+
this._cur_obj.verts[i++] = z;
|
286
|
+
this._cur_obj.verts[i++] = -y;
|
287
|
+
}
|
288
|
+
};
|
289
|
+
|
290
|
+
|
291
|
+
JSC3D.Autodesk3DSLoader.prototype.parseFaceList = function (reader) {
|
292
|
+
var i;
|
293
|
+
var len;
|
294
|
+
var count;
|
295
|
+
|
296
|
+
count = reader.readUInt16();
|
297
|
+
this._cur_obj.facesCount = count;
|
298
|
+
this._cur_obj.indices = [];
|
299
|
+
this._cur_obj.uvsIndexes = [];
|
300
|
+
|
301
|
+
|
302
|
+
i = 0;
|
303
|
+
len = count*3;
|
304
|
+
while (i < len) {
|
305
|
+
var i0, i1, i2;
|
306
|
+
|
307
|
+
i0 = reader.readUInt16();
|
308
|
+
i1 = reader.readUInt16();
|
309
|
+
i2 = reader.readUInt16();
|
310
|
+
i+=3;
|
311
|
+
this._cur_obj.indices.push(i0);
|
312
|
+
this._cur_obj.indices.push(i2);
|
313
|
+
this._cur_obj.indices.push(i1);
|
314
|
+
this._cur_obj.indices.push(-1);
|
315
|
+
|
316
|
+
this._cur_obj.uvsIndexes.push(i0);
|
317
|
+
this._cur_obj.uvsIndexes.push(i2);
|
318
|
+
this._cur_obj.uvsIndexes.push(i1);
|
319
|
+
this._cur_obj.uvsIndexes.push(-1);
|
320
|
+
|
321
|
+
|
322
|
+
// Skip "face info", irrelevant data
|
323
|
+
reader.skip(2);
|
324
|
+
}
|
325
|
+
|
326
|
+
this._cur_obj.smoothingGroups = [];
|
327
|
+
for ( index = 0 ; index <this._cur_obj.facesCount;index++){
|
328
|
+
this._cur_obj.smoothingGroups[index] = 0;
|
329
|
+
}
|
330
|
+
};
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
JSC3D.Autodesk3DSLoader.prototype.parseUVList = function (reader) {
|
335
|
+
var i;
|
336
|
+
var len;
|
337
|
+
var count;
|
338
|
+
|
339
|
+
count = reader.readUInt16();
|
340
|
+
this._cur_obj.uvs = [];
|
341
|
+
|
342
|
+
|
343
|
+
i = 0;
|
344
|
+
len = count;
|
345
|
+
|
346
|
+
while (i < len) {
|
347
|
+
this._cur_obj.uvs.push(reader.readFloat32());
|
348
|
+
this._cur_obj.uvs.push(1.0 - reader.readFloat32());
|
349
|
+
i+=1;
|
350
|
+
}
|
351
|
+
};
|
352
|
+
|
353
|
+
|
354
|
+
JSC3D.Autodesk3DSLoader.prototype.parseFaceMaterialList = function (reader) {
|
355
|
+
var mat;
|
356
|
+
var count;
|
357
|
+
var i;
|
358
|
+
var faces = [];
|
359
|
+
|
360
|
+
mat = this.readNulTermString(reader);
|
361
|
+
count = reader.readUInt16();
|
362
|
+
for (var index = 0 ; index < count;index++){
|
363
|
+
faces[index] = 0;
|
364
|
+
}
|
365
|
+
|
366
|
+
i = 0;
|
367
|
+
while (i<faces.length) {
|
368
|
+
faces[i++] = reader.readUInt16();
|
369
|
+
}
|
370
|
+
|
371
|
+
this._cur_obj.materials.push(mat);
|
372
|
+
this._cur_obj.materialFaces[mat] = faces;
|
373
|
+
};
|
374
|
+
|
375
|
+
|
376
|
+
JSC3D.Autodesk3DSLoader.prototype.readTransform = function (reader) {
|
377
|
+
var data =[];
|
378
|
+
for (var index = 0 ; index < 16;index++){
|
379
|
+
data[index] = 0;
|
380
|
+
}
|
381
|
+
|
382
|
+
|
383
|
+
// X axis
|
384
|
+
data[0] = reader.readFloat32(); // X
|
385
|
+
data[2] = reader.readFloat32(); // Z
|
386
|
+
data[1] = reader.readFloat32(); // Y
|
387
|
+
data[3] = 0;
|
388
|
+
|
389
|
+
// Z axis
|
390
|
+
data[8] = reader.readFloat32(); // X
|
391
|
+
data[10] = reader.readFloat32(); // Z
|
392
|
+
data[9] = reader.readFloat32(); // Y
|
393
|
+
data[11] = 0;
|
394
|
+
|
395
|
+
// Y Axis
|
396
|
+
data[4] = reader.readFloat32(); // X
|
397
|
+
data[6] = reader.readFloat32(); // Z
|
398
|
+
data[5] = reader.readFloat32(); // Y
|
399
|
+
data[7] = 0;
|
400
|
+
|
401
|
+
// Translation
|
402
|
+
data[12] = reader.readFloat32(); // X
|
403
|
+
data[14] = reader.readFloat32(); // Z
|
404
|
+
data[13] = reader.readFloat32(); // Y
|
405
|
+
data[15] = 1;
|
406
|
+
|
407
|
+
return data;
|
408
|
+
};
|
409
|
+
|
410
|
+
JSC3D.Autodesk3DSLoader.prototype.parseObjectAnimation = function (reader,end) {
|
411
|
+
var vo;
|
412
|
+
var obj;
|
413
|
+
var pivot;
|
414
|
+
var name;
|
415
|
+
var hier;
|
416
|
+
|
417
|
+
// Pivot defaults to origin
|
418
|
+
pivot = {};
|
419
|
+
|
420
|
+
while (reader.tell() < end) {
|
421
|
+
var cid;
|
422
|
+
var len;
|
423
|
+
|
424
|
+
cid = reader.readUInt16();
|
425
|
+
len = reader.readUInt32();
|
426
|
+
|
427
|
+
switch (cid) {
|
428
|
+
case 0xb010: // Name/hierarchy
|
429
|
+
name = this.readNulTermString(reader);
|
430
|
+
reader.skip(4);
|
431
|
+
hier = reader.readInt16();
|
432
|
+
break;
|
433
|
+
|
434
|
+
case 0xb013: // Pivot
|
435
|
+
pivot.x = reader.readFloat32();
|
436
|
+
pivot.z = reader.readFloat32();
|
437
|
+
pivot.y = reader.readFloat32();
|
438
|
+
break;
|
439
|
+
|
440
|
+
default:
|
441
|
+
reader.skip(len-6);
|
442
|
+
break;
|
443
|
+
}
|
444
|
+
}
|
445
|
+
|
446
|
+
// If name is "$$$DUMMY" this is an empty object (e.g. a container)
|
447
|
+
// and will be ignored in this version of the parser
|
448
|
+
// TODO: Implement containers in 3DS parser.
|
449
|
+
if (name != '$$$DUMMY' && this._unfinalized_objects.hasOwnProperty(name)) {
|
450
|
+
vo = this._unfinalized_objects[name];
|
451
|
+
// todo vasea
|
452
|
+
if(JSC3D.console)
|
453
|
+
JSC3D.console.logInfo('Construct object '+vo.name);
|
454
|
+
// this._cur_obj = null;
|
455
|
+
obj = null;//this.constructObject(vo, pivot);
|
456
|
+
|
457
|
+
if (obj) {
|
458
|
+
if(JSC3D.console)
|
459
|
+
JSC3D.console.logInfo('finished loading the object '+vo.name);
|
460
|
+
}
|
461
|
+
|
462
|
+
delete this._unfinalized_objects[name];
|
463
|
+
}
|
464
|
+
};
|
465
|
+
|
466
|
+
|
467
|
+
JSC3D.Autodesk3DSLoader.prototype.parseSmoothingGroups = function (reader) {
|
468
|
+
var len = this._cur_obj.facesCount;
|
469
|
+
var i = 0;
|
470
|
+
while (i < len) {
|
471
|
+
this._cur_obj.smoothingGroups[i] = reader.readUInt32();
|
472
|
+
i++;
|
473
|
+
}
|
474
|
+
};
|
475
|
+
|
476
|
+
|
477
|
+
JSC3D.Autodesk3DSLoader.prototype.finalizeCurrentMaterial = function () {
|
478
|
+
var mat = new JSC3D.Material;
|
479
|
+
|
480
|
+
if (this._cur_mat.colorMap) {
|
481
|
+
mat.textureFileName = this._cur_mat.colorMap.texture;
|
482
|
+
}
|
483
|
+
else {
|
484
|
+
mat.diffuseColor = this._cur_mat.diffuseColor;
|
485
|
+
}
|
486
|
+
|
487
|
+
mat.ambientColor = this._cur_mat.ambientColor;
|
488
|
+
mat.specularColor = this._cur_mat.specularColor;
|
489
|
+
mat.bothSides = this._cur_mat.twoSided;
|
490
|
+
|
491
|
+
this._materials[this._cur_mat.name] = this._cur_mat;
|
492
|
+
this._cur_mat.material = mat;
|
493
|
+
|
494
|
+
this._cur_mat = null;
|
495
|
+
};
|
496
|
+
|
497
|
+
/**
|
498
|
+
Asynchronously load a texture from a given url and set it to corresponding meshes when done.
|
499
|
+
@private
|
500
|
+
*/
|
501
|
+
JSC3D.Autodesk3DSLoader.prototype.setupTexture = function(meshList, textureUrlName) {
|
502
|
+
var self = this;
|
503
|
+
var texture = new JSC3D.Texture;
|
504
|
+
|
505
|
+
texture.onready = function() {
|
506
|
+
for(var i=0; i<meshList.length; i++)
|
507
|
+
meshList[i].setTexture(this);
|
508
|
+
if(self.onresource)
|
509
|
+
self.onresource(this);
|
510
|
+
};
|
511
|
+
|
512
|
+
texture.createFromUrl(textureUrlName);
|
513
|
+
};
|
514
|
+
|
515
|
+
/**
|
516
|
+
Parse contents of an Autodesk3DSLoader file and generate the scene.
|
517
|
+
@private
|
518
|
+
*/
|
519
|
+
JSC3D.Autodesk3DSLoader.prototype.parse3DS = function(scene, data) {
|
520
|
+
var mesh = new JSC3D.Mesh;
|
521
|
+
mesh.vertexBuffer = [];
|
522
|
+
mesh.indexBuffer = [];
|
523
|
+
mesh.faceNormalBuffer = [];
|
524
|
+
|
525
|
+
var reader = new JSC3D.BinaryStream(data);
|
526
|
+
|
527
|
+
reader.reset();
|
528
|
+
|
529
|
+
|
530
|
+
// TODO: With this construct, the loop will run no-op for as long
|
531
|
+
// as there is time once file has finished reading. Consider a nice
|
532
|
+
// way to stop loop when byte array is empty, without putting it in
|
533
|
+
// the while-conditional, which will prevent finalizations from
|
534
|
+
// happening after the last chunk.
|
535
|
+
while (!reader.eof()) {
|
536
|
+
|
537
|
+
// If we are currently working on an object, and the most recent chunk was
|
538
|
+
// the last one in that object, finalize the current object.
|
539
|
+
if (this._cur_mat && reader.tell() >= this._cur_mat_end) {
|
540
|
+
this.finalizeCurrentMaterial();
|
541
|
+
}
|
542
|
+
else if (this._cur_obj && reader.tell() >= this._cur_obj_end) {
|
543
|
+
// Can't finalize at this point, because we have to wait until the full
|
544
|
+
// animation section has been parsed for any potential pivot definitions
|
545
|
+
|
546
|
+
if(JSC3D.console)
|
547
|
+
JSC3D.console.logInfo('Optimize the mesh and add it to the scene. ');
|
548
|
+
|
549
|
+
//have to parse all the materials and create a mesh fdor each material
|
550
|
+
|
551
|
+
mesh = new JSC3D.Mesh;
|
552
|
+
mesh.faceCount = this._cur_obj.facesCount;
|
553
|
+
mesh.name = this._cur_obj.name;
|
554
|
+
mesh.vertexBuffer = this._cur_obj.verts;
|
555
|
+
mesh.indexBuffer = this._cur_obj.indices;
|
556
|
+
mesh.texCoordBuffer = this._cur_obj.uvs;
|
557
|
+
|
558
|
+
|
559
|
+
mesh.texCoordIndexBuffer = this._cur_obj.uvsIndexes;
|
560
|
+
// mesh.texCoordIndexBuffer = mesh.indexBuffer;
|
561
|
+
mesh.material = new JSC3D.Material;
|
562
|
+
|
563
|
+
var materialFaces = this._cur_obj.materialFaces;
|
564
|
+
var currentMaterial = null;
|
565
|
+
for(var materialName in materialFaces){
|
566
|
+
currentMaterial = this._materials[materialName];
|
567
|
+
if (currentMaterial.colorMap) {
|
568
|
+
if(JSC3D.console)
|
569
|
+
JSC3D.console.logInfo('set texture: '+currentMaterial.colorMap.url);
|
570
|
+
this.setupTexture([mesh], currentMaterial.colorMap.url);
|
571
|
+
}
|
572
|
+
else {
|
573
|
+
mesh.material.diffuseColor = currentMaterial.diffuseColor;
|
574
|
+
}
|
575
|
+
// mesh.material.ambientColor = currentMaterial.ambientColor;
|
576
|
+
// mesh.material.simulateSpecular = (currentMaterial.specularColor>0);
|
577
|
+
mesh.isDoubleSided = true;//currentMaterial.twoSided;
|
578
|
+
mesh.material.transparency = currentMaterial.transparency>0?(currentMaterial.transparency)/100:0;
|
579
|
+
}
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
//mesh.faceNormalBuffer = [];
|
584
|
+
// mesh.init();
|
585
|
+
/*
|
586
|
+
|
587
|
+
var tempVertexBuffer = this._cur_obj.verts; // temporary buffer as container for all vertices
|
588
|
+
|
589
|
+
var viBuffer = tempVertexBuffer.length >= 3 ? (new Array(tempVertexBuffer.length / 3)) : null;
|
590
|
+
|
591
|
+
// split vertices into the mesh, the indices are also re-calculated
|
592
|
+
if(tempVertexBuffer.length >= 3 && mesh.indexBuffer.length > 0) {
|
593
|
+
for(var i=0; i<viBuffer.length; i++)
|
594
|
+
viBuffer[i] = -1;
|
595
|
+
|
596
|
+
mesh.vertexBuffer = [];
|
597
|
+
var oldVI = 0, newVI = 0;
|
598
|
+
for(var i=0; i<mesh.indexBuffer.length; i++) {
|
599
|
+
oldVI = mesh.indexBuffer[i];
|
600
|
+
if(oldVI != -1) {
|
601
|
+
if(viBuffer[oldVI] == -1) {
|
602
|
+
var v = oldVI * 3;
|
603
|
+
mesh.vertexBuffer.push(tempVertexBuffer[v]);
|
604
|
+
mesh.vertexBuffer.push(tempVertexBuffer[v + 1]);
|
605
|
+
mesh.vertexBuffer.push(tempVertexBuffer[v + 2]);
|
606
|
+
mesh.indexBuffer[i] = newVI;
|
607
|
+
viBuffer[oldVI] = newVI;
|
608
|
+
newVI++;
|
609
|
+
}
|
610
|
+
else {
|
611
|
+
mesh.indexBuffer[i] = viBuffer[oldVI];
|
612
|
+
}
|
613
|
+
}
|
614
|
+
}
|
615
|
+
}
|
616
|
+
*/
|
617
|
+
|
618
|
+
|
619
|
+
|
620
|
+
// add mesh to scene
|
621
|
+
if(!mesh.isTrivial()){
|
622
|
+
scene.addChild(mesh);
|
623
|
+
this.totalFaces+=mesh.faceCount;
|
624
|
+
}
|
625
|
+
|
626
|
+
this._unfinalized_objects[this._cur_obj.name] = this._cur_obj;
|
627
|
+
|
628
|
+
this._cur_obj_end = Number.MAX_VALUE;
|
629
|
+
this._cur_obj = null;
|
630
|
+
}
|
631
|
+
|
632
|
+
|
633
|
+
var cid;
|
634
|
+
var len;
|
635
|
+
var end;
|
636
|
+
|
637
|
+
cid = reader.readUInt16();
|
638
|
+
len = reader.readUInt32();
|
639
|
+
end = reader.tell() + (len-6);
|
640
|
+
|
641
|
+
switch (cid) {
|
642
|
+
case 0x4D4D: // MAIN3DS
|
643
|
+
case 0x3D3D: // EDIT3DS
|
644
|
+
case 0xB000: // KEYF3DS
|
645
|
+
// This types are "container chunks" and contain only
|
646
|
+
// sub-chunks (no data on their own.) This means that
|
647
|
+
// there is nothing more to parse at this point, and
|
648
|
+
// instead we should progress to the next chunk, which
|
649
|
+
// will be the first sub-chunk of this one.
|
650
|
+
continue;
|
651
|
+
break;
|
652
|
+
|
653
|
+
case 0xAFFF: // MATERIAL
|
654
|
+
this._cur_mat_end = end;
|
655
|
+
this._cur_mat = this.parseMaterial(reader,end);
|
656
|
+
break;
|
657
|
+
|
658
|
+
case 0x4000: // EDIT_OBJECT
|
659
|
+
this._cur_obj_end = end;
|
660
|
+
this._cur_obj = {};
|
661
|
+
this._cur_obj.name = this.readNulTermString(reader);
|
662
|
+
this._cur_obj.materials = [];
|
663
|
+
this._cur_obj.materialFaces = [];
|
664
|
+
break;
|
665
|
+
|
666
|
+
case 0x4100: // OBJ_TRIMESH
|
667
|
+
|
668
|
+
this._cur_obj.type = 'mesh';
|
669
|
+
break;
|
670
|
+
|
671
|
+
case 0x4110: // TRI_VERTEXL
|
672
|
+
this.parseVertexList(reader);
|
673
|
+
break;
|
674
|
+
|
675
|
+
case 0x4120: // TRI_FACELIST
|
676
|
+
this.parseFaceList(reader);
|
677
|
+
break;
|
678
|
+
|
679
|
+
case 0x4140: // TRI_MAPPINGCOORDS
|
680
|
+
this.parseUVList(reader);
|
681
|
+
break;
|
682
|
+
|
683
|
+
case 0x4130: // Face materials
|
684
|
+
this.parseFaceMaterialList(reader);
|
685
|
+
break;
|
686
|
+
|
687
|
+
case 0x4160: // Transform
|
688
|
+
this._cur_obj.transform = this.readTransform(reader);
|
689
|
+
break;
|
690
|
+
|
691
|
+
case 0xB002: // Object animation (including pivot)
|
692
|
+
this.parseObjectAnimation(reader,end);
|
693
|
+
break;
|
694
|
+
|
695
|
+
case 0x4150: // Smoothing groups
|
696
|
+
this.parseSmoothingGroups(reader);
|
697
|
+
break;
|
698
|
+
|
699
|
+
default:
|
700
|
+
// Skip this (unknown) chunk
|
701
|
+
reader.skip(len-6);
|
702
|
+
break;
|
703
|
+
}
|
704
|
+
}
|
705
|
+
|
706
|
+
if(JSC3D.console) {
|
707
|
+
JSC3D.console.logInfo('3DS object was loaded !');
|
708
|
+
JSC3D.console.logInfo(' totalFaces=' + this.totalFaces);
|
709
|
+
}
|
710
|
+
};
|
711
|
+
|
712
|
+
JSC3D.Autodesk3DSLoader.prototype.onload = null;
|
713
|
+
JSC3D.Autodesk3DSLoader.prototype.onerror = null;
|
714
|
+
JSC3D.Autodesk3DSLoader.prototype.onprogress = null;
|
715
|
+
JSC3D.Autodesk3DSLoader.prototype.onresource = null;
|
716
|
+
|
717
|
+
JSC3D.LoaderSelector.registerLoader('3ds', JSC3D.Autodesk3DSLoader);
|