three实现控制人物在三维场景爬楼梯效果代码
代码语言:html
所属分类:三维
代码描述:three实现控制人物在三维场景爬楼梯效果代码,按住键盘方向键或wasd键进行前后左右移动。
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Soldier Model Control</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.133.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/GLTFLoader.133.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.133.js"></script>
<script>
let scene, camera, renderer, soldier, mixer, clock;
let moveForward = false, moveBackward = false, moveLeft = false, moveRight = false;
let controls;
let idleAction, walkAction;
let jump = false;
let velocity = new THREE.Vector3();
const gravity = -0.1;
const groundY = 0;
const stepHeight = 1;
const stepCount = 166;
init();
animate();
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x87CEEB); // Sky blue background
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(0, 5, 10);
camera.lookAt(0, 0, 0);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; // Enable shadow mapping
document.body.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 1.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1.5);
directionalLight.position.set(0, 6, 2);
directionalLight.castShadow = true; // Enable shadow casting
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
directionalLight.shadow.camera.near = 0.5;
directionalLight.shadow.camera.far = 500;
scene.add(directionalLight);
clock = new THREE.Clock();
// Load Soldier model
const loader = new THREE.GLTFLoader();
loader.load('https://threejs.org/examples/models/gltf/Soldier.glb', (gltf) => {
soldier = gltf.scene;
soldier.traverse((child) => {
if (child.isMesh) {
child.castShadow = true; // Enable shadow casting for soldier
}
});
scene.add(soldier);
mixer = new THREE.AnimationMixer(soldier);
const animations = gltf.animations;
idleAction = mixer.clipAction(animations[0.........完整代码请登录后点击上方下载按钮下载查看
网友评论0