每日见闻之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(0xf0f0f0);
        
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.z = 10;
        
        // 开启抗锯齿
        const renderer = new THREE.WebGLRenderer({ antialias: true });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);

        // 光源
        const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
        scene.add(ambientLight);
        const pointLight = new THREE.PointLight(0xffffff, 0.8);
        pointLight.position.set(5, 5, 5);
        scene.add(pointLight);

        // 1. 外层球体(双面渲染)只绘制一半 后面四个参数决定绘制的角度
        const outerGeometry = new THREE.SphereGeometry(3, 64, 32,0,Math.PI,0,Math.PI);
        const outerMaterial = new THREE.MeshStandardMaterial({
            color: 0x0099ff,
            metalness: 0.7,
            roughness: 0.3,
            side: THREE.DoubleSide // 关键:同时渲染内外表面
        });
        const outerSphere = new THREE.Mesh(outerGeometry, outerMaterial);
        scene.add(outerSphere);

        // 2. 内层小球(模拟实心填充,可选)
        const innerGeometry = new THREE.SphereGeometry(2.9, 64, 32); // 半径略小于外层
        const innerMaterial = new THREE.MeshStandardMaterial({
            color: 0x0099ff, // 与外层同色
            metalness: 0.5,
            roughness: 0.5
        });
        const innerSphere = new THREE.Mesh(innerGeometry, innerMaterial);
        scene.add(innerSphere); // 添加内层小球,增强实心感

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