Planets of the Universe.

The creative coding planet.

It's a creative coding work made with Processing. It draws a planet with star field background.

 I draws some pattern on 3D sphere with random walking.
You can see my article about Random Walking tips.
https://www.deconbatch.com/2020/02/the-joy-of-random-walking.html

And I use Vector Field calculation to draw star field.

Example code of Processing.

This code does not display any images on the screen but generates image files in frames directory.

Please feel free to use this example code.
To see other works based on my code is my pleasure. And my honor.

/**
 * Planets of the Universe.
 *
 * Processing 3.5.3
 * @author @deconbatch
 * @version 0.1
 * created 0.1 2020.04.12
 */

void setup() {
  size(980, 980, P3D);
  colorMode(HSB, 360, 100, 100, 100);
  sphereDetail(60);
  smooth();
}

void draw() {

  int   frmMax  = 3; // save 3 files
  float initHue = random(360.0);

  camera(700, 700, 700,
         0, 0, 0,
         0, 1, 0);
  lightFalloff(1.0, 0.001, 0.0);
  pointLight(0.0, 0.0, 100.0, 450.0, 200.0, 600.0);
  ambientLight(0.0, 0.0, 30.0);

  for (int frmCnt = 0; frmCnt < frmMax; frmCnt++) {
    background(0.0, 0.0, 0.0, 0.0);

    // draw background star field
    pushMatrix();
    rotateX(-HALF_PI * 0.5);
    rotateY(HALF_PI * 0.5);
    blendMode(ADD);
    starField(initHue + frmCnt * 120.0);
    blendMode(BLEND);
    drawNimbus();
    popMatrix();
 
    // draw planet
    blendMode(BLEND);
    drawPlanet(initHue + frmCnt * 120.0);
  
    saveFrame("frames/" + String.format("%04d", frmCnt + 1) + ".png");
  }

  exit();

}

/**
 * starField : draw star field.
 * @param  _initHue : star field color.
 */
private void starField(float _initHue) {

  int   ptnMax = 50; // draw 50 patterns
  float xPhase = random(1.0);
  float yPhase = random(1.0);
     
  for (int ptnCnt = 0; ptnCnt < ptnMax; ++ptnCnt) {
      
    float ptnRatio = map(ptnCnt, 0, ptnMax, 0.0, 1.0);

    int   plotMax = floor(random(5000.0, 20000.0));
    float plotDiv = 0.05;
    float baseHue = _initHue + map(ptnRatio, 0.0, 1.0, 0.0, 120.0);
    float baseSat = map(ptnRatio * ptnRatio, 0.0, 1.0, 40.0, 0.0);
    float baseBri = map(ptnRatio, 0.0, 1.0, 0.0, 40.0);
    float baseAlp = 100.0;
     
    // initial value of vector field calculation 
    float xPoint = cos(TWO_PI * (xPhase + ptnRatio)) * 0.3;
    float yPoint = sin(TWO_PI * (yPhase + ptnRatio)) * 0.3;
    float rPoint = 0.0;

    noFill();
    strokeWeight(0.05);
    stroke(baseHue % 360.0, baseSat, baseBri, baseAlp);
    beginShape();
    for (int plotCnt = 0; plotCnt < plotMax; ++plotCnt) {

      float plotRatio = map(plotCnt, 0, plotMax, 0.0, 1.0);

      float xPrev = xPoint;
      float yPrev = yPoint;
      float rPrev = rPoint;

      // vector field calculation 
      rPoint += random(-1.0, 1.0);
      xPoint += plotDiv * cos(TWO_PI * rPoint) * plotRatio;
      yPoint += plotDiv * sin(TWO_PI * rPoint) * plotRatio;

      vertex(xPoint * 1.25 * width, yPoint * 1.25 * height, -1000.0);

    }
    endShape();

  }
}

/**
 * drawNimbus : draw nimbus in center of the canvas.
 */
private void drawNimbus() {

  float rMax = min(width, height) * 2.0;
  pushMatrix();
  translate(50.0, 0.0, 0.0);
  for (int i = 0; i < 20; i++) {
    float alp = map(i, 0, 20, 30.0, 0.0);
    float rad = map(i, 0, 20, rMax * 0.5, rMax);
    noStroke();
    fill(0.0, 0.0, 0.0, alp);
    ellipse(0.0, 0.0, rad, rad);
  }
  popMatrix();
  
}

/**
 * drawPlanet : draw planet.
 * @param  _initHue : planet color.
 */
private void drawPlanet(float _initHue) {

  float radius  = min(width, height) * 0.5;
  float divLat  = random(10.0, 20.0);
  int   pCntMax = floor(map(divLat, 10.0, 20.0, 20000, 10000));

  noStroke();
  fill(_initHue % 360.0, 5.0, 90.0, 100.0);
  sphere(radius);

  // latitude from 0 to 180 degrees
  for (float lat = 15.0; lat <= 100.0; lat += divLat) {
    float initLat = radians(lat);
    float divLon = 180 / constrain(((180 / divLat) * sin(initLat)), 0.5, 180.0 / divLat);

    // longitude from -50 to 130 degrees
    for (float lon = -50.0; lon <= 130.0; lon += divLon) {

      float initLon = radians(lon);
      float radLat  = initLat;
      float radLon  = initLon;

      float sHue    = _initHue + (((radLat + radLon) * 60.0) % 30.0) * 2.5;
      float sSat    = noise(radLat * 2.0, radLon * 2.0) * 60.0 + 20.0;
      float sBri    = noise(radLon * 2.0, radLat * 2.0) * 30.0 + 50.0;
      float baseSiz = random(2.0, 10.0);

      noStroke();
      fill(sHue % 360.0, sSat, sBri, 10.0);
      for (int pCnt = 0; pCnt < pCntMax; pCnt++) {

        float pCntRatio = map(pCnt, 0, pCntMax, 0.0, 1.0);

        float prevLat = radLat;
        float prevLon = radLon;

        radLat += random(-0.001, 0.001) * PI;
        radLon += random(-0.001, 0.001) * TWO_PI;
        radLat = radLat % PI;
        radLon = radLon % TWO_PI;
       
        float cX = radius * cos(radLon) * sin(radLat);
        float cY = radius * sin(radLon) * sin(radLat);
        float cZ = radius * cos(radLat);
     
        pushMatrix();
        translate(cX, cY, cZ);
        // must be Z -> Y 
        rotateZ(radLon);
        rotateY(radLat);
        float eSiz = 0.0 + sin(PI * pCntRatio) * baseSiz;
        ellipse(0.0, 0.0, eSiz, eSiz);     
        popMatrix();

      }
    }
  } 
}


/*
Copyright (C) 2020- deconbatch

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/







No comments :

Post a Comment