three实现三维迷宫鼠标光照跟随效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维迷宫鼠标光照跟随效果代码

代码标签: three 三维 迷宫 鼠标 光照 跟随

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

  
  
  
<style>
@import url('https://fonts.googleapis.com/css2?family=Audiowide&display=swap');

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    overflow: hidden;
    cursor: none;
    background: #000;
}

h1 {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%);
    text-transform: uppercase;
    color: #f00;
    font-family: 'Audiowide', cursive;
    font-size: min(128px, 10vw);
    z-index: 1;
}

#root {
    opacity: 0;
    transition: opacity 1s ease-out;
}

#root.-loaded {
    opacity: 1;
}

.cursor {
    position: fixed;
    height: 64px;
    width: 64px;
    border-radius: 50%;
    border: 2px solid #ffffff;
    transform: translateX(calc(50vw - 32px)) translateY(calc(50vh - 32px));
}
</style>


</head>

<body  >
  <h1>Maze</h1>

<div id='root'>
    <div class='cursor'></div>
</div>

<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.146.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/EffectComposer.146.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/ShaderPass.146.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/CopyShader.146.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/RenderPass.146.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/LuminosityHighPassShader.146.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/UnrealBloomPass.146.js"></script>

<script type='module'>
    // https://www.npmjs.com/package/generate-maze
    import generateMaze from '//repo.bfw.wiki/bfwrepo/js/module/generate-maze.module.js';

    const COLOR_SCHEME = Object.freeze({
        background: 0x000000,
        wall: 0x777777,
        floor: 0x333333,
        box: 0xff0000,
    });

    class MaterialsLibrary {
        static wall = new THREE.MeshStandardMaterial({
            color: COLOR_SCHEME.wall,
        });

        static floor = new THREE.MeshStandardMaterial({
            color: COLOR_SCHEME.floor,
        });

        static box = new THREE.MeshStandardMaterial({
            color: COLOR_SCHEME.box,
        });
    }

    class Maze extends THREE.Group {
        static #boxGeometry = new THREE.BoxGeometry(0.2, 0.2, 0.2);
        static #wallGeometry = new THREE.BoxGeometry(1.2, 0.2, 1);

        static #addBox(room, roomOptions) {
            let wallsAround = 0;

            if (roomOptions.left) {
                wallsAround++;
            }

            if (roomOptions.right) {
                wallsAround++;
            }

            if (roomOptions.top) {
                wallsAround++;
            }

            if (roomOptions.bottom) {
                wallsAround++;
            }

            if (wallsAround > 1) {
                return;
            }

            const boxMaterial = MaterialsLibrary.box;
            const boxGeometry = Maze.#boxGeometry;
            const box = new THREE.Mesh(boxGeometry, boxMaterial);

            box.position.set(0, 0.5, -0.5);
            box.castShadow = true;
            box.receiveShadow = true;

            room.add(box);

        }

        static #addWalls(room, roomOptions, x, y, size) {
            const hasLeftWall = (x === 0) || (roomOptions.left);
            const hasRightWall = (x === size - 1);
            const hasTopWall = (y === 0) || (roomOptions.top);
            const hasBottomWall = (y === size - 1);

            const hasNoWalls = !hasLeftWall && !hasRightWall && !hasTopWall && !hasBottomWall;

            if (hasNoWalls) {
                return;
            }

            const wallMaterial = MaterialsLibrary.wall;
            const wallGeometry = Maze.#wallGeometry;

            if (hasTopWall) {
                const wall = new THREE.Mesh(wallGeometry, wallMaterial);

                wall.castShadow = true;
                wall.receiveShadow = true;

                room.add(wall);
            }

            if (hasBottomWall) {
                const wall = new THREE.Mesh(wallGeometry, wallMaterial);

                wall.castShadow = true;
                wall.receiveShadow = true;
                wall.position.set(0, 1, 0);

                room.add(wall);
            }

            if (hasLeftWall) {
                const wall = new THREE.Mesh(wallGeometry, wallMaterial);

                wall.position.set(-0.5, 0.5, 0);
                wall.rotation.set(0, 0, Math.PI / 2);
                wall.castShadow = true;
                wall.receiveShadow = true;

                room.add(wall);
            }

            if (hasRightWall) {
                const wall = new THREE.Mesh(wallGeometry, wallMaterial);

                wall.position.set(0.5, 0.5, 0);
                wall.rotation.set(0, 0, Math.PI / 2);
                wall.castShadow = true;
                wall.receiveShadow = true;

                room.add(wall);
            }
        }

        constructor() {
            super();

            this.scale.set(1, -1, 1);

            const size = 20;
            const areEdgesOpened = false;
            const seed = Math.random() * 10000;
            const pattern = generateMaze(size, size, areEdgesOpened, seed);

            for (let x = 0; x < size; x++) {
                for (let y = 0; y < size;.........完整代码请登录后点击上方下载按钮下载查看

网友评论0