Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save array creations on each raycaster update #3317

Merged
merged 1 commit into from
Dec 25, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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