javascript - 在三个 js 中使用鼠标悬停更改网格的颜色

标签 javascript three.js raycasting

我已经使用 jsonloader 和 three.js 编写了一个显示多个网格的 WebGL 脚本,现在我想添加 MouseOver 和 onClick 事件。第一个是在鼠标悬停在网格上时简单地更改网格的颜色:

function render() {
  requestAnimationFrame(render);    
  mesh.rotation.z += 0.090;    
  raycaster.setFromCamera(mouse, camera);    
  var intersects = raycaster.intersectObjects(scene.children);  

  for (var i = 0; i < intersects.length; i++) {    
    intersects[i].object.material.color.set(0xff0000);    
  }    
  renderer.render(scene, camera);

}

上面的渲染函数允许我通过将鼠标悬停在任何网格上来将其颜色更改为红色。我已经尝试了几种 if/else 变体来尝试让这种效果仅在鼠标悬停在网格上时起作用,但我无法让它起作用——它只是保持红色。谁能建议我如何修改我的脚本?

谢谢。

最佳答案

您必须在鼠标移开时将颜色设置回原始颜色,这不会自动发生...

检查 this examplehttp://stemkoski.github.io具体更新方法:

Here a fiddle with a demo更新到最新的three.js master:

// 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 = ray.intersectObjects( scene.children );

// INTERSECTED = the object in the scene currently closest to the camera 
//      and intersected by the Ray projected from the mouse position    

// if there is one (or more) intersections
if ( intersects.length > 0 )
{
    // if the closest object intersected is not the currently stored intersection object
    if ( intersects[ 0 ].object != INTERSECTED )
    {
        // restore previous intersection object (if it exists) to its original color
        if ( INTERSECTED )
            INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
        // store reference to closest object as current intersection object
        INTERSECTED = intersects[ 0 ].object;
        // store color of closest object (for later restoration)
        INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
        // set a new color for closest object
        INTERSECTED.material.color.setHex( 0xffff00 );
    }
}
else // there are no intersections
{
    // restore previous intersection object (if it exists) to its original color
    if ( INTERSECTED )
        INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
    // remove previous intersection object reference
    //     by setting current intersection object to "nothing"
    INTERSECTED = null;
}

关于javascript - 在三个 js 中使用鼠标悬停更改网格的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314521/

相关文章:

javascript - 自定义组件无法在此基本 ReactJS 应用程序上正确呈现

javascript - 是否可以像在 Javascript 中那样在 AS2 中使用闭包?

javascript - 更新 Three.js Raycaster 以调整窗口大小

javascript - 基于网格的环境中的 2D 光线转换

javascript - Ajax 代码未执行

javascript - jquery find 没有选择所需的 div

javascript - 在 Three.js 中对 THREE.TextGeometry 对象应用发光效果

javascript - 光度函数Three.js

javascript - 如何使用 Threejs 在球体上水平滑动纹理

c - TCC 错误 : index too large