I have a WebGl/ThreeJS application, where I have 3 WebGL Renderers, 1 Scene and 1 Canvas for each renderer. The first 2 Canvases are ok, they work fine.
The other canvas, is divided in 3 equal-dimension viewports and in each viewport I have added an orthographic camera in a different position.
So I want to be able to detect mouse events on the objects that there in this Scene. I want to detect them in all of the viewports independently. All this, because I want to create a drag and drop functionality of some spheres I have in it.
I create my orthographic cameras as the above code giving 3 different positions for eachcamera (12 ,0,0) (0, 12,0) (0,0, 12) and giving the width,height of the viewport (not the canvas) :
var viewSize = 50 ;
var aspectRatio = width/height;
var camera = new THREE.OrthographicCamera( viewSize*aspectRatio / - 2, viewSize*aspectRatio / 2, viewSize / 2, viewSize / - 2, 0, 100);
camera.position.set(x,y,z);
camera.lookAt(new THREE.Vector3(0,0,0) );
After having set up the cameras/viewports, and based on this very usefull post : post ,I created and called 1 time for each viewport/camera this function:
{
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
function MouseEvents( spheres, func, _camera, domElement ) {
this.plane = {'object3d' : undefined} ;
this.camera = _camera;
this.container = domElement;
this.objects = spheres;
var _this =this;
this.offset = new THREE.Vector3();
this.INTERSECTED;
this.SELECTED;
if(func === 'dragNdrop'){
this.plane.object3d = new THREE.Mesh(
new THREE.PlaneBufferGeometry( 10000, 10000, 2, 2 ),
new THREE.MeshBasicMaterial( { color: "#"+((1<<24)*Math.random()|0).toString(16) } )
);
this.plane.object3d.visible = true;
if(this.container === 'motifPosX' ) this.plane.object3d.position.z = -100;
if(this.container === 'motifPosY' ) this.plane.object3d.position.x = -100;
if(this.container === 'motifPosZ' ) this.plane.object3d.position.y = -100;
this.plane.object3d.lookAt(this.camera.position);
MotifExplorer.add(this.plane);
}
var mMoove = this.onDocumentMouseMove.bind(this) ;
var mDown = this.onDocumentMouseDown.bind(this) ;
var mUp = this.onDocumentMouseUp.bind(this) ;
document.getElementById(this.container).addEventListener("mousemove", mMoove, false);
document.getElementById(this.container).addEventListener("mousedown", mDown, false);
document.getElementById(this.container).addEventListener("mouseup", mUp, false);
};
MouseEvents.prototype.onDocumentMouseMove = function(event){
var _this = this;
event.preventDefault();
_this.plane.object3d.lookAt( _this.camera.position );
mouse.x = ( event.clientX / $('#'+_this.container).width() ) * 2 - 1;
mouse.y = - ( event.clientY / $('#'+_this.container).height() )* 2 + 1;
//
raycaster.setFromCamera( mouse, _this.camera );
if ( _this.SELECTED ) {
var intersects = raycaster.intersectObject( _this.plane.object3d );
_this.SELECTED.position.copy( intersects[ 0 ].point.sub( _this.offset ) );
return;
}
var intersects = raycaster.intersectObjects( _this.getAtoms() );
if ( intersects.length > 0 && intersects[0].object.parent.name ==='atom') {
if ( _this.INTERSECTED != intersects[0].object.parent ) {
_this.INTERSECTED = intersects[0].object.parent;
_this.plane.object3d.position.copy( _this.INTERSECTED.position );
}
document.getElementById(_this.container).style.cursor = 'pointer';
}
else {
_this.INTERSECTED = null;
document.getElementById(_this.container).style.cursor = 'auto';
}
}
MouseEvents.prototype.onDocumentMouseDown = function(event){
var _this =this;
event.preventDefault();
raycaster.setFromCamera( mouse, _this.camera );
var intersects = raycaster.intersectObjects( _this.getAtoms() );
if ( intersects.length > 0 && intersects[0].object.parent.name ==='atom') {
_this.SELECTED = intersects[0].object.parent;
var intersects = raycaster.intersectObject( _this.plane.object3d );
_this.offset.copy( intersects[ 0 ].point ).sub( _this.plane.object3d.position );
document.getElementById(_this.container).style.cursor = 'move';
}
}
MouseEvents.prototype.onDocumentMouseUp = function(event){
var _this =this;
event.preventDefault();
if ( _this.INTERSECTED ) {
_this.plane.object3d.position.copy( _this.INTERSECTED.position );
_this.SELECTED = null;
}
document.getElementById(_this.container).style.cursor = 'auto';
};
MouseEvents.prototype.getAtoms = function() {
var _this = this;
var objects = [];
MotifExplorer.getInstance().object3d.traverse (function (object) {
if (object.name === 'atom') {
for (var i = 0; i < object.children.length; i++) {
_this.objects.push(object.children[i]);
};
}
});
return _this.objects;
};
return MouseEvents;
});
So everything works without any errors in the console, but the spheres are not detectable by mouseovering or clicking. I think that something wrong with my set up numbers, mouse coordinates and canvases dimensions.
I want to note that I also has orbitControls set up for each viewport independently, but I have disabled every function except for the scroll-zoom.
Also, when I add a sphere, it is actually detectable by the function if I zoom so much that the sphere is all over the viewport.
I can't make a jsfiddle because of the complexity and the quantity of the code so I attach this image just to give you an idea: It is a very specific problem so thanks to anyone who at least tries to help me!
I forgot to say that for 1 renderer/camera/canvas that fits to the whole screen I was able to make a draggable sphere. My problem is what do i have to change for having this functionality in a canvas with specific dimensions and for its 3 viewports/cameras.