-
Notifications
You must be signed in to change notification settings - Fork 2
/
mesh-buffer.js
65 lines (55 loc) · 1.68 KB
/
mesh-buffer.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
"use strict"
var ndarray = require("ndarray")
var createBuffer = require("gl-buffer")
var createVAO = require("gl-vao")
var createAOMesh = require("./mesh.js")
var ops = require("ndarray-ops")
var mat4 = require("gl-mat4")
//Creates a mesh from a set of voxels
function createVoxelMesh(gl, voxels, voxelSideTextureIDs, voxelSideTextureSizes, position, pad, that) {
//Create mesh
var vert_data = createAOMesh(voxels, voxelSideTextureIDs, voxelSideTextureSizes)
var vertexArrayObjects = {}
if (vert_data === null) {
// no vertices allocated
} else {
//Upload triangle mesh to WebGL
var vert_buf = createBuffer(gl, vert_data)
var triangleVAO = createVAO(gl, [
{ "buffer": vert_buf,
"type": gl.UNSIGNED_BYTE,
"size": 4,
"offset": 0,
"stride": 8,
"normalized": false
},
{ "buffer": vert_buf,
"type": gl.UNSIGNED_BYTE,
"size": 4,
"offset": 4,
"stride": 8,
"normalized": false
}
])
triangleVAO.length = Math.floor(vert_data.length/8)
vertexArrayObjects.surface = triangleVAO
}
// move the chunk into place
var modelMatrix = mat4.create()
var w = voxels.shape[2] - pad // =[1]=[0]=game.chunkSize
var translateVector = [
position[0] * w,
position[1] * w,
position[2] * w]
mat4.translate(modelMatrix, modelMatrix, translateVector)
//Bundle result and return
var result = {
vertexArrayObjects: vertexArrayObjects, // other plugins can add their own VAOs
center: [w>>1, w>>1, w>>1],
radius: w,
modelMatrix: modelMatrix
}
if (that) that.emit('meshed', result, gl, vert_data, voxels)
return result
}
module.exports = createVoxelMesh