写一个可交互的3d地球页面,注意可以放大缩小,3d
- User
写一个可交互的3d地球页面,注意可以放大缩小,3d
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
写一个可交互的3d地球页面,注意可以放大缩小,3d
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
我来为你创建一个可交互的3D地球页面。这个页面将使用Three.js库来实现3D效果,支持鼠标控制旋转、缩放等交互功能。
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>交互式3D地球</title>
<style>
body {
margin: 0;
padding: 0;
background: #000;
overflow: hidden;
font-family: Arial, sans-serif;
}
#container {
width: 100vw;
height: 100vh;
position: relative;
}
#info {
position: absolute;
top: 20px;
left: 20px;
color: white;
z-index: 100;
background: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 8px;
font-size: 14px;
line-height: 1.5;
}
#controls {
position: absolute;
bottom: 20px;
left: 20px;
color: white;
z-index: 100;
background: rgba(0,0,0,0.7);
padding: 15px;
border-radius: 8px;
}
button {
background: #4CAF50;
color: white;
border: none;
padding: 8px 16px;
margin: 5px;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
}
button:hover {
background: #45a049;
}
#loading {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: white;
font-size: 18px;
z-index: 200;
}
</style>
</head>
<body>
<div id="container">
<div id="loading">加载中...</div>
<div id="info">
<h3>🌍 交互式3D地球</h3>
<p>• 鼠标左键拖拽:旋转地球</p>
<p>• 鼠标滚轮:缩放</p>
<p>• 鼠标右键拖拽:平移视角</p>
</div>
<div id="controls">
<button onclick="resetView()">重置视角</button>
<button onclick="toggleRotation()">自动旋转</button>
<button onclick="toggleWireframe()">线框模式</button>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<script>
let scene, camera, renderer, earth, controls;
let autoRotate = true;
let wireframeMode = false;
// 初始化场景
function init() {
// 创建场景
scene = new THREE.Scene();
// 创建相机
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 3;
// 创建渲染器
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000011);
document.getElementById('container').appendChild(renderer.domElement);
// 创建轨道控制器
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.minDistance = 1.5;
controls.maxDistance = 10;
// 创建地球
createEarth();
// 创建星空背景
createStarField();
// 添加光源
addLights();
// 隐藏加载提示
document.getElementById('loading').style.display = 'none';
// 开始渲染循环
animate();
}
// 创建地球
function createEarth() {
const geometry = new THREE.SphereGeometry(1, 64, 64);
// 创建地球材质
const textureLoader = new THREE.TextureLoader();
// 使用程序生成的地球纹理
const canvas = document.createElement('canvas');
canvas.width = 1024;
canvas.height = 512;
const ctx = canvas.getContext('2d');
// 绘制简单的地球纹理
const gradient = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
gradient.addColorStop(0, '#4a90e2');
gradient.addColorStop(0.3, '#2e7d32');
gradient.addColorStop(0.6, '#8bc34a');
gradient.addColorStop(1, '#4a90e2');
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, canvas.width, canvas.height);
// 添加一些随机的陆地形状
ctx.fillStyle = '#2e7d32';
for (let i = 0; i < 50; i++) {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const radius = Math.random() * 50 + 20;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fill();
}
const texture = new THREE.CanvasTexture(canvas);
const material = new THREE.MeshPhongMaterial({
map: texture,
shininess: 100,
transparent: true,
opacity: 0.9
});
earth = new THREE.Mesh(geometry, material);
scene.add(earth);
// 添加大气层效果
const atmosphereGeometry = new THREE.SphereGeometry(1.02, 64, 64);
const atmosphereMaterial = new THREE.MeshBasicMaterial({
color: 0x4a90e2,
transparent: true,
opacity: 0.1,
side: THREE.BackSide
});
const atmosphere = new THREE.Mesh(atmosphereGeometry, atmosphereMaterial);
scene.add(atmosphere);
}
// 创建星空背景
function createStarField() {
const starsGeometry = new THREE.BufferGeometry();
const starsMaterial = new THREE.PointsMaterial({
color: 0xffffff,
size: 2,
sizeAttenuation: false
});
const starsVertices = [];
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;
starsVertices.push(x, y, z);
}
starsGeometry.setAttribute('position', new THREE.Float32BufferAttribute(starsVertices, 3));
const stars = new THREE.Points(starsGeometry, starsMaterial);
scene.add(stars);
}
// 添加光源
function addLights() {
// 环境光
const ambientLight = new THREE.AmbientLight(0x404040, 0.4);
scene.add(ambientLight);
// 方向光(模拟太阳光)
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 3, 5);
scene.add(directionalLight);
// 点光源
const pointLight = new THREE.PointLight(0xffffff, 0.5, 100);
pointLight.position.set(-10, -10, -10);
scene.add(pointLight);
}
// 动画循环
function animate() {
requestAnimationFrame(animate);
// 自动旋转
if (autoRotate) {
earth.rotation.y += 0.005;
}
// 更新控制器
controls.update();
// 渲染场景
renderer.render(scene, camera);
}
// 重置视角
function resetView() {
camera.position.set(0, 0, 3);
controls.reset();
}
// 切换自动旋转
function toggleRotation() {
autoRotate = !autoRotate;
const button = event.target;
button.textContent = autoRotate ? '停止旋转' : '自动旋转';
}
// 切换线框模式
function toggleWireframe() {
wireframeMode = !wireframeMode;
earth.material.wireframe = wireframeMode;
const button = event.target;
button.textContent = wireframeMode ? '实体模式' : '线框模式';
}
// 窗口大小调整
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
// 事件监听
window.addEventListener('resize', onWindowResize, false);
// 初始化
init();
</script>
</body>
</html>
这个3D地球页面包含以下功能:
主要特性:
交互功能:
视觉效果:
你可以直接保存为HTML文件并在浏览器中打开。页面会自动加载必要的Three.js库,无需额外安装。地球会自动旋转,你可以用鼠标进行各种交互操作。