每日见闻之 Three.js 创建一个平面环形 并上面贴图

js 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Three.js RingGeometry 示例</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
</head>
<body>
    <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);
        
        // 创建环形几何体
        const geometry = new THREE.RingGeometry(
            1,          // 内半径
            3,          // 外半径
            32,         // 圆周分段(越多数值越光滑)
            8,          // 径向分段
            0,          // 起始角度
              Math.PI    // 角度范围(这里创建半圆环形)Math.PI * 2就是完整环形
        );
        
        // 加载纹理
        const textureLoader = new THREE.TextureLoader();
        const texture = textureLoader.load('https://qcloud.dpfile.com/pc/_vzQAXVr13f_7iwVuYMN-KZdECsM6WjeAFvnO_6J6g0hOLcGgiPlxc9FC8mAyrgC.jpg');
        
        // 创建材质(使用受光照影响的材质)
        const material = new THREE.MeshStandardMaterial({
            map: texture,
            side: THREE.DoubleSide, // 双面显示
            transparent: true,      // 允许透明
            alphaTest: 0.1          // 处理透明区域
        });
        
        // 创建网格并添加到场景
        const ring = new THREE.Mesh(geometry, material);
        scene.add(ring);
        
        // 添加光源
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
        scene.add(ambientLight);
        
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(5, 5, 10);
        scene.add(directionalLight);
        
        // 动画循环
        function animate() {
            requestAnimationFrame(animate);
            ring.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>
    
相关推荐
我登哥MVP20 小时前
Ajax 详解
java·前端·ajax·javaweb
非凡ghost20 小时前
Typora(跨平台MarkDown编辑器) v1.12.2 中文绿色版
前端·windows·智能手机·编辑器·软件需求
馨谙21 小时前
/dev/null 是什么,有什么用途?
前端·chrome
JamSlade21 小时前
流式响应 sse 系统全流程 react + fastapi为例子
前端·react.js·fastapi
徐同保1 天前
react useState ts定义类型
前端·react.js·前端框架
liangshanbo12151 天前
React 19 vs React 18全面对比
前端·javascript·react.js
望获linux1 天前
【实时Linux实战系列】Linux 内核的实时组调度(Real-Time Group Scheduling)
java·linux·服务器·前端·数据库·人工智能·深度学习
Never_Satisfied1 天前
在 JavaScript 中,删除数组中内容为xxx的元素
java·前端·javascript
_菜鸟果果1 天前
Vue3+echarts 3d饼图
前端·javascript·echarts
Luffe船长1 天前
前端vue2+js+springboot实现excle导入优化
前端·javascript·spring boot