-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathegg2.html
More file actions
436 lines (368 loc) · 14.7 KB
/
egg2.html
File metadata and controls
436 lines (368 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Procedural Terrain Generator</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- OrbitControls is needed for camera movement -->
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #050505;
color: #eee;
}
#ui-panel {
position: absolute;
top: 20px;
left: 20px;
width: 300px;
background: rgba(20, 20, 25, 0.85);
backdrop-filter: blur(10px);
padding: 20px;
border-radius: 12px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
border: 1px solid rgba(255, 255, 255, 0.1);
user-select: none;
z-index: 100;
}
h1 {
font-size: 1.2rem;
margin: 0 0 15px 0;
color: #4facfe;
text-transform: uppercase;
letter-spacing: 1px;
}
.control-group {
margin-bottom: 15px;
}
label {
display: block;
font-size: 0.8rem;
margin-bottom: 5px;
color: #bbb;
}
input[type="range"] {
width: 100%;
cursor: pointer;
accent-color: #4facfe;
}
.button-group {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 20px;
}
button {
background: #4facfe;
border: none;
color: white;
padding: 10px;
border-radius: 6px;
cursor: pointer;
font-weight: bold;
transition: all 0.2s;
}
button:hover {
background: #00f2fe;
transform: translateY(-1px);
}
button.secondary {
background: #444;
}
button.secondary:hover {
background: #555;
}
.stats {
position: absolute;
bottom: 20px;
right: 20px;
font-size: 0.8rem;
color: rgba(255,255,255,0.5);
pointer-events: none;
}
canvas {
display: block;
}
.loading-overlay {
position: fixed;
top: 0; left: 0; width: 100%; height: 100%;
background: #050505;
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
transition: opacity 0.5s;
}
.loading-overlay.hidden {
opacity: 0;
pointer-events: none;
}
</style>
</head>
<body>
<div id="loading" class="loading-overlay">
<div>Generating World...</div>
</div>
<div id="ui-panel">
<h1>Terrain Forge</h1>
<div class="control-group">
<label>Noise Scale: <span id="val-scale">40</span></label>
<input type="range" id="scale" min="10" max="100" value="45">
</div>
<div class="control-group">
<label>Elevation: <span id="val-height">20</span></label>
<input type="range" id="height" min="1" max="50" value="25">
</div>
<div class="control-group">
<label>Detail (Octaves): <span id="val-octaves">5</span></label>
<input type="range" id="octaves" min="1" max="8" value="5">
</div>
<div class="control-group">
<label>Roughness: <span id="val-pers">0.5</span></label>
<input type="range" id="persistence" min="0.1" max="0.9" step="0.05" value="0.5">
</div>
<div class="control-group">
<label>Sea Level: <span id="val-sea">0.2</span></label>
<input type="range" id="sealevel" min="0" max="0.8" step="0.05" value="0.2">
</div>
<div class="button-group">
<button id="regen">New Seed</button>
<button id="download" class="secondary">Export OBJ</button>
</div>
</div>
<div class="stats">WASD/Mouse to Orbit • Space to Toggle Auto-Rotate</div>
<script>
/** * SIMPLEX NOISE IMPLEMENTATION
* Compact version for procedural generation
*/
const SimplexNoise = function(seed = Math.random()) {
const p = new Uint8Array(256);
for (let i = 0; i < 256; i++) p[i] = i;
let m = 256, n, k;
while (m) {
n = Math.floor(seed * m--);
k = p[m]; p[m] = p[n]; p[n] = k;
seed = (seed * 1103515245 + 12345) & 0x7fffffff;
seed /= 0x7fffffff;
}
const perm = new Uint8Array(512);
const gradP = new Float32Array(512 * 3);
const grad3 = [[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0],[1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1],[0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]];
for (let i = 0; i < 512; i++) {
perm[i] = p[i & 255];
let g = grad3[perm[i] % 12];
gradP[i*3] = g[0]; gradP[i*3+1] = g[1]; gradP[i*3+2] = g[2];
}
const F2 = 0.5 * (Math.sqrt(3) - 1), G2 = (3 - Math.sqrt(3)) / 6;
this.noise2D = function(xin, yin) {
let s = (xin + yin) * F2, i = Math.floor(xin + s), j = Math.floor(yin + s);
let t = (i + j) * G2, x0 = xin - (i - t), y0 = yin - (j - t);
let i1, j1; if (x0 > y0) { i1 = 1; j1 = 0; } else { i1 = 0; j1 = 1; }
let x1 = x0 - i1 + G2, y1 = y0 - j1 + G2, x2 = x0 - 1 + 2 * G2, y2 = y0 - 1 + 2 * G2;
i &= 255; j &= 255;
let n0, n1, n2;
let t0 = 0.5 - x0 * x0 - y0 * y0;
if (t0 < 0) n0 = 0; else { t0 *= t0; n0 = t0 * t0 * (gradP[(i + perm[j]) * 3] * x0 + gradP[(i + perm[j]) * 3 + 1] * y0); }
let t1 = 0.5 - x1 * x1 - y1 * y1;
if (t1 < 0) n1 = 0; else { t1 *= t1; n1 = t1 * t1 * (gradP[(i + i1 + perm[j + j1]) * 3] * x1 + gradP[(i + i1 + perm[j + j1]) * 3 + 1] * y1); }
let t2 = 0.5 - x2 * x2 - y2 * y2;
if (t2 < 0) n2 = 0; else { t2 *= t2; n2 = t2 * t2 * (gradP[(i + 1 + perm[j + 1]) * 3] * x2 + gradP[(i + 1 + perm[j + 1]) * 3 + 1] * y2); }
return 70 * (n0 + n1 + n2);
};
};
// --- APP LOGIC ---
let scene, camera, renderer, terrain, water, controls;
let simplex = new SimplexNoise();
const GRID_SIZE = 128;
const WORLD_WIDTH = 100;
let params = {
scale: 45,
height: 25,
octaves: 5,
persistence: 0.5,
seaLevel: 0.2,
autoRotate: true
};
function init() {
scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a0a12);
scene.fog = new THREE.FogExp2(0x0a0a12, 0.005);
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.set(80, 60, 80);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
document.body.appendChild(renderer.domElement);
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
controls.autoRotate = params.autoRotate;
// Lights
const ambient = new THREE.AmbientLight(0xffffff, 0.4);
scene.add(ambient);
const sun = new THREE.DirectionalLight(0xffffff, 0.8);
sun.position.set(100, 100, 50);
sun.castShadow = true;
scene.add(sun);
createTerrain();
createWater();
window.addEventListener('resize', onWindowResize);
window.addEventListener('keydown', (e) => {
if(e.code === 'Space') {
params.autoRotate = !params.autoRotate;
controls.autoRotate = params.autoRotate;
}
});
// UI Events
setupUI();
animate();
document.getElementById('loading').classList.add('hidden');
}
function createTerrain() {
if (terrain) scene.remove(terrain);
const geometry = new THREE.PlaneBufferGeometry(WORLD_WIDTH, WORLD_WIDTH, GRID_SIZE, GRID_SIZE);
geometry.rotateX(-Math.PI / 2);
const vertices = geometry.attributes.position.array;
const colors = new Float32Array(vertices.length);
const color = new THREE.Color();
for (let i = 0; i < vertices.length; i += 3) {
const x = vertices[i];
const z = vertices[i + 2];
// Octave Noise
let h = 0;
let amp = 1;
let freq = 1 / params.scale;
let maxH = 0;
for (let o = 0; o < params.octaves; o++) {
h += simplex.noise2D(x * freq, z * freq) * amp;
maxH += amp;
amp *= params.persistence;
freq *= 2;
}
// Normalize and apply exponent for realistic valleys
h /= maxH;
h = Math.pow((h + 1) / 2, 1.5);
const finalY = h * params.height;
vertices[i + 1] = finalY;
// Coloring based on height
if (h < params.seaLevel + 0.02) {
color.setHex(0xd2b48c); // Sand
} else if (h < 0.5) {
color.setHex(0x3a5f0b); // Grass
} else if (h < 0.75) {
color.setHex(0x555555); // Rock
} else {
color.setHex(0xffffff); // Snow
}
colors[i] = color.r;
colors[i + 1] = color.g;
colors[i + 2] = color.b;
}
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.8,
metalness: 0.1,
flatShading: false
});
terrain = new THREE.Mesh(geometry, material);
terrain.receiveShadow = true;
terrain.castShadow = true;
scene.add(terrain);
}
function createWater() {
if (water) scene.remove(water);
const geometry = new THREE.PlaneBufferGeometry(WORLD_WIDTH, WORLD_WIDTH);
geometry.rotateX(-Math.PI / 2);
const material = new THREE.MeshStandardMaterial({
color: 0x006994,
transparent: true,
opacity: 0.6,
roughness: 0.1,
metalness: 0.5
});
water = new THREE.Mesh(geometry, material);
water.position.y = params.seaLevel * params.height;
scene.add(water);
}
function setupUI() {
const inputs = ['scale', 'height', 'octaves', 'persistence', 'sealevel'];
inputs.forEach(id => {
const el = document.getElementById(id);
el.addEventListener('input', (e) => {
const val = parseFloat(e.target.value);
if(id === 'sealevel') params.seaLevel = val;
else params[id] = val;
document.getElementById('val-' + id).innerText = val;
updateScene();
});
});
document.getElementById('regen').addEventListener('click', () => {
simplex = new SimplexNoise();
updateScene();
});
document.getElementById('download').addEventListener('click', exportToOBJ);
}
function updateScene() {
createTerrain();
water.position.y = params.seaLevel * params.height;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
function animate() {
requestAnimationFrame(animate);
controls.update();
// Subtle water movement
if(water) {
water.position.y = (params.seaLevel * params.height) + Math.sin(Date.now() * 0.001) * 0.1;
}
renderer.render(scene, camera);
}
/**
* Simple OBJ Exporter
*/
function exportToOBJ() {
const geo = terrain.geometry;
const vertices = geo.attributes.position.array;
const normals = geo.attributes.normal.array;
const indices = geo.index ? geo.index.array : null;
let objData = "# Terrain Forge Export\no Terrain\n";
// Vertices
for (let i = 0; i < vertices.length; i += 3) {
objData += `v ${vertices[i].toFixed(4)} ${vertices[i+1].toFixed(4)} ${vertices[i+2].toFixed(4)}\n`;
}
// Faces (based on PlaneBufferGeometry order)
// For a regular grid PlaneBufferGeometry
const segments = GRID_SIZE;
for (let j = 0; j < segments; j++) {
for (let i = 0; i < segments; i++) {
const a = i + j * (segments + 1) + 1;
const b = i + (j + 1) * (segments + 1) + 1;
const c = (i + 1) + (j + 1) * (segments + 1) + 1;
const d = (i + 1) + j * (segments + 1) + 1;
objData += `f ${a} ${b} ${c}\n`;
objData += `f ${a} ${c} ${d}\n`;
}
}
const blob = new Blob([objData], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'terrain.obj';
a.click();
URL.revokeObjectURL(url);
}
window.onload = init;
</script>
</body>
</html>