forked from michaljaz/webmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbg.js
More file actions
106 lines (82 loc) · 3.58 KB
/
mbg.js
File metadata and controls
106 lines (82 loc) · 3.58 KB
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
/**
* Combine indexed and non-indexed BufferGeometry into a new indexed BufferGeometry. All missing uniforms are set to 0.
* @param {array} geometries Array of THREE.BufferGeometry instances
* @returns {BufferGeometry}
*/
function mergeBufferGeometries (geometries) {
var indexLength = 0,
verticesLength = 0,
attributesInfos = {},
geometriesInfos = [],
geometryInfo,
referenceAttributesKeys = [],
attributesKeys,
countVertices,
i,
j,
k;
// read the geometries and attributes information, calculate indexLength and verticesLength
for (i = 0; i < geometries.length; i++) {
attributesKeys = Object.keys(geometries[i].attributes);
geometryInfo = {
indexed: geometries[i].index !== null,
vertices: geometries[i].attributes[attributesKeys[0]].count
};
geometriesInfos.push(geometryInfo);
if (geometryInfo.indexed) {
indexLength += geometries[i].index.count;
} else {
indexLength += geometryInfo.vertices;
}
verticesLength += geometryInfo.vertices;
for (j = 0; j < attributesKeys.length; j++) {
if (referenceAttributesKeys.indexOf(attributesKeys[j]) === -1) {
referenceAttributesKeys.push(attributesKeys[j]);
attributesInfos[attributesKeys[j]] = {
array: null,
constructor: geometries[i].attributes[attributesKeys[j]].array.constructor,
itemSize: geometries[i].attributes[attributesKeys[j]].itemSize
};
}
}
}
// prepare the new BufferGeometry and its attributes
var newGeometry = new THREE.BufferGeometry(),
indexArray = verticesLength > 0xFFFF ? new Uint32Array(indexLength) : new Uint16Array(indexLength);
for (i = 0; i < referenceAttributesKeys.length; i++) {
attributesInfos[referenceAttributesKeys[i]].array = new (attributesInfos[referenceAttributesKeys[i]].constructor)(
verticesLength * attributesInfos[referenceAttributesKeys[i]].itemSize
);
newGeometry.addAttribute(referenceAttributesKeys[i], new THREE.BufferAttribute(
attributesInfos[referenceAttributesKeys[i]].array,
attributesInfos[referenceAttributesKeys[i]].itemSize
));
}
// copy all the data in the new BufferGeometry
var offsetIndices = 0,
offsetVertices = 0,
offsetAttribute;
for (i = 0; i < geometries.length; i++) {
geometryInfo = geometriesInfos[i];
if (geometryInfo.indexed) {
for (j = 0; j < geometries[i].index.count; j++) {
indexArray[offsetIndices + j] = offsetVertices + geometries[i].index.array[j];
}
offsetIndices += geometries[i].index.count;
} else {
for (j = 0; j < geometryInfo.vertices; j++) {
indexArray[offsetIndices + j] = offsetVertices + j;
}
offsetIndices += geometryInfo.vertices;
}
for (j = 0; j < referenceAttributesKeys.length; j++) {
offsetAttribute = offsetVertices * attributesInfos[referenceAttributesKeys[j]].itemSize;
if (geometries[i].attributes[referenceAttributesKeys[j]]) {
attributesInfos[referenceAttributesKeys[j]].array.set(geometries[i].attributes[referenceAttributesKeys[j]].array, offsetAttribute);
}
}
offsetVertices += geometryInfo.vertices;
}
newGeometry.setIndex(new THREE.BufferAttribute(indexArray, 1));
return newGeometry;
}