-
Notifications
You must be signed in to change notification settings - Fork 426
/
index.js
90 lines (67 loc) · 2.39 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
if (!THREE.BufferGeometryUtils) {
require('./lib/BufferGeometryUtils');
}
AFRAME.utils.setBufferGeometryColor = function () {
const colorHelper = new THREE.Color();
return function (geometry, color, start, end) {
// ES5 compatible default parameters
if (start === undefined) start = 0;
if (end === undefined) end = Infinity;
var i;
const colors = geometry.getAttribute('color')
const itemSize = colors.itemSize;
const array = colors.array
colorHelper.set(color);
const verticesEnd = Math.min(end, colors.count) * itemSize
for (i = start * itemSize; i <= verticesEnd; i += itemSize) {
array[i] = colorHelper.r;
array[i + 1] = colorHelper.g;
array[i + 2] = colorHelper.b;
}
colors.needsUpdate = true;
}
}()
AFRAME.registerComponent('geometry-merger', {
schema: {
preserveOriginal: {default: false}
},
init: function () {
var geometries = [];
this.vertexIndex = {};
var self = this;
var vertexCount = 0;
this.el.object3D.updateMatrixWorld()
this.el.object3D.traverse(function (mesh) {
if (mesh.type !== 'Mesh' || mesh.el === self.el) { return; }
var geometry = mesh.geometry.clone();
var currentMesh = mesh;
while (currentMesh !== self.el.object3D) {
geometry.applyMatrix4(currentMesh.parent.matrix);
currentMesh = currentMesh.parent;
}
geometries.push(geometry);
meshPositions = mesh.geometry.getAttribute('position');
self.vertexIndex[mesh.parent.uuid] = [
vertexCount,
vertexCount + meshPositions.count - 1
];
vertexCount += meshPositions.count;
// Remove mesh if not preserving.
if (!self.data.preserveOriginal) { mesh.parent.remove(mesh); }
});
this.geometry = THREE.BufferGeometryUtils.mergeBufferGeometries(geometries);
this.mesh = new THREE.Mesh(this.geometry);
this.el.setObject3D('mesh', this.mesh);
// dereference original geometries (so they can be freed when no longer needed)
geometries.length = 0;
},
getColor: function (uuid, color) {
const colors = this.geometry.getAttribute('color')
color.fromBufferAttribute(colors, this.vertexIndex[uuid][0]);
return color;
},
setColor: function (uuid, color) {
const vertexData = this.vertexIndex[uuid];
AFRAME.utils.setBufferGeometryColor(this.geometry, color, vertexData[0], vertexData[1]);
}
});