Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ghemingway/cad.js into fe…
Browse files Browse the repository at this point in the history
…ature/tyson
  • Loading branch information
Gabor Pap committed Mar 24, 2014
2 parents ec782bf + 24eae9f commit b778c5b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions public/javascript/build.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions public/javascript/cad.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ define(["jquery", "jstree", "data_loader", "viewer"], function($, jstree, DataLo
// Add the part to the list
self._parts.push(part);
// calculate the scene's radius for draw distance calculations
self._viewer.controls.sceneRadius = part.getBoundingBox().size().length() * 0.5;
self._viewer.updateSceneBoundingBox(part.getBoundingBox());
// center the view
self._viewer.zoomToFit(part);
// Update the tree
Expand Down Expand Up @@ -418,12 +418,13 @@ define(["jquery", "jstree", "data_loader", "viewer"], function($, jstree, DataLo
obj.explode(distance);
}
}
this._viewer.updateSceneBoundingBox(this._parts[0].getBoundingBox());
this._viewer.invalidate();
}
};

CADjs.prototype.getExplodeDistance = function () {
return this._viewer.controls.sceneRadius * 0.05;
return this._viewer.sceneRadius * 0.05;
};
/*
CADjs.prototype.setSelectedOpacity = function(opacity) {
Expand Down
25 changes: 21 additions & 4 deletions public/javascript/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

define(["THREE", "compass", "viewer_controls"], function(THREE, Compass, ViewerControls) {
function Viewer(CADjs) {
var shouldRender = false,
var that = this,
shouldRender = false,
continuousRendering = false,
canvasParent, renderer, canvas, geometryScene, annotationScene, overlayScene, camera,
controls, compass,
Expand All @@ -25,6 +26,9 @@ define(["THREE", "compass", "viewer_controls"], function(THREE, Compass, ViewerC
format: THREE.RGBAFormat
};

this.sceneCenter = new THREE.Vector3(0,0,0);
this.sceneRadius = 10000;

// RENDERER
canvasParent = document.getElementById(CADjs._viewContainerId);
renderer = new THREE.WebGLRenderer({
Expand Down Expand Up @@ -161,7 +165,16 @@ define(["THREE", "compass", "viewer_controls"], function(THREE, Compass, ViewerC

// CONTROL EVENT HANDLERS
controls.addEventListener("change", function() {
camera.far = (controls.sceneRadius || 0) + camera.position.length();
var x0 = that.sceneCenter,
x1 = camera.position,
x2 = controls.target,
x2subX1 = x2.clone().sub(x1),
x1subX0 = x1.clone().sub(x0),
c = x2subX1.clone().cross(x1.clone().sub(x0)).lengthSq() / x2subX1.lengthSq(),
d = Math.sqrt(Math.abs(c - x1subX0.lengthSq()));
camera.near = Math.max(0.1, d - that.sceneRadius);
camera.far = d + that.sceneRadius;
camera.updateProjectionMatrix();
invalidate();
});
controls.addEventListener("start", function() {
Expand All @@ -181,10 +194,9 @@ define(["THREE", "compass", "viewer_controls"], function(THREE, Compass, ViewerC
renderPassFXAA.uniforms['resolution'].value.set(1/canvasParent.offsetWidth, 1/canvasParent.offsetHeight);
renderer.setSize(canvasParent.offsetWidth, canvasParent.offsetHeight);
camera.aspect = canvasParent.offsetWidth / canvasParent.offsetHeight;
camera.updateProjectionMatrix();
camera.lookAt(geometryScene.position);
composer.reset();
controls.handleResize();
controls.dispatchEvent({type: 'change'});
render();
});

Expand All @@ -197,6 +209,11 @@ define(["THREE", "compass", "viewer_controls"], function(THREE, Compass, ViewerC
animate(true); // Initial Rendering
}

Viewer.prototype.updateSceneBoundingBox = function (newBoundingBox) {
this.sceneCenter.copy(newBoundingBox.center());
this.sceneRadius = newBoundingBox.size().length()/2;
};

// Extend Viewer with events
THREE.EventDispatcher.prototype.apply(Viewer.prototype);
return Viewer;
Expand Down
1 change: 0 additions & 1 deletion public/javascript/viewer_controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ define(["THREE", "TrackballControls"], function(THREE, TrackballControls) {
trackballControl.up0.set( 0, 1, 0 );
trackballControl.position0 = referenceOrientation.clone().applyEuler( viewAngles );
trackballControl.position0.multiplyScalar( -viewDistance );
trackballControl.sceneRadius = viewDistance;
trackballControl.reset();
trackballControl.setRotationFromEuler = function (euler, opt_upVector) {
var distance = camera.position.distanceTo(this.target);
Expand Down

0 comments on commit b778c5b

Please sign in to comment.