1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
| <template> <div id="container"></div> </template> <script setup> import * as THREE from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js"; import { onMounted } from "vue"; import { Capsule } from "three/examples/jsm/math/Capsule.js"; import { Octree } from "three/examples/jsm/math/Octree.js"; import Stats from "three/examples/jsm/libs/stats.module.js";
onMounted(()=>{ const clock = new THREE.Clock(); const scene = new THREE.Scene(); scene.background = new THREE.Color(0x88ccee); scene.fog = new THREE.Fog(0x88ccee, 0, 50); const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); camera.position.set(0,5,10);
const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setPixelRatio(window.devicePixelRatio); renderer.setSize(window.innerWidth, window.innerHeight); renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.VSMShadowMap; renderer.outputEncoding = THREE.SRGBColorSpace; renderer.toneMapping = THREE.ACESFilmicToneMapping;
const container=document.getElementById("container") container.appendChild(renderer.domElement); const stats = new Stats(); stats.domElement.style.position = "absolute"; stats.domElement.style.top = "0px"; container.appendChild(stats.domElement);
// const controls = new OrbitControls(camera, renderer.domElement); // controls.target.set(0, 0, 0);
// 创建一个平面 const planeGeometry = new THREE.PlaneGeometry(20, 20, 1, 1); const planeMaterial = new THREE.MeshBasicMaterial({ color: 0xffffff, side: THREE.DoubleSide, }); const plane = new THREE.Mesh(planeGeometry, planeMaterial); plane.receiveShadow = true; plane.rotation.x = -Math.PI / 2; scene.add(plane);
// 创建一个平面 const capsuleBodyGeometry = new THREE.PlaneGeometry(1, 0.5, 1, 1); const capsuleBodyMaterial = new THREE.MeshBasicMaterial({ color: 0x0000ff, side: THREE.DoubleSide, }); const capsuleBody = new THREE.Mesh(capsuleBodyGeometry, capsuleBodyMaterial); capsuleBody.position.set(0, 0.5, 0); // 创建一个胶囊物体 const capsuleGeometry = new THREE.CapsuleGeometry(0.35, 1, 32); const capsuleMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000, side: THREE.DoubleSide, }); const capsule = new THREE.Mesh(capsuleGeometry, capsuleMaterial); capsule.position.set(0, 0.85, 0);
// 将相机作为胶囊的子元素,就可以实现跟随 camera.position.set(0, 2, -5); camera.lookAt(capsule.position); capsule.add(camera); capsule.add(capsuleBody);
scene.add(capsule); // 创建玩家的碰撞体 const playerCollider = new Capsule( new THREE.Vector3(0, 0.35, 0), new THREE.Vector3(0, 1.35, 0), 0.35 ); // 设置重力 const gravity = -9.8; // 玩家的速度 const playerVelocity = new THREE.Vector3(0, 0, 0); // 方向向量 const playerDirection = new THREE.Vector3(0, 0, 0);
// 键盘按下事件 const keyStates = { KeyW: false, KeyA: false, KeyS: false, KeyD: false, Space: false, isDown: false, };
// 根据键盘按下的键来更新键盘的状态 document.addEventListener( "keydown", (event) => { keyStates[event.code] = true; keyStates.isDown = true; }, false ); document.addEventListener( "keyup", (event) => { keyStates[event.code] = false; keyStates.isDown = false; }, false );
// 根据鼠标在屏幕移动,来旋转胶囊 window.addEventListener( "mousemove", (event) => { capsule.rotation.y -= event.movementX * 0.003; }, false );
// 根据键盘状态更新玩家的速度 function controlPlayer(deltaTime) { if (keyStates["KeyW"]) { playerDirection.z = 1; //获取胶囊的正前面方向 const capsuleFront = new THREE.Vector3(0, 0, 0); capsule.getWorldDirection(capsuleFront); // console.log(capsuleFront); // 计算玩家的速度 playerVelocity.add(capsuleFront.multiplyScalar(deltaTime)); } if (keyStates["KeyS"]) { playerDirection.z = 1; //获取胶囊的正前面方向 const capsuleFront = new THREE.Vector3(0, 0, 0); capsule.getWorldDirection(capsuleFront); // console.log(capsuleFront); // 计算玩家的速度 playerVelocity.add(capsuleFront.multiplyScalar(-deltaTime)); } if (keyStates["KeyA"]) { playerDirection.x = 1; //获取胶囊的正前面方向 const capsuleFront = new THREE.Vector3(0, 0, 0); capsule.getWorldDirection(capsuleFront);
// 侧方的方向,正前面的方向和胶囊的正上方求叉积,求出侧方的方向 capsuleFront.cross(capsule.up); // console.log(capsuleFront); // 计算玩家的速度 playerVelocity.add(capsuleFront.multiplyScalar(-deltaTime)); } if (keyStates["KeyD"]) { playerDirection.x = 1; //获取胶囊的正前面方向 const capsuleFront = new THREE.Vector3(0, 0, 0); capsule.getWorldDirection(capsuleFront);
// 侧方的方向,正前面的方向和胶囊的正上方求叉积,求出侧方的方向 capsuleFront.cross(capsule.up); // console.log(capsuleFront); // 计算玩家的速度 playerVelocity.add(capsuleFront.multiplyScalar(deltaTime)); } if (keyStates["Space"]) { playerVelocity.y = 15; } }
// 归位 function resetPlayer() { if (capsule.position.y < -20) { playerCollider.start.set(0, 2.35, 0); playerCollider.end.set(0, 3.35, 0); playerCollider.radius = 0.35; playerVelocity.set(0, 0, 0); playerDirection.set(0, 0, 0); } }
// 创建一个octree const worldOctree = new Octree(); worldOctree.fromGraphNode(plane);
// 判断是否在平面上 let playerOnFloor = false;
// 碰撞检验 function playerCollisions() { // 人物碰撞检测 const result = worldOctree.capsuleIntersect(playerCollider); playerOnFloor = false; if (result) { playerOnFloor = result.normal.y > 0; playerCollider.translate(result.normal.multiplyScalar(result.depth)); } }
function updatePlayer(deltaTime) { let damping = -0.05; if (playerOnFloor) { playerVelocity.y = 0; keyStates.isDown || playerVelocity.addScaledVector(playerVelocity, damping); } else { playerVelocity.y += gravity * deltaTime; } // 计算玩家移动的距离 const playerMoveDistance = playerVelocity.clone().multiplyScalar(deltaTime); playerCollider.translate(playerMoveDistance); // 将胶囊的位置进行设置 playerCollider.getCenter(capsule.position); // 碰撞检测 playerCollisions() } const animate = function () { let delta = clock.getDelta(); updatePlayer(delta); controlPlayer(delta) resetPlayer(); stats.update(); requestAnimationFrame(animate); // controls.update(); renderer.render(scene, camera); }; animate(); }) </script> <style> * { margin: 0; padding: 0; } #container { width: 100vw; height: 100vh; } </style>
|