Skip to content
鼓励作者:欢迎打赏犒劳

threeJs初体验

教程:https://discoverthreejs.com/zh/book/first-steps/

场景,相机,渲染器,网格(几何体+材质) 重要的概念

shell
 "three": "^0.173.0"

threeJs小demo

html
<!DOCTYPE html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>My first three.js app</title>
</head>
<body>
    <script type="module">
        import * as THREE from 'three';

        // 1. 创建场景、相机和渲染器
        const scene = new THREE.Scene();
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const renderer = new THREE.WebGLRenderer();

        // 2. 设置渲染器大小
        renderer.setSize(window.innerWidth, window.innerHeight);
        // 设置设备像素比(DPR)
        renderer.setPixelRatio(window.devicePixelRatio);

        // 3. 将渲染器的 DOM 元素添加到页面中
        document.body.appendChild(renderer.domElement);


        // 4. 创建几何体和材质,并添加到场景中
        const geometry = new THREE.BoxGeometry();
        const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        // 5. 设置相机位置
        camera.position.z = 5;

        // 6. 渲染场景
        function animate() {
        requestAnimationFrame(animate);
        cube.rotation.x += 0.01; // 旋转
        cube.rotation.y += 0.01; // 旋转
        renderer.render(scene, camera); // 渲染场景
    }
        animate(); // 启动动画循环
    </script>
</body>
</html>

上次更新:

如有转载或 CV 的请标注本站原文地址