Three.js-我们可以使用raycaster与模型的材质ID相交吗?

时间:2018-08-30 10:06:11

标签: three.js

我一直试图使用它们的ID来使加载的模型onclick的3种材料中的一种材料仅相交,但是,控制仍继续到整个模型。让我知道是否可以根据物料ID进行相交

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

for ( var i = 0; i < intersects.length; i++ ) {

    object.scene.traverse(function(child) {

        if ( child instanceof THREE.Mesh ) {

            if (child.material.name == "heap")  {

              child.material.color = new THREE.Color( 0x00ff00 );
            }
        }
    })
}

1 个答案:

答案 0 :(得分:0)

您可以通过访问相交结果的material上的object属性来更新光线投射对象的材质。 需要记住的一点是,当您更新材质对象的属性时,需要设置needsUpdate = true,以便threejs知道该材质需要在内部进行更新。

请考虑以下代码来实现所追求的目标:

// If only interested in one intersection, you can use .intersectObject()
var intersection = raycaster.intersectObject( scene.children );

// If intersection found, update material in some way
if(intersection) {

  // Extract object and material from intersection
  var object = intersection.object;
  var material = object.material;

  /*
  Manipulate your material as you need to
  material.color = new THREE.Color( 1, 0, 0 );  
  */

  // Tell threejs that the material was changed and
  // needs to be updated internally
  material.needsUpdate = true; 
}