• three.js 鼠标左右拖动改变玩家视角


    这里主要用到了 一个方法   obj.getWorldDirection();  

    obj.getWorldDirection()表示的获取obj对象自身z轴正方向在世界坐标空间中的方向。

    按下 W键前进运动;

     

    1. <template>
    2. <div>
    3. <el-container>
    4. <el-main>
    5. <div class="box-card-left">
    6. <div id="threejs">div>
    7. {{ movementX }}
    8. div>
    9. el-main>
    10. el-container>
    11. div>
    12. template>s
    13. <script>
    14. // 引入轨道控制器扩展库OrbitControls.js
    15. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
    16. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
    17. import TWEEN from '@tweenjs/tween.js';
    18. export default {
    19. data() {
    20. return {
    21. scene: null,
    22. camera: null,
    23. renderer: null,
    24. res1: null,
    25. res2: null,
    26. clock: null,
    27. left_mouse_down: false,
    28. keyState: {
    29. W: false,
    30. S: false,
    31. A: false,
    32. D: false,
    33. },
    34. left_rotation: true, // 向左旋转的标志
    35. right_rotation: true, // 向右旋转的标志
    36. VW: new this.$three.Vector3(0, 0, 0),
    37. VS: new this.$three.Vector3(0, 0, 0),
    38. curr_v: new this.$three.Vector3(0, 0, 0),
    39. person: null,
    40. movementX: null,
    41. deltaTime: 0,
    42. a: 60, // 加速度
    43. damping: -0.04,
    44. };
    45. },
    46. created() {},
    47. mounted() {
    48. this.init();
    49. },
    50. methods: {
    51. goBack() {
    52. this.$router.go(-1);
    53. },
    54. init() {
    55. // 创建场景对象
    56. this.scene = new this.$three.Scene();
    57. // 创建坐标轴辅助对象
    58. const axesHelper = new this.$three.AxesHelper(100);
    59. this.scene.add(axesHelper);
    60. // 创建时钟对象
    61. this.clock = new this.$three.Clock();
    62. // 创建环境光对象
    63. const ambientLight = new this.$three.AmbientLight(0xffffff, 6);
    64. this.scene.add(ambientLight);
    65. // this.createMesh();
    66. // 创建相机对象
    67. this.camera = new this.$three.PerspectiveCamera(60,1,0.01,2000);
    68. // this.camera.position.set(0,5,-5);
    69. // this.camera.lookAt(0,0,0);
    70. // 创建渲染器对象
    71. this.renderer = new this.$three.WebGLRenderer();
    72. this.renderer.setSize(1000,800);
    73. // 创建gltfLoader加载器对象
    74. const gltfLoader = new GLTFLoader();
    75. gltfLoader.load("models/gltf/person2/scene.gltf", gltf => {
    76. this.person = gltf.scene;
    77. // 将相机添加到人物模型上
    78. this.person.add(this.camera);
    79. // 调整相机位置
    80. this.camera.position.add(new this.$three.Vector3(0,5,-6));
    81. this.camera.translateZ(-1);
    82. let camera_look_position = this.person.position.clone().add(new this.$three.Vector3(0,4,0));
    83. // 设置相机指向
    84. this.camera.lookAt(camera_look_position);
    85. this.scene.add(gltf.scene);
    86. this.renderer.render(this.scene, this.camera);
    87. window.document.getElementById("threejs").appendChild(this.renderer.domElement);
    88. })
    89. // 监听事件方法
    90. this.addEventListenerFn();
    91. this.renderLoop();
    92. },
    93. createMesh() {
    94. const geometry = new this.$three.BoxGeometry(1,1,1);
    95. const material = new this.$three.MeshBasicMaterial({color: 0xffaadd});
    96. const mesh = new this.$three.Mesh(geometry, material);
    97. // mesh.rotateY(Math.PI / 2);
    98. const dir = new this.$three.Vector3();
    99. mesh.getWorldDirection(dir);
    100. this.scene.add(mesh);
    101. console.log('dir', dir);
    102. },
    103. addEventListenerFn() {
    104. document.addEventListener("mousemove", e => {
    105. // 鼠标左键按下的情况
    106. if(this.left_mouse_down) {
    107. this.movementX = e.movementX;
    108. this.person.rotation.y -= e.movementX / 100;
    109. // this.camera.rotateY(e.movementX / 100);
    110. const dir = new this.$three.Vector3();
    111. this.person.children[0].getWorldDirection(dir);
    112. this.renderer.render(this.scene, this.camera);
    113. }
    114. })
    115. document.addEventListener("mousedown", e => {
    116. // e.button == 0 左键;e.button == 2 右键;
    117. if(e.button == 0) {
    118. this.left_mouse_down = true;
    119. }
    120. })
    121. document.addEventListener("mouseup", e => {
    122. // e.button == 0 左键;e.button == 2 右键;
    123. if(e.button == 0) {
    124. this.left_mouse_down = false;
    125. }
    126. })
    127. // 监听键盘按下
    128. document.addEventListener("keydown", e => {
    129. // 如果按下的是 w键
    130. if(e.code == "KeyW") {
    131. this.keyState.W = true;
    132. }
    133. if(e.code == "KeyS") {
    134. this.keyState.S = true;
    135. }
    136. })
    137. // 监听键盘弹起
    138. document.addEventListener("keyup", e => {
    139. // 如果按下的是 w键
    140. if(e.code == "KeyW") {
    141. this.keyState.W = false;
    142. }
    143. if(e.code == "KeyS") {
    144. this.keyState.S = false;
    145. }
    146. })
    147. },
    148. renderLoop() {
    149. let deltaTime = this.clock.getDelta();
    150. if(this.keyState.W) {
    151. const front = new this.$three.Vector3();
    152. this.person.getWorldDirection(front);
    153. let person_v = this.curr_v.clone().add(front.multiplyScalar(deltaTime * this.a));
    154. const pos = person_v.clone().multiplyScalar(deltaTime);
    155. this.person.position.add(pos);
    156. }
    157. if(this.keyState.S) {
    158. const front = new this.$three.Vector3();
    159. this.person.getWorldDirection(front);
    160. let person_v = this.curr_v.clone().add(front.multiplyScalar(deltaTime * (-this.a)));
    161. const pos = person_v.clone().multiplyScalar(deltaTime);
    162. this.person.position.add(pos);
    163. }
    164. this.renderer.render(this.scene, this.camera);
    165. window.requestAnimationFrame(this.renderLoop);
    166. },
    167. },
    168. };
    169. script>
    170. <style lang="less" scoped>
    171. .box-card-left {
    172. display: flex;
    173. align-items: flex-start;
    174. flex-direction: row;
    175. width: 100%;
    176. .box-right {
    177. img {
    178. width: 500px;
    179. user-select: none;
    180. }
    181. }
    182. }
    183. style>

  • 相关阅读:
    [Java]源码角度深入理解哈希表,手撕常见面试题
    1035 Password
    对MMVAE中IWAE代码实现的理解
    科技改变视听4K 120HZ高刷新率的投影、电视、电影终有用武之地
    spark3 spark-sql explain 命令的执行过程
    【Bash】记录一个长命令换行的BUG
    go语言基础之常量与itoa
    ArrayList、LinkedList、Collections.singletonList、Arrays.asList与ImmutableList.of
    javaWeb宿舍管理系统
    互联网+教育时代,线下教培机构的新机遇
  • 原文地址:https://blog.csdn.net/yinge0508/article/details/136685953