three.js实现鼠标点击控制物体移动(带动画效果,位置精确)

文章展示了如何使用THREE.js库创建3D场景,结合GSAP实现物体的动画效果。通过轨道控制器操纵相机视角,解决鼠标点击坐标偏移问题,当点击时,物体能移动到鼠标点击的位置,同时包含了环境光和平行光的设置,以及窗口大小变化时的响应式调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过查阅各种资料,最终确定解决方案,动画效果使用gsap组件库实现,也可不用,代码稍作修改即可。解决鼠标点击坐标偏移问题,复制可直接运行。
在这里插入图片描述

完整代码如下:

import * as THREE from "three";
// 导入动画库
import gsap from "gsap";
// 导入轨道控制器
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls'

// 1、创建场景
const scene = new THREE.Scene();

// 2、创建相机
const camera = new THREE.PerspectiveCamera(
  65,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// 设置相机位置
camera.position.set(0, -10, 2);
scene.add(camera);

// 添加物体
// 创建几何体
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const cubeMaterial = new THREE.MeshStandardMaterial();
// 根据几何体和材质创建物体
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.castShadow = true;
scene.add(cube);

// 添加平面
const geometry = new THREE.PlaneGeometry( 20, 20 );
const material = new THREE.MeshStandardMaterial( {color: 0xCCFFFF, side: THREE.DoubleSide} );
const plane = new THREE.Mesh( geometry, material );
plane.receiveShadow = true;
plane.position.z = -0.5;
scene.add( plane );

// 初始化渲染器
const renderer = new THREE.WebGLRenderer();
// 设置渲染的尺寸大小
renderer.setSize(window.innerWidth, window.innerHeight);
// 开启阴影计算
renderer.shadowMap.enabled = true;
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);

// 使用渲染器,通过相机将场景渲染进来
renderer.render(scene, camera);

// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼,让控制器更有真是效果,必须在动画循环里调用update()
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper( 5 );
scene.add( axesHelper );

var mouse = new THREE.Vector2();
function onMouseClick( e ) {
	// 获取鼠标点击点的场景坐标
  const mousePosition = getMousePosition(event.clientX, event.clientY);

  // 将物体的位置更新为鼠标点击点的位置
  // 直接移动
  // cube.position.copy(mousePosition);
  // 带有动画效果
  gsap.to(cube.position,{
    x: mousePosition.x,
    duration: 3
  });
  gsap.to(cube.position,{
    y: mousePosition.y,
    duration: 3
  });
}

// 获取鼠标点击点的场景坐标
// 解决坐标偏移问题核心代码
function getMousePosition(clientX, clientY) {
  const mouse = new THREE.Vector2();
  mouse.x = (clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(clientY / window.innerHeight) * 2 + 1;

  const raycaster = new THREE.Raycaster();
  raycaster.setFromCamera(mouse, camera);
  const intersects = raycaster.intersectObjects(scene.children);

  if (intersects.length > 0) {
      return intersects[0].point;
  }

  return null;
}
window.addEventListener( 'click', onMouseClick, false );

// 添加平行光
const directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
directionalLight.position.set(10,-10,10);
directionalLight.castShadow = true;
scene.add( directionalLight );

// 添加环境光
const light = new THREE.AmbientLight( 0x404040 ); // soft white light
scene.add( light );

// 实现每一帧渲染一次场景和相机
function render(time) {
    controls.update();
    renderer.render(scene,camera);
    // 每一帧执行一次render函数
    requestAnimationFrame(render);
}
render();

// 监听画面变化,更新渲染画面
window.addEventListener("resize", ()=>{
    // 更新摄像头
    camera.aspect = window.innerWidth / window.innerHeight;
    // 更新摄像机的投影矩阵
    camera.updateProjectionMatrix();

    // 更新渲染器
    renderer.setSize(window.innerWidth,window.innerHeight);
    // 设置渲染器的像素比
    renderer.setPixelRatio(window.devicePixelRatio);
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值