-
Notifications
You must be signed in to change notification settings - Fork 16
/
geometry.js
168 lines (147 loc) · 4.18 KB
/
geometry.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
const Signal = require('signals')
const aabb = require('pex-geom/aabb')
const AttributesMap = {
positions: 'aPosition',
normals: 'aNormal',
tangents: 'aTangent',
texCoords: 'aTexCoord0',
texCoords1: 'aTexCoord1',
uvs: 'aTexCoord0',
uvs2: 'aTexCoord1',
vertexColors: 'aVertexColor',
offsets: 'aOffset',
scales: 'aScale',
rotations: 'aRotation',
colors: 'aColor',
joints: 'aJoint',
weights: 'aWeight'
}
const IndicesMap = {
indices: 'indices',
cells: 'indices'
}
function Geometry(opts) {
this.type = 'Geometry'
this.enabled = true
this.changed = new Signal()
this.bounds = aabb.create()
this.primitive = opts.ctx.Primitive.Triangles
this.count = undefined
this._indices = null
this._attributes = {}
this.set(opts)
}
Geometry.prototype.init = function(entity) {
this.entity = entity
}
Geometry.prototype.set = function(opts) {
const ctx = opts.ctx || this.ctx
// This is a bit messy because indices will could ovewrite this.indices
// so we have to use this._indices internally
Object.assign(this, opts)
for (let prop in opts) {
const val = opts[prop]
if (AttributesMap[prop]) {
const attribName = AttributesMap[prop]
let attrib = this._attributes[attribName]
if (!attrib) {
attrib = this._attributes[attribName] = {
buffer: null,
offset: 0,
stride: 0,
divisor: 0,
type: undefined
}
}
const data = val.length !== undefined ? val : val.data
// TODO: test different type configurations
if (data) {
if (!attrib.buffer) {
attrib.buffer = ctx.vertexBuffer({
data: data,
type: val.type || attrib.type
})
} else {
ctx.update(attrib.buffer, {
data: data,
type: val.type || attrib.type,
offset: attrib.offset || 0
})
}
if (attribName === 'aPosition') {
// If we have list of vectors we can calculate bounding box otherwise
// the user has to provide it
if (data[0].length) {
this.bounds = opts.bounds || aabb.fromPoints(data)
}
}
} else if (val.buffer) {
// can we read data from the buffer
// TODO: should we delete previous buffer?
attrib.buffer = val.buffer
}
// Update attrib data type from buffer
// It will be either re-used from val.type or attrib.type
// or guessed from the typed array
attrib.type = val.type || attrib.buffer.type
if (val.offset !== undefined) {
attrib.offset = val.offset
}
if (val.stride !== undefined) {
attrib.stride = val.stride
}
if (val.divisor !== undefined) {
attrib.divisor = val.divisor
}
if (!attrib.type) {
throw new Error(`Unknown ${attribName} attrib type`)
}
}
}
for (let prop in opts) {
if (IndicesMap[prop]) {
// TODO: this is just a guestimate, we should probably push creating flat uint32 array to the user space
// const numPositions = this._attributes['aPosition'].count
const val = opts[prop]
let indices = this._indices
if (!indices) {
indices = this._indices = {
buffer: null,
offset: 0,
type: undefined
}
}
const data = val.length !== undefined ? val : val.data
if (data) {
if (!indices.buffer) {
indices.buffer = ctx.indexBuffer({
data: data,
type: val.type || indices.type
})
} else {
ctx.update(indices.buffer, {
data: data,
type: val.type || indices.type
})
}
} else if (val.buffer) {
// TODO: should we delete previous buffer?
indices.buffer = val.buffer
}
if (val.offset !== undefined) {
indices.offset = val.offset
}
if (val.count !== undefined) {
indices.count = val.count
}
indices.type = val.type || indices.buffer.type
if (!indices.type) {
throw new Error(`Unknown indices type`)
}
}
this.changed.dispatch(prop)
}
}
module.exports = function(opts) {
return new Geometry(opts)
}