每日见闻之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>
相关推荐
柳杉3 小时前
别再手写大屏了!用这套「3D 地图 + 拖拽脚手架」示例源码,交付效率提升 80%
前端·three.js·数据可视化
郝学胜-神的一滴3 小时前
[简化版 GAMES 101] 计算机图形学 16:纹理走样、Mipmap、三线性插值、各向异性过滤原理全解
unity·游戏引擎·godot·图形渲染·three.js·opengl·unreal
柳杉5 天前
可视化大屏设计器脚手架:从设计到交付的一站式方案
前端·three.js·数据可视化
jump_jump6 天前
网页 UI 终于能进游戏和 3D 场景了:HTML-in-Canvas 为什么重要
浏览器·three.js·canvas
牧艺12 天前
用 Next.js + React Three Fiber 打造 3D 快递仓储可视化
前端·three.js
Yuhua_Cesium_Threejs14 天前
《在 Cesium 中用 Three.js 实现气象雷达三维体渲染——从原理到性能优化》
three.js
牧艺14 天前
用 Three.js 实现一个浏览器端 3D 看车的项目
前端·three.js
凌涘19 天前
从零掌握 CSS 3D:用几行代码让网页"立"起来
three.js
柳杉19 天前
我用Threejs 搓了一个 3D 中国地图设计器,开箱即用
前端·three.js·数据可视化
郝学胜-神的一滴1 个月前
[简化版 GAMES 101] 计算机图形学 12:可见性与 Z‑Buffer 深度缓存
unity·godot·图形渲染·three.js·opengl·unreal