-
Notifications
You must be signed in to change notification settings - Fork 3
/
files.js
231 lines (228 loc) · 4.53 KB
/
files.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*Used for heads, guns, armour...*/
function load_abo (binary)
{
let f = new Binary_reader (binary);
let MAGICK = 0x214f4241;
let VERSION = 0x20170503;
if (f.getint () != MAGICK)
{
webgl_log ("Mismatched model magick!");
return null;
}
if (f.getint () != VERSION)
{
webgl_log ("Mismatched model version!");
return null;
}
let nnames = f.getint ();
let names = [];
for (let i = 0; i < nnames; i++)
{
names.push (f.getarray (1));
}
let nskin = f.getint ();
let skn = [];
for (let i = 0; i < nskin; i++)
{
let map = f.getint ();
f.getint ();
skn.push (webgl_image ("data/textures/" + map.toString ()));
}
let nmats = f.getint ();
let mats = [];
for (let i = 0; i < nmats; i++)
{
f.getint ();
mats.push ({
diffuse: f.getvector (3),
ambient: f.getvector (3),
emissive: f.getvector (3),
specular: f.getvector (3),
shine: f.getfloat (),
alpha: f.getfloat ()
});
}
let nmesh = f.getint ();
let m = [];
for (let i = 0; i < nmesh; i++)
{
let index = f.getint ();
let v = gl.createBuffer ();
gl.bindBuffer (gl.ARRAY_BUFFER, v);
gl.bufferData (gl.ARRAY_BUFFER, f.getarray (32), gl.STATIC_DRAW);
let n = gl.createBuffer ();
let data = new Uint16Array (f.getarray (2));
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, n);
gl.bufferData (
gl.ELEMENT_ARRAY_BUFFER,
data,
gl.STATIC_DRAW
);
m.push ({
slot: index,
verts: v,
nindices: data.length,
indices: n
});
}
return {
skin: skn,
materials: mats,
meshes: m
};
}
/*Animated meshes*/
function load_cir2 (binary)
{
let f = new Binary_reader (binary);
let MAGICK = 0x32524943;
let VERSION = 0x20170313;
if (f.getint () != MAGICK)
{
webgl_log ("Mismatched model magick!");
return null;
}
if (f.getint () != VERSION)
{
webgl_log ("Mismatched model version!");
return null;
}
/*Skip names*/
let nnames = f.getint ();
let names = [];
for (let i = 0; i < nnames; i++)
{
names.push (f.getstring ());
}
/*Read materials*/
let mats = {};
let slots = [];
let nmats = f.getint ();
for (let i = 0; i < nmats; i++)
{
let key = names[f.getint ()];
slots.push (key);
mats[key] = {
ambient: f.getvector (3),
diffuse: f.getvector (3),
specular: f.getvector (3),
emissive: f.getvector (3),
shine: f.getfloat (),
alpha: f.getfloat ()
};
}
/*Read bones*/
let b = [];
let nbones = f.getint ();
for (let i = 0; i < nbones; i++)
{
f.getint ();
b.push ({
parent: f.getint (),
scale: f.getfloat ()
});
}
/*Read attractors*/
let att = {};
let natt = f.getint ();
for (let i = 0; i < natt; i++)
{
let key = names[f.getint ()];
att[key] = {
id: f.getint (),
or: f.getvector (4),
tr: f.getvector (3),
};
}
/*Read meshes*/
let m = [];
let nmeshes = f.getint ();
for (let i = 0; i < nmeshes; i++)
{
let s = f.getint ();
let v = f.getarray (40);
let n = new Uint16Array (f.getarray (2));
/*Create buffers*/
let b1 = gl.createBuffer ();
gl.bindBuffer (gl.ARRAY_BUFFER, b1);
gl.bufferData (gl.ARRAY_BUFFER, v, gl.STATIC_DRAW);
let b2 = gl.createBuffer ();
gl.bindBuffer (gl.ELEMENT_ARRAY_BUFFER, b2);
gl.bufferData (gl.ELEMENT_ARRAY_BUFFER, n, gl.STATIC_DRAW);
m.push ({
slot: slots[s],
verts: b1,
nindices: n.length,
indices: b2,
});
}
/*Put it all together*/
return {
bones: b,
materials: mats,
attractors: att,
meshes: m
};
}
function load_cir2anim (binary)
{
let f = new Binary_reader (binary);
let MAGICK = 0x32494e41;
let VERSION = 0x20170318;
if (f.getint () != MAGICK)
{
webgl_log ("Mismatched animation magick!");
return null;
}
if (f.getint () != VERSION)
{
webgl_log ("Mismatched animation version!");
return null;
}
let len = f.getfloat ();
let nb = f.getint ();
let b = [];
for (let i = 0; i < nb; i++)
{ /*Rotation keys*/
let rot = [];
let d1 = new Binary_reader (f.getarray (12));
let nrot = d1.dv.buffer.byteLength/12;
for (let j = 0; j < nrot; j++)
{
rot.push ({
time: d1.getfloat (),
or: [
tofloat32 (d1.getint16 ()),
tofloat32 (d1.getint16 ()),
tofloat32 (d1.getint16 ()),
tofloat32 (d1.getint16 ())
]
});
}
/*Position keys*/
let pos = [];
let d2 = new Binary_reader (f.getarray (16));
let npos = d2.dv.buffer.byteLength/16;
for (let j = 0; j < npos; j++)
{
pos.push ({
time: d2.getfloat (),
tr: [
d2.getfloat (),
d2.getfloat (),
d2.getfloat ()
]
});
}
/*Mint a new curve*/
b.push ({
r: {data: rot, index: 0},
p: {data: pos, index: 0},
});
}
/*Put everything together*/
return {
length: len,
bones: b
};
}