美文网首页
three.js 实现物体反向跟随鼠标晃动

three.js 实现物体反向跟随鼠标晃动

作者: seaflyj | 来源:发表于2023-04-09 16:55 被阅读0次
    dddd.gif

    本文章介绍运用 threejs 操作3d物体实现鼠标跟随,核心思想就是控制相机跟随鼠标的反向移动,获取鼠标事件,计算方向;
    核心代码:
    camera.position.y = -(window.scrollY / window.innerHeight) * 30;

    camera.position.x += (mouse.x * 5 - camera.position.x) * deltaTime * 5;

    运用案例:
    https://www.weedensenteret.no/
    一个很漂亮的3d官网

    还有腾讯云轮播的3d物体交互都是相通的原理
    https://cloud.tencent.com/

    截屏2023-04-10 17.06.51.png
    import * as THREE from "three";
    // 导入轨道控制器
    import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
    // 导入动画库
    import gsap from "gsap";
    // 导入dat.gui
    import * as dat from "dat.gui";
    
    // 目标:raycaster
    
    // const gui = new dat.GUI();
    // 1、创建场景
    const scene = new THREE.Scene();
    
    // 2、创建相机
    const camera = new THREE.PerspectiveCamera(
      75,
      window.innerWidth / window.innerHeight,
      0.1,
      300
    );
    
    const textureLoader = new THREE.TextureLoader();
    const particlesTexture = textureLoader.load("./textures/particles/1.png");
    // 设置相机位置
    camera.position.set(0, 0, 18);
    scene.add(camera);
    
    const cubeGeometry = new THREE.BoxBufferGeometry(2, 2, 2);
    const material = new THREE.MeshBasicMaterial({
      wireframe: true,
    });
    const redMaterial = new THREE.MeshBasicMaterial({
      color: "#ff0000",
    });
    
    // 1000立方体
    let cubeArr = [];
    let cubeGroup = new THREE.Group();
    for (let i = 0; i < 5; i++) {
      for (let j = 0; j < 5; j++) {
        for (let z = 0; z < 5; z++) {
          const cube = new THREE.Mesh(cubeGeometry, material);
          cube.position.set(i * 2 - 4, j * 2 - 4, z * 2 - 4);
          cubeGroup.add(cube);
          cubeArr.push(cube);
        }
      }
    }
    
    scene.add(cubeGroup);
    
    const smallBall = new THREE.Mesh(
      new THREE.SphereBufferGeometry(0.1, 20, 20),
      new THREE.MeshBasicMaterial({ color: 0xff0000 })
    );
    smallBall.position.set(2, 2, 2);
    //直线光源
    const pointLight = new THREE.PointLight(0xff0000, 3);
    // pointLight.position.set(2, 2, 2);
    pointLight.castShadow = true;
    
    // 设置阴影贴图模糊度
    pointLight.shadow.radius = 20;
    // 设置阴影贴图的分辨率
    pointLight.shadow.mapSize.set(512, 512);
    
    // 设置透视相机的属性
    smallBall.add(pointLight);
    
    // 创建投射光线对象
    const raycaster = new THREE.Raycaster();
    
    // 鼠标的位置对象
    const mouse = new THREE.Vector2();
    
    // 监听鼠标的位置
    window.addEventListener("mousemove", (event) => {
      mouse.x = event.clientX / window.innerWidth - 0.5;
      mouse.y = event.clientY / window.innerHeight - 0.5;
    });
    
    // 监听鼠标的位置
    window.addEventListener("click", (event) => {
      //   console.log(event);
      mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
      mouse.y = -((event.clientY / window.innerHeight) * 2 - 1);
      raycaster.setFromCamera(mouse, camera);
      let result = raycaster.intersectObjects(cubeArr);
      //   console.log(result);
      //   result[0].object.material = redMaterial;
      result.forEach((item) => {
        item.object.material = redMaterial;
      });
    });
    
    // 初始化渲染器
    // 渲染器透明
    const renderer = new THREE.WebGLRenderer({ alpha: true });
    // 设置渲染的尺寸大小
    renderer.setSize(window.innerWidth, window.innerHeight);
    // 开启场景中的阴影贴图
    renderer.shadowMap.enabled = true;
    renderer.physicallyCorrectLights = true;
    
    // console.log(renderer);
    // 将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);
    // 设置时钟
    const clock = new THREE.Clock();
    
    function render() {
      //   let time = clock.getElapsedTime();
      let deltaTime = clock.getDelta();
      //   根据当前滚动的scrolly,去设置相机移动的位置
      camera.position.y = -(window.scrollY / window.innerHeight) * 30;
    
      camera.position.x += (mouse.x * 5 - camera.position.x) * deltaTime * 5;
      //   controls.update();
      renderer.render(scene, camera);
      //   渲染下一帧的时候就会调用render函数
      requestAnimationFrame(render);
    }
    
    render();
    
    // 监听画面变化,更新渲染画面
    window.addEventListener("resize", () => {
      //   console.log("画面变化了");
    
      // 更新摄像头
      camera.aspect = window.innerWidth / window.innerHeight;
      //   更新摄像机的投影矩阵
      camera.updateProjectionMatrix();
    
      //   更新渲染器
      renderer.setSize(window.innerWidth, window.innerHeight);
      //   设置渲染器的像素比
      renderer.setPixelRatio(window.devicePixelRatio);
    });
    
    
    
    

    相关文章

      网友评论

          本文标题:three.js 实现物体反向跟随鼠标晃动

          本文链接:https://www.haomeiwen.com/subject/rfdbddtx.html