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

A few updates. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
42 changes: 20 additions & 22 deletions sim/sim.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,11 @@ Sim.App.prototype.init = function(param)
var root = new THREE.Object3D();
scene.add(root);

// Create a projector to handle picking
var projector = new THREE.Projector();

// Save away a few things
this.container = container;
this.renderer = renderer;
this.scene = scene;
this.camera = camera;
this.projector = projector;
this.root = root;

// Set up event handlers
Expand Down Expand Up @@ -188,12 +184,8 @@ Sim.App.prototype.initMouse = function()
function(e) { that.onDocumentMouseDown(e); }, false );
dom.addEventListener( 'mouseup',
function(e) { that.onDocumentMouseUp(e); }, false );

$(dom).mousewheel(
function(e, delta) {
that.onDocumentMouseScroll(e, delta);
}
);

dom.addEventListener('wheel', function(e){that.onDocumentMouseScroll(e)}, false);

this.overObject = null;
this.clickedObject = null;
Expand Down Expand Up @@ -327,34 +319,40 @@ Sim.App.prototype.onDocumentMouseUp = function(event)
this.clickedObject = null;
}

Sim.App.prototype.onDocumentMouseScroll = function(event, delta)
Sim.App.prototype.onDocumentMouseScroll = function(event)
{
event.preventDefault();

if (this.handleMouseScroll)
{
var e = window.event || e;
var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
this.handleMouseScroll(delta);
}
}

Sim.App.prototype.objectFromMouse = function(pagex, pagey)
{
// Translate page coords to element coords
var offset = $(this.renderer.domElement).offset();
var eltx = pagex - offset.left;
var elty = pagey - offset.top;
var offsetLeft = this.renderer.domElement.offsetLeft;
var offsetTop = this.renderer.domElement.offsetTop;
var eltx = pagex - offsetLeft;
var elty = pagey - offsetTop;

// Translate client coords into viewport x,y
var vpx = ( eltx / this.container.offsetWidth ) * 2 - 1;
var vpy = - ( elty / this.container.offsetHeight ) * 2 + 1;

var vector = new THREE.Vector3( vpx, vpy, 0.5 );

this.projector.unprojectVector( vector, this.camera );
var vpy = - ( elty / this.container.offsetHeight ) * 2 + 1;

var ray = new THREE.Ray( this.camera.position, vector.subSelf( this.camera.position ).normalize() );

var intersects = ray.intersectScene( this.scene );
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();

mouse.x = vpx;
mouse.y = vpy;

raycaster.setFromCamera(mouse, this.camera);

var intersects = raycaster.intersectObjects( this.scene.children );


if ( intersects.length > 0 ) {

Expand Down