-
Notifications
You must be signed in to change notification settings - Fork 233
/
values.ts
314 lines (268 loc) · 12.3 KB
/
values.ts
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
import * as _ from 'underscore';
import * as THREE from 'three';
import * as utils from './utils.js';
/* Manages a list of scalar and arrays for use with WebGL instanced rendering
*/
class Values {
public static defaults: {[key:string]: number} = {
vx: 0,
vy: 1,
vz: 0,
x: 0,
y: 0,
z: 0,
size: 0
};
public length = Infinity;
public scalar: {[key: string]: any} = {};
public scalar_vec3: {[key: string]: any} = {};
public scalar_vec4: {[key: string]: any} = {};
public array: {[key: string]: any} = {};
public array_vec3: {[key: string]: any} = {};
public array_vec4: {[key: string]: any} = {};
public values: {[key: string]: any} = {};
public constructor(names: [string],
names_vec3: [string],
getter: any,
sequence_index: number,
names_vec4: [string]) {
_.each(names, (name) => {
let value = getter(name, sequence_index, Values.defaults[name]);
if(utils.is_typedarray(value)) {
if(name != 'selected') // hardcoded.. hmm bad
this.length = Math.min(this.length, value.length);
this.array[name] = value;
} else {
this.scalar[name] = value;
}
this.values[name] = value;
});
_.each(names_vec3, (name) => {
let value = getter(name, sequence_index, Values.defaults[name]);
if(name.indexOf('color') !== -1 && typeof value === "string") { // special case to support controlling color from a widget
let color = new THREE.Color(value);
value = new Float32Array([color.r, color.g, color.b]); // no sequence, scalar
}
if(utils.is_typedarray(value) && value.length > 3) { // single value is interpreted as scalar
this.array_vec3[name] = value;
this.length = Math.min(this.length, value.length/3);
} else {
this.scalar_vec3[name] = value;
}
this.values[name] = value;
});
_.each(names_vec4, (name) => {
let value = getter(name, sequence_index, Values.defaults[name]);
if(name.indexOf('color') !== -1 && typeof value === "string") {
// special case to support controlling color from a widget
let color = new THREE.Color(value);
value = new Float32Array([color.r, color.g, color.b, 1.0]);
}
if(utils.is_typedarray(value) && value.length > 4) {
this.array_vec4[name] = value;
// color vectors have 4 components
this.length = Math.min(this.length, value.length / 4);
} else {
// single value is interpreted as scalar
this.scalar_vec4[name] = value;
}
this.values[name] = value;
});
}
public trim = (new_length: number) => {
this.array = _.mapObject(this.array, function(array) {
return array.length === new_length ? array : array.slice(0, new_length);
});
this.array_vec3 = _.mapObject(this.array_vec3, function(array_vec3) {
return array_vec3.length === new_length*3 ? array_vec3 : array_vec3.slice(0, new_length*3);
});
this.array_vec4 = _.mapObject(this.array_vec4, (array_vec4) => {
return (array_vec4.length === new_length * 4) ? array_vec4 : array_vec4.slice(0, new_length * 4);
});
this.length = new_length;
};
public ensure_array = (name: string | [string]) => {
let names = _.isArray(name) ? name : [name];
_.each(names, (name) => {
if(typeof this.scalar[name] !== 'undefined') {
let array = this.array[name] = new Float32Array(this.length);
array.fill(this.scalar[name]);
delete this.scalar[name];
delete this.values[name];
}
let value_vec3 = this.scalar_vec3[name];
let value_vec4 = this.scalar_vec4[name];
if(typeof value_vec3 !== 'undefined') {
let array = this.array_vec3[name] = new Float32Array(this.length*3);
for(let i = 0; i < this.length; i++) {
array[i*3+0] = value_vec3[0];
array[i*3+1] = value_vec3[1];
array[i*3+2] = value_vec3[2];
}
delete this.scalar_vec3[name];
delete this.values[name];
}
if(typeof value_vec4 !== 'undefined') {
this.array_vec4[name] = new Float32Array(this.length * 4);
let array = this.array_vec4[name];
for(let i = 0; i < this.length; i++) {
array[i * 4 + 0] = value_vec4[0];
array[i * 4 + 1] = value_vec4[1];
array[i * 4 + 2] = value_vec4[2];
array[i * 4 + 3] = value_vec4[3];
}
delete this.scalar_vec4[name];
delete this.values[name];
}
});
};
public grow = (new_length: number) => {
this.array = _.mapObject(this.array, (array) => {
let new_array = new array.constructor(new_length);
new_array.set(array);
return new_array;
});
this.array_vec3 = _.mapObject(this.array_vec3, (array_vec3) => {
let new_array = new array_vec3.constructor(new_length*3);
new_array.set(array_vec3);
return new_array;
})
this.length = length;
};
public pad = (other: Values) => {
this.array = _.mapObject(this.array, (array, name) => {
let new_array = new array.constructor(other.length);
if(typeof other.array[name] === "undefined") { // then other must be a scalar
new_array.fill(other.scalar[name], this.length);
} else {
new_array.set(other.array[name].slice(this.length), this.length);
}
new_array.set(array);
return new_array;
});
this.array_vec3 = _.mapObject(this.array_vec3, (array_vec3, name) => {
let new_array = new array_vec3.constructor(other.length*3);
if(typeof other.array_vec3[name] === "undefined") { // then other must be a scalar
let other_scalar = other.scalar_vec3[name];
for(let i = this.length; i < other.length; i++) {
new_array[i*3+0] = other_scalar[0];
new_array[i*3+1] = other_scalar[1];
new_array[i*3+2] = other_scalar[2];
}
} else {
new_array.set(other.array_vec3[name].slice(this.length*3), this.length*3);
}
new_array.set(array_vec3);
return new_array;
});
this.array_vec4 = _.mapObject(this.array_vec4, (array_vec4, name) => {
let new_array = new array_vec4.constructor(other.length * 4);
if(typeof other.array_vec4[name] === "undefined") {
// then other must be a scalar
let other_scalar = other.scalar_vec4[name];
for(let i = this.length; i < other.length; i++) {
new_array[i * 4 + 0] = other_scalar[0];
new_array[i * 4 + 1] = other_scalar[1];
new_array[i * 4 + 2] = other_scalar[2];
new_array[i * 4 + 3] = other_scalar[3];
}
} else {
new_array.set(other.array_vec4[name].slice(this.length * 4), this.length * 4);
}
new_array.set(array_vec4);
return new_array;
});
this.length = other.length;
};
public select = (selected: [number]) => {
// copy since we will modify
let sizes = this.array['size'] = this.array['size'].slice();
let size_selected = this.array['size_selected'];
// copy since we will modify
let color = this.array_vec4['color'] = this.array_vec4['color'].slice();
let color_selected = this.array_vec4['color_selected'];
// this assumes, and requires that color_selected is an array, maybe a bit inefficient
_.each(selected, (index) => {
if(index < this.length) {
sizes[index] = size_selected[index];
color[index*4+0] = color_selected[index*4+0];
color[index*4+1] = color_selected[index*4+1];
color[index*4+2] = color_selected[index*4+2];
color[index*4+3] = color_selected[index*4+3];
}
});
};
public merge_to_vec3 = (names: [string], new_name: string) => {
let element_length = names.length;
let array = new Float32Array(this.length * element_length); // Float32Array should be replaced by a good common value
_.each(names, (name, index) => {
this.ensure_array(name);
let array1d = this.array[name];
for(let i = 0; i < this.length; i++) {
array[i*element_length + index] = array1d[i];
}
delete this.array[name];
delete this.values[name];
});
this.array_vec3[new_name] = array;
};
public pop = (name: string | [string]) => {
let names = _.isArray(name) ? name : [name];
_.each(names, (name) => {
_.each([this.scalar,
this.scalar_vec3,
this.array,
this.array_vec3],
(storage) => {
if(typeof storage[name] !== 'undefined') {
delete storage[name];
}
})
});
};
public add_attributes = (geometry: any, postfix: any) => {
postfix = postfix || '';
// set all attributes
_.each(this.array, (array, name) => {
if(name.indexOf("selected") === -1) { // selected attributes should not be send to the shader
let attr = new THREE.InstancedBufferAttribute(array, 1, 1);
geometry.addAttribute(name+postfix, attr);
}
});
_.each(this.array_vec3, (array, name) => {
if(name.indexOf("selected") === -1) { // selected attributes should not be send to the shader
let attr = new THREE.InstancedBufferAttribute(array, 3, 1);
attr.normalized = name.indexOf("color") === -1 ? false : true; // color should be normalized
geometry.addAttribute(name+postfix, attr);
}
});
_.each(this.array_vec4, (array, name) => {
if(name.indexOf("selected") === -1) { // selected attributes should not be send to the shader
let attr = new THREE.InstancedBufferAttribute(array, 4, 1);
attr.normalized = name.indexOf("color") === -1 ? false : true; // color should be normalized
geometry.addAttribute(name+postfix, attr);
}
});
_.each(this.scalar, (scalar, name) => {
if(name.indexOf("selected") === -1) { // selected attributes should not be send to the shader
let attr = new THREE.InstancedBufferAttribute(new Float32Array([scalar]), 1, this.length);
geometry.addAttribute(name+postfix, attr);
}
});
_.each(this.scalar_vec3, (scalar_vec3, name) => {
if(name.indexOf("selected") === -1) { // selected attributes should not be send to the shader
let attr = new THREE.InstancedBufferAttribute(scalar_vec3, 3, this.length);
attr.normalized = name.indexOf("color") == -1 ? false : true; // color should be normalized
geometry.addAttribute(name+postfix, attr);
}
});
_.each(this.scalar_vec4, (scalar_vec4, name) => {
if(name.indexOf("selected") === -1) { // selected attributes should not be send to the shader
let attr = new THREE.InstancedBufferAttribute(scalar_vec4, 4, this.length);
attr.normalized = name.indexOf("color") == -1 ? false : true; // color should be normalized
geometry.addAttribute(name+postfix, attr);
}
});
};
}
export = {Values: Values};