-
Notifications
You must be signed in to change notification settings - Fork 5
/
README
99 lines (79 loc) · 2.91 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
=======================================================
js3ds is a javascript library to read .3ds files.
The library is inspired by lib3ds : http://code.google.com/p/lib3ds/, so,
if you have experience with lib3ds, many objects will look familiar (Lib3dsMesh etc.).
At the moment only meshes are read: vertices, faces, texels, and some material properties.
NOTE:
I didn't actually test this, so... one thing: Endian.
Default is now little endian, You might want to pass 'false' as
the 4th argumang to the jDataView constructor to setup big endian.
CREDITS
=======================================================
This library couldn't have been created without Jonas Raoni Soares Silva's BinaryParser: http://jsfromhell.com/classes/binary-parser.
Although recent version have started using jDataVew, a big thank you to Jonas to let us get the ball rolling.
Speaking of jDataView, thanks to Christopher Chedeau for implementing it: https://github.com/vjeux/jDataView.
TODO
=======================================================
Much :-)
I suspect that especially the chunk reading code could be much smarter.
USAGE
=======================================================
Loading a 3ds file
-------------------------------------------------------
/**
* Load up a 3DS file
* @param url
*/
function loadFile(url) {
var req = new XMLHttpRequest();
if(req.overrideMimeType) {
req.overrideMimeType("text/plain; charset=x-user-defined"); // urgh, that took a while to google
}
req.onreadystatechange = function() {
if(req.readyState == 4) {
if(req.status == 0 || req.status == 200) {
// @debugDiv is some div to log some stuff, ie: document.getElementById("myDebugDiv")
// @bDebugging is a boolean indicating whether to log
var lib3ds = new Lib3ds(debugDiv, bDebugging);
// read the 3DS
lib3ds.readFile(req.responseText);
}
}
}
req.open("GET", url, true);
req.send(null);
}
-------------------------------------------------------
After lib3ds.readFile() was called:
// loop over the parsed meshes
var i, j;
for (i = 0; i < lib3ds.meshes.length; i++) {
var mesh = lib3ds.meshes[i]; // a mesh is of type Lib3dsMesh
// vertices
for (j = 0; j < mesh.points; j++) {
var vert = mesh.pointL[j]; // a vert is an Array(3)
}
// faces
for (j = 0; j < mesh.faces; j++) {
var face = mesh.faceL[j] // a face is of type Lib3dsFace
// indices into the vert array above
var idx0 = face.points[0];
var idx1 = face.points[1];
var idx2 = face.points[2];
// so the face vertices are:
var v0 = mesh.pointL[ idx0 ];
var v1 = mesh.pointL[ idx1 ];
var v2 = mesh.pointL[ idx2 ];
// and the material for the face is:
var materialName = face.material;
var material = lib3ds.materials[materialName];
var ambientColor = material.ambientColor;
// etc....
}
// texels / uv: guess you can use the face indices above
for (j = 0; j < mesh.texels; j++) {
var uv = mesh.texelL[j];
var u = uv[0];
var v = uv[1];
}
}