-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
473 lines (407 loc) · 11 KB
/
main.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
var DataDirPath;
var ModelsDirPath;
var TexturesDirPath;
// init info
var isIIICar;
var isSACar;
var carColors;
// the scene
var running = false;
var myclump;
var modelinfo;
var camPitch;
var camYaw;
var camDist;
// gl things
var state = {};
var whitetex;
var camera;
var envFrame;
var defaultProgram;
var envMapProgram;
var carPS2Program;
var backgroundColor = [0, 0, 0, 0];
function deg2rad(d) { return d / 180.0 * Math.PI; }
var rotating, zooming;
function
mouseDown(e)
{
if(e.button == 0)
rotating = true;
else if(e.button == 1)
zooming = true;
old_x = e.pageX;
old_y = e.pageY;
e.preventDefault();
}
function
mouseUp(e)
{
rotating = false;
zooming = false;
}
function
mouseMove(e)
{
let dX, dY;
if(rotating){
dX = (e.pageX-old_x)*2*Math.PI/gl.canvas.width,
dY = (e.pageY-old_y)*2*Math.PI/gl.canvas.height;
camYaw -= dX;
camPitch += dY;
if(camPitch > Math.PI/2 - 0.01) camPitch = Math.PI/2 - 0.01
if(camPitch < -Math.PI/2 + 0.01) camPitch = -Math.PI/2 + 0.01
old_x = e.pageX;
old_y = e.pageY;
e.preventDefault();
}
if(zooming){
dY = (e.pageY-old_y)/gl.canvas.height;
camDist += dY;
if(camDist < 0.1) camDist = 0.1;
old_x = e.pageX;
e.preventDefault();
}
};
function
InitRW()
{
console.log("InitRW()");
let canvas = document.querySelector('#glcanvas');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// Get background color from stylesheet
var bgColorStr = window.getComputedStyle(canvas, null).getPropertyValue("background-color");
bgColorStr = bgColorStr.substring(4, bgColorStr.length-1);
backgroundColor = bgColorStr.replace(" ", "").split(",");
backgroundColor = [parseFloat(backgroundColor[0])/255, parseFloat(backgroundColor[1])/255, parseFloat(backgroundColor[2])/255, 1.0];
gl = canvas.getContext('webgl');
if(!gl){
alert('Unable to initialize WebGL. Your browser or machine may not support it.');
return;
}
canvas.addEventListener("mousedown", mouseDown, false);
canvas.addEventListener("mouseup", mouseUp, false);
canvas.addEventListener("mouseout", mouseUp, false);
canvas.addEventListener("mousemove", mouseMove, false);
whitetex = loadTexture("textures/white.png");
defaultProgram = loadShaders(defaultVS, defaultFS);
envMapProgram = loadShaders(envVS, envFS);
carPS2Program = loadShaders(carPS2VS, carPS2FS);
state.alphaRef = 0.1;
state.projectionMatrix = mat4.create();
state.viewMatrix = mat4.create();
state.worldMatrix = mat4.create();
state.envMatrix = mat4.create();
state.matColor = vec4.create();
state.surfaceProps = vec4.create();
state.ambLight = vec3.fromValues(0.4, 0.4, 0.4);
const alpha = 45;
const beta = 45;
state.lightDir = vec3.fromValues(
-Math.cos(deg2rad(beta))*Math.cos(deg2rad(alpha)),
-Math.sin(deg2rad(beta))*Math.cos(deg2rad(alpha)),
-Math.sin(deg2rad(alpha))
);
state.lightCol = vec3.fromValues(1.0, 1.0, 1.0);
AttachPlugins();
camera = RwCameraCreate();
camera.nearPlane = 0.1;
camera.farPlane = 100.0;
let frm = RwFrameCreate();
RwCameraSetFrame(camera, frm);
const fov = deg2rad(70);
const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
camera.viewWindow[1] = Math.tan(fov / 2);
camera.viewWindow[0] = camera.viewWindow[1]*aspect;
envFrame = RwFrameCreate();
mat4.rotateX(envFrame.matrix, envFrame.matrix, deg2rad(60));
rwFrameSynchLTM(envFrame);
}
function
displayFrames(frame, parelem)
{
let li = document.createElement('li');
li.innerHTML = frame.name;
for(let i = 0; i < frame.objects.length; i++){
let o = frame.objects[i];
if(o.type == rwID_ATOMIC){
let checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.onclick = function() { o.visible = checkbox.checked; };
checkbox.checked = o.visible;
li.appendChild(checkbox);
}
}
parelem.appendChild(li);
if(frame.child){
let ul = document.createElement('ul');
parelem.appendChild(ul);
for(let c = frame.child; c != null; c = c.next)
displayFrames(c, ul);
}
}
function
loadCarIII(filename)
{
loadDFF(filename, function(clump){
myclump = clump;
modelinfo = processVehicle(myclump);
setupIIICar(myclump);
setVehicleColors(modelinfo, carColors[0], carColors[1]);
main();
});
}
function
loadCarVC(filename)
{
loadDFF(filename, function(clump){
myclump = clump;
modelinfo = processVehicle(myclump);
setVehicleColors(modelinfo, carColors[0], carColors[1]);
main();
});
}
function
loadCarSA(filename)
{
loadDFF(filename, function(clump){
myclump = clump;
modelinfo = processVehicle(myclump);
setupSACar(myclump);
setVehicleColors(modelinfo,
carColors[0], carColors[1], carColors[2], carColors[3]);
setVehicleLightColors(modelinfo,
[ 255, 255, 255, 255 ],
[ 255, 255, 255, 255 ],
[ 255, 255, 255, 255 ],
[ 255, 255, 255, 255 ]);
main();
});
}
function
loadModel(filename)
{
loadDFF(filename, function(clump){
myclump = clump;
main();
});
}
function
removeChildren(x)
{
while(x.firstChild)
x.removeChild(x.firstChild);
}
function
main()
{
let ul = document.getElementById('frames');
removeChildren(ul);
displayFrames(myclump.frame, ul);
if(!running){
running = true;
let then = 0;
function render(now){
now *= 0.001; // convert to seconds
const deltaTime = now - then;
then = now;
drawScene(deltaTime);
requestAnimationFrame(render);
}
requestAnimationFrame(render);
}
}
function
setupIIICar(clump)
{
for(let i = 0; i < clump.atomics.length; i++){
let a = clump.atomics[i];
a.pipeline = matFXPipe;
for(let j = 0; j < a.geometry.materials.length; j++){
m = a.geometry.materials[j];
if(m.surfaceProperties[1] <= 0.0)
continue;
m.matfx = {
type: 2,
envCoefficient: m.surfaceProperties[1],
envTex: RwTextureRead("reflection01", "")
};
}
}
}
function
setupSACar(clump)
{
for(let i = 0; i < clump.atomics.length; i++){
let a = clump.atomics[i];
a.pipeline = carPipe;
for(let j = 0; j < a.geometry.materials.length; j++){
m = a.geometry.materials[j];
m.fxFlags = 0;
if(!m.matfx || m.matfx.type != 2) continue;
if(m.matfx.envTex && m.envMap && m.envMap.shininess != 0){
m.envMap.texture = m.matfx.envTex;
if(m.envMap.texture.name[0] == 'x')
m.fxFlags |= 2;
else
m.fxFlags |= 1;
}
if(m.specMap && m.specMap.specularity != 0)
m.fxFlags |= 4;
}
}
}
function
setVehicleColors(vehinfo, c1, c2, c3, c4)
{
for(let i = 0; i < vehinfo.firstMaterials.length; i++)
vehinfo.firstMaterials[i].color = c1;
for(let i = 0; i < vehinfo.secondMaterials.length; i++)
vehinfo.secondMaterials[i].color = c2;
for(let i = 0; i < vehinfo.thirdMaterials.length; i++)
vehinfo.thirdMaterials[i].color = c3;
for(let i = 0; i < vehinfo.fourthMaterials.length; i++)
vehinfo.fourthMaterials[i].color = c4;
if(c1) document.getElementById("custom-color0").value = RGB2HTML(c1);
if(c2) document.getElementById("custom-color1").value = RGB2HTML(c2);
if(c3) document.getElementById("custom-color2").value = RGB2HTML(c3);
if(c4) document.getElementById("custom-color3").value = RGB2HTML(c4);
}
function
setVehicleLightColors(vehinfo, c1, c2, c3, c4)
{
for(let i = 0; i < vehinfo.firstLightMaterials.length; i++)
vehinfo.firstLightMaterials[i].color = c1;
for(let i = 0; i < vehinfo.secondLightMaterials.length; i++)
vehinfo.secondLightMaterials[i].color = c2;
for(let i = 0; i < vehinfo.thirdLightMaterials.length; i++)
vehinfo.thirdLightMaterials[i].color = c3;
for(let i = 0; i < vehinfo.fourthLightMaterials.length; i++)
vehinfo.fourthLightMaterials[i].color = c4;
}
function
findEditableMaterials(geo, vehinfo)
{
for(let i = 0; i < geo.materials.length; i++){
m = geo.materials[i];
if(m.color[0] == 0x3C && m.color[1] == 0xFF && m.color[2] == 0)
vehinfo.firstMaterials.push(m);
else if(m.color[0] == 0xFF && m.color[1] == 0 && m.color[2] == 0xAF)
vehinfo.secondMaterials.push(m);
else if(m.color[0] == 0 && m.color[1] == 0xFF && m.color[2] == 0xFF)
vehinfo.thirdMaterials.push(m);
else if(m.color[0] == 0xFF && m.color[1] == 0x00 && m.color[2] == 0xFF)
vehinfo.fourthMaterials.push(m);
else if(m.color[0] == 0xFF && m.color[1] == 0xAF && m.color[2] == 0)
vehinfo.firstLightMaterials.push(m);
else if(m.color[0] == 0 && m.color[1] == 0xFF && m.color[2] == 0xC8)
vehinfo.secondLightMaterials.push(m);
else if(m.color[0] == 0xB9 && m.color[1] == 0xFF && m.color[2] == 0)
vehinfo.thirdLightMaterials.push(m);
else if(m.color[0] == 0xFF && m.color[1] == 0x3C && m.color[2] == 0)
vehinfo.fourthLightMaterials.push(m);
}
}
function
processVehicle(clump)
{
let vehicleInfo = {
firstMaterials: [],
secondMaterials: [],
thirdMaterials: [],
fourthMaterials: [],
firstLightMaterials: [], // front left
secondLightMaterials: [], // front right
thirdLightMaterials: [], // back left
fourthLightMaterials: [], // back right
clump: clump
};
// Wheel atomic to clone
let wheel = null;
for(let i = 0; i < clump.atomics.length; i++){
a = clump.atomics[i];
f = a.frame;
if(f.name.endsWith("_dam") ||
f.name.endsWith("_lo") ||
f.name.endsWith("_vlo"))
a.visible = false;
if(!wheel && f.name.startsWith("wheel")) {
wheel = a;
}
findEditableMaterials(a.geometry, vehicleInfo);
}
// Clone wheels
let frame = clump.frame.child;
while(wheel && frame) {
if(["wheel_rb_dummy", "wheel_rm_dummy", "wheel_lf_dummy", "wheel_lb_dummy", "wheel_lm_dummy"].includes(frame.name)) {
let wheel2 = RpAtomicClone(wheel);
mat4.copy(wheel2.frame.ltm, frame.ltm);
if(["wheel_lf_dummy", "wheel_lb_dummy", "wheel_lm_dummy"].includes(frame.name)) {
// Rotate cloned wheel
mat4.rotate(wheel2.frame.ltm, wheel2.frame.ltm, Math.PI, [0, 0, 1]);
}
frame.child = wheel2.frame;
clump.atomics.push(wheel2);
}
frame = frame.next;
}
return vehicleInfo;
}
function
drawScene(deltaTime)
{
if(window.autoRotateCamera) {
camYaw += deltaTime * 0.9;
}
gl.clearColor(backgroundColor[0], backgroundColor[1], backgroundColor[2], 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
let x = camDist * Math.cos(camYaw)* Math.cos(camPitch);
let y = camDist * Math.sin(camYaw)* Math.cos(camPitch);
let z = camDist * Math.sin(camPitch);
RwFrameLookAt(camera.frame,
[ x, y, z ],
[ 0.0, 0.0, 0.0 ],
[ 0.0, 0.0, 1.0 ]);
rwFrameSynchLTM(camera.frame);
RwCameraBeginUpdate(camera);
RenderPass = 0;
RpClumpRender(myclump);
RenderPass = 1;
RpClumpRender(myclump);
RenderPass = -1;
}
function
loadDFF(filename, cb)
{
let req = new XMLHttpRequest();
req.open("GET", ModelsDirPath + "/" + filename, true);
req.responseType = "arraybuffer";
req.onload = function(oEvent){
let arrayBuffer = req.response;
if(arrayBuffer){
stream = RwStreamCreate(arrayBuffer);
if(RwStreamFindChunk(stream, rwID_CLUMP)){
let c = RpClumpStreamRead(stream);
if(c != null)
cb(c);
}
return null;
}
};
req.send(null);
}
function
rgbToCSSString(r, g, b)
{
return ["rgb(",r,",",g,",",b,")"].join("");
}
function
RGB2HTML(color)
{
let decColor = 0x1000000 + color[2] + 0x100 * color[1] + 0x10000 * color[0];
return '#' + decColor.toString(16).substr(1);
}