每日见闻之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>
相关推荐
三维搬砖者6 天前
AI 解密大厂 Three.js 三维引擎开发 03|从经纬度到三维世界的坐标解码
webgl·three.js
一马平川的大草原14 天前
基于Vue+Three.js实现三维油藏模型解析与可视化交互切割操作
vue.js·three.js·三维油藏模型
RulerMike15 天前
three 实现简单机械臂逆运动
前端·ai编程·three.js
程序员阿峰17 天前
前端3D·Three.js一学就会系列:第二 画线
前端·three.js
程序员阿峰18 天前
前端3D·Three.js一学就会系列: 第一个3D网站
前端·three.js
烛阴18 天前
Three.js 场景完全入门指南:让你的 3D 场景不在乱成一团
webgl·three.js
ai超级个体21 天前
前端下午茶:这 3 个网页特效建议收藏(送源码)
前端·three.js·threejs·网页设计·vibe coding·网页灵感·网页分享
codingWhat23 天前
用 three.js 实现 3D 地图
three.js
答案answer1 个月前
我的Three.js3D场景编辑器免费开源啦🎉🎉🎉
前端·github·three.js
一拳不是超人1 个月前
Three.js一起学-如何通过官方例子高效学习 Three.js?手把手带你“抄”出一个3D动画
前端·webgl·three.js