Click and drag only grabs part of an object - javascript

I'm on the final stretch of a this project I've been working on and I'm having issues with the ability to click and drag objects. I've added them all into an array called items, and it sort of works right now. Here is the link to the page in action. If you add any of the items from the menu in the upper right, it'll show up but you can only drag it around piece by piece. From what I can tell, the issue is that it is treating each item as a series of items instead of as one item. This makes sense as each model is several models pieced together, but I'm not sure how to work around that. Any ideas?
Here are the three functions I have controlling mouse interaction:
function onMouseMove( event ){
event.preventDefault();
mouse.x = ( event.clientX / width ) * 2 - 1;
mouse.y = - ( event.clientY / height ) * 2 + 1;
var vector = new THREE.Vector3( mouse.x, mouse.y, 0 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
if ( SELECTED ) {
var intersects = ray.intersectObject( plane );
SELECTED.position.copy( intersects[ 0 ].point.subSelf( offset ) );
return;
}
var intersects = ray.intersectObjects( items );
if ( intersects.length > 0 ) {
if ( INTERSECTED != intersects[ 0 ] ) {
INTERSECTED = intersects[ 0 ].object;
plane.position.copy( INTERSECTED.position );
}
container.style.cursor = 'pointer';
}
else {
INTERSECTED = null;
container.style.cursor = 'auto';
}
}
function onMouseDown( event ) {
event.preventDefault();
var vector = new THREE.Vector3( mouse.x, mouse.y, 0 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
var intersects = ray.intersectObjects( items );
if ( intersects.length > 0 ) {
SELECTED = intersects[ 0 ].object;
var intersects = ray.intersectObject( plane );
offset.copy( intersects[ 0 ].point ).subSelf( plane.position );
container.style.cursor = 'move';
}
}
function onMouseUp( event ) {
event.preventDefault();
if ( INTERSECTED ) {
plane.position.copy( INTERSECTED.position );
SELECTED = null;
}
container.style.cursor = 'auto';
}
It's heavily based on this example, but without the color bits.
By changing the code in onMouseDown like so
// OLD
SELECTED = intersects[0].object;
// NEW
SELECTED = intersects[0].object.parent;
I can now move the full object. This only works if the object only has one parent though, and so some items are not able to move with this code. Anyone have a suggestion on determining if it has parent objects and moving up if it does?

If somebody is still interested in this question subSelf method is now called sub.

Resolved by adding the following to onMouseDown
SELECTED = intersects[0].object;
while(SELECTED.parent != scene){
SELECTED = SELECTED.parent;
}
This ensures that the object grabbed will be the highest level that isn't the scene and makes all the models drag-able.

Related

How to bind onClick or onMouseEnter event to three.js?

Like their`s official example, we can use Raycaster to get current matched objects https://threejs.org/docs/?q=Raycaster#api/en/core/Raycaster.
The official example is:
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
function onPointerMove( event ) {
// calculate pointer position in normalized device coordinates
// (-1 to +1) for both components
pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
function render() {
// update the picking ray with the camera and pointer position
raycaster.setFromCamera( pointer, camera );
// calculate objects intersecting the picking ray
const intersects = raycaster.intersectObjects( scene.children );
for ( let i = 0; i < intersects.length; i ++ ) {
intersects[ i ].object.material.color.set( 0xff0000 );
}
renderer.render( scene, camera );
}
window.addEventListener( 'pointermove', onPointerMove );
window.requestAnimationFrame(render);
Accrodding to this example, in my understanding, I can just get intersects[i].object, which is a threejs Mesh class`s instance.
I want to bind onClick function to object3d like this way:
function createObject(id, position) {
// ...
const mesh = new Mesh()
// ...
mesh.onClick = () => handleClickFn(id, position);
scene.add(mesh);
}
Then I can call intersects[i].object.onClick(); to trigger it.
Every examples I found seems like they do some operation to intersects[i].object directly. Just like intersects[ i ].object.material.color.set( 0xff0000 );.
So, is there any way I can bind functions to each intersects[i].object like this?

Use " vector.applyQuaternion " or similar in ammo.js

I want to make a browser VR shooting game, using Three.js and Ammo.js for the physics and rigidbodies. The vr head and controllers are set up the models are loaded, but the bullets won't shoot from the gun as I want to. When I tried to do the same using only Three.js with no rigidbodies in the scene, I used the "vector.applyQuaternion" from the Three.js documentation, and it worked, the bullets were fired from the top of the gun. The problem is I didn't find anything similar for using ammo.js
Code without using ammo.js
... function handleController( controller ) {
if ( controller1.userData.isSelecting ) {
bullet1.position.set( controller1.position.x , controller1.position.y + 0.018 , controller1.position.z -0.01);
bullet1.userData.velocity.x = 0;
bullet1.userData.velocity.y = 10;
bullet1.userData.velocity.z = 10;
bullet1.userData.velocity.applyQuaternion( controller1.quaternion );
scene.add(bullet1);
}
if ( controller2.userData.isSelecting ) {
bullet2.position.set( controller2.position.x , controller2.position.y + 0.018 , controller2.position.z -0.01 );
bullet2.userData.velocity.x = 0;
bullet2.userData.velocity.y = 10;
bullet2.userData.velocity.z = 10;
bullet2.userData.velocity.applyQuaternion( controller2.quaternion );
scene.add(bullet2);
}
} ...
function render() {
handleController( controller1 );
handleController( controller2 );
var delta = clock.getDelta()
bullet1.position.x -= bullet1.userData.velocity.x * delta;
bullet1.position.y -= bullet1.userData.velocity.y * delta;
bullet1.position.z -= bullet1.userData.velocity.z * delta;
bullet2.position.x -= bullet2.userData.velocity.x * delta;
bullet2.position.y -= bullet2.userData.velocity.y * delta;
bullet2.position.z -= bullet2.userData.velocity.z * delta;
renderer.render( scene, camera );
}
code with ammo.js
function createBullet1RigidBody( threeObject, physicsShape, mass ) {
//threeObject.position.copy( pos );
//threeObject.quaternion.copy( quat1 );
quat1.set( controller1.position.x, controller1.position.y , controller1.position.z , Math.PI/2 );
pos1 = new THREE.Vector3 ( controller1.position.x, controller1.position.y , controller1.position.z );
var transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin( new Ammo.btVector3( controller1.position.x, controller1.position.y, controller1.position.z -0.5) );
transform.setRotation( new Ammo.btQuaternion( quat1.x, quat1.y, quat1.z, quat1.w ) );
var motionState = new Ammo.btDefaultMotionState( transform );
var localInertia = new Ammo.btVector3( 0, 0, 0 );
physicsShape.calculateLocalInertia( mass, localInertia );
var rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia );
var body1 = new Ammo.btRigidBody( rbInfo );
threeObject.userData.physicsBody = body1;
scene.add( threeObject );
if ( mass > 0 ) {
bullet1Bodies.push( threeObject );
// Disable deactivation
body1.setActivationState( 4 );
}
physicsWorld.addRigidBody( body1 );
return body1;
}
function createBullet2RigidBody( threeObject, physicsShape, mass ) {
//threeObject.position.copy( pos );
//threeObject.quaternion.copy( quat2 );
var transform = new Ammo.btTransform();
transform.setIdentity();
transform.setOrigin( new Ammo.btVector3( controller2.position.x, controller2.position.y, controller2.position.z -0.5 ) );
transform.setRotation( new Ammo.btQuaternion( quat.x, quat.y, quat.z, quat.w ) );
var motionState = new Ammo.btDefaultMotionState( transform );
var localInertia = new Ammo.btVector3( 0, 0, 0 );
physicsShape.calculateLocalInertia( mass, localInertia );
var rbInfo = new Ammo.btRigidBodyConstructionInfo( mass, motionState, physicsShape, localInertia );
var body2 = new Ammo.btRigidBody( rbInfo );
threeObject.userData.physicsBody = body2;
scene.add( threeObject );
if ( mass > 0 ) {
bullet2Bodies.push( threeObject );
// Disable deactivation
body2.setActivationState( 4 );
}
physicsWorld.addRigidBody( body2 );
return body2;
}
...
function handleController( controller ) {
if ( controller1.userData.isSelecting ) {
var bullet1Mass = 0.1;
var bullet1Radius = 0.6;
var bullet1 = new THREE.Mesh( geometry_bullet, new THREE.MeshLambertMaterial( { color: 0x2661b9 } ) );
var bullet1Shape = new Ammo.btSphereShape ( bullet1Radius );
bullet1Shape.setMargin ( margin );
var bullet1_body = createBullet1RigidBody( bullet1, bullet1Shape, bullet1Mass, bullet1Radius );
//bullet1.position.set( pos1.x, pos1.y, pos1.z );
bullet1.userData.physicsBody.setFriction( 0 );
bullet1_body.setLinearVelocity( new Ammo.btVector3( 100, 100, 100 , controller1.quaternion) );
}
if ( controller2.userData.isSelecting ) {
var bullet2Mass = 0.1;
var bullet2Radius = 0.6;
var bullet2 = new THREE.Mesh( geometry_bullet, new THREE.MeshLambertMaterial( { color: 0xee2f2f } ) );
var bullet2Shape = new Ammo.btSphereShape ( bullet2Radius );
var pos2 = new THREE.Vector3 ( controller2.position.x, controller2.position.y + 10, controller2.position.z +10 );
bullet2Shape.setMargin ( margin );
//quat2.set( 0,0,0,1 );
var bullet2_body = createBullet2RigidBody( bullet2, bullet2Shape, bullet2Mass, bullet2Radius, pos2, quat2 );
//var pos2 = bullet2.position.set( controller2.position.x, controller2.position.y, controller2.position.z -0.1 );
bullet2.userData.physicsBody.setFriction( 0 );
bullet2_body.setLinearVelocity( new Ammo.btVector3( 0, 0, controller2.position.z *100 ) );
//bullet2.userData.physicsBody.applyQuaternion(controller2.quaternion);
}
}
The result is that the bullets are created but they're moving only on the z axis. The velocity of the bullet2 changes if I move the controller on the x axis, and the bullets are moving only on z axis. Bullets of the bullet1 follows the movement on the x,y,z axis but not the rotation of the controller. The "bullet2.userData.physicsBody.applyQuaternion(controller2.quaternion);" line is a comment, if I remove the bars I get this error "TypeError: bullet2.userData.physicsBody.applyQuaternion is not a function". I was expecting to do the same as I did in the previous example without ammo (userData.velocity.applyQuaternion) but there's no LinearVelocity in the ammo.js, I can only use getLinearVelocity and setLinearVelocity.
Based on ammo versions I've seen, your call to setLinearVelocity() is not correct. The btVector3 constructor must take x,y,z only. Calculate it using ThreeJS math similarly to where you've done elsewhere (choose magnitude and rotate the vector3 by the controller's quaternion). That probably solves your issue.
Note: I question why your bullets need to be physically simulated. Most shooting games do not do this. Easier and faster is to visually update its progress while checking for when it intercepts a player/wall. If you desire a gravity effect then apply small vertical drop every frame. If you want ricochet then use the normal of the intercepted surface to alter the velocity. Ammo engine is good for things like a grenade bouncing (complex, unpredictable interactions).

Can I add an invisible bounding box to a three.js scene?

I am trying to detect a click on a bounding box for an object (rather than just on the object itself - more clickable area). When I load the object like this:
var loader2 = new THREE.ObjectLoader();
loader2.load( "models/Platform/Platform.json", function(object, materials){
object.rotation.x = - (Math.PI / 2);
object.rotation.y = Math.PI;
object.scale.set(.025, .025, .025);
object.position.set(0, 1, .4);
var bbox = new THREE.BoundingBoxHelper(object, 0xffffff);
bbox.update();
scene.add(object);
scene.add(bbox);
objects.push(bbox);
});
And detect the click like this:
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
window.addEventListener( 'resize', onWindowResize, false );
function onDocumentTouchStart( event ) {
event.preventDefault();
event.clientX = event.touches[0].clientX;
event.clientY = event.touches[0].clientY;
onDocumentMouseDown( event );
}
function onDocumentMouseDown( event ) {
console.log("here");
event.preventDefault();
mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
console.log(mouse.x);
console.log(mouse.y);
var intersects = raycaster.intersectObjects( objects, true );
if ( intersects.length > 0 ) {
console.log("click");
}
The bounding box shows up correctly, and I can click on it!!!!! However, the bounding box is visible on the screen:
I want the bounding box to be transparent/invisible/hidden. Is there any way I can have a bounding box attached to the object which is clickable but not visible?
I read that to make the bounding box invisible I should remove the scene.add(bbox); (not add it to the scene), but if I do that, then it is not in the scene for the ray to intersect, and thus the click is not registered.
Solutions?
Thanks so much!!!
You can try to set the material to invisible:
bbox.material.visible = false;
So, there seem to be (at least) two solutions.
As suggested by #prisoner849:
bbox.material.opacity = 0;
bbox.material.transparent = true;
As suggested by #tomacco and refined by #WestLangley:
bbox.material.visible = false;
Both of these solutions worked for me!

Issue with Mousemove in Three.js

I'm having an issue with the mousemove function in a three.js scene. The goal is for the text to change color when the mouse is over the object. I've followed many examples from the web and the raycaster doesn't register the object. I tried adding update() in the function animate() like in the link above, but the console indicates an error, "update is not defined." I can't put the mousemove function after the init function because it doesn't recognize the object variable.
var mouse = { x: 0, y: 0 }, INTERSECTED;
var projector = new THREE.Projector();
var scene, camera,renderer;
function init(font){...
//loaded the font, camera, etc.
var option="object";
var geometry_option= new THREE.TextGeometry( option{font:font, size:200,height:20, curveSegments:2});
var material_option = new THREE.MeshBasicMaterial( { color: 0x0000000, side: THREE.BackSide } );
var option1= new THREE.Mesh(geometry_option, material_option); //added the position and added it to the scene.
//added other functions under function init such as mousedown,mousemove, etc.
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
function update()
{
// find intersections
// create a Ray with origin at the mouse position
// and direction into the scene (camera direction)
var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
// create an array containing all objects in the scene with which the ray intersects
var intersects_option= ray.intersectObjects([option1] );
if ( intersects_option.length > 0 )
{
if ( intersects_option[ 0 ].object != INTERSECTED )
{
if ( INTERSECTED )
INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
INTERSECTED = intersects_option[ 0 ].object;
INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
INTERSECTED.material.color.setHex( 0xffff00 );
}
}
else
{
if ( INTERSECTED )
INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
INTERSECTED = null;
}
}
} // close init function

Want to have click event on the .obj file shown on the webpage

I have a .obj file on the web page using Three js.
My aim is, when I drag the mouse left/right, the OBJ model should rotate which I am able to do so USING THREE.TrackballControls().
Next thing is, I want to touch on the specific points on that OBJ model and if the mouse is down on those points something would happen(like a counter increase which will be shown on the web page).
I have seen DOMevents for three js but it looks like it allows us to click on the whole object not on specific points on the objects.
How can I achieve that?
You have to create a raycaster. (r69)
mouse_vector = new THREE.Vector3(),
mouse = { x: 0, y: 0, z: 1 };
var vector = new THREE.Vector3();
var raycaster = new THREE.Raycaster();
var dir = new THREE.Vector3();
function onMouseDown( event_info )
{
event_info.preventDefault();
mouse.x = ( event_info.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event_info.clientY / window.innerHeight ) * 2 + 1;
mouse_vector.set( mouse.x, mouse.y, mouse.z );
mouse_vector.unproject(camera);
var direction = mouse_vector.sub( camera.position ).normalize();
ray = new THREE.Raycaster( camera.position, direction );
ray.set( camera.position, direction );
intersects = ray.intersectObjects(scene.children, true);
if( intersects.length )
{
intersects.forEach(function(clicked)
{
// Your stuff like
if (clicked.object.typ == 'yourObject')
{
//Event on click..
}
});
}
}

Categories