每日见闻之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>
    
相关推荐
姓蔡小朋友4 分钟前
React基础
前端·react.js·前端框架
IT_陈寒20 分钟前
Vue的动态组件坑了我整整一天!
前端·人工智能·后端
恋猫de小郭22 分钟前
Flutter 最好的 AI 自动化测试工具:Patrol
android·前端·flutter
Cobyte23 分钟前
AI 的个人便签纸:Claude Code 的 TodoWrite 模式
前端·后端·aigc
风兮雨露31 分钟前
Java 从入门到精通,前端资料
java·开发语言·前端
ZC跨境爬虫36 分钟前
跟着 MDN 学CSS day_43:CSS布局挑战——从浮动到弹性盒与栅格的综合实践
前端·css·ui·html·tensorflow
Qres82144 分钟前
Hexo博客本地配置
前端·博客·hexo
Bigger1 小时前
GitLab-Runner + AI 代码审查服务 + 远程大模型 全套部署运维实战
前端·ci/cd·ai编程
_xaboy1 小时前
开源AI表单设计器 FcDesigner v3.5 版本发布!
前端·vue.js·低代码·开源·表单
爱讲故事的1 小时前
操作系统第三讲:Context Switch —— 用户态如何安全地进入内核态?
前端·javascript·安全