Skip to content

Commit

Permalink
Implement inView property on objects to determine whether currently…
Browse files Browse the repository at this point in the history
… seen by camera #18
  • Loading branch information
brianchirls committed Nov 17, 2015
1 parent 36075d7 commit 3ac3dcb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build/vr.dev.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 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.

6 changes: 6 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
VR.cylinder().moveTo(2, 1, 1);

var torus = VR.torus().moveTo(3, 2, 4);
var torusWasInView = false;

var oldColor = new THREE.Color();
VR.on('lookat', function () {
Expand All @@ -57,6 +58,11 @@
torus.rotateY(delta / 10);

soundSource.moveTo(10 * Math.cos(time / 1), 4, 0);

if (torus.inView && !torusWasInView) {
torus.rotateX(Math.PI / 4);
}
torusWasInView = torus.inView;
});

VR.on('shake', function () {
Expand Down
34 changes: 31 additions & 3 deletions src/vr-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,34 @@ module.exports = (function () {
return object.geometry.boundingBox.distanceToPoint(scratchVector1);
}

function VRObject(parent, creator, camera, options) {
function VRObject(parent, creator, body, options) {
var material,
object,
self = this,

isNear = false,
isTarget = false,

camera = body && (function () {
var i,
c;
for (i = 0; i < body.children.length; i++) {
c = body.children[i];
if (c instanceof THREE.PerspectiveCamera) {
return c;
}
}
}()),
frustum,
cameraViewProjectionMatrix,

raycaster;

options = options || {};

eventEmitter(this);

if (camera) {
if (body) {
// raycaster = new THREE.Raycaster();

this.update = function () {
Expand Down Expand Up @@ -141,9 +155,23 @@ module.exports = (function () {

Object.defineProperty(this, 'distance', {
get: function () {
return distance(self.object, camera || parent);
return distance(self.object, body || parent);
}
});

if (camera) {
frustum = new THREE.Frustum();
cameraViewProjectionMatrix = new THREE.Matrix4();
Object.defineProperty(this, 'inView', {
get: function () {
camera.updateMatrixWorld(); // make sure the camera matrix is updated
camera.matrixWorldInverse.getInverse(camera.matrixWorld);
cameraViewProjectionMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
frustum.setFromMatrix(cameraViewProjectionMatrix);
return frustum.intersectsObject(self.object);
}
});
}
}

VRObject.prototype.hide = function () {
Expand Down

0 comments on commit 3ac3dcb

Please sign in to comment.