aouellets of Academic Technical Team Code Base
5/8/2018 - 2:18 PM

hologram shader

ktm holographic shader

<script name="hologram" type="x-shader/x-vertex">
  attribute vec3 vertexPosition; 
  attribute vec2 vertexTexCoord;
			
  varying vec2 texcoord;
  uniform mat4 modelViewProjectionMatrix;
			  
  void main() { 
    vec4 vp     = vec4(vertexPosition, 1.0);
    gl_Position=vec4(modelViewProjectionMatrix * vp);
    texcoord = vertexTexCoord;
  }
</script>
<script name="hologram" type="x-shader/x-fragment">
  precision mediump float;
  uniform sampler2D tex0;
  uniform float tick;
  varying vec2 texcoord;

#define ITERS 64
#define CLOCK 0.1

vec3 rotateX(float a, vec3 v)
{
	return vec3(v.x, cos(a) * v.y + sin(a) * v.z, cos(a) * v.z - sin(a) * v.y);
}


vec3 rotateY(float a, vec3 v)
{
	return vec3(cos(a) * v.x + sin(a) * v.z, v.y, cos(a) * v.z - sin(a) * v.x);
	
}

// Returns the entry and exit points along the given ray with
// a cube of edge length 2 centered at the origin.
vec2 cubeInterval(vec3 ro, vec3 rd)
{
	vec3 slabs0 = (vec3(+1.0) - ro) / rd;
	vec3 slabs1 = (vec3(-1.0) - ro) / rd;
	
	vec3 mins = min(slabs0, slabs1);
	vec3 maxs = max(slabs0, slabs1);
	
	return vec2(max(max(mins.x, mins.y), mins.z),				
				min(min(maxs.x, maxs.y), maxs.z));
	
}

vec2 hologramInterval(vec3 ro, vec3 rd)
{
	vec3 scale = vec3(1.0, 1.0, 0.1);   
	return cubeInterval(ro / scale, rd / scale);
}

float hologramBrightness(vec2 p)
{
	return dot(texture2D(tex0, p).rgb, vec3(1.0 / 3.0));
}

float flicker(float x)
{
	x = fract(x);
	return smoothstep(0.0, 0.1, x) - smoothstep(0.1, 0.2, x);
}

float flickers()
{
  float itick =  0. - mod(CLOCK*tick,6.);
  return 1.0 + flicker(itick) + flicker(itick* 1.2);
}

vec3 hologramImage(vec2 p)
{
        vec2 tc = p;
	float d = 1e-3;
	
	float b0 = hologramBrightness(tc);
	float b1 = (hologramBrightness(tc + vec2(d, 0.0)) - b0) / d;
	float b2 = (hologramBrightness(tc + vec2(0.0, d)) - b0) / d;
	

	float f = flickers();
	float sharp = pow(length(vec2(b1, b2)) * 0.1, 5.0) * 0.02;
	float itick = 0. - mod(CLOCK*tick, 6.);
	return (vec3(sharp + b0) * 3.0) * vec3(0.5, 0.7, 1.0) *
				mix(0.5, 0.9, pow(0.5 + 0.5 * cos(p.y * 80.0 + itick * 70.0), 4.0)) * f *
		(2.0 - tc.y * 2.0 + (1.0 - smoothstep(0.0, 0.1, tc.y)) * 10.0);
}

vec2 rotate(float a, vec2 v)
{
	return vec2(cos(a) * v.x + sin(a) * v.y,
				cos(a) * v.y - sin(a) * v.x);
}

void main()
{  
	vec2 uv = texcoord;
        vec4 img = texture2D(tex0, uv);

	vec3 accum = hologramImage(uv);	

	gl_FragColor.rgb = accum * img.rgb;
	gl_FragColor.a = 0.5 * img.a;

}
</script>