latterworks commited on
Commit
eb8c123
·
verified ·
1 Parent(s): e5bdf72

* A utility for generating heightmap functions by additive composition.
*
*

@param
{Float32Array} g
* The geometry's z-positions to modify with heightmap data.
*

@param
{Object} [options]
* A map of settings that control how the terrain is constructed and
* displayed. Valid values are the same as those for the `options` parameter
* of {@link THREE.Terrain}().
*

@param
{Object[]} passes
* Determines which heightmap functions to compose to create a new one.
* Consists of an array of objects with the following properties:
* - `method`: Contains something that will be passed around as an
* `options.heightmap` (a heightmap-generating function or a heightmap image)
* - `amplitude`: A multiplier for the heightmap of the pass. Applied before
* the result of the pass is added to the result of previous passes.
* - `frequency`: For terrain generation methods that support it (Perlin,
* Simplex, and Worley) the octave of randomness. This basically controls
* how big features of the terrain will be (higher frequencies result in
* smaller features). Often running multiple generation functions with
* different frequencies and amplitudes results in nice detail.
*/
THREE.Terrain.MultiPass = function(g, options, passes) {
var clonedOptions = {};
for (var opt in options) {
if (options.hasOwnProperty(opt)) {
clonedOptions[opt] = options[opt];
}
}
var range = options.maxHeight - options.minHeight;
for (var i = 0, l = passes.length; i < l; i++) {
var amp = typeof passes[i].amplitude === 'undefined' ? 1 : passes[i].amplitude,
move = 0.5 * (range - range * amp);
clonedOptions.maxHeight = options.maxHeight - move;
clonedOptions.minHeight = options.minHeight + move;
clonedOptions.frequency = typeof passes[i].frequency === 'undefined' ? options.frequency : passes[i].frequency;
passes[i].method(g, clonedOptions);
}
};

/**
* Generate random terrain using a curve.
*
*

@param
{Float32Array} g
* The geometry's z-positions to modify with heightmap data.
*

@param
{Object} options
* A map of settings that control how the terrain is constructed and
* displayed. Valid values are the same as those for the `options` parameter
* of {@link THREE.Terrain}().
*

@param
{Function} curve
* A function that takes an x- and y-coordinate and returns a z-coordinate.
* For example, `function(x, y) { return Math.sin(x*y*Math.PI*100); }`
* generates sine noise, and `function() { return Math.random(); }` sets the
* vertex elevations entirely randomly. The function's parameters (the x- and
* y-coordinates) are given as percentages of a phase (i.e. how far across
* the terrain in the relevant direction they are).
*/
THREE.Terrain.Curve = function(g, options, curve) {
var range = (options.maxHeight - options.minHeight) * 0.5,
scalar = options.frequency / (Math.min(options.xSegments, options.ySegments) + 1);
for (var i = 0, xl = options.xSegments + 1, yl = options.ySegments + 1; i < xl; i++) {
for (var j = 0; j < yl; j++) {
g[j * xl + i] += curve(i * scalar, j * scalar) * range;
}
}
};

/**
* Generate random terrain using the Cosine waves.
*
* Parameters are the same as those for {@link THREE.Terrain.DiamondSquare}.
*/
THREE.Terrain.Cosine = function(g, options) {
var amplitude = (options.maxHeight - options.minHeight) * 0.5,
frequencyScalar = options.frequency * Math.PI / (Math.min(options.xSegments, options.ySegments) + 1),
phase = Math.random() * Math.PI * 2;
for (var i = 0, xl = options.xSegments + 1; i < xl; i++) {
for (var j = 0, yl = options.ySegments + 1; j < yl; j++) {
g[j * xl + i] += amplitude * (Math.cos(i * frequencyScalar + phase) + Math.cos(j * frequencyScalar + phase));
}
}
};

/**
* Generate random terrain using layers of Cosine waves.
*
* Parameters are the same as those for {@link THREE.Terrain.DiamondSquare}.
*/
THREE.Terrain.CosineLayers = function(g, options) {
THREE.Terrain.MultiPass(g, options, [
{ method: THREE.Terrain.Cosine, frequency: 2.5 },
{ method: THREE.Terrain.Cosine, amplitude: 0.1, frequency: 12 },
{ method: THREE.Terrain.Cosine, amplitude: 0.05, frequency: 15 },
{ method: THREE.Terrain.Cosine, amplitude: 0.025, frequency: 20 },
]);
};

