 // console commands
 // 'xs height'
 // shader inputs
 // uniform vec3        iVolumeSize;    // volume size [1-126]
 // uniform float    iColorIndex;    // current color index [1-255]
 // uniform vec3        iMirror;    // current mirror mode [0-1]
 // uniform vec3        iAxis;        // current axis mode [0-1]
 // uniform float     iFrame;        // current anim frame index
 // uniform float    iNumFrames;    // total num of anim frames
 // uniform float    iIter;        // current iteration index
 // uniform vec4        iRand;        // random number
 // uniform float    iArgs[8];    // user args
 // built-in functions
 // float voxel( vec3 v );        // get voxel color index at position v
 // generate new voxel color index at position v

 float noise(int x, int y){
  x %= 25;
  y %= 25;
  int n = x + y * 57;
  n = (n<<13) ^ n;
  float o=1.0f-(float((n*(n*n*int(iArgs[0]) + 789221 ) + 1376312589 ) & 0xFFFFFFFF)/1073741824.0f);
  return o;
}

float snoise(int x, int y){
  float co = (noise(x-1,y-1)+noise(x+1,y-1)+noise(x-1,y+1)+noise(x+1,y+1))/16.0f;
  float si = (noise(x-1,y)+noise(x+1,y)+noise(x,y-1)+noise(x,y+1))/8.0f;
  float ce = noise(x,y)/4.0f;
  return co+si+ce;
}

float cosini(float a, float b, float x){
  float f=(1-cos(x*3.1415927))*0.5f;
  return a*(1-f)+b*f;
}

float inno(float x, float y){
  int intX=int(x);
  float fraX=x-intX;
  int intY=int(y);
  float fraY=y-intY;
  float i1=cosini(snoise(intX,intY),snoise(intX+1,intY),fraX);
  float i2=cosini(snoise(intX,intY+1),snoise(intX+1,intY+1),fraX);
  return cosini(i1,i2,fraY);
}

float pernoi(float x, float y){
  float total=0.;
  for(int i=0;i<=20;i++){
    float freq=pow(float(2),i);
    total+=inno(x*freq,y*freq)*pow(0.45f,i);
  }
  return total;
}

float map( vec3 v ) {
    float height = 255.0 - voxel( vec3( v.xy, 0.0 ) ); 
    height = (height * 0.5f - 1.0)/8 + 16;
    float extra = pernoi(0.08f*v.x,0.08f*v.y);
    if (extra>2){
        height = height + 1;
    }
    return ( v.z <= height ? iColorIndex : 0.0 );
 }