每日见闻之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>
相关推荐
DaMu39 分钟前
Dreamcore3D ARPG IDE “手搓”游戏引擎,轻量级实时3D创作工具,丝滑操作,即使小白也能轻松愉快的创作出属于你自己的游戏世界!
前端·架构·three.js
烛阴1 天前
从“无”到“有”:手动实现一个 3D 渲染循环全过程
前端·webgl·three.js
烛阴3 天前
拒绝配置地狱!5 分钟搭建 Three.js + Parcel 完美开发环境
前端·webgl·three.js
XiaoYu20026 天前
第9章 Three.js载入模型GLTF
前端·javascript·three.js
XiaoYu20027 天前
第8章 Three.js入门
前端·javascript·three.js
AlanHou16 天前
Three.js:Web 最重要的 3D 渲染引擎的技术综述
前端·webgl·three.js
一颗烂土豆19 天前
🚴‍♂️ Vue3 + Three.js 实战:如何写一个“不晕车”的沉浸式骑行播放器 🎥
vue.js·游戏·three.js
Elaine33620 天前
Gemini生成的3D交互圣诞树(娱乐版)
3d·交互·three.js·前端可视化
Awu122721 天前
Vue3自定义渲染器:原理剖析与实践指南
前端·vue.js·three.js
龙猫不热21 天前
THREE.js 关于Material基类下的depthTest 和 depthWrite的理解
前端·three.js