/**
* Generate random terrain using the Diamond-Square method.
*
* Based on https://github.com/srchea/Terrain-Generation/blob/master/js/classes/TerrainGeneration.js
*
*

@param
{Float32Array} g
* The geometry's z-positions to modify with heightmap data.
*

@param
{Object} options
* A map of settings that control how the terrain is constructed and
* displayed. Valid values are the same as those for the `options` parameter
* of {@link THREE.Terrain}().
*/
THREE.Terrain.DiamondSquare = function(g, options) {
// Set the segment length to the smallest power of 2 that is greater than
// the number of vertices in either dimension of the plane
var segments = THREE.Math.ceilPowerOfTwo(Math.max(options.xSegments, options.ySegments) + 1);

// Initialize heightmap
var size = segments + 1,
heightmap = [],
smoothing = (options.maxHeight - options.minHeight),
i,
j,
xl = options.xSegments + 1,
yl = options.ySegments + 1;
for (i = 0; i <= segments; i++) {
heightmap[i] = new Float64Array(segments+1);
}

// Generate heightmap
for (var l = segments; l >= 2; l /= 2) {
var half = Math.round(l*0.5),
whole = Math.round(l),
x,
y,
avg,
d,
e;
smoothing /= 2;
// square
for (x = 0; x < segments; x += whole) {
for (y = 0; y < segments; y += whole) {
d = Math.random() * smoothing * 2 - smoothing;
avg = heightmap[x][y] + // top left
heightmap[x+whole][y] + // top right
heightmap[x][y+whole] + // bottom left
heightmap[x+whole][y+whole]; // bottom right
avg *= 0.25;
heightmap[x+half][y+half] = avg + d;
}
}
// diamond
for (x = 0; x < segments; x += half) {
for (y = (x+half) % l; y < segments; y += l) {
d = Math.random() * smoothing * 2 - smoothing;
avg = heightmap[(x-half+size)%size][y] + // middle left
heightmap[(x+half)%size][y] + // middle right
heightmap[x][(y+half)%size] + // middle top
heightmap[x][(y-half+size)%size]; // middle bottom
avg *= 0.25;
avg += d;
heightmap[x][y] = avg;
// top and right edges
if (x === 0) heightmap[segments][y] = avg;
if (y === 0) heightmap[x][segments] = avg;
}
}
}

// Apply heightmap
for (i = 0; i < xl; i++) {
for (j = 0; j < yl; j++) {
g[j * xl + i] += heightmap[i][j];
}
}

// THREE.Terrain.SmoothConservative(g, options);
};

/**
* Generate random terrain using the Fault method.
*
* Based on http://www.lighthouse3d.com/opengl/terrain/index.php3?fault
* Repeatedly draw random lines that cross the terrain. Raise the terrain on
* one side of the line and lower it on the other.
*
* Parameters are the same as those for {@link THREE.Terrain.DiamondSquare}.
*/
THREE.Terrain.Fault = function(g, options) {
var d = Math.sqrt(options.xSegments*options.xSegments + options.ySegments*options.ySegments),
iterations = d * options.frequency,
range = (options.maxHeight - options.minHeight) * 0.5,
displacement = range / iterations,
smoothDistance = Math.min(options.xSize / options.xSegments, options.ySize / options.ySegments) * options.frequency;
for (var k = 0; k < iterations; k++) {
var v = Math.random(),
a = Math.sin(v * Math.PI * 2),
b = Math.cos(v * Math.PI * 2),
c = Math.random() * d - d*0.5;
for (var i = 0, xl = options.xSegments + 1; i < xl; i++) {
for (var j = 0, yl = options.ySegments + 1; j < yl; j++) {
var distance = a*i + b*j - c;
if (distance > smoothDistance) {
g[j * xl + i] += displacement;
}
else if (distance < -smoothDistance) {
g[j * xl + i] -= displacement;
}
else {
g[j * xl + i] += Math.cos(distance / smoothDistance * Math.PI * 2) * displacement;
}
}
}
}
// THREE.Terrain.Smooth(g, options);
};

