-
Notifications
You must be signed in to change notification settings - Fork 4
/
landing.html
256 lines (231 loc) · 8.47 KB
/
landing.html
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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/p5"></script>
</head>
<style>
body {
background-color: black;
}
</style>
<body>
<script>
let seed = 12345
let planetSize = 45 // random(30, 170)
let hasRings = true // random() < 0.5 // 50% chance
let numMoons = 1 // floor(random(0, 5)) // Up to 3 moons
let planetType = 0 // 0 = gas, 1 = solid
let hasAtmosphere = true // random() < 0.5 // 50% chance
let colors = []
let angle = 0
let textureImg
let stars = []
let initialRotationX
let initialRotationY
let rings = []
let ringSize
let numRingParticles
let moonSize
let moons = []
let moonTextures = []
let ringTextureImg
function setup() {
//randomSeed(seed)
createCanvas(windowWidth, windowHeight, WEBGL)
let numColors = 5
let randomPalette = []
for (let i = 0; i < numColors; i++) {
randomPalette.push(color(random(255), random(255), random(255)))
}
let thresholds = []
for (let i = 0; i < numColors - 1; i++) {
thresholds.push(random())
}
thresholds.sort()
let randomColorPalette = {
thresholds: thresholds,
colors: randomPalette,
}
// Generate stars
for (let i = 0; i < 200; i++) {
stars.push({
x: random(-width * 1.5, width * 1.5),
y: random(-height * 1.5, height * 1.5),
z: random(width * 1.5),
radius: random(0.5, 2),
})
}
planetSize = random(20, 50)
initialRotationX = random(360)
initialRotationY = 148
planetType = random() < 0.5 ? "gas" : "solid"
textureImg = generateTexture(randomColorPalette.thresholds, randomColorPalette.colors, planetType)
hasRings = true // 50% chance of having rings
if (hasRings) {
ringSize = random(planetSize * 1.5, planetSize * 1.7)
let baseColor = random(randomColorPalette.colors) // Select a random color from the planet's color palette
ringTextureImg = generateRingTexture(baseColor) // Pass the base color to generateRingTexture()
}
// Generate moons
numMoons = random(1, random(4)) // Up to 3 moons
for (let i = 0; i < numMoons; i++) {
let moonRadius = random(planetSize * 0.08, planetSize * 0.21)
let minMoonDistance = hasRings ? ringSize * 1.2 : planetSize * 1.5 // Change minimum distance based on presence of rings
let maxMoonDistance = planetSize * 8
let moonDistance = random(minMoonDistance, maxMoonDistance)
let moonAngle = random(TWO_PI) // Change this line to add
let moonSpeed = random(0.001, 0.011) // Add random speed property
moonTextures.push(generateMoonTexture())
let orbitAngle = radians(random(-15, 15))
moons.push({
radius: moonRadius,
distance: moonDistance,
angle: moonAngle,
speed: moonSpeed, // Add speed property to moon object
orbitAngle: orbitAngle, // Add the orbitAngle property to the moon object
})
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight)
}
function generateRingTexture(baseColor) {
let textureImg = createGraphics(248, 124)
let numStripes = random(5, 15)
let stripeHeight = textureImg.height / numStripes
for (let i = 0; i < numStripes; i++) {
let brightnessAdjustment = random(-30, 100)
let adjustedColor = color(
red(baseColor) + brightnessAdjustment,
green(baseColor) + brightnessAdjustment,
blue(baseColor) + brightnessAdjustment,
100,
)
textureImg.fill(adjustedColor)
textureImg.noStroke()
textureImg.rect(0, i * stripeHeight, textureImg.width, stripeHeight)
}
textureImg.updatePixels()
return textureImg
}
function generateTexture(elevationThresholds, colors, planetType) {
let textureImg = createGraphics(248, 124)
textureImg.noiseSeed(random(1000))
let noiseScale = planetType === "gas" ? 1 : 4
for (let x = 0; x < textureImg.width; x++) {
for (let y = 0; y < textureImg.height; y++) {
// Convert x and y to spherical coordinates
let lon = map(x, 0, textureImg.width, 0, TWO_PI)
let lat = map(y, 0, textureImg.height, -PI / 2, PI / 2)
let u = (cos(lat) * cos(lon) + 1) / 2
let v = (cos(lat) * sin(lon) + 1) / 2
let elevation = textureImg.noise(u * noiseScale, v * noiseScale)
let col = color(255)
let found = false
for (let i = 0; i < elevationThresholds.length; i++) {
if (elevation < elevationThresholds[i]) {
col = lerpColor(
colors[i],
colors[i + 1],
(elevation - (i === 0 ? 0 : elevationThresholds[i - 1])) *
(1 / (elevationThresholds[i] - (i === 0 ? 0 : elevationThresholds[i - 1]))),
)
found = true
break
}
}
if (!found) {
col = colors[colors.length - 1] // Assign the last color if elevation is greater than the last threshold
}
textureImg.set(x, y, col)
}
}
textureImg.updatePixels()
return textureImg
}
function generateMoonTexture() {
let textureImg = createGraphics(62, 31)
textureImg.noiseSeed(random(1000))
for (let x = 0; x < textureImg.width; x++) {
for (let y = 0; y < textureImg.height; y++) {
let lon = map(x, 0, textureImg.width, 0, TWO_PI)
let lat = map(y, 0, textureImg.height, -PI / 2, PI / 2)
let u = (cos(lat) * cos(lon) + 1) / 2
let v = (cos(lat) * sin(lon) + 1) / 2
let elevation = textureImg.noise(u * 4, v * 4)
let col = color(map(elevation, 0, 1, 20, 255))
textureImg.set(x, y, col)
}
}
textureImg.updatePixels()
return textureImg
}
function drawMoon(moon, moonTexture) {
push()
texture(moonTexture)
noStroke()
rotateX(moon.orbitAngle) // Add this line to apply the orbit angle
rotateY(moon.angle)
translate(moon.distance, 0, 0)
rotateZ(radians(95))
sphere(moon.radius)
pop()
}
function draw() {
background(0)
// Set up point light
pointLight(255, 255, 255, 400, -10, 1200)
// Draw stars
push()
translate(0, 0, -1200)
for (let star of stars) {
stroke(255)
strokeWeight(star.radius)
point(star.x, star.y, star.z)
}
pop()
let fixedDistance = 450
let maxAngle = radians(30)
let mouseXRatio = map(mouseX, 0, width, -maxAngle, maxAngle)
let mouseYRatio = map(mouseY, 0, height, -maxAngle, maxAngle)
let camX = 400 * sin(mouseXRatio)
let camY = -100 * cos(mouseXRatio)
let camZ = 400
camera(camX, camY, camZ, 0, 0, 0, 0, 1, 0)
// Rotate planet
push() // Add push() to isolate the rotation transformation
rotateY(initialRotationY + angle)
rotateX(initialRotationX + QUARTER_PI)
rotateZ(QUARTER_PI)
angle += 0.005
// Draw planet
texture(textureImg)
noStroke()
sphere(planetSize)
pop()
if (hasRings) {
push()
rotateX(PI / 2) // Rotate the rings to lie on the XY plane
pointLight(255, 255, 255, 400, -400, 1200)
scale(1, 1, 0.01) // Scale the Z-axis
texture(ringTextureImg) // Apply the ring texture
noStroke()
torus(ringSize, planetSize * 0.3) // Draw the rings using torus function
pop()
}
// Draw moons and their orbits
for (let i = 0; i < numMoons; i++) {
let moon = moons[i]
let moonTexture = moonTextures[i]
push() // Add push() to isolate the moon rotation transformation
drawMoon(moon, moonTexture)
pop() // Add pop() to isolate the moon rotation transformation
}
// Update moon angles
for (let moon of moons) {
moon.angle += moon.speed
}
}
</script>
</body>
</html>