Tfe

Ongi etorri tfe-ren webgunera...

2016-09-21-ean

Game creation: using raycasts

Here is the article 5 about my series of posts about the game creation.

If you missed it, check out the previous part.

So here is my first attempt of using raycasts. The idea is to detect on which mesh the user is clicking, and applying a custom color to that mesh.

Here is part of the code that i am using for that:

    raycaster.setFromCamera( mouse, camera );   
    var intersects = raycaster.intersectObjects( scene.children, true );
    if(intersects.length>0)
    {   
        for ( var i = 0; i < intersects.length; i++ ) { 
            if(intersects[ i ].object.material.materials)
            {
                intersects[ i ].object.material.materials[0].color.set(Math.random()* 0xffffff );
            }
        }
    }

Here is the final result:

threejs using raycast

You can get: the source code or View the demo directly.

Tags: Projects