每日见闻之 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>
    
相关推荐
小胖霞13 小时前
vite+ts+monorepo从0搭建vue3组件库(五):vite打包组件库
前端·vue.js·前端框架
神算大模型APi--天枢64613 小时前
国产硬件架构算力平台:破解大模型本地化部署难题,标准化端口加速企业 AI 落地
大数据·前端·人工智能·架构·硬件架构
AAA阿giao13 小时前
从“拼字符串”到“魔法响应”:一场数据驱动页面的奇幻进化之旅
前端·javascript·vue.js
donecoding13 小时前
解决 npm 发布 403 错误:全局配置 NPM Automation Token 完整指南
前端·javascript
潜水豆13 小时前
浅记录一下专家体系
前端
梨子同志13 小时前
Node.js 事件循环(Event Loop)
前端
北慕阳13 小时前
背诵-----------------------------
java·服务器·前端
JS_GGbond13 小时前
Vue3 组件入门:像搭乐高一样玩转前端!
前端·vue.js
SakuraOnTheWay13 小时前
拆解一个由 setTimeout 引发的“页面假死”悬案
前端·javascript
渔_13 小时前
【已解决】uni-textarea 无法绑定 v-model / 数据不回显?换原生 textarea 一招搞定!
前端