1

i am trying to build a picking ray to see if my 3D body in three.js has been clicked. It doesn't work at the moment and i followed these Three.js raycast produces empty intersects array and these three.js Raycaster intersectObjects tips.

My current code is this:

function checkClick() {
                // On every click, check for body hit
                clickInfo.x = event.clientX;
                clickInfo.y = event.clientY;

                var x = ( clickInfo.x / window.innerWidth ) * 2 - 1;
                var y = -( clickInfo.y / window.innerHeight ) * 2 + 1;

                var objects = [];
                objects.push(model);
                var raycaster = projector.pickingRay(directionVector.clone(),camera);
                var intersects = raycaster.intersectObjects(scene.children);
                if (intersects.length) {
                     alert("found something");
                }
                else {
                     alert("found nothing");
                }
           }

I've learned now that projector.pickingRay saves me a lot of work since in the 2nd linked example above the user needed to calculate all that alone before. But it doesn't work. I don't get any JS error, just the "found nothing" messages (the intersects array is empty). I do have one single object in the scene (model) which i add to objects because i know raycaster.intersectObjects needs an array as its parameter. It still doesn't work. Then i followed the tip in the first linked example and used scene.children instead. Still it always gives "found nothing". Why? What am i doing wrong?

Thanks a lot for any help.

Community
  • 1
  • 1
  • Please post a link to a live example of your complete code so it will be easier to figure out what the problem is. – Stemkoski Jun 13 '13 at 18:35
  • @ Lee Stemkoski: I would like to upload a live example of this, but i can't since i dont know any freehoster which allows the use of .obj Files. If you could tell me one, i could upload a live example! –  Jun 18 '13 at 13:20
  • @ Lee Stemkoski: I can upload the body.obj there, yes, but i cannot use it in a online-live example. When hosting the test.html on a freehoster somewhere and adding dropbox/mylink/body.obj in the loader code it won't let me due to security reasons. And i cannot upload the complete html code on dropbox since it won't let it run like an apache server. –  Jun 18 '13 at 14:48

1 Answers1

-1

Here's a link to a working example, where a 3D object changes color when the mouse is hovering over it. Perhaps this code could be adapted to fit your needs?

http://stemkoski.github.io/Three.js/Mouse-Over.html

Hope it helps!

Stemkoski
  • 8,936
  • 3
  • 47
  • 61
  • 1
    I already found this example earlier, but simply copying the code doesn't work AND it is still using methods like unprojectVector etc. which are outdated from what i've learned so far because in the newest version you should be able to just use projector.pickingRay(directionVector.clone(),camera); and it should work. But it doesn't! –  Jun 13 '13 at 13:22