three实现三维星球步行无限漫游体验代码
代码语言:html
所属分类:其他
代码由gemini-4-hydrogen ai生成,可能有错误,仅供参考:点击查看提示词
代码描述:三维星球步行无限漫游体验
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>无界球星漫游 · Procedural Spherical Planet Explorer</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
user-select: none;
-webkit-user-select: none;
}
html, body {
width: 100%;
height: 100%;
overflow: hidden;
background: #0d1b2a;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
color: #f0f6fc;
}
#canvas-container {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
cursor: grab;
}
#canvas-container.dragging {
cursor: grabbing;
}
/* 极简玻璃拟态 HUD */
.hud {
position: fixed;
inset: 0;
pointer-events: none;
padding: 16px;
display: flex;
flex-direction: column;
justify-content: space-between;
transition: opacity 0.3s ease;
z-index: 10;
}
.hud.hidden {
opacity: 0;
}
.hud-top {
display: flex;
justify-content: space-between;
align-items: flex-start;
gap: 12px;
flex-wrap: wrap;
}
.panel {
background: rgba(11, 22, 36, 0.58);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 14px;
padding: 12px 16px;
box-shadow: 0 8px 28px rgba(0, 0, 0, 0.28);
pointer-events: auto;
}
.biome-header {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 8px;
}
.biome-dot {
width: 12px;
height: 12px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
flex-shrink: 0;
}
.biome-title {
font-size: 16px;
font-weight: 700;
letter-spacing: 0.3px;
}
.biome-sub {
font-size: 12px;
color: rgba(230, 240, 250, 0.72);
margin-top: 2px;
}
.biome-pills {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-top: 8px;
max-width: 340px;
}
.pill {
display: inline-flex;
align-items: center;
gap: 5px;
padding: 4px 9px;
border-radius: 999px;
font-size: 11px;
font-weight: 500;
background: rgba(255, 255, 255, 0.08);
border: 1px solid rgba(255, 255, 255, 0.12);
color: rgba(240, 246, 252, 0.62);
cursor: pointer;
transition: all 0.2s ease;
}
.pill:hover {
background: rgba(255, 255, 255, 0.16);
color: #fff;
}
.pill.visited {
background: rgba(72, 199, 142, 0.2);
border-color: rgba(72, 199, 142, 0.55);
color: #c8fce3;
}
.pill.tracking {
border-color: #ffd166;
box-shadow: 0 0 0 1px rgba(255, 209, 102, 0.45);
color: #ffe8a8;
}
.pill-dot {
width: 7px;
height: 7px;
border-radius: 50%;
}
.guide-line {
margin-top: 8px;
font-size: 11.5px;
color: #ffe29a;
min-height: 16px;
}
.stats-panel {
font-variant-numeric: tabular-nums;
font-size: 12px;
line-height: 1.55;
min-width: 205px;
}
.stats-title {
font-size: 11px;
text-transform: uppercase;
letter-spacing: 1px;
color: rgba(230, 240, 250, 0.58);
margin-bottom: 4px;
display: flex;
justify-content: space-between;
align-items: center;
}
.stats-row {
display: flex;
justify-content: space-between;
gap: 14px;
}
.stats-val {
font-weight: 600;
color: #9be7ff;
}
.hud-bottom {
display: flex;
justify-content: center;
pointer-events: none;
}
.controls-bar {
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 12px;
font-size: 12px;
padding: 8px 16px;
color: rgba(240, 246, 252, 0.85);
}
.key {
display: inline-block;
padding: 1px 6px;
margin-right: 4px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.14);
border: 1px solid rgba(255, 255, 255, 0.22);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 11px;
color: #fff;
}
.badge-btn {
font-size: 11px;
padding: 2px 7px;
border-radius: 6px;
border: 1px solid rgba(255, 255, 255, 0.2);
background: rgba(255, 255, 255, 0.08);
color: #e6f0fa;
cursor: pointer;
}
.badge-btn:hover {
background: rgba(255, 255, 255, 0.2);
}
</style>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js"
}
}
</script>
</head>
<body>
<div id="canvas-container"></div>
<div class="hud" id="hud">
<div class="hud-top">
<div class="panel">
<div class="biome-header">
<div class="biome-dot" id="biome-dot" style="background:#57cc99;color:#57cc99"></div>
<div>
<div class="biome-title" id="biome-title">正在初始化星球...</div>
<div class="biome-sub" id="biome-sub">海拔 -- m · 纬度 --°</div>
</div>
</div>
<div class="biome-pills" id="biome-pills"></div>
<div class="guide-line" id="guide-line">点击群系标签可指引最近方位</div>
</div>
<div class="panel stats-panel">
<div class="stats-title">
<span>Streaming & GPU Memory</span>
<button class="badge-btn" id="toggle-hud-btn" title="按 H 键隐藏/显示">H 隐藏</button>
</div>
<div class="stats-row"><span>FPS</span><span class="stats-val" id="stat-fps">60</span></div>
<div class="stats-row"><span>活跃区块 (Chunks)</span><span class="stats-val" id="stat-chunks">0</span></div>
<div class="stats-row"><span>绘制调用 (Draw Calls)</span><span class="stats-val" id="stat-calls">0</span></div>
<div class="stats-row"><span>GPU 几何体 (Geometries)</span><span class="stats-val" id="stat-geos">0</span></div>
<div class="stats-row"><span>GPU 贴图 (Textures)</span><span class="stats-val" id="stat-texs">0</span></div>
</div>
</div>
<div class="hud-bottom">
<div class="panel controls-bar">
<span><span class="key">WASD / 方向键</span>移动</span>
<span><span class="key">Shift</span>疾跑</span>
<span><span class="key">Space</span>跳跃</span>
<span><span class="key">鼠标拖拽</span>旋转视角</span>
<span><span class="key">滚轮</span>缩放</span>
<span><span class="key">H</span>切换面板</span>
</div>
</div>
</div>
<script type="module">
import * as THREE from 'three';
// =========================================================================
// 1. 星球与流式加载全局参数
// =========================================================================
const PLANET_RADIUS = 180; // 星球基准半径
const SEA_LEVEL = 0.0; // 海平面偏移
const CHUNKS_PER_EDGE = 20; // 立方体单面每边划分区块数 (6 * 20 * 20 = 2400 块)
const TOTAL_CHUNKS = 6 * CHUNKS_PER_EDGE * CHUNKS_PER_EDGE;
const LOAD_RADIUS = 72; // 区块加载球面弧距半径
const UNLOAD_RADIUS = 84; // 区块卸载半径(滞后带防抖)
const LOD0_DIST = 32; // 近景高精度半径 (20x20)
const LOD1_DIST = 54; // 中景中等精度半径 (10x10)
const MAX_CHUNKS_PER_FRAME = 3; // 每帧最大异步分批生成区块数,保障 60fps
const BIOME_META = {
ocean: { name: '海洋 Ocean', color: '#219ebc' },
forest: { name: '森林 Forest', color: '#2d936c' },
snow: { name: '雪山 Snow Peak', color: '#e0fbfc' },
waterfall: { name: '瀑布 Waterfall', color: '#48cae4' },
farmland: { name: '农田 Farmland', color: '#e9c46a' },
flower: { name: '花海 Meadow', color: '#ff6b9d' }
};
const BIOME_KEYS = ['ocean', 'forest', 'snow', 'waterfall', 'farmland', 'flower'];
// =========================================================================
// 2. 数学工具与确定性 3D Simplex 噪声 (无缝球面采样核心)
// =========================================================================
const clamp = (v, min, max) => (v < min ? min : v > max ? max : v);
const smoothstep = (e0, e1, x) => {
const t = clamp((x - e0) / (e1 - e0), 0, 1);
return t * t * (3 - 2 * t);
};
const lerp = (a, b, t) => a + (b - a) * t;
function mulberry32(seed) {
let a = seed >>> 0;
return function () {
a |= 0;
a = (a + 0x6d2b79f5) | 0;
let t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function hashInt(a, b, c) {
let h = (a * 374761393 + b * 668265263 + c * 2147483647) | 0;
h = Math.imul(h ^ (h >>> 13), 1274126177);
return (h ^ (h >>> 16)) >>> 0;
}
// 3D Simplex Noise(基于 Gustavson 算法,固定种子置换表)
const PERM = new Uint8Array(512);
const PERM_MOD12 = new Uint8Array(512);
(function initNoisePerm() {
const p = new Uint8Array(256);
for (let i = 0; i < 256; i++) p[i] = i;
const rand = mulberry32(20250517);
for (let i = 255; i > 0; i--) {
const j = Math.floor(rand() * (i + 1));
const tmp = p[i]; p[i] = p[j]; p[j] = tmp;
}
for (let i = 0; i < 512; i++) {
PERM[i] = p[i & 255];
PERM_MOD12[i] = PER.........完整代码请登录后点击上方下载按钮下载查看















网友评论0