varying vec3 center;
varying vec3 offset;

varying float radius;
varying vec4 eyePosition;

varying float depth;

uniform mat4 pm;
uniform mat4 mvm;

const float boxCorrection = 1.0;


uniform float offsetFade;

varying float doOffsetFade;

uniform int time;


uniform float largeBrownian = 0.0;
uniform float smallBrownian = 0.0;
uniform float brownianSpeed = 0.5;
uniform float brownian = 0.1;

void main()
{
   
  doOffsetFade = gl_Normal.z;

  brownian *= pow(1.0 - offsetFade * doOffsetFade, 0.2);

  time *= brownianSpeed;

  gl_Vertex.z += sin(gl_Vertex.x * 1.7 + time * 0.001) * 0.08 * brownian * largeBrownian;
  gl_Vertex.z += sin(gl_Vertex.y * 1.67 + time * 0.0013) * 0.08 * brownian * largeBrownian;
  gl_Vertex.z += sin(gl_Vertex.x * 7.4 + time * 0.004) * 0.01 * brownian;
  gl_Vertex.z += sin(gl_Vertex.y * 8.67 + time * 0.0022) * 0.01 * brownian;
  gl_Vertex.z += sin(gl_Vertex.x * 23.4 + time * 0.008) * 0.001 * brownian;
  gl_Vertex.z += sin(gl_Vertex.y * 54.67 + time * 0.0091) * 0.001 * brownian;


  gl_Vertex.x += sin(gl_Vertex.x * 15.6 + time * 0.0076) * 0.004 * brownian;
  gl_Vertex.x += sin(gl_Vertex.y * 11.3 + time * 0.0035) * 0.004 * brownian;
  gl_Vertex.x += sin(gl_Vertex.x * 165.6 + time * 0.0021) * 0.006 * brownian;
  gl_Vertex.x += sin(gl_Vertex.y * 134.3 + time * 0.0076) * 0.006 * brownian;

  gl_Vertex.y += sin(gl_Vertex.x * 11.7 + time * 0.0024) * 0.004 * brownian;
  gl_Vertex.y += sin(gl_Vertex.y * 18.4 + time * 0.0076) * 0.004 * brownian;
  gl_Vertex.y += sin(gl_Vertex.x * 123.4 + time * 0.0096) * 0.006 * brownian;
  gl_Vertex.y += sin(gl_Vertex.y * 176.7 + time * 0.0034) * 0.006 * brownian;
  
  
  
  gl_Vertex.x += sin(gl_Vertex.x * 265.4 + time * 0.0086) * 0.002 * brownian * smallBrownian;
  gl_Vertex.x += sin(gl_Vertex.y * 145.7 + time * 0.0064) * 0.002 * brownian * smallBrownian;
  gl_Vertex.y += sin(gl_Vertex.x * 161.4 + time * 0.0069) * 0.002 * brownian * smallBrownian;
  gl_Vertex.y += sin(gl_Vertex.y * 357.7 + time * 0.0096) * 0.002 * brownian * smallBrownian;
  gl_Vertex.z += sin(gl_Vertex.x * 324.4 + time * 0.0078) * 0.002 * brownian * smallBrownian;
  gl_Vertex.z += sin(gl_Vertex.y * 413.7 + time * 0.0037) * 0.002 * brownian * smallBrownian;









	float r = gl_Normal.x;  
	radius = r;

	if (gl_Normal.y == 0.0)
		offset = vec3(-1, -1, 0);
	if (gl_Normal.y == 0.1)
		offset = vec3(+1, -1, 0);
	if (gl_Normal.y == 0.2)
		offset = vec3(+1, +1, 0);
	if (gl_Normal.y == 0.3)
		offset = vec3(-1, +1, 0);
    
    
	offset *= boxCorrection;
	eyePosition = mvm * gl_Vertex;

	depth = eyePosition.z;
  
  offset.xyz = mix(offset.xyz, offset.zyx, offsetFade * doOffsetFade);
                                                      
	gl_Position = eyePosition;
	gl_Position.xyz += offset * radius;
	gl_Position = pm * gl_Position;
  
	gl_FrontColor = gl_Color;

}