/**
* Generate random terrain using the Hill method.
*
* The basic approach is to repeatedly pick random points on or near the
* terrain and raise a small hill around those points. Those small hills
* eventually accumulate into large hills with distinct features.
*
*

@param
{Float32Array} g
* The geometry's z-positions to modify with heightmap data.
*

@param
{Object} options
* A map of settings that control how the terrain is constructed and
* displayed. Valid values are the same as those for the `options` parameter
* of {@link THREE.Terrain}().
*

@param
{Function} [feature=THREE.Terrain.Influences.Hill]
* A function describing the feature to raise at the randomly chosen points.
* Typically this is a hill shape so that the accumulated features result in
* something resembling mountains, but it could be any function that accepts
* one parameter representing the distance from the feature's origin
* expressed as a number between -1 and 1 inclusive. Optionally it can accept
* a second and third parameter, which are the x- and y- distances from the
* feature's origin, respectively. It should return a number between -1 and 1
* representing the height of the feature at the given coordinate.
* `THREE.Terrain.Influences` contains some

Files changed (6) hide show
  1. README.md +8 -5
  2. components/footer.js +66 -0
  3. components/navbar.js +103 -0
  4. index.html +143 -19
  5. script.js +349 -0
  6. style.css +35 -18
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Terraingen Explorer
3
- emoji: 🐠
4
- colorFrom: green
5
- colorTo: red
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: TerrainGen Explorer 🏔️
3
+ colorFrom: red
4
+ colorTo: pink
5
+ emoji: 🐳
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite-v3
10
  ---
11
 
12
+ # Welcome to your new DeepSite project!
13
+ This project was created with [DeepSite](https://huggingface.co/deepsite).
components/footer.js ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class CustomFooter extends HTMLElement {
2
+ connectedCallback() {
3
+ this.attachShadow({ mode: 'open' });
4
+ this.shadowRoot.innerHTML = `
5
+ <style>
6
+ footer {
7
+ background-color: rgba(17, 24, 39, 0.9);
8
+ backdrop-filter: blur(8px);
9
+ border-top: 1px solid rgba(255, 255, 255, 0.1);
10
+ }
11
+
12
+ .footer-container {
13
+ max-width: 1280px;
14
+ margin: 0 auto;
15
+ padding: 2rem 1rem;
16
+ }
17
+
18
+ .social-link {
19
+ color: rgba(156, 163, 175, 0.9);
20
+ transition: color 0.2s ease, transform 0.2s ease;
21
+ }
22
+
23
+ .social-link:hover {
24
+ color: white;
25
+ transform: translateY(-2px);
26
+ }
27
+ </style>
28
+ <footer class="text-gray-400 py-8">
29
+ <div class="footer-container">
30
+ <div class="flex flex-col md:flex-row justify-between items-center">
31
+ <div class="mb-6 md:mb-0">
32
+ <div class="flex items-center space-x-2 mb-4">
33
+ <i data-feather="mountain" class="text-blue-400"></i>
34
+ <span class="text-xl font-bold text-white">TerrainGen Explorer</span>
35
+ </div>
36
+ <p class="text-sm max-w-md">
37
+ A visual playground for exploring procedural terrain generation algorithms.
38
+ </p>
39
+ </div>
40
+
41
+ <div class="flex space-x-6">
42
+ <a href="#" class="social-link">
43
+ <i data-feather="github" class="w-5 h-5"></i>
44
+ </a>
45
+ <a href="#" class="social-link">
46
+ <i data-feather="twitter" class="w-5 h-5"></i>
47
+ </a>
48
+ <a href="#" class="social-link">
49
+ <i data-feather="linkedin" class="w-5 h-5"></i>
50
+ </a>
51
+ <a href="#" class="social-link">
52
+ <i data-feather="mail" class="w-5 h-5"></i>
53
+ </a>
54
+ </div>
55
+ </div>
56
+
57
+ <div class="mt-8 pt-8 border-t border-gray-800 text-sm text-center">
58
+ <p>© ${new Date().getFullYear()} TerrainGen Explorer. All rights reserved.</p>
59
+ </div>
60
+ </div>
61
+ </footer>
62
+ `;
63
+ }
64
+ }
65
+
66
+ customElements.define('custom-footer', CustomFooter);
components/navbar.js ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class CustomNavbar extends HTMLElement {
2
+ connectedCallback() {
3
+ this.attachShadow({ mode: 'open' });
4
+ this.shadowRoot.innerHTML = `
5
+ <style>
6
+ nav {
7
+ background-color: rgba(17, 24, 39, 0.9);
8
+ backdrop-filter: blur(8px);
9
+ position: sticky;
10
+ top: 0;
11
+ z-index: 50;
12
+ border-bottom: 1px solid rgba(255, 255, 255, 0.1);
13
+ }
14
+
15
+ .nav-container {
16
+ max-width: 1280px;
17
+ margin: 0 auto;
18
+ padding: 0 1rem;
19
+ }
20
+
21
+ .nav-link {
22
+ position: relative;
23
+ color: rgba(209, 213, 219, 0.9);
24
+ transition: color 0.2s ease;
25
+ }
26
+
27
+ .nav-link:hover {
28
+ color: white;
29
+ }
30
+
31
+ .nav-link::after {
32
+ content: '';
33
+ position: absolute;
34
+ bottom: -2px;
35
+ left: 0;
36
+ width: 0;
37
+ height: 2px;
38
+ background-color: #3b82f6;
39
+ transition: width 0.3s ease;
40
+ }
41
+
42
+ .nav-link:hover::after {
43
+ width: 100%;
44
+ }
45
+
46
+ .mobile-menu {
47
+ max-height: 0;
48
+ overflow: hidden;
49
+ transition: max-height 0.3s ease-out;
50
+ }
51
+
52
+ .mobile-menu.open {
53
+ max-height: 500px;
54
+ }
55
+ </style>
56
+ <nav class="text-white py-4">
57
+ <div class="nav-container flex items-center justify-between">
58
+ <div class="flex items-center space-x-2">
59
+ <i data-feather="mountain" class="text-blue-400"></i>
60
+ <span class="text-xl font-bold">TerrainGen Explorer</span>
61
+ </div>
62
+
63
+ <div class="hidden md:flex items-center space-x-6">
64
+ <a href="#" class="nav-link font-medium">Home</a>
65
+ <a href="#" class="nav-link font-medium">Algorithms</a>
66
+ <a href="#" class="nav-link font-medium">Gallery</a>
67
+ <a href="#" class="nav-link font-medium">About</a>
68
+ </div>
69
+
70
+ <button id="mobile-menu-button" class="md:hidden focus:outline-none">
71
+ <i data-feather="menu" class="w-6 h-6"></i>
72
+ </button>
73
+ </div>
74
+
75
+ <div id="mobile-menu" class="mobile-menu md:hidden bg-gray-800">
76
+ <div class="px-4 py-2 space-y-3">
77
+ <a href="#" class="block nav-link py-2">Home</a>
78
+ <a href="#" class="block nav-link py-2">Algorithms</a>
79
+ <a href="#" class="block nav-link py-2">Gallery</a>
80
+ <a href="#" class="block nav-link py-2">About</a>
81
+ </div>
82
+ </div>
83
+ </nav>
84
+ `;
85
+
86
+ // Initialize mobile menu toggle
87
+ this.shadowRoot.getElementById('mobile-menu-button').addEventListener('click', () => {
88
+ const menu = this.shadowRoot.getElementById('mobile-menu');
89
+ menu.classList.toggle('open');
90
+
91
+ // Change icon
92
+ const icon = this.shadowRoot.querySelector('#mobile-menu-button i');
93
+ if (menu.classList.contains('open')) {
94
+ icon.setAttribute('data-feather', 'x');
95
+ } else {
96
+ icon.setAttribute('data-feather', 'menu');
97
+ }
98
+ feather.replace();
99
+ });
100
+ }
101
+ }
102
+
103
+ customElements.define('custom-navbar', CustomNavbar);
index.html CHANGED
@@ -1,19 +1,143 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>TerrainGen Explorer</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ <script src="https://cdn.tailwindcss.com"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
10
+ <script src="https://unpkg.com/feather-icons"></script>
11
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
12
+ <script src="https://cdn.jsdelivr.net/npm/simplex-noise@2.4.0/simplex-noise.min.js"></script>
13
+ </head>
14
+ <body class="bg-gray-900 text-white min-h-screen">
15
+ <custom-navbar></custom-navbar>
16
+
17
+ <main class="container mx-auto px-4 py-8">
18
+ <section class="mb-12">
19
+ <h1 class="text-4xl font-bold mb-4">TerrainGen Explorer 🏔️</h1>
20
+ <p class="text-xl text-gray-300 mb-6">Visualize different procedural terrain generation algorithms in 3D</p>
21
+
22
+ <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
23
+ <!-- Terrain Generation Cards -->
24
+ <div class="bg-gray-800 rounded-lg overflow-hidden shadow-lg hover:shadow-xl transition-shadow">
25
+ <div class="h-48 bg-gray-700 flex items-center justify-center">
26
+ <canvas id="diamond-square-canvas" class="w-full h-full"></canvas>
27
+ </div>
28
+ <div class="p-4">
29
+ <h3 class="text-xl font-semibold mb-2">Diamond-Square</h3>
30
+ <p class="text-gray-400 mb-4">Fractal-based terrain generation using recursive subdivision</p>
31
+ <button onclick="generateTerrain('diamond-square')" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded transition-colors">
32
+ Generate
33
+ </button>
34
+ </div>
35
+ </div>
36
+
37
+ <div class="bg-gray-800 rounded-lg overflow-hidden shadow-lg hover:shadow-xl transition-shadow">
38
+ <div class="h-48 bg-gray-700 flex items-center justify-center">
39
+ <canvas id="perlin-canvas" class="w-full h-full"></canvas>
40
+ </div>
41
+ <div class="p-4">
42
+ <h3 class="text-xl font-semibold mb-2">Perlin Noise</h3>
43
+ <p class="text-gray-400 mb-4">Natural-looking terrain using gradient noise</p>
44
+ <button onclick="generateTerrain('perlin')" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded transition-colors">
45
+ Generate
46
+ </button>
47
+ </div>
48
+ </div>
49
+
50
+ <div class="bg-gray-800 rounded-lg overflow-hidden shadow-lg hover:shadow-xl transition-shadow">
51
+ <div class="h-48 bg-gray-700 flex items-center justify-center">
52
+ <canvas id="simplex-canvas" class="w-full h-full"></canvas>
53
+ </div>
54
+ <div class="p-4">
55
+ <h3 class="text-xl font-semibold mb-2">Simplex Noise</h3>
56
+ <p class="text-gray-400 mb-4">Improved performance over Perlin with similar results</p>
57
+ <button onclick="generateTerrain('simplex')" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded transition-colors">
58
+ Generate
59
+ </button>
60
+ </div>
61
+ </div>
62
+ </div>
63
+ </section>
64
+
65
+ <section class="mb-12">
66
+ <h2 class="text-2xl font-bold mb-4">Terrain Parameters</h2>
67
+ <div class="bg-gray-800 rounded-lg p-6">
68
+ <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
69
+ <div>
70
+ <label class="block text-gray-300 mb-2">Size</label>
71
+ <input type="range" id="size-slider" min="16" max="128" step="16" value="64" class="w-full">
72
+ <div class="text-right text-gray-400" id="size-value">64x64</div>
73
+ </div>
74
+ <div>
75
+ <label class="block text-gray-300 mb-2">Roughness</label>
76
+ <input type="range" id="roughness-slider" min="0.1" max="1" step="0.1" value="0.5" class="w-full">
77
+ <div class="text-right text-gray-400" id="roughness-value">0.5</div>
78
+ </div>
79
+ <div>
80
+ <label class="block text-gray-300 mb-2">Height Scale</label>
81
+ <input type="range" id="height-slider" min="1" max="50" value="20" class="w-full">
82
+ <div class="text-right text-gray-400" id="height-value">20</div>
83
+ </div>
84
+ <div>
85
+ <label class="block text-gray-300 mb-2">Water Level</label>
86
+ <input type="range" id="water-slider" min="0" max="100" value="40" class="w-full">
87
+ <div class="text-right text-gray-400" id="water-value">40%</div>
88
+ </div>
89
+ </div>
90
+ </div>
91
+ </section>
92
+
93
+ <section>
94
+ <h2 class="text-2xl font-bold mb-4">3D View</h2>
95
+ <div class="bg-gray-800 rounded-lg overflow-hidden h-96">
96
+ <canvas id="terrain-canvas" class="w-full h-full"></canvas>
97
+ </div>
98
+ <div class="mt-4 flex justify-center space-x-4">
99
+ <button onclick="rotateCamera('left')" class="bg-gray-700 hover:bg-gray-600 text-white px-4 py-2 rounded flex items-center">
100
+ <i data-feather="rotate-ccw" class="mr-2"></i> Rotate Left
101
+ </button>
102
+ <button onclick="resetCamera()" class="bg-gray-700 hover:bg-gray-600 text-white px-4 py-2 rounded flex items-center">
103
+ <i data-feather="refresh-ccw" class="mr-2"></i> Reset View
104
+ </button>
105
+ <button onclick="rotateCamera('right')" class="bg-gray-700 hover:bg-gray-600 text-white px-4 py-2 rounded flex items-center">
106
+ <i data-feather="rotate-cw" class="mr-2"></i> Rotate Right
107
+ </button>
108
+ </div>
109
+ </section>
110
+ </main>
111
+
112
+ <custom-footer></custom-footer>
113
+
114
+ <script src="components/navbar.js"></script>
115
+ <script src="components/footer.js"></script>
116
+ <script src="script.js"></script>
117
+ <script>
118
+ feather.replace();
119
+ // Initialize all canvases
120
+ document.addEventListener('DOMContentLoaded', function() {
121
+ generateTerrain('diamond-square');
122
+
123
+ // Update slider values
124
+ document.getElementById('size-slider').addEventListener('input', function() {
125
+ document.getElementById('size-value').textContent = this.value + 'x' + this.value;
126
+ });
127
+
128
+ document.getElementById('roughness-slider').addEventListener('input', function() {
129
+ document.getElementById('roughness-value').textContent = this.value;
130
+ });
131
+
132
+ document.getElementById('height-slider').addEventListener('input', function() {
133
+ document.getElementById('height-value').textContent = this.value;
134
+ });
135
+
136
+ document.getElementById('water-slider').addEventListener('input', function() {
137
+ document.getElementById('water-value').textContent = this.value + '%';
138
+ });
139
+ });
140
+ </script>
141
+ <script src="https://huggingface.co/deepsite/deepsite-badge.js"></script>
142
+ </body>
143
+ </html>
script.js ADDED
@@ -0,0 +1,349 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Global Three.js variables
2
+ let scene, camera, renderer, terrainMesh, controls;
3
+ let terrainSize = 64;
4
+ let isAnimating = false;
5
+ let animationId = null;
6
+
7
+ // Initialize the 3D scene
8
+ function initScene() {
9
+ const canvas = document.getElementById('terrain-canvas');
10
+
11
+ // Scene setup
12
+ scene = new THREE.Scene();
13
+ scene.background = new THREE.Color(0x1a202c);
14
+
15
+ // Camera setup
16
+ camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
17
+ camera.position.set(terrainSize * 0.7, terrainSize * 0.7, terrainSize * 0.7);
18
+ camera.lookAt(0, 0, 0);
19
+
20
+ // Renderer setup
21
+ renderer = new THREE.WebGLRenderer({ canvas, antialias: true });
22
+ renderer.setSize(canvas.clientWidth, canvas.clientHeight);
23
+
24
+ // Lighting
25
+ const ambientLight = new THREE.AmbientLight(0x404040);
26
+ scene.add(ambientLight);
27
+
28
+ const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
29
+ directionalLight.position.set(1, 1, 1);
30
+ scene.add(directionalLight);
31
+
32
+ // Orbit controls
33
+ controls = new THREE.OrbitControls(camera, renderer.domElement);
34
+ controls.enableDamping = true;
35
+ controls.dampingFactor = 0.25;
36
+
37
+ // Handle window resize
38
+ window.addEventListener('resize', onWindowResize);
39
+ }
40
+
41
+ // Generate terrain using specified algorithm
42
+ function generateTerrain(algorithm) {
43
+ // Remove existing terrain if any
44
+ if (terrainMesh) {
45
+ scene.remove(terrainMesh);
46
+ terrainMesh.geometry.dispose();
47
+ terrainMesh.material.dispose();
48
+ }
49
+
50
+ // Get parameters from sliders
51
+ terrainSize = parseInt(document.getElementById('size-slider').value);
52
+ const roughness = parseFloat(document.getElementById('roughness-slider').value);
53
+ const heightScale = parseInt(document.getElementById('height-slider').value);
54
+ const waterLevel = parseInt(document.getElementById('water-slider').value) / 100;
55
+
56
+ // Generate heightmap
57
+ let heightmap = generateHeightmap(algorithm, terrainSize, roughness, heightScale);
58
+
59
+ // Create geometry from heightmap
60
+ const geometry = createGeometryFromHeightmap(heightmap, terrainSize, heightScale);
61
+
62
+ // Create material with water effect
63
+ const material = createTerrainMaterial(heightmap, terrainSize, heightScale, waterLevel);
64
+
65
+ // Create mesh
66
+ terrainMesh = new THREE.Mesh(geometry, material);
67
+ scene.add(terrainMesh);
68
+
69
+ // Update camera position based on terrain size
70
+ camera.position.set(terrainSize * 0.7, terrainSize * 0.7, terrainSize * 0.7);
71
+ camera.lookAt(0, 0, 0);
72
+ controls.update();
73
+
74
+ // Start animation if not already running
75
+ if (!isAnimating) {
76
+ animate();
77
+ }
78
+
79
+ // Update 2D previews
80
+ updatePreviews(heightmap, algorithm);
81
+ }
82
+
83
+ // Generate heightmap using specified algorithm
84
+ function generateHeightmap(algorithm, size, roughness, heightScale) {
85
+ const heightmap = new Array(size * size).fill(0);
86
+
87
+ // Simplified implementations for demo purposes
88
+ switch(algorithm) {
89
+ case 'diamond-square':
90
+ // Implement diamond-square algorithm
91
+ diamondSquare(heightmap, size, roughness);
92
+ break;
93
+
94
+ case 'perlin':
95
+ // Implement Perlin noise
96
+ for (let y = 0; y < size; y++) {
97
+ for (let x = 0; x < size; x++) {
98
+ const value = noise.perlin2(x * roughness / 10, y * roughness / 10);
99
+ heightmap[y * size + x] = (value + 1) * 0.5 * heightScale;
100
+ }
101
+ }
102
+ break;
103
+
104
+ case 'simplex':
105
+ // Implement Simplex noise
106
+ for (let y = 0; y < size; y++) {
107
+ for (let x = 0; x < size; x++) {
108
+ const value = noise.simplex2(x * roughness / 10, y * roughness / 10);
109
+ heightmap[y * size + x] = (value + 1) * 0.5 * heightScale;
110
+ }
111
+ }
112
+ break;
113
+
114
+ default:
115
+ console.error('Unknown algorithm:', algorithm);
116
+ }
117
+
118
+ return heightmap;
119
+ }
120
+
121
+ // Simplified Diamond-Square implementation
122
+ function diamondSquare(heightmap, size, roughness) {
123
+ // Initialize corners
124
+ heightmap[0] = Math.random() * roughness;
125
+ heightmap[size - 1] = Math.random() * roughness;
126
+ heightmap[size * (size - 1)] = Math.random() * roughness;
127
+ heightmap[size * size - 1] = Math.random() * roughness;
128
+
129
+ let step = size - 1;
130
+ let scale = roughness;
131
+
132
+ while (step > 1) {
133
+ // Diamond step
134
+ for (let y = 0; y < size - 1; y += step) {
135
+ for (let x = 0; x < size - 1; x += step) {
136
+ const avg = (
137
+ heightmap[y * size + x] +
138
+ heightmap[y * size + (x + step)] +
139
+ heightmap[(y + step) * size + x] +
140
+ heightmap[(y + step) * size + (x + step)]
141
+ ) / 4;
142
+
143
+ const centerX = x + step / 2;
144
+ const centerY = y + step / 2;
145
+ const centerIndex = centerY * size + centerX;
146
+ heightmap[centerIndex] = avg + (Math.random() * 2 - 1) * scale;
147
+ }
148
+ }
149
+
150
+ // Square step
151
+ for (let y = 0; y < size; y += step / 2) {
152
+ for (let x = (y + step / 2) % step; x < size; x += step) {
153
+ let sum = 0;
154
+ let count = 0;
155
+
156
+ // Top neighbor
157
+ if (y - step / 2 >= 0) {
158
+ sum += heightmap[(y - step / 2) * size + x];
159
+ count++;
160
+ }
161
+
162
+ // Bottom neighbor
163
+ if (y + step / 2 < size) {
164
+ sum += heightmap[(y + step / 2) * size + x];
165
+ count++;
166
+ }
167
+
168
+ // Left neighbor
169
+ if (x - step / 2 >= 0) {
170
+ sum += heightmap[y * size + (x - step / 2)];
171
+ count++;
172
+ }
173
+
174
+ // Right neighbor
175
+ if (x + step / 2 < size) {
176
+ sum += heightmap[y * size + (x + step / 2)];
177
+ count++;
178
+ }
179
+
180
+ if (count > 0) {
181
+ heightmap[y * size + x] = sum / count + (Math.random() * 2 - 1) * scale;
182
+ }
183
+ }
184
+ }
185
+
186
+ step = Math.floor(step / 2);
187
+ scale *= roughness;
188
+ }
189
+ }
190
+
191
+ // Create Three.js geometry from heightmap
192
+ function createGeometryFromHeightmap(heightmap, size, heightScale) {
193
+ const geometry = new THREE.PlaneGeometry(size, size, size - 1, size - 1);
194
+
195
+ // Apply heightmap to vertices
196
+ for (let i = 0; i < geometry.attributes.position.count; i++) {
197
+ const x = i % size;
198
+ const y = Math.floor(i / size);
199
+ geometry.attributes.position.setZ(i, heightmap[y * size + x]);
200
+ }
201
+
202
+ geometry.rotateX(-Math.PI / 2);
203
+ geometry.computeVertexNormals();
204
+
205
+ return geometry;
206
+ }
207
+
208
+ // Create terrain material with water effect
209
+ function createTerrainMaterial(heightmap, size, heightScale, waterLevel) {
210
+ // Find min and max heights
211
+ let minHeight = Infinity;
212
+ let maxHeight = -Infinity;
213
+
214
+ for (const height of heightmap) {
215
+ if (height < minHeight) minHeight = height;
216
+ if (height > maxHeight) maxHeight = height;
217
+ }
218
+
219
+ const waterHeight = minHeight + (maxHeight - minHeight) * waterLevel;
220
+
221
+ // Create color gradient
222
+ const texture = new THREE.CanvasTexture(createTerrainTexture(size, minHeight, maxHeight, waterHeight));
223
+
224
+ return new THREE.MeshStandardMaterial({
225
+ map: texture,
226
+ metalness: 0.1,
227
+ roughness: 0.7,
228
+ flatShading: false
229
+ });
230
+ }
231
+
232
+ // Create terrain texture with elevation colors
233
+ function createTerrainTexture(size, minHeight, maxHeight, waterHeight) {
234
+ const canvas = document.createElement('canvas');
235
+ canvas.width = size;
236
+ canvas.height = size;
237
+ const ctx = canvas.getContext('2d');
238
+
239
+ // Draw gradient based on height
240
+ const imageData = ctx.createImageData(size, size);
241
+
242
+ for (let y = 0; y < size; y++) {
243
+ for (let x = 0; x < size; x++) {
244
+ const idx = (y * size + x) * 4;
245
+ const height = heightmap[y * size + x];
246
+
247
+ // Water
248
+ if (height <= waterHeight) {
249
+ const depth = (waterHeight - height) / (waterHeight - minHeight);
250
+ imageData.data[idx] = 30 + 50 * depth; // R
251
+ imageData.data[idx + 1] = 80 + 100 * depth; // G
252
+ imageData.data[idx + 2] = 150 + 100 * depth; // B
253
+ }
254
+ // Beach/Sand
255
+ else if (height <= waterHeight + (maxHeight - waterHeight) * 0.05) {
256
+ imageData.data[idx] = 210; // R
257
+ imageData.data[idx + 1] = 190; // G
258
+ imageData.data[idx + 2] = 140; // B
259
+ }
260
+ // Grass
261
+ else if (height <= waterHeight + (maxHeight - waterHeight) * 0.3) {
262
+ const t = (height - waterHeight) / ((maxHeight - waterHeight) * 0.3);
263
+ imageData.data[idx] = 50 + 80 * t; // R
264
+ imageData.data[idx + 1] = 100 + 80 * t; // G
265
+ imageData.data[idx + 2] = 50 + 30 * t; // B
266
+ }
267
+ // Rock
268
+ else if (height <= waterHeight + (maxHeight - waterHeight) * 0.7) {
269
+ const t = (height - waterHeight - (maxHeight - waterHeight) * 0.3) / ((maxHeight - waterHeight) * 0.4);
270
+ imageData.data[idx] = 100 + 50 * t; // R
271
+ imageData.data[idx + 1] = 80 + 20 * t; // G
272
+ imageData.data[idx + 2] = 60 + 20 * t; // B
273
+ }
274
+ // Snow
275
+ else {
276
+ const t = (height - waterHeight - (maxHeight - waterHeight) * 0.7) / ((maxHeight - waterHeight) * 0.3);
277
+ imageData.data[idx] = 180 + 75 * t; // R
278
+ imageData.data[idx + 1] = 190 + 65 * t; // G
279
+ imageData.data[idx + 2] = 200 + 55 * t; // B
280
+ }
281
+
282
+ imageData.data[idx + 3] = 255; // Alpha
283
+ }
284
+ }
285
+
286
+ ctx.putImageData(imageData, 0, 0);
287
+ return canvas;
288
+ }
289
+
290
+ // Update 2D preview canvases
291
+ function updatePreviews(heightmap, algorithm) {
292
+ const size = Math.min(128, terrainSize);
293
+ const previewCanvas = document.getElementById(`${algorithm}-canvas`);
294
+ const ctx = previewCanvas.getContext('2d');
295
+
296
+ // Scale heightmap to fit preview
297
+ const step = Math.floor(terrainSize / size);
298
+ const imageData = ctx.createImageData(size, size);
299
+
300
+ for (let y = 0; y < size; y++) {
301
+ for (let x = 0; x < size; x++) {
302
+ const idx = (y * size + x) * 4;
303
+ const height = heightmap[y * step * terrainSize + x * step];
304
+ const normalizedHeight = Math.floor((height / Math.max(...heightmap)) * 255);
305
+
306
+ imageData.data[idx] = normalizedHeight; // R
307
+ imageData.data[idx + 1] = normalizedHeight; // G
308
+ imageData.data[idx + 2] = normalizedHeight; // B
309
+ imageData.data[idx + 3] = 255; // Alpha
310
+ }
311
+ }
312
+
313
+ ctx.putImageData(imageData, 0, 0);
314
+ }
315
+
316
+ // Animation loop
317
+ function animate() {
318
+ isAnimating = true;
319
+ animationId = requestAnimationFrame(animate);
320
+ controls.update();
321
+ renderer.render(scene, camera);
322
+ }
323
+
324
+ // Handle window resize
325
+ function onWindowResize() {
326
+ const canvas = document.getElementById('terrain-canvas');
327
+ camera.aspect = canvas.clientWidth / canvas.clientHeight;
328
+ camera.updateProjectionMatrix();
329
+ renderer.setSize(canvas.clientWidth, canvas.clientHeight);
330
+ }
331
+
332
+ // Camera controls
333
+ function rotateCamera(direction) {
334
+ const angle = direction === 'left' ? -Math.PI / 8 : Math.PI / 8;
335
+ camera.position.applyAxisAngle(new THREE.Vector3(0, 1, 0), angle);
336
+ camera.lookAt(0, 0, 0);
337
+ }
338
+
339
+ function resetCamera() {
340
+ camera.position.set(terrainSize * 0.7, terrainSize * 0.7, terrainSize * 0.7);
341
+ camera.lookAt(0, 0, 0);
342
+ controls.update();
343
+ }
344
+
345
+ // Initialize when DOM is loaded
346
+ document.addEventListener('DOMContentLoaded', function() {
347
+ initScene();
348
+ generateTerrain('diamond-square');
349
+ });
style.css CHANGED
@@ -1,28 +1,45 @@
 
 
1
  body {
2
- padding: 2rem;
3
- font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  }
5
 
6
- h1 {
7
- font-size: 16px;
8
- margin-top: 0;
9
  }
10
 
11
- p {
12
- color: rgb(107, 114, 128);
13
- font-size: 15px;
14
- margin-bottom: 10px;
15
- margin-top: 5px;
16
  }
17
 
18
- .card {
19
- max-width: 620px;
20
- margin: 0 auto;
21
- padding: 16px;
22
- border: 1px solid lightgray;
23
- border-radius: 16px;
24
  }
25
 
26
- .card p:last-child {
27
- margin-bottom: 0;
28
  }
 
 
 
 
 
 
 
 
1
+ @import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');
2
+
3
  body {
4
+ font-family: 'Inter', sans-serif;
5
+ }
6
+
7
+ canvas {
8
+ display: block;
9
+ background: #1a202c;
10
+ }
11
+
12
+ /* Custom scrollbar */
13
+ ::-webkit-scrollbar {
14
+ width: 8px;
15
+ height: 8px;
16
+ }
17
+
18
+ ::-webkit-scrollbar-track {
19
+ background: #2d3748;
20
  }
21
 
22
+ ::-webkit-scrollbar-thumb {
23
+ background: #4a5568;
24
+ border-radius: 4px;
25
  }
26
 
27
+ ::-webkit-scrollbar-thumb:hover {
28
+ background: #718096;
 
 
 
29
  }
30
 
31
+ /* Animation for buttons */
32
+ button {
33
+ transition: all 0.2s ease;
 
 
 
34
  }
35
 
36
+ button:active {
37
+ transform: scale(0.95);
38
  }
39
+
40
+ /* Responsive canvas containers */
41
+ @media (max-width: 768px) {
42
+ .canvas-container {
43
+ height: 300px !important;
44
+ }
45
+ }