问题描述
我在three.js中创建了一个盒子
i created a box in three.js
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 100, window.innerWidth/window.innerHeight, 0.1, 1000 );
camera.position.set(5, 5, 10);
var geo = new THREE.BoxGeometry(5,2,5);
var mat = new THREE.MeshBasicMaterial({color:0xff0ff0, wireframe:false, vertexColors: THREE.FaceColors});
var mesh = new THREE.Mesh( geo, mat);
scene.add(mesh);
var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
renderer.render(scene, camera);
之后我想将光线投射到鼠标悬停的立方体面上.因此我创建了这个函数
After that i want to raycast to cubes faces which mouse over on.Thus i created this function
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
function onMouseMove( event ) {
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
var intersects = raycaster.intersectObject( mesh);
for ( var i = 0; i < intersects.length; i++ ) {
intersects[ i ].face.color.setHex( 0xDDC2A3);
mesh.geometry.colorsNeedUpdate = true;
}
renderer.render( scene, camera );
}
window.addEventListener( 'mousemove', onMouseMove, false );
它几乎工作正常.我的意思是,它会改变半边脸的颜色.你知道,由 BoxGeometry()
创建的网格有 12 个 Face3
.我想要完整的脸或硬脸或Face4
,由Face3+Face3.知道我怎么能做到这一点吗?
It nearly works correct. i mean, it changes color of the half of the face. You know, the mesh which created by BoxGeometry()
has 12 Face3
. I want the complete face or hard-face or Face4
which took shape by Face3+Face3
. Any idea how i can accomplish this?
推荐答案
仅适用于现成的非缓冲区几何图形,例如 .. BoxGeometry、SphereGeometry、CircleGeometry 等...
Only works for ready-built, non-Buffer geometries like .. BoxGeometry, SphereGeometry, CircleGeometry etc...
var intersects = raycaster.intersectObject( mesh);
for ( var i = 0; i < intersects.length; i++ ) {
var faceIndex = intersects[i].faceIndex;
if(faceIndex == 0 || (faceIndex % 2) == 0) {
intersects[i].object.geometry.faces[faceIndex].color.setHex( 0xD1B3B3);
intersects[i].object.geometry.faces[faceIndex + 1].color.setHex( 0xD1B3B3);
intersects[i].object.geometry.colorsNeedUpdate = true;
}else {
intersects[i].object.geometry.faces[faceIndex].color.setHex( 0xD1B3B3);
intersects[i].object.geometry.faces[faceIndex - 1].color.setHex( 0xD1B3B3);
intersects[i].object.geometry.colorsNeedUpdate = true;
}
}
renderer.render(scene, camera);
这篇关于网格的光线投射硬面 -- Three.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!