每日见闻之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>
相关推荐
阿里巴啦1 天前
用React+Three.js 做 3D Web版搭建三维交互场景:模型的可视化摆放与轻量交互
前端·react·three.js·模型可视化·web三维·web三维交互场景
阿里巴啦3 天前
React + Three.js + R3F + Vite 实战:可交互的三维粒子化展厅
react.js·three.js·粒子化·drei·postprocessing·三维粒子化
叫我詹躲躲3 天前
基于 Three.js 的 3D 地图可视化:核心原理与实现步骤
前端·three.js
map_3d_vis4 天前
JSAPIThree 加载单体三维模型学习笔记:SimpleModel 简易加载方式
学习笔记·three.js·gltf·glb·初学者·三维模型·mapvthree·jsapithree·simplemodel
Addisonx7 天前
深度复盘 III: 核心逻辑篇:构建 WebGL 数字孪生的“业务中枢”与“安全防线”
webgl·three.js
爱看书的小沐7 天前
【小沐学WebGIS】基于Three.JS绘制二三维地图地球晨昏效果(WebGL / vue / react )
javascript·vue.js·gis·webgl·three.js·opengl·晨昏线
Addisonx10 天前
深度复盘: WebGL 数字孪生前端架构:如何打造高颜值、高性能的 Web 3D 可视化系统
three.js
BUG创建者12 天前
thee.js完成线上展厅demo
开发语言·前端·javascript·css·html·css3·three.js
一千柯橘22 天前
从摄影新手到三维光影师:Three.js 核心要素的故事
前端·three.js
big男孩23 天前
OrbitControls 的完整原理
three.js