每日见闻之Three.js 创建一个四边形 并双面贴图

js 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Three.js PlaneGeometry 示例</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(0x87CEEB); // 天空蓝色背景
        
        // 创建相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(0, 2, 5); // 相机位置:x=0, y=2, z=5
        camera.lookAt(0, 0, 0); // 相机看向原点
        
        // 创建渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        
        // 创建平面几何体(地面)
        const planeGeometry = new THREE.PlaneGeometry(
            5,  // 宽度
            5,  // 高度
            10,  // 宽度分段
            10   // 高度分段
        );
        
        // 加载地面纹理
        const textureLoader = new THREE.TextureLoader();
        const groundTexture = textureLoader.load('https://qcloud.dpfile.com/pc/_vzQAXVr13f_7iwVuYMN-KZdECsM6WjeAFvnO_6J6g0hOLcGgiPlxc9FC8mAyrgC.jpg');
        
        // 设置纹理重复 可以设置平铺的次数
        groundTexture.wrapS = THREE.RepeatWrapping;
        groundTexture.wrapT = THREE.RepeatWrapping;
        groundTexture.repeat.set(3, 5); // 重复5x5次
        
        // 创建材质
        const groundMaterial = new THREE.MeshStandardMaterial({
            map: groundTexture,
            side: THREE.DoubleSide
        });
        
        // 创建地面网格并旋转(默认平面沿XZ轴,这里旋转后沿XY轴作为地面)
        const ground = new THREE.Mesh(planeGeometry, groundMaterial);

        ground.position.y = 0; // 放置在y=0位置
        scene.add(ground);
        
        // 添加光源
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
        scene.add(ambientLight);
        
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(5, 10, 7.5); // 光源位置
        scene.add(directionalLight);
        
        // 动画循环
        function animate() {
          ground.rotation.x += 0.001;
            requestAnimationFrame(animate);
            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>
    
相关推荐
code_YuJun11 分钟前
corepack 作用
前端
千寻girling11 分钟前
Koa.js 教程 | 一份不可多得的 Node.js 的 Web 框架 Koa.js 教程
前端·后端·面试
全栈前端老曹12 分钟前
【MongoDB】Node.js 集成 —— Mongoose ORM、Schema 设计、Model 操作
前端·javascript·数据库·mongodb·node.js·nosql·全栈
code_YuJun13 分钟前
pnpm-workspace.yaml
前端
天才熊猫君16 分钟前
“破案”笔记:iframe动态加载内容后,打印功能为何失灵?
前端
五月君_33 分钟前
炸裂!Claude Opus 4.6 与 GPT-5.3 同日发布:前端人的“自动驾驶“时刻到了?
前端·gpt
Mr Xu_37 分钟前
前端开发中CSS代码的优化与复用:从公共样式提取到CSS变量的最佳实践
前端·css
鹏北海-RemHusband1 小时前
从零到一:基于 micro-app 的企业级微前端模板完整实现指南
前端·微服务·架构
LYFlied1 小时前
AI大时代下前端跨端解决方案的现状与演进路径
前端·人工智能
光影少年1 小时前
AI 前端 / 高级前端
前端·人工智能·状态模式