Nebula Shader from Examples (What causes the 'zoom')

edited December 2016 in GLSL / Shaders

I was toying around with the nebula shader example and specifically the nebula.glsl file.

The code is essentially made up of a nebula portion and stars portion. I have questions on the nebula portion and so have commented out the stars portion.

I'm only looking at just one of the nebula that is created (there are actually two in the original example). I stopped the rotation and have been adjusting some of the parameters. I'm trying to find out what is it that seems to be making the nebula zoom in and out. I don't want it to do that. However, I am having trouble pinpointing what is it that is making this zoom effect. Any help would be appreciated.

//Utility functions

vec3 fade(vec3 t) {
  return vec3(1.0,1.0,1.0);//t*t*t*(t*(t*6.0-15.0)+10.0);
}

vec2 rotate(vec2 point, float rads) {
    float cs = cos(rads);
    float sn = sin(rads);
    return point * mat2(cs, -sn, sn, cs);
}   

vec4 randomizer4(const vec4 x)
{
    vec4 z = mod(x, vec4(5612.0));
    z = mod(z, vec4(3.1415927 * 2.0));
    return(fract(cos(z) * vec4(56812.5453)));
}

// Fast computed noise
// http://www.gamedev.net/topic/502913-fast-computed-noise/

const float A = 1.0;
const float B = 57.0;
const float C = 113.0;
const vec3 ABC = vec3(A, B, C);
const vec4 A3 = vec4(0, B, C, C+B);
const vec4 A4 = vec4(A, A+B, C+A, C+A+B);

float cnoise4(const in vec3 xx)
{
    vec3 x = mod(xx + 32768.0, 65536.0);
    vec3 ix =   floor(x);
    vec3 fx = fract(x); // returns fractional part of x.  
    //vec3 wx = vec3(0,0,0);
    //vec3 wx = vec3(0.0,-1.,1.0);
    //vec3 wx = fx; wx.y=0;

    vec3 wx = fx*fx*(3.0-2.0*fx);
    //vec3 wx = fx*fx*fx;
    //wx.x=0.0;
    float nn = dot(ix, ABC);

    vec4 N1 = nn + A3;
    vec4 N2 =  nn + A4;
    vec4 R1 = randomizer4(N1);
    vec4 R2 = randomizer4(N2);
    vec4 R = mix(R1, R2, wx.x);
    float re = mix(mix(R.x, R.y, wx.y), mix(R.z, R.w, wx.y), wx.z);

    return 1.0 - 2.0 * re;
}
float surface3 ( vec3 coord, float frequency ) {

    float n = 0.0;  

    n += 1.0    * abs( cnoise4( coord * frequency ) );
    n += 0.5    * abs( cnoise4( coord * frequency * 2.0 ) );
    n += 0.25   * abs( cnoise4( coord * frequency * 4.0 ) );
    n += 0.125  * abs( cnoise4( coord * frequency * 8.0 ) );
    n += 0.0625 * abs( cnoise4( coord * frequency * 16.0 ) );

    return n;
}

