每日见闻之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>
相关推荐
大松鼠君13 小时前
GLSL 动画动作万能规律表
webgl·three.js
郝学胜-神的一滴15 小时前
中级OpenGL教程 006:高光反射原理与 Shader 实现
c++·unity·godot·图形渲染·three.js·opengl·unreal
李剑一1 天前
520了,程序员就得有点儿独特的浪漫
前端·three.js
贵州数擎科技有限公司1 天前
分形金字塔的 Ray Marching 实现
webgl·three.js
谢小飞2 天前
Three.js三球轮播沉浸式落地页开发
前端·three.js
贵州数擎科技有限公司3 天前
雨滴特效的 Three.js 实现
前端·three.js
郝学胜-神的一滴8 天前
中级OpenGL教程 005:为球体&平面注入法线灵魂
c++·unity·图形渲染·three.js·opengl·unreal
我胖虎不答应!!8 天前
Three.js开发思想笔记
javascript·笔记·three.js
白鳯10 天前
塔罗神谕:星月神域莱诺薇为您占卜
react·web·three.js·codex·deepseek·vibe coding·塔罗占卜
李剑一14 天前
耗时 2 小时!我复刻了全网超火的通透 3D 水晶球动效,Vue3+Three.js 做出高级视觉特效
前端·three.js