-
Notifications
You must be signed in to change notification settings - Fork 0
/
webgl-meshes.js
307 lines (277 loc) · 14.1 KB
/
webgl-meshes.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
class ObjectBuffers {
constructor() {
this.type = false
this.vertexCount= false
this.position = false
this.normal = false
this.uvs = false
this.indices = false
this.color = false
}
}
class WebGLMesh {
static initPlaneBuffers(gl, width=1.0, height=1.0, color=vec4.fromValues(1.0, 1.0, 1.0, 1.0)) {
const positions = [
width, height,
-width, height,
width, -height,
-width, -height,
];
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
const normals = [
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 1.0, 0.0,
];
const normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
const uvs = [
1.0, 1.0,
0.0, 1.0,
1.0, 0.0,
0.0, 0.0,
];
const uvBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, uvBuffer)
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(uvs), gl.STATIC_DRAW)
const colors = [
...color,
...color,
...color,
...color,
];
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
var bufObject = new ObjectBuffers()
bufObject.type = gl.TRIANGLE_STRIP
bufObject.vertexCount = 4
bufObject.position = {buffer: positionBuffer, comp: 2, type: gl.FLOAT}
bufObject.normal = {buffer: normalBuffer, comp: 4, type: gl.FLOAT}
bufObject.uvs = {buffer: uvBuffer, comp: 2, type: gl.FLOAT}
bufObject.color = {buffer: colorBuffer, comp: 4, type: gl.FLOAT}
return bufObject;
}
static initCubeBuffers(gl, sideLength = 1) {
const delta = sideLength * 0.5
const positions = [delta, delta, delta, // 0
delta, -delta, delta, // 1
-delta, delta, delta, // 2
-delta, -delta, delta, // 3
delta, delta, -delta, // 4
delta, -delta, -delta, // 5
-delta, delta, -delta, // 6
-delta, -delta, -delta, // 7
];
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW);
const normVal = 0.5773502588272095
const normals = [normVal, normVal, normVal, 0.0, // 0
normVal, -normVal, normVal, 0.0, // 1
-normVal, normVal, normVal, 0.0, // 2
-normVal, -normVal, normVal, 0.0, // 3
normVal, normVal, -normVal, 0.0, // 4
normVal, -normVal, -normVal, 0.0, // 5
-normVal, normVal, -normVal, 0.0, // 6
-normVal, -normVal, -normVal, 0.0, // 7
];
const normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
const indices = [0, 1, 3, 0, 3, 2, // front
0, 5, 1, 0, 4, 5, // right
4, 6, 7, 4, 7, 5, // back
6, 2, 3, 6, 3, 7, // left
6, 4, 0, 6, 0, 2, // top
1, 5, 7, 1, 7, 3, // bottom
];
const indicesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
const colors = [
0.0, 0.0, 1.0, 1.0,
0.0, 1.0, 0.0, 1.0,
1.0, 0.0, 0.0, 1.0,
0.0, 0.0, 0.0, 1.0,
1.0, 0.0, 1.0, 1.0,
1.0, 1.0, 0.0, 1.0,
1.0, 0.0, 1.0, 1.0,
0.0, 1.0, 1.0, 1.0,
];
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
var bufObject = new ObjectBuffers()
bufObject.type = gl.TRIANGLES
bufObject.vertexCount = indices.length
bufObject.position = {buffer: positionBuffer, comp: 3, type: gl.FLOAT}
bufObject.normal = {buffer: normalBuffer, comp: 4, type: gl.FLOAT}
bufObject.indices = {buffer: indicesBuffer, comp: 3, type: gl.UNSIGNED_SHORT}
bufObject.color = {buffer: colorBuffer, comp: 4, type: gl.FLOAT}
return bufObject
}
static initSphereBuffers(gl, radius, subdivisions = 0, color=null) {
const octahedroneVertices = [0.0, -radius, 0.0,
radius, 0.0, 0.0,
0.0, 0.0, radius,
-radius, 0.0, 0.0,
0.0, 0.0, -radius,
0.0, radius, 0.0,
]
var sphereVertices = octahedroneVertices
const octahedroneIndices = [0, 1, 2,
0, 2, 3,
0, 3, 4,
0, 4, 1,
1, 5, 2,
2, 5, 3,
3, 5, 4,
4, 5, 1,
]
var sphereIndices = octahedroneIndices
for (var i=0; i<subdivisions; ++i) {
var newIndices = [] // start fresh
const triCount = sphereIndices.length / 3
for (var triIndex = 0; triIndex < triCount; ++triIndex) {
const triIndices = [sphereIndices[triIndex*3],
sphereIndices[triIndex*3+1],
sphereIndices[triIndex*3+2]]
var newVertices = []
for (var edge = 0; edge < 3; ++edge) {
const v1Index = triIndices[edge]*3
const v1 = vec3.fromValues(sphereVertices[v1Index],
sphereVertices[v1Index+1],
sphereVertices[v1Index+2])
const v2Index = triIndices[(edge+1)%3]*3
const v2 = vec3.fromValues(sphereVertices[v2Index],
sphereVertices[v2Index+1],
sphereVertices[v2Index+2])
// console.log("triIndex/edgeIndex: "+triIndex+"/"+edge+" triIndices: "+triIndices+" v1Index: "+v1Index+" v2Index: "+v2Index)
var delta = vec3.subtract(vec3.create(), v2, v1)
vec3.scale(delta, delta, 0.5)
var newVert = vec3.add(vec3.create(), v1, delta)
const newVertDir = vec3.normalize(vec3.create(), newVert)
vec3.scale(newVert, newVertDir, radius)
// console.log("triIndex: "+triIndex+" v1: "+v1+" v2: "+v2+" newVert: "+newVert)
// console.log("newVert: "+newVert+" <= delta: "+delta+" newVertDir: " +newVertDir)
newVertices = newVertices.concat([newVert[0], newVert[1], newVert[2]])
}
// todo, optimize by checking if those vertices where added already
sphereVertices = sphereVertices.concat(newVertices)
const newVertCount = sphereVertices.length / 3
const newVerticesIndices = [newVertCount - 3,
newVertCount - 2,
newVertCount - 1]
newIndices = newIndices.concat([
triIndices[0], newVerticesIndices[2], newVerticesIndices[0],
newVerticesIndices[0], newVerticesIndices[1], triIndices[1],
newVerticesIndices[2], triIndices[2], newVerticesIndices[1],
newVerticesIndices[0], newVerticesIndices[2], newVerticesIndices[1],
])
}
sphereIndices = [...newIndices] // copy back
}
const positionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(sphereVertices), gl.STATIC_DRAW);
const indicesBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indicesBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(sphereIndices), gl.STATIC_DRAW);
var sphereNormals = []
const vertCount = sphereVertices.length / 3
for (var vertIndex=0; vertIndex<vertCount; ++vertIndex) {
const compIndex = vertIndex * 3
const v = vec4.fromValues(sphereVertices[compIndex],
sphereVertices[compIndex+1],
sphereVertices[compIndex+2],
0.0)
const n = vec4.normalize(vec4.create(), v)
sphereNormals = sphereNormals.concat([n[0], n[1], n[2], n[3]])
}
const normalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, normalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(sphereNormals), gl.STATIC_DRAW);
var sphereColors = []
for (var vertIndex=0; vertIndex<vertCount; ++vertIndex) {
const vertColor = color != null ? [color[0], color[1], color[2], color[3]]
: [Math.random(), Math.random(), Math.random(), 1.0]
sphereColors = sphereColors.concat(vertColor)
}
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(sphereColors), gl.STATIC_DRAW);
// console.log("[sphere] vert count: "+sphereVertices.length / 3+" triangle count: "+sphereIndices.length / 3)
// console.log("[sphere] ---- vertices ----");
// for (var i=0; i<sphereVertices.length; ++i) {
// console.log("sphereVertices["+i+"]: "+sphereVertices[i])
// }
// console.log("[sphere] ---- indices ----")
// for (var i=0; i<sphereIndices.length; i+=3) {
// console.log("sphereIndices["+i+", "+(i+1)+", "+(i+2)+"]: "
// +sphereIndices[i]+", "
// +sphereIndices[i+1]+", "
// +sphereIndices[i+2])
// }
// console.log("[sphere] ---- triangles ----")
// for (var i=0; i<sphereIndices.length / 3; ++i) {
// var index = i * 3
// const v0Index = sphereIndices[index]*3
// const v1Index = sphereIndices[index+1]*3
// const v2Index = sphereIndices[index+2]*3
// console.log("t"+i+"["+v0Index+","+v1Index+","+v2Index+"] "
// +"("+sphereVertices[v0Index].toFixed(2)+", "
// + sphereVertices[v0Index+1].toFixed(2)+", "
// + sphereVertices[v0Index+2].toFixed(2)+") | "
// +"("+sphereVertices[v1Index].toFixed(2)+", "
// + sphereVertices[v1Index+1].toFixed(2)+", "
// + sphereVertices[v1Index+2].toFixed(2)+") | "
// +"("+sphereVertices[v2Index].toFixed(2)+", "
// + sphereVertices[v2Index+1].toFixed(2)+", "
// + sphereVertices[v2Index+2].toFixed(2)+") | "
// )
// }
var bufObject = new ObjectBuffers()
bufObject.type = gl.TRIANGLES
bufObject.vertexCount = sphereIndices.length,
bufObject.position = {buffer: positionBuffer, comp: 3, type: gl.FLOAT}
bufObject.normal = {buffer: normalBuffer, comp: 4, type: gl.FLOAT}
bufObject.indices = {buffer: indicesBuffer, comp: 3, type: gl.UNSIGNED_SHORT}
bufObject.color = {buffer: colorBuffer, comp: 4, type: gl.FLOAT}
return bufObject
}
static initMeshBuffers(gl, meshData) {
var mesh = new OBJ.Mesh(meshData)
if (!OBJ.areNormalValid) {
OBJ.generateNormals(mesh)
//console.log("generated normals: "+mesh.vertexNormals)
}
OBJ.initMeshBuffers(gl, mesh)
// mesh.normalBuffer
// mesh.textureBuffer
// mesh.vertexBuffer
// mesh.indexBuffer
const vertexCount = mesh.indexBuffer.numItems
// --- temp ---
var vertexColors = []
for (var i=0; i<vertexCount; ++i) {
vertexColors = vertexColors.concat([Math.random(), Math.random(), Math.random(), 1.0])
}
// ---
const colorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertexColors), gl.STATIC_DRAW);
var bufObject = new ObjectBuffers()
bufObject.type = gl.TRIANGLES
bufObject.vertexCount = vertexCount
bufObject.position = {buffer: mesh.vertexBuffer, comp: 3, type: gl.FLOAT}
bufObject.normal = {buffer: mesh.normalBuffer, comp: 3, type: gl.FLOAT}
bufObject.indices = {buffer: mesh.indexBuffer, comp: 3, type: gl.UNSIGNED_SHORT}
bufObject.color = {buffer: colorBuffer, comp: 4, type: gl.FLOAT}
return bufObject
}
}