void main( void ) {
    float rads = radians(time*3.15);
    vec2 position = gl_FragCoord.xy / resolution.xy;
    //position += rotate(position, rads); // rotates everything slowly.  

    //float n = surface3(vec3(position*sin(time*0.1), time * 0.05)*mat3(1,0,0,0,.8,.6,0,-.6,.8),2.0); //.9  //layer one
    //float n = surface3(vec3(position*sin(time*0.1), time * 0.05) ,2.0); //.9  //layer one

    float n2 = surface3(vec3(position*cos(time*0.1), time * 0.04)*mat3(1,0,0,0,.8,.6,0,-.6,.8),6.0); // .8 // layer two
    //vec2 test = position*cos(time*0.1);
    //float n2 = surface3(vec3(test, time * 0.04) ,6.0); // .8 // layer two
    //float n2 = surface3(vec3(position*cos(time*0.1), 0) ,6); // .8 // layer two . z not affected

    //float n2 = test.x;

    //float lum = length(n);
    float lum2 =  length(n2); //length of a scalar float is abs value.

    //vec3 tc = pow(vec3(1.0-lum),vec3(sin(position.x)+cos(time)+4.0,8.0+sin(time)+4.0,8.0)); // layer one 
    vec3 tc2 = pow(vec3(1.1-lum2),vec3(5.0,position.y+cos(time)+7.0,sin(position.x)+sin(time)+2.0)); // layer 2
    //vec3 tc2 = pow(vec3(1.1-lum2,1.1-lum2,1.1-lum2),vec3(5.0,position.y+cos(time)+7.0,sin(position.x)+sin(time)+2.0)); // layer 2
    //vec3 tc2 = pow(vec3(1.1-lum2,1.1-lum2,1.1-lum2),vec3(5.0,7, 2.0)); // layer 2

    vec3 curr_color = /*(tc*0.8) */+ (tc2*0.5);
    //curr_color.z=0;
    curr_color =vec3(lum2, lum2, lum2); //makes black n white
    ///////////////////////////////////////////////end nebula...//////////////////////////////
    //Let's draw some stars

    float scale = sin(0.3 * time) + 20.0;
    vec2 position2 = (((gl_FragCoord.xy / resolution) - 0.5) * scale);
    float gradient = 0.0;
    //vec3 color = vec3(100.0); // not used
    //float fade = 0.0; // this fade is not used.  
    float z = 0.0;
    vec2 centered_coord = position2;// - vec2(sin(time*0.1),sin(time*0.1)); //what does this do?
    //vec2 centered_coord = position2 - vec2(sin(time*0.4),sin(time*0.4));  // it makes you move along x y plane back n forth.
    //centered_coord = rotate(centered_coord, rads); // rotates about z.  

    for (float i=1.0; i<=100.0; i++)
    {
        //vec2 star_pos = vec2(sin(i) * 250.0, sin(i*i*i) * 250.0);
        vec2 star_pos = vec2(sin(i) * 1000.0, sin(i*i*i) * 1000.0);

        //vec2 star_pos = vec2(sin(i) * 250.0, sin(i*i*i) * 1000.0);
        //vec2 star_pos = vec2( sin(i)*250.0, i  );
        //float z = mod(i*i - 50.0*time, 256.0); // x *time is speed.
        //float z = mod(i - 50.0*time, 256.0); // i is ?? but if not there..it will  .  
        //float z = mod(i*i*i*i*i - 10.0*time, 256.0); // i is ?? but if not there..it will  .  
        //float z = mod(i- 10.0*time, 1024.0); // i is ?? but if not there..it will  .  
        float z = mod(i*i- 10.0*time, 256.0); // lava lamp also happens here..  

        float fade = (256.0 - z) /256.0;
        //float fade = (1024.0 - z) /256.0; // cool effect almost lava lamp 
        vec2 blob_coord = star_pos / z;
        gradient += ((fade / 384.0) / pow(length(centered_coord - blob_coord), 1.5)) * ( fade);
    }

    //curr_color = vec3(0,0,0);// only does stars
    //curr_color += gradient; // if comment out, only does nebula

    gl_FragColor = vec4(curr_color, 1.0); // might be alpha
    //gl_FragColor = vec4(gradient, 1.0); // does not work
}

also providing the pde code

/**
 * Nebula. 
 * 
 * From CoffeeBreakStudios.com (CBS)
 * Ported from the webGL version in GLSL Sandbox:
 * http://glsl.heroku.com/e#3265.2
 */

PShader nebula;

void setup() {
  fullScreen( P2D);
  //size(500,500, P2D);
  noStroke();

  nebula = loadShader("nebula.glsl");
  nebula.set("resolution", float(width), float(height));
}

void draw() {
  nebula.set("time", millis() / 500.0);  
  shader(nebula); 
  // This kind of raymarching effects are entirely implemented in the
  // fragment shader, they only need a quad covering the entire view 
  // area so every pixel is pushed through the shader. 
  rect(0, 0, width, height);

  resetShader();
  text("fr: " + frameRate, width/2, 10);
}
Tagged:

Answers

  • found it.
    it was within the call to surface, by changing cos(arg) to cos(0) it stops the zooming.
    float n2 = surface3(vec3(position*cos(0), time * 0.04)*mat3(1,0,0,0,.8,.6,0,-.6,.8),6.0); // .8 // layer two

  • cos(0) is always 1 btw

Sign In or Register to comment.