每日见闻之Three.js 创建一个立体并且每个面贴个图

js 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Three.js 带贴图的立方体</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();
        
        // 创建相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 5;
        
        // 创建渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        
        // 1. 创建立方体几何体
        const geometry = new THREE.BoxGeometry(2, 2, 2,3,3,3);
        
        // 2. 加载纹理贴图
        const textureLoader = new THREE.TextureLoader();
        // 加载示例图片作为纹理(可替换为自己的图片URL)
        const texture = textureLoader.load('https://qcloud.dpfile.com/pc/_vzQAXVr13f_7iwVuYMN-KZdECsM6WjeAFvnO_6J6g0hOLcGgiPlxc9FC8mAyrgC.jpg');

       // 设置纹理重复(需配合wrapS/wrapT)代表每个方向平铺的次数
        texture.wrapS = THREE.RepeatWrapping;
        texture.wrapT = THREE.RepeatWrapping;
        texture.repeat.set(2, 2); // 水平和垂直各重复5次
       g ou z ou zg ou z ou zgou
        // 3. 创建带纹理的材质
        const material = new THREE.MeshBasicMaterial({ 
            map: texture, // 将纹理应用到材质
            side: THREE.DoubleSide // 双面显示(可选)
        });
        
        // 4. 创建网格(几何体+材质)
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);
        
        // 添加环境光(让纹理更清晰可见)
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
        scene.add(ambientLight);
        
        // 添加平行光
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(5, 5, 5);
        scene.add(directionalLight);
        
        // 动画循环(让立方体旋转)
        function animate() {
            requestAnimationFrame(animate);
            cube.rotation.x += 0.01;
            cube.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>
    
相关推荐
feixing_fx12 小时前
解构与展开:可读性和性能的取舍
开发语言·前端·javascript·前端框架
前端架构师-老李13 小时前
html教程:header、nav、main、footer 四个标签各管哪一块?
前端·html
烟雨空灵14 小时前
25 天做了个独立开发者作品橱窗,收录 340+ 件作品,代码全开源
前端·node.js
wangruofeng14 小时前
怎么让你的项目更好看?用 UI Lab 练设计原则,给界面去 AI 味
前端·aigc·ai编程
用户693717500138415 小时前
AI 越强,为什么我们反而越来越担心失业?
前端·后端·github
涛涛ing16 小时前
线上突发大面积白屏,排查一天后,我发现是 `JSON.parse` 背了锅
前端
狮子座的男孩16 小时前
javascript 基础 es6:1、let 声明,let 实践案例,const 声明,解构赋值
前端·javascript·经验分享·es6·const声明·解构赋值·let声明及案例
拖孩17 小时前
又又又用 AI 写了个小程序:我站起来猛蹬 AI,使劲薅羊毛
前端·后端·微信小程序
粥里有勺糖17 小时前
视野修炼-技术周刊第138期 | 不要让 AI 代表你
前端·github·aigc
WJ.Polar18 小时前
JavaScript入门
前端·javascript