每日见闻之Three.js 给球体贴个纹理图

js 复制代码
<!DOCTYPE html>
<html>
<head>
    <title>Three.js 月球模型</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
</head>
<body>
    <script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
    <script>
        // 创建场景
        const scene = new THREE.Scene();
        scene.background = new THREE.Color(0x000000); // 黑色背景模拟太空

        // 创建相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 10;
        camera.position.x = -3;

        // 创建渲染器(开启抗锯齿)
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // 加载纹理(使用示例图片替代实际月球纹理)
        const textureLoader = new THREE.TextureLoader();
        // 月球表面纹理(使用灰色纹理模拟月球表面)
        const moonTexture = textureLoader.load('https://qcloud.dpfile.com/pc/_vzQAXVr13f_7iwVuYMN-KZdECsM6WjeAFvnO_6J6g0hOLcGgiPlxc9FC8mAyrgC.jpg');


        // 创建月球模型
        const moon = new THREE.Mesh(
            new THREE.SphereGeometry(3, 32, 32), // 高分段数让表面更平滑
            new THREE.MeshStandardMaterial({
              
                map: moonTexture,       // 表面纹理
                metalness: 0,           // 非金属质感
                roughness: 0.8          // 粗糙表面,符合月球特性
            })
        );
        scene.add(moon);

        // 添加光源
        // 环境光(基础照明)
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
        scene.add(ambientLight);

        // 定向光(模拟太阳光,产生阴影和明暗对比)
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(5, 3, 5); // 从斜上方照射
        scene.add(directionalLight);

        // 动画循环(月球自转)
        function animate() {
            requestAnimationFrame(animate);
            moon.rotation.y += 0.01; // 缓慢自转
            moon.rotation.x += 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 天前
有了大屏设计稿还不够,我又用 gpt-image-2把里面的素材扒了出来
前端·three.js·数据可视化
用户78937733908535 天前
Vue3 + Three.js 仓储数字孪生:按需渲染架构与五大核心功能复盘
vue.js·three.js
孙凯亮9 天前
Three.js VR 模拟器(Immersive Web Emulator)踩坑全记录:从报错到可用,避坑指南一次性奉上
前端·three.js
苏武难飞12 天前
THREE.JS实现一个魔法镜子!
前端·css·three.js
郝学胜-神的一滴12 天前
[简化版 Games 101] 计算机图形学 05:二维变换下
c++·unity·图形渲染·three.js·opengl·unreal
qq_120840937114 天前
Three.js 场景性能优化实战:首屏、帧率与内存的工程化治理
开发语言·javascript·性能优化·three.js
qq_120840937115 天前
Three.js 模型加载稳定性实战:从资源失败到可用发布的工程化方案
前端·javascript·vue.js·vue3·three.js
烛阴19 天前
Three.js 材质完全入门指南:让你的 3D 物体「活」起来
webgl·three.js
三维搬砖者25 天前
AI 解密大厂 Three.js 三维引擎开发 03|从经纬度到三维世界的坐标解码
webgl·three.js
一马平川的大草原1 个月前
基于Vue+Three.js实现三维油藏模型解析与可视化交互切割操作
vue.js·three.js·三维油藏模型