Skip to content

Commit

Permalink
Overhaul raycasting/lookat code
Browse files Browse the repository at this point in the history
- fixed math so lookat/lookaway fire at the right times
- panorama, sky and video spheres are no longer raycasted. huge performance boost on mobile
  • Loading branch information
brianchirls committed Aug 20, 2015
1 parent d2ed8f7 commit 07203b7
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 135 deletions.
224 changes: 112 additions & 112 deletions build/vr.dev.js

Large diffs are not rendered by default.

32 changes: 15 additions & 17 deletions build/vr.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/vr.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"author": "Brian Chirls <[email protected]> (http://chirls.com)",
"license": "MIT",
"devDependencies": {
"css-loader": "^0.12.0",
"css-loader": "^0.16.0",
"event-emitter": "^0.3.2",
"file-loader": "^0.8.1",
"gulp": "^3.8.10",
Expand Down
2 changes: 2 additions & 0 deletions src/objects/panorama.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ module.exports = (function () {

parent.add(mesh);

this.raycastable = false;

return mesh;
};
}());
2 changes: 2 additions & 0 deletions src/objects/sky.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ module.exports = (function () {

this.setOptions(options);

this.raycastable = false;

return obj.mesh;
};
}());
1 change: 1 addition & 0 deletions src/objects/video.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ module.exports = (function () {
document.addEventListener('webkitvisibilitychange', visibilityChange);

mesh.name = 'video';
this.raycastable = !(options && options.sphere);
parent.add(mesh);

return mesh;
Expand Down
2 changes: 2 additions & 0 deletions src/vr-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ module.exports = (function () {

VRObject.prototype.update = function () {};

VRObject.prototype.raycastable = true;

VRObject.repeat = function (count, options) {
var i,
change = false,
Expand Down
43 changes: 39 additions & 4 deletions src/vr.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

//todo: use a weak map or set instead
vrObjects = [],
raycastable = [],

lastTick = 0,
animationCallbacks = [];
Expand All @@ -81,25 +82,53 @@
}
}

function pruneObject(object) {
var i = raycastable.indexOf(object);
if (i >= 0) {
raycastable.splice(i, 1);
}

i = vrObjects.indexOf(VRObject.findObject(object));
if (i >= 0) {
vrObjects.splice(i, 1);
}

object.children.forEach(pruneObject);
}

function raycast() {
var i,
intersect,
object,
intersects,
parent,
prune = [],
vrObject;

raycaster.ray.origin.copy( camera.position );
raycaster.ray.direction.set(0, 0, 0.5).unproject(camera).sub(camera.position).normalize();
raycaster.ray.origin.setFromMatrixPosition(camera.matrixWorld); // world position
raycaster.ray.direction.set(0, 0, 0.5).unproject(camera).sub(raycaster.ray.origin).normalize();

intersects = raycaster.intersectObjects( scene.children );
intersects = raycaster.intersectObjects(raycastable, true);
for (i = 0; i < intersects.length; i++) {
intersect = intersects[i];
if (intersect.object instanceof THREE.Mesh) {

// if object has been removed from scene, remove it from raycastable
parent = intersect.object;
while (parent && parent !== scene) {
if (!parent.parent) {
prune.push(parent);
}
parent = parent.parent;
}

if (parent && intersect.object instanceof THREE.Mesh) {
object = intersect.object;
break;
}
}

prune.forEach(pruneObject);

if (target !== object) {
if (target) {
vrObject = VRObject.findObject(target);
Expand Down Expand Up @@ -522,12 +551,18 @@
VR[method] = function (options) {
var obj = new VRObject(scene, creator, body, options);
vrObjects.push(obj);
if (obj.raycastable) {
raycastable.push(obj.object);
}
return obj;
};

VRObject.prototype[method] = function (options) {
var obj = new VRObject(this.object, creator, body, options);
vrObjects.push(obj);
if (obj.raycastable) {
raycastable.push(obj.object);
}
return obj;
};

Expand Down

0 comments on commit 07203b7

Please sign in to comment.