-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
163 lines (130 loc) · 3.74 KB
/
index.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
var normalize = require('./normalize')
var glType = require('gl-to-dtype')
var createVAO = require('gl-vao')
var dtype = require('dtype')
module.exports = GLGeometry
function GLGeometry (gl) {
if (!(this instanceof GLGeometry)) {
return new GLGeometry(gl)
}
this._elementsType = 5123
this._elementsBytes = 2
this._attributes = []
this._dirty = true
this._attrLength = 0
this._facesLength = 0
this._index = null
this._vao = null
this._keys = []
this.gl = gl
}
GLGeometry.prototype.dispose = function () {
for (var i = 0; i < this._attributes.length; i++) {
this._attributes[i].buffer.dispose()
}
this._attributes = []
this._keys = []
this._attrLength = 0 // Length of this attribute (the number of vertices it feeds)
this._facesLength = 0 // Number of vertices needed to draw all faces
this._dirty = true
if (this._index) {
this._index.dispose()
this._index = null
}
if (this._vao) {
this._vao.dispose()
this._vao = null
}
}
GLGeometry.prototype.faces = function faces (attr, opts) {
var size = opts && opts.size || 3
attr = attr.cells ? attr.cells : attr
this._dirty = true
if (this._index) {
this._index.dispose()
}
this._index = normalize.create(this.gl
, attr
, size
, this.gl.ELEMENT_ARRAY_BUFFER
, 'uint16'
)
this._facesLength = this._index.length * size
this._index = this._index.buffer
return this
}
GLGeometry.prototype.attr = function (name, attr, opts) {
// If we get a simplicial complex
if (attr.cells && attr.positions) {
return this.attr(name, attr.positions).faces(attr.cells, opts)
}
opts = opts || {}
var size = opts.size || 3
// Is this a known attribute (ie, an update)?
var keyIndex = this._keys.indexOf(name)
if (keyIndex > -1) {
var toUpdate = this._attributes[keyIndex].buffer
var offset = opts.offset || undefined
normalize.update(toUpdate, attr, size, 'float32', offset)
this._attrLength = toUpdate.length / size / 4
return this
}
this._dirty = true
var gl = this.gl
var first = !this._attributes.length
var attribute = normalize.create(gl, attr, size, gl.ARRAY_BUFFER, 'float32')
if (!attribute) {
throw new Error(
'Unexpected attribute format: needs an ndarray, array, typed array, ' +
'gl-buffer or simplicial complex'
)
}
var buffer = attribute.buffer
var length = attribute.length
this._keys.push(name)
this._attributes.push({
size: size,
buffer: buffer
})
if (first) {
this._attrLength = length
}
return this
}
GLGeometry.prototype.bind = function bind (shader) {
this.update()
this._vao.bind()
if (!this._keys) return
if (!shader) return
for (var i = 0; i < this._keys.length; i++) {
var attr = shader.attributes[this._keys[i]]
if (attr) attr.location = i
}
shader.bind()
}
GLGeometry.prototype.draw = function draw (mode, start, stop) {
start = typeof start === 'undefined' ? 0 : start
mode = typeof mode === 'undefined' ? this.gl.TRIANGLES : mode
this.update()
if (this._vao._useElements) {
stop = typeof stop === 'undefined' ? this._facesLength : stop
this.gl.drawElements(mode, stop - start, this._elementsType, start * this._elementsBytes)
} else {
stop = typeof stop === 'undefined' ? this._attrLength : stop
this.gl.drawArrays(mode, start, stop - start)
}
}
GLGeometry.prototype.unbind = function unbind () {
this.update()
this._vao.unbind()
}
GLGeometry.prototype.update = function update () {
if (!this._dirty) return
this._dirty = false
if (this._vao) this._vao.dispose()
this._vao = createVAO(this.gl, this._attributes, this._index)
this._elementsType = this._vao._elementsType
this._elementsBytes = dtype(
glType(this._elementsType) || 'array'
).BYTES_PER_ELEMENT || 2
}