three.js 相机跟着玩家走(第三人称漫游)

a8c7d4849c74457ab642181146466cff.gif

<template>
  <div>
    <el-container>
      <el-main>
        <div class="box-card-left">
          <div id="threejs"></div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>s
<script>
// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
import TWEEN from '@tweenjs/tween.js';
export default {
  data() {
    return {
      scene: null,
      camera: null,
      renderer: null,
      res1: null,
      res2: null,
      clock: null,
      keyState: {
        W: false,
        S: false,
        A: false,
        D: false,
      },
      left_rotation: true, // 向左旋转的标志
      right_rotation: true, // 向右旋转的标志
      VW: new this.$three.Vector3(0, 0, 0),
      VS: new this.$three.Vector3(0, 0, 0),
      curr_v: new this.$three.Vector3(0, 0, 0),
      person: null,
      deltaTime: 0,
      a: 30, // 加速度
      damping: -0.04,
    };
  },
  created() {},
  mounted() {
    this.name = this.$route.query.name;
    this.init();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    init() {
      this.clock = new this.$three.Clock();
      // 创建场景
      this.scene = new this.$three.Scene();
      // 创建辅助坐标轴对象
      const axesHelper = new this.$three.AxesHelper(100);
      this.scene.add(axesHelper);
      // 创建环境光

      const ambientLight = new this.$three.AmbientLight(0xffffff, 10);
      this.scene.add(ambientLight);
      // 创建相机对象
      this.camera = new this.$three.PerspectiveCamera(60,1,0.01,2000);
  
      // 创建渲染器对象
      this.renderer = new this.$three.WebGLRenderer();
      this.renderer.setSize(1500,1200);
      // 创建GLTFLoader对象;加载人物模型
      const gltfLoader = new GLTFLoader();
      gltfLoader.load("models/gltf/person2/scene.gltf", gltf => {
        gltf.scene.position.set(0,0,-10);
        gltf.scene.scale.set(2,2,2);
        this.person = gltf.scene;
          
        gltf.scene.add(this.camera);
        let p = gltf.scene.position;
        let person_p = p.clone();
        person_p.add(new this.$three.Vector3(0,5,3));
        this.camera.position.copy(person_p);
        
        this.camera.lookAt(p.x, p.y, p.z);

        this.scene.add(gltf.scene);

        this.renderer.render(this.scene, this.camera);
        window.document.getElementById("threejs").appendChild(this.renderer.domElement);
      })
      this.addEventListenerFn();
      this.renderLoop();
      
    },
    renderLoop() {
      const deltaTime = this.clock.getDelta();
      if(this.keyState.W) {
        if(this.VW.length() < 5) {
          this.VW.add(new this.$three.Vector3(0,0,1).multiplyScalar(this.a * deltaTime));
          this.curr_v = this.VW.clone();
        }
        let pos = this.VW.clone().multiplyScalar(deltaTime);
        this.person.position.add(pos);
      }

      if(this.keyState.S) {
        if(this.VS.length() < 5) {
          this.VS.add(new this.$three.Vector3(0,0,-1).multiplyScalar(this.a * deltaTime));
          this.curr_v = this.VS.clone();
        }
        let pos = this.VS.clone().multiplyScalar(deltaTime);
        this.person.position.add(pos);
      }
      if(this.keyState.A) {
      }
      if(this.person) {
        // .addScaledVector ( v : Vector3, s : Float ) : 将所传入的v与s相乘所得的乘积和这个向量相加。
        this.curr_v.addScaledVector(this.curr_v, this.damping);
        let pos = this.curr_v.clone().multiplyScalar(deltaTime);
        this.person.position.add(pos);
      }
      this.renderer.render(this.scene, this.camera);
      
      TWEEN.update();
      requestAnimationFrame(this.renderLoop);
    },
    addEventListenerFn() {
      // 监听按下的 W 键
      document.addEventListener("keydown", e => {
        if(e.code == "KeyW") {
          this.keyState.W = true;
        }
        if(e.code == "KeyS") {
          this.keyState.S = true;
        }
        if(e.code == "KeyA") {
          this.keyState.A = true;
          if(!this.left_rotation)return false;
          const tween0 = new TWEEN.Tween(this.person.rotation);
          let deg = this.$three.MathUtils.radToDeg(this.person.rotation.y);
          let rad = this.$three.MathUtils.degToRad(deg + 90);
          
          if(rad != null) {
            tween0.to({x:0, y: rad, z:0}, 1000);
            tween0.start();
            tween0.onStart(() => {
              this.left_rotation = false;
            });
            tween0.onComplete(() => {
              this.left_rotation = true;
            });
          }
        }
        if(e.code == "KeyD") {
          this.keyState.D = true;
          if(!this.right_rotation)return false;
          const tween0 = new TWEEN.Tween(this.person.rotation);
          let deg = this.$three.MathUtils.radToDeg(this.person.rotation.y);
          let rad = this.$three.MathUtils.degToRad(deg - 90);
          
          if(rad != null) {
            tween0.to({x:0, y: rad, z:0}, 1000);
            tween0.start();
            tween0.onStart(() => {
              this.right_rotation = false;
            });
            tween0.onComplete(() => {
              this.right_rotation = true;
            });
          }
        }
      })
      document.addEventListener("keyup", e => {
        if(e.code == "KeyW") {
          this.keyState.W = false;
          this.VW = new this.$three.Vector3(0, 0, 0);
        }
        if(e.code == "KeyS") {
          this.keyState.S = false;
          this.VS = new this.$three.Vector3(0, 0, 0);
        }
        if(e.code == "KeyA") {
          this.keyState.A = false;
        }
        if(e.code == "KeyD") {
          this.keyState.D = false;
        }
      })
    }
  },
};
</script>
<style lang="less" scoped>
.box-card-left {
  display: flex;
  align-items: flex-start;
  flex-direction: row;
  width: 100%;
  .box-right {
    img {
      width: 500px;
      user-select: none;
    }
  }
}
</style>

 

 

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对整threeJS体系进行全面剖析。整理出全面的教学大纲,涵盖内容面非常广。此教学版本为threeJS107版本。关于版本不建议大家使用低于90的版本学习。以下是课程目录1-ThreeJS概览(基本图形简介,什么是点线面如何绘制点线面,什么是材质,什么是几何体,什么是相机,什么是渲染器,什么是场景)2-相机和渲染器(详解相机类型,渲染器如何使用,针对不同场景怎么用,怎么调效果,怎么渲染,怎么输出画布,如何解决透明问题等等)3-创建平面几何(常见的几何体如何使用,如何使用简单的几何体绘制出自定义自己想要的几何体,关于几何体的性能剖析,如何解决性能,几何体的渲染原理)4-高级图形算法常见库(求直线的斜率  计算线段与圆的交点 计算线段的长度 判断折线是否在多边形内 等等)5-sprite精灵(怎么让一个图标永远朝向屏幕,精灵的属性,精灵材质原理等,广告提示框必用)6-骨骼游戏动画(什么是模型动画,常见游戏案例,如何让人头进行各种攻击动作)7-3d模型加载(常见模型格式,如何渲染不同格式,不同格式的特点,什么格式性能优越,模型渲染异常,贴图不显示等问题详解)8-高阶动态纹理(你所不知道的纹理用法,我说你不知道,你肯定不知道)9-漫游轨迹以及其动画路径(怎么绘制贝塞尔曲线,如何使用曲线上的路径,跟随路径移动的原理,相机如何运动,物体如何运动)10-着色器(什么是着色器。初识着色器基础,着色器材质怎么用,怎么使用着色器库)11-常见渲染以及透明度问题12-对象拾取以及拖拽(3d世界里面如何拖拽物体,拖拽的原理,mousemove mouseon等的事件效果)13-世界坐标以及组的问题(什么是相对坐标,什么是世界坐标,什么是当前坐标,怎么转化父子坐标系,组的优化,为什么用组,组的优势)14-指定对象旋转中心(什么是物体的几何体中心,如何改变中心,如何绕轴转动)15-层级对象渲染(多个场景一键切换,切换的优势,针对大项目的用法)16-拓展了解系列(不定期不断更新案例,各种酷炫效果bloom,halo等,以及各种3d图表,粒子案例等,不断构建你的3d实践能力)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值