-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
433 lines (358 loc) · 13.4 KB
/
helper.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
'use strict';
async function getFileText(url) {
let response = await fetch(url)
let text = await response.text()
return text
}
async function makeSkybox(gl, worldViewMatrix, projectionMatrix, textureTarget) {
let fragmentShaderText = await getFileText('shaders/skyboxFragmentShader')
let vertexShaderText = await getFileText('shaders/skyboxVertexShader')
const vertexShader = initVertexShader(gl, vertexShaderText)
const fragmentShader = initFragShader(gl, fragmentShaderText)
const program = gl.createProgram()
gl.attachShader(program, vertexShader)
gl.attachShader(program, fragmentShader)
gl.linkProgram(program)
gl.useProgram(program)
gl.enable(gl.DEPTH_TEST)
gl.enable(gl.CULL_FACE)
gl.cullFace(gl.FRONT)
const vertices = [
// pos(x,y,z)
// Top
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Left
-1.0, 1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
// Right
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
// Front
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
// Back
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
// Bottom
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, -1.0, -1.0,
]
var indices = [
// Top
0, 1, 2,
0, 2, 3,
// Left
5, 4, 6,
6, 4, 7,
// Right
8, 9, 10,
8, 10, 11,
// Front
13, 12, 14,
15, 14, 12,
// Back
16, 17, 18,
16, 18, 19,
// Bottom
21, 20, 22,
22, 20, 23,
]
const vbo = vboSetup(gl, vertices)
const ibo = iboSetup(gl, indices)
gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, ibo)
var positionAttribLocation = gl.getAttribLocation(program, "vertPos")
gl.vertexAttribPointer(
positionAttribLocation,
3,
gl.FLOAT,
gl.FALSE,
3 * Float32Array.BYTES_PER_ELEMENT,
0 * Float32Array.BYTES_PER_ELEMENT
)
gl.enableVertexAttribArray(positionAttribLocation)
// var modelMatrix = new Float32Array(16)
// var viewMatrix = new Float32Array(16)
// var projectionMatrix = new Float32Array(16)
// var modelViewMatrix = new Float32Array(16)
// identity(worldMatrix)
// lookAt(viewMatrix, [10,10,40], [0,10,0], [0,1,0])
// perspective(projectionMatrix, 45 * Math.PI / 180, canvas.clientWidth / canvas.clientHeight, 0.1, 1000.0)
// multiply(modelViewMatrix, viewMatrix, modelMatrix)
const worldViewLocation = gl.getUniformLocation(program, "modelViewMatrix")
const projLocation = gl.getUniformLocation(program, "projMatrix")
gl.uniformMatrix4fv(worldViewLocation, gl.FALSE, worldViewMatrix)
gl.uniformMatrix4fv(projLocation, gl.FALSE, projectionMatrix)
const textureUniformLocation = gl.getUniformLocation(program, 'texture')
gl.uniform1i(textureUniformLocation, textureTarget)
var texture = gl.createTexture()
gl.bindTexture(gl.TEXTURE_CUBE_MAP, texture)
var leftTexture = await loadTextureWithTarget(gl, 'skybox/left.png', gl.TEXTURE_CUBE_MAP_POSITIVE_X)
var rightTexture = await loadTextureWithTarget(gl, 'skybox/right.png', gl.TEXTURE_CUBE_MAP_NEGATIVE_X)
var topTexture = await loadTextureWithTarget(gl, 'skybox/down.png', gl.TEXTURE_CUBE_MAP_POSITIVE_Y)
var botTexture = await loadTextureWithTarget(gl, 'skybox/up.png', gl.TEXTURE_CUBE_MAP_NEGATIVE_Y)
var frontTexture = await loadTextureWithTarget(gl, "skybox/front.png", gl.TEXTURE_CUBE_MAP_POSITIVE_Z)
var backTexture = await loadTextureWithTarget(gl, 'skybox/back.png', gl.TEXTURE_CUBE_MAP_NEGATIVE_Z)
gl.generateMipmap(gl.TEXTURE_CUBE_MAP)
gl.texParameteri(gl.TEXTURE_CUBE_MAP, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_LINEAR)
return [program, vbo, indices.length]
}
function objectSetup(gl, vbo, program) {
// const vbo = vboSetup(gl, vertices)
gl.bindBuffer(gl.ARRAY_BUFFER, vbo)
var positionAttribLocation = gl.getAttribLocation(program, "vertPos")
gl.vertexAttribPointer(
positionAttribLocation,
3,
gl.FLOAT,
gl.FALSE,
8 * Float32Array.BYTES_PER_ELEMENT,
0 * Float32Array.BYTES_PER_ELEMENT
)
gl.enableVertexAttribArray(positionAttribLocation)
var normalAttribLocation = gl.getAttribLocation(program, "v_Normal")
gl.vertexAttribPointer(
normalAttribLocation,
3,
gl.FLOAT,
gl.FALSE,
8 * Float32Array.BYTES_PER_ELEMENT,
5 * Float32Array.BYTES_PER_ELEMENT
)
gl.enableVertexAttribArray(normalAttribLocation)
var texCoordAttribLocation = gl.getAttribLocation(program, "vertTexCoord")
gl.vertexAttribPointer(
texCoordAttribLocation,
2,
gl.FLOAT,
gl.FALSE,
8 * Float32Array.BYTES_PER_ELEMENT,
3 * Float32Array.BYTES_PER_ELEMENT
)
gl.enableVertexAttribArray(texCoordAttribLocation)
gl.bindBuffer(gl.ARRAY_BUFFER, null)
return vbo
}
function setTransformUniforms(gl, program, worldMatrix, viewMatrix, projectionMatrix, normalMatrix) {
var worldMatUniformLocation = gl.getUniformLocation(program, "worldMat")
var viewMatUniformLocation = gl.getUniformLocation(program, "viewMat")
var projectionMatUniformLocation = gl.getUniformLocation(program, "projectionMat")
var normalMatUniformLocation = gl.getUniformLocation(program, "normalMat")
gl.uniformMatrix4fv(worldMatUniformLocation, gl.FALSE, worldMatrix)
gl.uniformMatrix4fv(viewMatUniformLocation, gl.FALSE, viewMatrix)
gl.uniformMatrix4fv(projectionMatUniformLocation, gl.FALSE, projectionMatrix)
gl.uniformMatrix3fv(normalMatUniformLocation, gl.FALSE, normalMatrix)
}
function setLightUniforms(gl, program, lightPosition, lightAmbient, lightDiffuse, lightSpecular, lightHalfVector) {
var lightPositonUniformLocation = gl.getUniformLocation(program, "lightPosition");
var lightAmbientUniformLocation = gl.getUniformLocation(program, "lightAmbient");
var lightDiffuseUniformLocation = gl.getUniformLocation(program, "lightDiffuse");
var lightSpecularUniformLocation = gl.getUniformLocation(program, "lightSpecular");
var lightHalfVectorUniformLocation = gl.getUniformLocation(program, "lightHalfVector");
gl.uniform4fv(lightPositonUniformLocation, lightPosition)
gl.uniform4fv(lightAmbientUniformLocation, lightAmbient)
gl.uniform4fv(lightDiffuseUniformLocation,lightDiffuse)
gl.uniform4fv(lightSpecularUniformLocation, lightSpecular)
gl.uniform3fv(lightHalfVectorUniformLocation, lightHalfVector)
}
function setMaterialUniforms(gl, program, materialAmbient, materialDiffuse, materialSpecular, materialEmission, materialShininess) {
var materialEmissionUniformLocation = gl.getUniformLocation(program, "materialEmission");
var materialAmbientUniformLocation = gl.getUniformLocation(program, "materialAmbient");
var materialDiffuseUniformLocation = gl.getUniformLocation(program, "materialDiffuse");
var materialSpecularUniformLocation = gl.getUniformLocation(program, "materialSpecular");
var materialShininessUniformLocation = gl.getUniformLocation(program, "materialShininess");
gl.uniform4fv(materialEmissionUniformLocation, materialEmission)
gl.uniform4fv(materialAmbientUniformLocation, materialAmbient)
gl.uniform4fv(materialDiffuseUniformLocation, materialDiffuse)
gl.uniform4fv(materialSpecularUniformLocation, materialSpecular)
gl.uniform1f(materialShininessUniformLocation, materialShininess)
}
async function getDataFromObj(url) {
let text = await getFileText(url)
let lineList = text.split(/\r*\n/)
var vList = [] // 3-4 elemnts per vertex x,y,z(,w)
var vtList = [] // 1-3 elements per texture coordinate u(,v,w)
var vnList = [] // 3 elements per vertex normal x,y,z
var fList = [] // TODO: should vbo?
lineList.forEach(line => {
let splitLine = line.trim().split(/\s+/)
switch (splitLine.shift()) {
case "v":
addSplitLineToList(splitLine, vList)
break;
case "vn":
addSplitLineToList(splitLine, vnList)
break;
case "vt":
addSplitLineToList(splitLine, vtList)
break;
case "f":
splitLine.forEach(element => {
let indices = element.split("/")
let index = parseInt(indices[0]) - 1
vList[index].forEach(coord => {
fList.push(coord)
});
if (vtList.length != 0) {
vtList[parseInt(indices[1]) - 1].forEach(coord => {
fList.push(coord)
});
}
if (vnList.length != 0) {
vnList[parseInt(indices[2]) - 1].forEach(coord => {
fList.push(coord)
});
}
});
break;
default:
break;
}
});
return fList
}
async function setupVideo(id) {
const video = document.getElementById(id)
var playing = false
var timeUpdate = false
var copyVideo = false
video.autoplay = true
video.muted = true
video.loop = true
video.addEventListener('playing', function() {
playing = true
checkReady()
}, true)
video.addEventListener('timeupdate', function() {
timeUpdate = true
checkReady()
}, true)
video.play()
function checkReady() {
if( playing && timeUpdate){
copyVideo = true
}
}
return video
}
async function loadTexture(gl, url) {
const texture = gl.createTexture()
const level = 0
const internalFormat = gl.RGBA
const srcFormat = gl.RGBA
const srcType = gl.UNSIGNED_BYTE
let promise = new Promise(resolve => {
const img = new Image()
img.onload = function() {
gl.bindTexture(gl.TEXTURE_2D, texture)
gl.texImage2D(gl.TEXTURE_2D,
level,
internalFormat,
srcFormat,
srcType,
img)
gl.generateMipmap(gl.TEXTURE_2D)
resolve()
}
img.src = url
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); // flip texture
})
return promise
}
// // returns a promise, so one can wait for the image to load before using it
async function loadTextureWithTarget(gl, url, glTarget) {
const texture = gl.createTexture()
// gl.bindTexture(glTarget, texture)
// make texture with one pixel to allow texture to load
const level = 0
const internalFormat = gl.RGBA
const srcFormat = gl.RGBA
const srcType = gl.UNSIGNED_BYTE
// attach and load real texture
return new Promise( resolve => {
const image = new Image()
image.onload = () => resolve(texture,
gl.texImage2D(glTarget,
level,
internalFormat,
srcFormat,
srcType,
image
))
image.src = url
})
}
function addSplitLineToList(line, list) {
for (let i = 0; i < line.length; i++) {
line[i] = parseFloat(line[i])
}
// if (splitLine.length == 3) { // add default w if only x,y,z were given
// splitLine.push(1.0)
// }
list.push(line)
}
function vboSetup(context, vertices) {
let vbo = context.createBuffer()
context.bindBuffer(context.ARRAY_BUFFER, vbo)
context.bufferData(context.ARRAY_BUFFER,
new Float32Array(vertices),
context.STATIC_DRAW)
context.bindBuffer(context.ARRAY_BUFFER, null)
return vbo
}
function iboSetup(context, indices) {
let ibo = context.createBuffer()
context.bindBuffer(context.ELEMENT_ARRAY_BUFFER, ibo)
context.bufferData(context.ELEMENT_ARRAY_BUFFER,
new Uint16Array(indices),
context.STATIC_DRAW)
context.bindBuffer(context.ELEMENT_ARRAY_BUFFER, null)
return ibo
}
function feedDataToShader(context, program, shaderAttrName) {
const positionAttributeLocation = context.getAttribLocation(program, shaderAttrName)
context.vertexAttribPointer(
positionAttributeLocation,
2,
context.FLOAT,
context.FALSE,
2 * Float32Array.BYTES_PER_ELEMENT,
0 * Float32Array.BYTES_PER_ELEMENT
)
context.enableVertexAttribArray(positionAttributeLocation)
}
function initVertexShader(context, vertexShaderText) {
const vertexShader = context.createShader(context.VERTEX_SHADER)
context.shaderSource(vertexShader, vertexShaderText)
context.compileShader(vertexShader)
// check for compile errors (not automatic)
if (!context.getShaderParameter(vertexShader, context.COMPILE_STATUS))
{
console.error("Error compiling vertexShader: ", context.getShaderInfoLog(vertexShader))
}
return vertexShader
}
function initFragShader(context, fragmentShaderText) {
const fragShader = context.createShader(context.FRAGMENT_SHADER)
context.shaderSource(fragShader, fragmentShaderText)
context.compileShader(fragShader)
// check for compile errors (not automatic)
if (!context.getShaderParameter(fragShader, context.COMPILE_STATUS))
{
console.error("Error compiling fragmentShader: ", context.getShaderInfoLog(fragShader))
}
return fragShader
}