gpt4 book ai didi

javascript - ThreeJs Object轻松看鼠标

转载 作者:行者123 更新时间:2023-11-30 14:14:09 26 4
gpt4 key购买 nike

我试图让一个对象以一种自然的方式一直看着鼠标。到目前为止,我已经成功了

  • 让物体一直看着鼠标
  • 添加缓动更自然

现在的问题是该对象不遵循与鼠标相同的路径,而是始终采用最后一个位置以缓和。

我不确定如何处理这个问题。

// create object and add to scene
const sphere = new THREE.Mesh( geometry, material );
const origin = new THREE.Vector3(0, 0, 75);
sphere.position.x = 0;
sphere.position.z = 0;
sphere.lookAt(origin);
scene.add( sphere );

window.addEventListener("mousemove", onmousemove, false);

var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var intersectPoint = new THREE.Vector3();

function onmousemove(event) {
var startRotation = sphere.quaternion.clone();

mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
raycaster.ray.intersectPlane(plane, intersectPoint);
intersectPoint.z = 75; // so that the object is still always facing the camera which has a position.z of 75 too
sphere.lookAt(intersectPoint);
var endRotation = sphere.quaternion.clone();
sphere.quaternion.copy( startRotation );
createjs.Tween.get(sphere.quaternion).to(endRotation, 1000, createjs.Ease.getPowOut(2.2));


marker.position.copy(intersectPoint);
}

所以现在的目标是找到一种方法让对象跟随鼠标,但不仅仅是最后一个位置,而是它的整个路径。有什么想法吗?

最佳答案

您可以使用像这样的模式使对象通过延迟缓动来注视鼠标:

var target = new THREE.Vector3();

var mouseX = 0, mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;

...

function onDocumentMouseMove( event ) {

mouseX = ( event.clientX - windowHalfX );
mouseY = ( event.clientY - windowHalfY );

}

function animate() {

requestAnimationFrame( animate );

target.x += ( mouseX - target.x ) * .02;
target.y += ( - mouseY - target.y ) * .02;
target.z = camera.position.z; // assuming the camera is located at ( 0, 0, z );

object.lookAt( target );

renderer.render( scene, camera );

}

three.js r.131

关于javascript - ThreeJs Object轻松看鼠标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53887057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com