每日见闻之Three.js 画一个会动的环形

js 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>MeshStandardMaterial Example</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
    <script>
        // 创建场景
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0xf0f0f0);

        // 创建相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 10;

        // 创建渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        //创建光源(MeshStandardMaterial需要光源才能显示)
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // 环境光
        scene.add(ambientLight);
        
        const pointLight = new THREE.PointLight(0xffffff, 0.8); // 点光源
        pointLight.position.set(5, 5, 5);
        scene.add(pointLight);

        // 创建圆环几何体
        const geometry = new THREE.TorusGeometry(3, 1, 16, 100);
        
        // 创建标准材质
        const material = new THREE.MeshStandardMaterial({
            color: 0x00ff00,      // 蓝色
            metalness: 0.8,       // 较高金属度
            roughness: 0.2,       // 较低粗糙度(较光滑)
           //  wireframe: true    // 取消注释可查看线框
        });

        // 创建网格并添加到场景
        const torus = new THREE.Mesh(geometry, material);
        scene.add(torus);

        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            torus.rotation.x += 0.01;
            torus.rotation.y += 0.01;
            renderer.render(scene, camera);
        }
        animate();

        // 响应窗口大小变化
        window.addEventListener('resize', () => {
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
            renderer.setSize(window.innerWidth, window.innerHeight);
        });
    </script>
</body>
</html>
相关推荐
Becauseofyou13715 小时前
如果你刚入门Three.js,这几个开源项目值得你去学习
前端·three.js
Jedi Hongbin3 天前
Three.js shader内置矩阵注入
前端·javascript·three.js
接着奏乐接着舞。3 天前
3D地球可视化教程 - 第1篇:基础地球渲染系统
前端·javascript·vue.js·3d·three.js
刘皇叔code4 天前
记一次用Three.js展示360°全景图的折腾
webgl·three.js
世伟爱吗喽5 天前
threejs入门学习日记
前端·javascript·three.js
拜无忧7 天前
three.js纸飞机飞行撞建筑
前端·three.js
拜无忧7 天前
three/文字爆裂效果
three.js
前端人类学8 天前
构筑数字夜空:Three.js 建筑群灯光特效全解析
javascript·three.js
柳杉10 天前
使用three.js搭建3d隧道监测-3
前端·javascript·three.js
三维搬砖者11 天前
06Threejs电影拍摄角度-第三章:搭建场景 - 初始化环境
three.js