Skip to content

Instantly share code, notes, and snippets.

@CharStiles
Created February 5, 2019 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CharStiles/58e1a1803a1b90480e0f6758a857d31c to your computer and use it in GitHub Desktop.
Save CharStiles/58e1a1803a1b90480e0f6758a857d31c to your computer and use it in GitHub Desktop.
#version 150
uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;
uniform sampler2D noise2;
uniform sampler2D noise1;
uniform sampler2D texture0;
uniform sampler2D texture4;
uniform sampler2D midiIn;
uniform sampler2D prevFrame;
uniform sampler2D prevPass;
uniform float spawn;
uniform float percentOfScreen;
uniform float magicDiv;
uniform float mouseCol;
uniform int frameNum;
float _nx = resolution.x *(percentOfScreen/100); // pixels wid
float _ny = resolution.y *(percentOfScreen/100);
in VertexData
{
vec4 v_position;
vec3 v_normal;
vec2 v_texcoord;
} inData;
out vec4 fragColor;
#ifdef GL_ES
precision mediump float;
#endif
float terrain( vec3 p, float height, float offset )
{
float n = texture(noise1, vec2(-1)*p.xz/1000).x;
float nn = texture(texture4, vec2(-1)*p.xz/10 +(n*0.1)).x;
return p.y -( (nn)*0.01);
}
float sphere( vec3 p, float radius )
{
float nn = texture(texture4, p.xy ).x;
return length( p ) - (radius + (nn*0.1));
}
float map( vec3 p )
{
float c = 8;
float offset = mod(p.y,17);
float offset2 = mod(p.y,0);
p = vec3(p.x,offset,p.z);
return terrain( p,1, offset);
}
vec3 getNormal( vec3 p )
{
vec3 e = vec3( 0.001, 0.00, 0.00 );
float deltaX = map( p + e.xyy ) - map( p - e.xyy );
float deltaY = map( p + e.yxy ) - map( p - e.yxy );
float deltaZ = map( p + e.yyx ) - map( p - e.yyx );
vec3 ret = normalize( vec3( deltaX, deltaY, deltaZ ));
return ret;
}
float trace( vec3 origin, vec3 direction, out vec3 p )
{
float totalDistanceTraveled = 0.0;
for( int i=0; i <32; ++i)
{
p = origin + direction * totalDistanceTraveled;
// distance traveled from our current position along
float distanceFromPointOnRayToClosestObjectInScene = map( p );
totalDistanceTraveled += distanceFromPointOnRayToClosestObjectInScene;
if( distanceFromPointOnRayToClosestObjectInScene < 0.00000001 )
{
break;
}
if( totalDistanceTraveled > 30.0 )// make bigger for mo blobby effect
{
totalDistanceTraveled = 0.0000;
break;
}
}
if (totalDistanceTraveled < 10){
return 0;
}
return totalDistanceTraveled;
}
//-----------------------------------------------------------------------------------------------
// Standard Blinn lighting model.
// This model computes the diffuse and specular components of the final surface color.
vec3 calculateLighting(vec3 pointOnSurface, vec3 surfaceNormal, vec3 lightPosition, vec3 cameraPosition)
{
vec3 fromPointToLight = normalize(lightPosition - pointOnSurface) *2;
float diffuseStrength = pow(clamp( dot( surfaceNormal, fromPointToLight ), 0.0, 1.0 ),1.3);
vec4 midiColor = texture(midiIn, pointOnSurface.xy);
vec3 diffuseColor = diffuseStrength * vec3(0.6,0.6+(0.9) ,0.);
vec3 reflectedLightVector = normalize( reflect( -fromPointToLight, surfaceNormal ) );
vec3 fromPointToCamera = normalize( cameraPosition - pointOnSurface );
float specularStrength = pow( clamp( dot(reflectedLightVector, fromPointToCamera), 0.0, 1.0 ), 10.0 );
// Ensure that there is no specular lighting when there is no diffuse lighting.
specularStrength = min( diffuseStrength, specularStrength );
vec3 specularColor = vec3(specularStrength);
vec3 finalColor = diffuseColor + specularColor;
return surfaceNormal;
}
void main( void )
{
vec2 uv = ( gl_FragCoord.xy / resolution.xy ) * 2.0 - 1.;
uv.x *= resolution.x / resolution.y;
vec2 uv2 = gl_FragCoord.xy / resolution.xy;
uv2.x *= resolution.x/resolution.y;
vec2 cellunit = (vec2(_nx,_ny));
vec3 cameraPosition = vec3( time-3.0,10, time*0.2 );
//REMEBER TO REST frameNUM
// cameraPosition = vec3(20.0,50. - frameNum/10. - spectrum.x, -50+cos(time) );
vec3 cameraDirection = normalize( vec3( uv.x, uv.y -9 ,3) ); // perp to image plane
// y makes ya look down x dont change z that is projection
vec3 pointOnSurface;
float distanceToClosestPointInScene = trace( cameraPosition, cameraDirection, pointOnSurface );
vec3 finalColor = vec3(0.0);
if( distanceToClosestPointInScene > 0.10 )
{
vec3 lightPosition = cameraPosition;
vec3 surfaceNormal = getNormal( pointOnSurface );
finalColor = calculateLighting( pointOnSurface, surfaceNormal, lightPosition, cameraPosition );
}
else{
finalColor = mix(texture(prevFrame,inData.v_texcoord).gbr * 10,texture(prevFrame,inData.v_texcoord).bgr, (sin(time*(10*uv.y))+1)/2);
}
fragColor = vec4(finalColor,1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment