-
Notifications
You must be signed in to change notification settings - Fork 240
/
primitive.js
728 lines (635 loc) · 28 KB
/
primitive.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
import { initGlForMembers } from './utils.js';
import { GltfObject } from './gltf_object.js';
import { gltfBuffer } from './buffer.js';
import { gltfImage } from './image.js';
import { ImageMimeType } from './image_mime_type.js';
import { gltfTexture } from './texture.js';
import { gltfTextureInfo } from './texture.js';
import { gltfSampler } from './sampler.js';
import { gltfBufferView } from './buffer_view.js';
import { DracoDecoder } from '../ResourceLoader/draco.js';
import { GL } from '../Renderer/webgl.js';
class gltfPrimitive extends GltfObject
{
constructor()
{
super();
this.attributes = [];
this.targets = [];
this.indices = undefined;
this.material = undefined;
this.mode = GL.TRIANGLES;
// non gltf
this.glAttributes = [];
this.morphTargetTextureInfo = undefined;
this.defines = [];
this.skip = true;
this.hasWeights = false;
this.hasJoints = false;
this.hasNormals = false;
this.hasTangents = false;
this.hasTexcoord = false;
this.hasColor = false;
// The primitive centroid is used for depth sorting.
this.centroid = undefined;
}
initGl(gltf, webGlContext)
{
// Use the default glTF material.
if (this.material === undefined)
{
this.material = gltf.materials.length - 1;
}
initGlForMembers(this, gltf, webGlContext);
const maxAttributes = webGlContext.getParameter(GL.MAX_VERTEX_ATTRIBS);
// https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes
if (this.extensions !== undefined)
{
if (this.extensions.KHR_draco_mesh_compression !== undefined)
{
const dracoDecoder = new DracoDecoder();
if (dracoDecoder !== undefined && Object.isFrozen(dracoDecoder))
{
let dracoGeometry = this.decodeDracoBufferToIntermediate(
this.extensions.KHR_draco_mesh_compression, gltf);
this.copyDataFromDecodedGeometry(gltf, dracoGeometry, this.attributes);
}
else
{
console.warn('Failed to load draco compressed mesh: DracoDecoder not initialized');
}
}
}
// VERTEX ATTRIBUTES
for (const attribute of Object.keys(this.attributes))
{
if(this.glAttributes.length >= maxAttributes)
{
console.error("To many vertex attributes for this primitive, skipping " + attribute);
break;
}
const idx = this.attributes[attribute];
this.glAttributes.push({ attribute: attribute, name: "a_" + attribute.toLowerCase(), accessor: idx });
this.defines.push(`HAS_${attribute}_${gltf.accessors[idx].type} 1`);
switch (attribute)
{
case "POSITION":
this.skip = false;
break;
case "NORMAL":
this.hasNormals = true;
break;
case "TANGENT":
this.hasTangents = true;
break;
case "TEXCOORD_0":
this.hasTexcoord = true;
break;
case "TEXCOORD_1":
this.hasTexcoord = true;
break;
case "COLOR_0":
this.hasColor = true;
break;
case "JOINTS_0":
this.hasJoints = true;
break;
case "WEIGHTS_0":
this.hasWeights = true;
break;
case "JOINTS_1":
this.hasJoints = true;
break;
case "WEIGHTS_1":
this.hasWeights = true;
break;
default:
console.log("Unknown attribute: " + attribute);
}
}
// MORPH TARGETS
if (this.targets !== undefined && this.targets.length > 0)
{
const max2DTextureSize = Math.pow(webGlContext.getParameter(GL.MAX_TEXTURE_SIZE), 2);
const maxTextureArraySize = webGlContext.getParameter(GL.MAX_ARRAY_TEXTURE_LAYERS);
// Check which attributes are affected by morph targets and
// define offsets for the attributes in the morph target texture.
const attributeOffsets = {};
let attributeOffset = 0;
// Gather used attributes from all targets (some targets might
// use more attributes than others)
const attributes = Array.from(this.targets.reduce((acc, target) => {
Object.keys(target).map(val => acc.add(val));
return acc;
}, new Set()));
const vertexCount = gltf.accessors[this.attributes[attributes[0]]].count;
this.defines.push(`NUM_VERTICIES ${vertexCount}`);
let targetCount = this.targets.length;
if (targetCount * attributes.length > maxTextureArraySize)
{
targetCount = Math.floor(maxTextureArraySize / attributes.length);
console.warn(`Morph targets exceed texture size limit. Only ${targetCount} of ${this.targets.length} are used.`);
}
for (const attribute of attributes)
{
// Add morph target defines
this.defines.push(`HAS_MORPH_TARGET_${attribute} 1`);
this.defines.push(`MORPH_TARGET_${attribute}_OFFSET ${attributeOffset}`);
// Store the attribute offset so that later the
// morph target texture can be assembled.
attributeOffsets[attribute] = attributeOffset;
attributeOffset += targetCount;
}
this.defines.push("HAS_MORPH_TARGETS 1");
if (vertexCount <= max2DTextureSize) {
// Allocate the texture buffer. Note that all target attributes must be vec3 types and
// all must have the same vertex count as the primitives other attributes.
const width = Math.ceil(Math.sqrt(vertexCount));
const singleTextureSize = Math.pow(width, 2) * 4;
const morphTargetTextureArray = new Float32Array(singleTextureSize * targetCount * attributes.length);
// Now assemble the texture from the accessors.
for (let i = 0; i < targetCount; ++i)
{
let target = this.targets[i];
for (let [attributeName, offsetRef] of Object.entries(attributeOffsets)){
if (target[attributeName] != undefined) {
const accessor = gltf.accessors[target[attributeName]];
const offset = offsetRef * singleTextureSize;
if (accessor.componentType != GL.FLOAT && accessor.normalized == false){
console.warn("Unsupported component type for morph targets");
attributeOffsets[attributeName] = offsetRef + 1;
continue;
}
const data = accessor.getNormalizedDeinterlacedView(gltf);
switch(accessor.type)
{
case "VEC2":
case "VEC3":
{
// Add padding to fit vec2/vec3 into rgba
let paddingOffset = 0;
let accessorOffset = 0;
const componentCount = accessor.getComponentCount(accessor.type);
for (let j = 0; j < accessor.count; ++j) {
morphTargetTextureArray.set(data.subarray(accessorOffset, accessorOffset + componentCount), offset + paddingOffset);
paddingOffset += 4;
accessorOffset += componentCount;
}
break;
}
case "VEC4":
morphTargetTextureArray.set(data, offset);
break;
default:
console.warn("Unsupported attribute type for morph targets");
break;
}
}
attributeOffsets[attributeName] = offsetRef + 1;
}
}
// Add the morph target texture.
// We have to create a WebGL2 texture as the format of the
// morph target texture has to be explicitly specified
// (gltf image would assume uint8).
let texture = webGlContext.createTexture();
webGlContext.bindTexture( webGlContext.TEXTURE_2D_ARRAY, texture);
// Set texture format and upload data.
let internalFormat = webGlContext.RGBA32F;
let format = webGlContext.RGBA;
let type = webGlContext.FLOAT;
let data = morphTargetTextureArray;
webGlContext.texImage3D(
webGlContext.TEXTURE_2D_ARRAY,
0, //level
internalFormat,
width,
width,
targetCount * attributes.length, //Layer count
0, //border
format,
type,
data);
// Ensure mipmapping is disabled and the sampler is configured correctly.
webGlContext.texParameteri( GL.TEXTURE_2D_ARRAY, GL.TEXTURE_WRAP_S, GL.CLAMP_TO_EDGE);
webGlContext.texParameteri( GL.TEXTURE_2D_ARRAY, GL.TEXTURE_WRAP_T, GL.CLAMP_TO_EDGE);
webGlContext.texParameteri( GL.TEXTURE_2D_ARRAY, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
webGlContext.texParameteri( GL.TEXTURE_2D_ARRAY, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
// Now we add the morph target texture as a gltf texture info resource, so that
// we can just call webGl.setTexture(..., gltfTextureInfo, ...) in the renderer.
const morphTargetImage = new gltfImage(
undefined, // uri
GL.TEXTURE_2D_ARRAY, // type
0, // mip level
undefined, // buffer view
undefined, // name
ImageMimeType.GLTEXTURE, // mimeType
texture // image
);
gltf.images.push(morphTargetImage);
gltf.samplers.push(new gltfSampler(GL.NEAREST, GL.NEAREST, GL.CLAMP_TO_EDGE, GL.CLAMP_TO_EDGE, undefined));
const morphTargetTexture = new gltfTexture(
gltf.samplers.length - 1,
gltf.images.length - 1,
GL.TEXTURE_2D_ARRAY);
// The webgl texture is already initialized -> this flag informs
// webgl.setTexture about this.
morphTargetTexture.initialized = true;
gltf.textures.push(morphTargetTexture);
this.morphTargetTextureInfo = new gltfTextureInfo(gltf.textures.length - 1, 0, true);
this.morphTargetTextureInfo.samplerName = "u_MorphTargetsSampler";
this.morphTargetTextureInfo.generateMips = false;
} else {
console.warn("Mesh of Morph targets too big. Cannot apply morphing.");
}
}
this.computeCentroid(gltf);
}
computeCentroid(gltf)
{
const positionsAccessor = gltf.accessors[this.attributes.POSITION];
const positions = positionsAccessor.getNormalizedTypedView(gltf);
if(this.indices !== undefined)
{
// Primitive has indices.
const indicesAccessor = gltf.accessors[this.indices];
const indices = indicesAccessor.getTypedView(gltf);
const acc = new Float32Array(3);
for(let i = 0; i < indices.length; i++) {
const offset = 3 * indices[i];
acc[0] += positions[offset];
acc[1] += positions[offset + 1];
acc[2] += positions[offset + 2];
}
const centroid = new Float32Array([
acc[0] / indices.length,
acc[1] / indices.length,
acc[2] / indices.length,
]);
this.centroid = centroid;
}
else
{
// Primitive does not have indices.
const acc = new Float32Array(3);
for(let i = 0; i < positions.length; i += 3) {
acc[0] += positions[i];
acc[1] += positions[i + 1];
acc[2] += positions[i + 2];
}
const positionVectors = positions.length / 3;
const centroid = new Float32Array([
acc[0] / positionVectors,
acc[1] / positionVectors,
acc[2] / positionVectors,
]);
this.centroid = centroid;
}
}
getShaderIdentifier()
{
return "primitive.vert";
}
getDefines()
{
return this.defines;
}
fromJson(jsonPrimitive)
{
super.fromJson(jsonPrimitive);
if(jsonPrimitive.extensions !== undefined)
{
this.fromJsonPrimitiveExtensions(jsonPrimitive.extensions);
}
}
fromJsonPrimitiveExtensions(jsonExtensions)
{
if(jsonExtensions.KHR_materials_variants !== undefined)
{
this.fromJsonVariants(jsonExtensions.KHR_materials_variants);
}
}
fromJsonVariants(jsonVariants)
{
if(jsonVariants.mappings !== undefined)
{
this.mappings = jsonVariants.mappings;
}
}
copyDataFromDecodedGeometry(gltf, dracoGeometry, primitiveAttributes)
{
// indices
let indexBuffer = dracoGeometry.index.array;
if (this.indices !== undefined){
this.loadBufferIntoGltf(indexBuffer, gltf, this.indices, 34963,
"index buffer view");
}
// Position
if(dracoGeometry.attributes.POSITION !== undefined)
{
let positionBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.POSITION.array,
dracoGeometry.attributes.POSITION.componentType);
this.loadBufferIntoGltf(positionBuffer, gltf, primitiveAttributes["POSITION"], 34962,
"position buffer view");
}
// Normal
if(dracoGeometry.attributes.NORMAL !== undefined)
{
let normalBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.NORMAL.array,
dracoGeometry.attributes.NORMAL.componentType);
this.loadBufferIntoGltf(normalBuffer, gltf, primitiveAttributes["NORMAL"], 34962,
"normal buffer view");
}
// TEXCOORD_0
if(dracoGeometry.attributes.TEXCOORD_0 !== undefined)
{
let uvBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.TEXCOORD_0.array,
dracoGeometry.attributes.TEXCOORD_0.componentType);
this.loadBufferIntoGltf(uvBuffer, gltf, primitiveAttributes["TEXCOORD_0"], 34962,
"TEXCOORD_0 buffer view");
}
// TEXCOORD_1
if(dracoGeometry.attributes.TEXCOORD_1 !== undefined)
{
let uvBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.TEXCOORD_1.array,
dracoGeometry.attributes.TEXCOORD_1.componentType);
this.loadBufferIntoGltf(uvBuffer, gltf, primitiveAttributes["TEXCOORD_1"], 34962,
"TEXCOORD_1 buffer view");
}
// Tangent
if(dracoGeometry.attributes.TANGENT !== undefined)
{
let tangentBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.TANGENT.array,
dracoGeometry.attributes.TANGENT.componentType);
this.loadBufferIntoGltf(tangentBuffer, gltf, primitiveAttributes["TANGENT"], 34962,
"Tangent buffer view");
}
// Color
if(dracoGeometry.attributes.COLOR_0 !== undefined)
{
let colorBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.COLOR_0.array,
dracoGeometry.attributes.COLOR_0.componentType);
this.loadBufferIntoGltf(colorBuffer, gltf, primitiveAttributes["COLOR_0"], 34962,
"color buffer view");
}
// JOINTS_0
if(dracoGeometry.attributes.JOINTS_0 !== undefined)
{
let jointsBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.JOINTS_0.array,
dracoGeometry.attributes.JOINTS_0.componentType);
this.loadBufferIntoGltf(jointsBuffer, gltf, primitiveAttributes["JOINTS_0"], 34963,
"JOINTS_0 buffer view");
}
// WEIGHTS_0
if(dracoGeometry.attributes.WEIGHTS_0 !== undefined)
{
let weightsBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.WEIGHTS_0.array,
dracoGeometry.attributes.WEIGHTS_0.componentType);
this.loadBufferIntoGltf(weightsBuffer, gltf, primitiveAttributes["WEIGHTS_0"], 34963,
"WEIGHTS_0 buffer view");
}
// JOINTS_1
if(dracoGeometry.attributes.JOINTS_1 !== undefined)
{
let jointsBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.JOINTS_1.array,
dracoGeometry.attributes.JOINTS_1.componentType);
this.loadBufferIntoGltf(jointsBuffer, gltf, primitiveAttributes["JOINTS_1"], 34963,
"JOINTS_1 buffer view");
}
// WEIGHTS_1
if(dracoGeometry.attributes.WEIGHTS_1 !== undefined)
{
let weightsBuffer = this.loadArrayIntoArrayBuffer(dracoGeometry.attributes.WEIGHTS_1.array,
dracoGeometry.attributes.WEIGHTS_1.componentType);
this.loadBufferIntoGltf(weightsBuffer, gltf, primitiveAttributes["WEIGHTS_1"], 34963,
"WEIGHTS_1 buffer view");
}
}
loadBufferIntoGltf(buffer, gltf, gltfAccessorIndex, gltfBufferViewTarget, gltfBufferViewName)
{
const gltfBufferObj = new gltfBuffer();
gltfBufferObj.byteLength = buffer.byteLength;
gltfBufferObj.buffer = buffer;
gltf.buffers.push(gltfBufferObj);
const gltfBufferViewObj = new gltfBufferView();
gltfBufferViewObj.buffer = gltf.buffers.length - 1;
gltfBufferViewObj.byteLength = buffer.byteLength;
if(gltfBufferViewName !== undefined)
{
gltfBufferViewObj.name = gltfBufferViewName;
}
gltfBufferViewObj.target = gltfBufferViewTarget;
gltf.bufferViews.push(gltfBufferViewObj);
gltf.accessors[gltfAccessorIndex].byteOffset = 0;
gltf.accessors[gltfAccessorIndex].bufferView = gltf.bufferViews.length - 1;
}
loadArrayIntoArrayBuffer(arrayData, componentType)
{
let arrayBuffer;
switch (componentType)
{
case "Int8Array":
arrayBuffer = new ArrayBuffer(arrayData.length);
let int8Array = new Int8Array(arrayBuffer);
int8Array.set(arrayData);
break;
case "Uint8Array":
arrayBuffer = new ArrayBuffer(arrayData.length);
let uint8Array = new Uint8Array(arrayBuffer);
uint8Array.set(arrayData);
break;
case "Int16Array":
arrayBuffer = new ArrayBuffer(arrayData.length * 2);
let int16Array = new Int16Array(arrayBuffer);
int16Array.set(arrayData);
break;
case "Uint16Array":
arrayBuffer = new ArrayBuffer(arrayData.length * 2);
let uint16Array = new Uint16Array(arrayBuffer);
uint16Array.set(arrayData);
break;
case "Int32Array":
arrayBuffer = new ArrayBuffer(arrayData.length * 4);
let int32Array = new Int32Array(arrayBuffer);
int32Array.set(arrayData);
break;
case "Uint32Array":
arrayBuffer = new ArrayBuffer(arrayData.length * 4);
let uint32Array = new Uint32Array(arrayBuffer);
uint32Array.set(arrayData);
break;
default:
case "Float32Array":
arrayBuffer = new ArrayBuffer(arrayData.length * 4);
let floatArray = new Float32Array(arrayBuffer);
floatArray.set(arrayData);
break;
}
return arrayBuffer;
}
decodeDracoBufferToIntermediate(dracoExtension, gltf)
{
let dracoBufferViewIDX = dracoExtension.bufferView;
const origGltfDrBufViewObj = gltf.bufferViews[dracoBufferViewIDX];
const origGltfDracoBuffer = gltf.buffers[origGltfDrBufViewObj.buffer];
const totalBuffer = new Int8Array( origGltfDracoBuffer.buffer );
const actualBuffer = totalBuffer.slice(origGltfDrBufViewObj.byteOffset,
origGltfDrBufViewObj.byteOffset + origGltfDrBufViewObj.byteLength);
// decode draco buffer to geometry intermediate
let dracoDecoder = new DracoDecoder();
let draco = dracoDecoder.module;
let decoder = new draco.Decoder();
let decoderBuffer = new draco.DecoderBuffer();
decoderBuffer.Init(actualBuffer, origGltfDrBufViewObj.byteLength);
let geometry = this.decodeGeometry( draco, decoder, decoderBuffer, dracoExtension.attributes, gltf );
draco.destroy( decoderBuffer );
return geometry;
}
getDracoArrayTypeFromComponentType(componentType)
{
switch (componentType)
{
case GL.BYTE:
return "Int8Array";
case GL.UNSIGNED_BYTE:
return "Uint8Array";
case GL.SHORT:
return "Int16Array";
case GL.UNSIGNED_SHORT:
return "Uint16Array";
case GL.INT:
return "Int32Array";
case GL.UNSIGNED_INT:
return "Uint32Array";
case GL.FLOAT:
return "Float32Array";
default:
return "Float32Array";
}
}
decodeGeometry(draco, decoder, decoderBuffer, gltfDracoAttributes, gltf) {
let dracoGeometry;
let decodingStatus;
// decode mesh in draco decoder
let geometryType = decoder.GetEncodedGeometryType( decoderBuffer );
if ( geometryType === draco.TRIANGULAR_MESH ) {
dracoGeometry = new draco.Mesh();
decodingStatus = decoder.DecodeBufferToMesh( decoderBuffer, dracoGeometry );
}
else
{
throw new Error( 'DRACOLoader: Unexpected geometry type.' );
}
if ( ! decodingStatus.ok() || dracoGeometry.ptr === 0 ) {
throw new Error( 'DRACOLoader: Decoding failed: ' + decodingStatus.error_msg() );
}
let geometry = { index: null, attributes: {} };
let vertexCount = dracoGeometry.num_points();
// Gather all vertex attributes.
for(let dracoAttr in gltfDracoAttributes)
{
let componentType = GL.BYTE;
let accessotVertexCount;
// find gltf accessor for this draco attribute
for (const [key, value] of Object.entries(this.attributes))
{
if(key === dracoAttr)
{
componentType = gltf.accessors[value].componentType;
accessotVertexCount = gltf.accessors[value].count;
break;
}
}
// check if vertex count matches
if(vertexCount !== accessotVertexCount)
{
throw new Error(`DRACOLoader: Accessor vertex count ${accessotVertexCount} does not match draco decoder vertex count ${vertexCount}`);
}
componentType = this.getDracoArrayTypeFromComponentType(componentType);
let dracoAttribute = decoder.GetAttributeByUniqueId( dracoGeometry, gltfDracoAttributes[dracoAttr]);
var tmpObj = this.decodeAttribute( draco, decoder,
dracoGeometry, dracoAttr, dracoAttribute, componentType);
geometry.attributes[tmpObj.name] = tmpObj;
}
// Add index buffer
if ( geometryType === draco.TRIANGULAR_MESH ) {
// Generate mesh faces.
let numFaces = dracoGeometry.num_faces();
let numIndices = numFaces * 3;
let dataSize = numIndices * 4;
let ptr = draco._malloc( dataSize );
decoder.GetTrianglesUInt32Array( dracoGeometry, dataSize, ptr );
let index = new Uint32Array( draco.HEAPU32.buffer, ptr, numIndices ).slice();
draco._free( ptr );
geometry.index = { array: index, itemSize: 1 };
}
draco.destroy( dracoGeometry );
return geometry;
}
decodeAttribute( draco, decoder, dracoGeometry, attributeName, attribute, attributeType) {
let numComponents = attribute.num_components();
let numPoints = dracoGeometry.num_points();
let numValues = numPoints * numComponents;
let ptr;
let array;
let dataSize;
switch ( attributeType ) {
case "Float32Array":
dataSize = numValues * 4;
ptr = draco._malloc( dataSize );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_FLOAT32, dataSize, ptr );
array = new Float32Array( draco.HEAPF32.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
case "Int8Array":
ptr = draco._malloc( numValues );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_INT8, numValues, ptr );
array = new Int8Array( draco.HEAP8.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
case "Int16Array":
dataSize = numValues * 2;
ptr = draco._malloc( dataSize );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_INT16, dataSize, ptr );
array = new Int16Array( draco.HEAP16.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
case "Int32Array":
dataSize = numValues * 4;
ptr = draco._malloc( dataSize );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_INT32, dataSize, ptr );
array = new Int32Array( draco.HEAP32.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
case "Uint8Array":
ptr = draco._malloc( numValues );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_UINT8, numValues, ptr );
array = new Uint8Array( draco.HEAPU8.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
case "Uint16Array":
dataSize = numValues * 2;
ptr = draco._malloc( dataSize );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_UINT16, dataSize, ptr );
array = new Uint16Array( draco.HEAPU16.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
case "Uint32Array":
dataSize = numValues * 4;
ptr = draco._malloc( dataSize );
decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, draco.DT_UINT32, dataSize, ptr );
array = new Uint32Array( draco.HEAPU32.buffer, ptr, numValues ).slice();
draco._free( ptr );
break;
default:
throw new Error( 'DRACOLoader: Unexpected attribute type.' );
}
return {
name: attributeName,
array: array,
itemSize: numComponents,
componentType: attributeType
};
}
}
export { gltfPrimitive };