xxxxxxxxxx
let sandColors;
let landscape;
let offset; // Phase offset for the sine wave (for animation)
let amplitude; // Amplitude of the sine wave
let wavelength; // Wavelength of the sine wave
let segments; // Number of segments for the sandworm
let diameter; // Diameter for each segment
let segmentSpacing; // Space between the centers of each segment
let sandwormLayer; // The layer index after which the sandworm will be drawn
let mySound; // Variable to hold the sound
let sunPosition, moon1Position, moon2Position; // Positions for sun and moons
let dayTime; // Flag for day and night cycle
let bgColor; // Background color
function preload() {
mySound = loadSound('dune.mp3');
}
function setup() {
createCanvas(800, 800);
sandColors = [
color(170, 82, 45),
color(180, 92, 55),
color(190, 102, 65),
color(200, 112, 75),
color(210, 122, 85),
color(220, 132, 95)
];
landscape = new Landscape(6, sandColors);
segments = 50;
diameter = 20;
segmentSpacing = diameter * 0.5;
offset = -segments * segmentSpacing;
setNewPath();
sunPosition = createVector(width / 2, -100); // Start sun off-screen
moon1Position = createVector(width / 2, height + 100); // Start first moon off-screen
moon2Position = createVector(width / 2 + 80, height + 180); // Start second moon off-screen
dayTime = true; // Start with daytime
bgColor = color(255, 233, 181); // Initial background color for daytime
mySound.loop();
}
function draw() {
if (dayTime) {
bgColor = lerpColor(bgColor, color(255, 233, 181), 0.05);
} else {
bgColor = lerpColor(bgColor, color(10, 10, 40), 0.05);
}
background(bgColor);
drawCelestialBodies();
landscape.display();
}
function setNewPath() {
amplitude = random(10, 80);
wavelength = random(width * 0.5, width * 1.5);
sandwormLayer = floor(random(2, 5));
}
function drawCelestialBodies() {
noStroke();
if (dayTime) {
fill(255, 204, 0);
ellipse(sunPosition.x, sunPosition.y, 100, 100);
sunPosition.y += 2;
if (sunPosition.y > height + 100) {
dayTime = false;
moon1Position.y = moon2Position.y = -100;
}
} else {
fill(240, 240, 240);
ellipse(moon1Position.x, moon1Position.y, 80, 80);
ellipse(moon2Position.x, moon2Position.y, 50, 50);
moon1Position.y += 1;
moon2Position.y += 1.1;
if (moon1Position.y > height + 150) {
dayTime = true;
sunPosition.y = -100;
}
}
}
function mousePressed() {
saveCanvas('dune', 'png');
}
class Landscape {
constructor(numLayers, layerColors) {
this.numLayers = numLayers;
this.layerColors = layerColors;
}
display() {
noStroke();
for (let i = this.numLayers - 1; i >= 0; i--) {
fill(this.layerColors[i]);
beginShape();
let baseHeight = map(i, 0, this.numLayers - 1, height * 0.6, height * 0.5);
let noiseHeight = map(i, 0, this.numLayers - 1, 20, 60);
for (let x = 0; x <= width; x++) {
let y = noise(x * 0.015, i * 0.5) * noiseHeight + baseHeight;
vertex(x, y);
}
vertex(width, height);
vertex(0, height);
endShape(CLOSE);
if (i == sandwormLayer) {
drawSandworm();
}
}
}
}
function drawSandworm() {
const startY = height / 1.63;
fill(100, 50, 20);
noStroke();
for (let i = 0; i < segments; i++) {
let x = offset + segmentSpacing * i;
let y = startY + amplitude * sin((TWO_PI / wavelength) * x);
ellipse(x, y, diameter, diameter);
}
offset += 3;
if (offset > width + segments * segmentSpacing) {
offset = -segments * segmentSpacing;
setNewPath();
}
}