-
Notifications
You must be signed in to change notification settings - Fork 4
/
base.js
271 lines (238 loc) · 7.65 KB
/
base.js
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
// let seed = 12345
// let planetSize = 45 // random(30, 170)
// let hasRings = true // random() < 0.5 // 50% chance
// let numMoons = 2 // floor(random(0, 5)) // Up to 3 moons
// let planetType = 0 // 0 = gas, 1 = solid
// let baseHue = 123 // random(0, 360)
// let hasWater = true // random() < 0.1 // 10% chance
let angle = 0
let textureImg
let initialRotationX
let initialRotationY
let ringSize
let numRingParticles
let moonSize
let ringTextureImg
let stars = []
let rings = []
let moons = []
let moonTextures = []
function setup() {
randomSeed(seed)
createCanvas(windowWidth, windowHeight, WEBGL)
colorMode(HSL, 360, 100, 100)
let randomPalette = generatePlanetColors(baseHue)
let numColors = randomPalette.length
colorMode(RGB, 255)
let thresholds = []
for (let i = 0; i < numColors - 1; i++) {
let r
do {
r = random()
} while (r < 0.1 || r > 0.9) // Should have at least 20% of the planet covered by water or primary colour
thresholds.push(r)
}
thresholds.sort()
let textureInput = {
thresholds: thresholds,
colors: randomPalette,
}
// Generate stars
for (let i = 0; i < 300; i++) {
stars.push({
x: random(min(-3000, -width * 1.5), max(3000, width * 1.5)),
y: random(min(-3000, -height * 1.5), max(3000, height * 1.5)),
z: random(min(-3000, -width * 1.5), max(3000, height * 1.5)),
radius: random(0.5, 2),
})
}
initialRotationX = random(360)
initialRotationY = 148
textureImg = generateTexture(textureInput.thresholds, textureInput.colors, planetType)
if (hasRings) {
ringSize = random(planetSize * 1.4, planetSize * 1.5)
ringTextureImg = generateRingTexture(randomPalette)
}
// Generate moons
for (let i = 0; i < numMoons; i++) {
let moonRadius = random(planetSize * 0.05, planetSize * 0.21)
let minMoonDistance = hasRings ? ringSize * 1.3 : planetSize * 1.5 // Change minimum distance based on presence of rings
let maxMoonDistance = 300
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 generatePlanetColors(baseHue) {
let colorPalette = []
const hueVariation = 5
const saturationBase = 60
const saturationVariation = 15
const lightnessBase = 40
const lightnessVariation = 10
const waterHue = 210
const waterSaturation = 70
const waterLightness = 50
for (let i = 0; i < 4; i++) {
let hue, saturation, lightness
hue = (baseHue + i * hueVariation) % 360
saturation = Math.max(0, Math.min(100, saturationBase - i * saturationVariation))
lightness = Math.max(0, Math.min(100, lightnessBase + i * lightnessVariation))
colorPalette.push(color(hue, saturation, lightness))
}
if (hasWater) {
colorPalette = [color(waterHue, waterSaturation, waterLightness), ...colorPalette]
}
return colorPalette
}
function generateRingTexture(colors) {
let textureImg = createGraphics(248, 124)
let numStripes = random(5, 15)
let stripeHeight = textureImg.height / numStripes
let mutedColors = [colors[2], colors[3]]
for (let i = 0; i < numStripes; i++) {
const col = mutedColors[i % mutedColors.length]
col.setAlpha(0.5)
textureImg.fill(col)
textureImg.noStroke()
textureImg.rect(0, i * stripeHeight, textureImg.width, stripeHeight)
}
textureImg.updatePixels()
return textureImg
}
function generateTexture(elevationThresholds, colors, planetType) {
let textureImg = createGraphics(248 * 2, 124 * 2)
textureImg.noiseSeed(random(1000))
let noiseScale = planetType === 0 ? 2 : 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]) {
if (i == 0 && hasWater) {
col = colors[0]
} else {
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(100000))
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 ambient light
ambientLight(100)
ambientMaterial(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 maxAngle = radians(10)
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(mouseYRatio)
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
}
}