Skip to content

Commit

Permalink
save array creations on each raycaster update (#3317)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngokevin authored and dmarcos committed Dec 25, 2017
1 parent 6311f75 commit cfe3566
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions src/components/raycaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,13 @@ module.exports.Component = registerComponent('raycaster', {
*/
refreshObjects: function () {
var data = this.data;
var els;

// If objects not defined, intersect with everything.
var els = data.objects
els = data.objects
? this.el.sceneEl.querySelectorAll(data.objects)
: this.el.sceneEl.children;
this.objects = flattenChildrenShallow(els);
this.objects = this.flattenChildrenShallow(els);
this.dirty = false;
},

Expand Down Expand Up @@ -339,40 +341,44 @@ module.exports.Component = registerComponent('raycaster', {
this.lineData.start = data.origin;
this.lineData.end = endVec3.copy(this.unitLineEndVec3).multiplyScalar(length);
el.setAttribute('line', this.lineData);
}
});

/**
* Returns children of each element's object3D group. Children are flattened
* by one level, removing the THREE.Group wrapper, so that non-recursive
* raycasting remains useful.
*
* @param {Array<Element>} els
* @return {Array<THREE.Object3D>}
*/
function flattenChildrenShallow (els) {
var groups = [];
var objects = [];
var children;
var i;

// Push meshes onto list of objects to intersect.
for (i = 0; i < els.length; i++) {
if (els[i].object3D) {
groups.push(els[i].object3D);
}
}
},

// Each entity's root is a THREE.Group. Return the group's chilrden.
for (i = 0; i < groups.length; i++) {
children = groups[i].children;
if (children && children.length) {
objects.push.apply(objects, children);
}
}
/**
* Return children of each element's object3D group. Children are flattened
* by one level, removing the THREE.Group wrapper, so that non-recursive
* raycasting remains useful.
*
* @param {Array<Element>} els
* @return {Array<THREE.Object3D>}
*/
flattenChildrenShallow: (function () {
var groups = [];

return function (els) {
var children;
var i;
var objects = this.objects;

// Push meshes onto list of objects to intersect.
groups.length = 0;
for (i = 0; i < els.length; i++) {
if (els[i].object3D) {
groups.push(els[i].object3D);
}
}

return objects;
}
// Each entity's root is a THREE.Group. Return the group's chilrden.
objects.length = 0;
for (i = 0; i < groups.length; i++) {
children = groups[i].children;
if (children && children.length) {
objects.push.apply(objects, children);
}
}
return objects;
};
})()
});

/**
* Copy contents of one array to another without allocating new array.
Expand Down

0 comments on commit cfe3566

Please sign in to comment.