每日见闻之 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>
    
相关推荐
五月君_4 小时前
炸裂!Claude Opus 4.6 与 GPT-5.3 同日发布:前端人的“自动驾驶“时刻到了?
前端·gpt
Mr Xu_4 小时前
前端开发中CSS代码的优化与复用:从公共样式提取到CSS变量的最佳实践
前端·css
鹏北海-RemHusband4 小时前
从零到一:基于 micro-app 的企业级微前端模板完整实现指南
前端·微服务·架构
LYFlied4 小时前
AI大时代下前端跨端解决方案的现状与演进路径
前端·人工智能
光影少年4 小时前
AI 前端 / 高级前端
前端·人工智能·状态模式
一位搞嵌入式的 genius4 小时前
深入 JavaScript 函数式编程:从基础到实战(含面试题解析)
前端·javascript·函数式
anOnion5 小时前
构建无障碍组件之Alert Dialog Pattern
前端·html·交互设计
choke2335 小时前
[特殊字符] Python 文件与路径操作
java·前端·javascript
云飞云共享云桌面5 小时前
高性能图形工作站的资源如何共享给10个SolidWorks研发设计用
linux·运维·服务器·前端·网络·数据库·人工智能
Deng9452013145 小时前
Vue + Flask 前后端分离项目实战:从零搭建一个完整博客系统
前端·vue.js·flask