three实现控制人物在三维场景爬楼梯效果代码

代码语言:html

所属分类:三维

代码描述:three实现控制人物在三维场景爬楼梯效果代码,按住键盘方向键或wasd键进行前后左右移动。

代码标签: three 控制 人物 三维 场景 楼梯

下面为部分代码预览,完整代码请点击下载或在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]);
                walkAction = mixer.clipAction(animations[1]);

                idleAction.play();

                // Set up controls
                document.addEventListener('keydown', onKeyDown);
                document.addEventListener('keyup', onKeyUp);
            });

            // Ground
            const groundGeometry = new THREE.PlaneGeometry(100, 100);
            const groundMaterial = new THREE.MeshStandardMaterial({ color: 0xcccccc });
            const ground = new THREE.Mesh(groundGeometry, groundMaterial);
            ground.rotation.x = -Math.PI / 2;
            ground.receiveShadow = true; // Enable shadow receiving for ground
            scene.add(ground);

    
            // Stairs
            const stairGeometry = new THREE.BoxGeometry(5, 0.5, 1);
            const stairMaterial = new THREE.MeshStandardMaterial({ color: 0x777777 });
            for (let i = 0; i < stepCount; i++) {
                const stair = new THREE.Mesh(stairGeometry, stairMaterial);
                stair.position.set(0, i * 0.5 + 0.25, -3+i * -1);
                stair.castShadow = true; // Enable shadow casting for stairs
                scene.add(stair);
            }

            // OrbitControls for mouse interaction
            controls = new THREE.OrbitControls(camera, renderer.domElement);
            controls.target.set(0, 1, 0);
            controls.update();
        }

        function onKeyDown(event) {
            switch (event.code) {
                case 'ArrowUp':
   .........完整代码请登录后点击上方下载按钮下载查看

网友评论0