在three.js中勾勒出一个3d对象

时间:2014-10-13 13:38:56

标签: javascript three.js

在许多视频游戏中,我看到一种模式,当你将鼠标悬停在一个物体上时,它会在物体的2D形状周围显示一个漂亮的渐变高光。我设置了一个相当基本的Three.js场景,里面有一个球体

首先,我设置了raycaster变量:

var projector = new THREE.Projector();
var mouse2D = new THREE.Vector2(0, 0);
var mouse3D = new THREE.Vector3(0, 0, 0);

然后我做了一个raycaster功能

document.body.onmousemove = function highlightObject(event) {
    mouse3D.x = mouse2D.x = mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
    mouse3D.y = mouse2D.y = mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
    mouse3D.z = 0.5;

    projector.unprojectVector(mouse3D, camera);
    var raycaster = new THREE.Raycaster(camera.position, mouse3D.sub(camera.position).normalize());
    var intersects = raycaster.intersectObject(mesh);
     if (intersects.length > 0) {
         var obj = intersects[0].object; //the object that was intersected
         rotate = false;
     } else {
         rotate = true;
     }
}

这会让我看到他们正在盘旋的对象悬停。现在,如何围绕物体制作轮廓?

1 个答案:

答案 0 :(得分:6)

您需要在编码中使用OutlinePass

init()功能

中创建outlinepass
outlinePass = new THREE.OutlinePass(new THREE.Vector2(window.innerWidth, window.innerHeight), scene, camera);
composer.addPass( outlinePass );

然后设置轮廓选定的对象。

if (intersects.length > 0) {
    var obj = intersects[0].object; // the object that was intersected
    rotate = false;
    outlinePass.selectedObjects = obj;
} else {
    rotate = true;
}

看一下这个例子:https://threejs.org/examples/?q=out#webgl_postprocessing_outline