当前位置:首页 > JavaScript

js实现星球转动

2026-02-02 13:21:37JavaScript

使用Three.js实现星球转动

Three.js是一个强大的JavaScript 3D库,可以轻松创建3D场景和动画。以下是一个简单的星球转动实现示例。

安装Three.js:

npm install three

HTML结构:

<!DOCTYPE html>
<html>
<head>
    <title>3D Planet Rotation</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
</head>
<body>
    <script src="main.js"></script>
</body>
</html>

JavaScript代码(main.js):

import * as THREE from 'three';

// 创建场景
const scene = new THREE.Scene();

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

// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

// 创建球体(星球)
const geometry = new THREE.SphereGeometry(1, 32, 32);
const material = new THREE.MeshBasicMaterial({ 
    color: 0x00ff00,
    wireframe: true 
});
const sphere = new THREE.Mesh(geometry, material);
scene.add(sphere);

// 动画循环
function animate() {
    requestAnimationFrame(animate);

    // 旋转星球
    sphere.rotation.x += 0.01;
    sphere.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);
});

添加纹理和光照

为了使星球看起来更真实,可以添加纹理和光照:

// 替换之前的material创建代码
const textureLoader = new THREE.TextureLoader();
const texture = textureLoader.load('path/to/your/texture.jpg');
const material = new THREE.MeshPhongMaterial({ 
    map: texture 
});

// 添加光源
const light = new THREE.PointLight(0xffffff, 1, 100);
light.position.set(5, 5, 5);
scene.add(light);

// 添加环境光
const ambientLight = new THREE.AmbientLight(0x404040);
scene.add(ambientLight);

实现轨道控制

添加轨道控制器可以允许用户交互式查看星球:

import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

// 在渲染器创建后添加
const controls = new OrbitControls(camera, renderer.domElement);
controls.update(); // 必须在动画循环中调用

// 修改animate函数
function animate() {
    requestAnimationFrame(animate);
    controls.update(); // 需要更新控制器
    renderer.render(scene, camera);
}

添加星空背景

创建星空背景增强视觉效果:

// 创建星空背景
const starGeometry = new THREE.BufferGeometry();
const starMaterial = new THREE.PointsMaterial({
    color: 0xffffff,
    size: 0.05
});

const starVertices = [];
for (let i = 0; i < 10000; i++) {
    const x = (Math.random() - 0.5) * 2000;
    const y = (Math.random() - 0.5) * 2000;
    const z = (Math.random() - 0.5) * 2000;
    starVertices.push(x, y, z);
}

starGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starVertices, 3));
const stars = new THREE.Points(starGeometry, starMaterial);
scene.add(stars);

性能优化

对于更复杂的场景,可以考虑以下优化:

js实现星球转动

// 使用性能监视器
import Stats from 'three/examples/jsm/libs/stats.module';
const stats = new Stats();
document.body.appendChild(stats.dom);

// 在animate函数中更新
function animate() {
    requestAnimationFrame(animate);
    stats.update();
    // ...其他代码
}

// 使用低多边形球体减少顶点数
const geometry = new THREE.SphereGeometry(1, 16, 16);

这个实现展示了如何使用Three.js创建一个基本的3D星球旋转动画,并逐步添加纹理、光照和交互控制等功能。根据需求可以进一步扩展,比如添加多个行星、实现公转轨迹或更复杂的光照效果。

标签: 星球js
分享给朋友:

相关文章

js实现倒计时

js实现倒计时

使用 setInterval 实现倒计时 通过 setInterval 定时器每秒更新剩余时间,适用于简单倒计时场景。 function countdown(seconds, callbac…

js实现复制到剪贴板

js实现复制到剪贴板

使用document.execCommand方法(传统方式,已废弃但部分浏览器仍支持) 该方法适用于旧版浏览器,但已被标记为废弃。执行前需确保文本已被选中: function copyToClipb…

js实现百叶窗

js实现百叶窗

使用CSS和JavaScript实现百叶窗效果 通过CSS动画和JavaScript事件监听可以实现百叶窗效果。核心思路是将内容区域分割为多个条状元素,通过控制它们的展开/折叠状态来模拟百叶窗。…

js实现轮播代码

js实现轮播代码

基础轮播实现 使用HTML、CSS和JavaScript创建一个简单的轮播效果。HTML部分定义轮播容器和图片元素。 <div class="carousel"> <div c…

js 实现拖拽

js 实现拖拽

实现拖拽的基本步骤 在JavaScript中实现拖拽功能需要监听几个关键事件:mousedown、mousemove和mouseup。以下是实现的基本逻辑。 监听目标元素的mousedown事件,记…

js实现图片放大

js实现图片放大

使用 CSS transform 实现图片放大 通过 CSS 的 transform: scale() 属性实现图片放大效果。当鼠标悬停时,图片会平滑放大。 const img = document…