REVIEWED: Shaders formating to follow raylib code conventions

This commit is contained in:
Ray
2025-08-11 20:22:31 +02:00
parent 8dae39fbda
commit 9b598f6bcf
62 changed files with 797 additions and 797 deletions

View File

@ -21,8 +21,8 @@ vec4 PostFX(sampler2D tex, vec2 uv)
{
vec4 c = vec4(0.0);
float size = stitchingSize;
vec2 cPos = uv * vec2(renderWidth, renderHeight);
vec2 tlPos = floor(cPos / vec2(size, size));
vec2 cPos = uv*vec2(renderWidth, renderHeight);
vec2 tlPos = floor(cPos/vec2(size, size));
tlPos *= size;
int remX = int(mod(cPos.x, size));
@ -36,11 +36,11 @@ vec4 PostFX(sampler2D tex, vec2 uv)
if ((remX == remY) || (((int(cPos.x) - int(blPos.x)) == (int(blPos.y) - int(cPos.y)))))
{
if (invert == 1) c = vec4(0.2, 0.15, 0.05, 1.0);
else c = texture2D(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4;
else c = texture2D(tex, tlPos*vec2(1.0/renderWidth, 1.0/renderHeight))*1.4;
}
else
{
if (invert == 1) c = texture2D(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4;
if (invert == 1) c = texture2D(tex, tlPos*vec2(1.0/renderWidth, 1.0/renderHeight))*1.4;
else c = vec4(0.0, 0.0, 0.0, 1.0);
}

View File

@ -15,22 +15,22 @@ const float PI = 3.1415926535;
void main()
{
float aperture = 178.0;
float apertureHalf = 0.5 * aperture * (PI / 180.0);
float apertureHalf = 0.5*aperture*(PI/180.0);
float maxFactor = sin(apertureHalf);
vec2 uv = vec2(0.0);
vec2 xy = 2.0 * fragTexCoord.xy - 1.0;
vec2 xy = 2.0*fragTexCoord.xy - 1.0;
float d = length(xy);
if (d < (2.0 - maxFactor))
{
d = length(xy * maxFactor);
float z = sqrt(1.0 - d * d);
float r = atan(d, z) / PI;
d = length(xy*maxFactor);
float z = sqrt(1.0 - d*d);
float r = atan(d, z)/PI;
float phi = atan(xy.y, xy.x);
uv.x = r * cos(phi) + 0.5;
uv.y = r * sin(phi) + 0.5;
uv.x = r*cos(phi) + 0.5;
uv.y = r*sin(phi) + 0.5;
}
else
{

View File

@ -38,7 +38,7 @@ void main()
vec3 viewD = normalize(viewPos - fragPosition);
vec3 specular = vec3(0.0);
vec4 tint = colDiffuse * fragColor;
vec4 tint = colDiffuse*fragColor;
// NOTE: Implement here your fragment shader code

View File

@ -16,5 +16,5 @@ void main()
vec4 texelColor = texture2D(texture0, fragTexCoord);
vec4 texelColor2 = texture2D(texture1, fragTexCoord2);
gl_FragColor = texelColor * texelColor2;
gl_FragColor = texelColor*texelColor2;
}

View File

@ -32,21 +32,21 @@ void main()
normal = texture(normalMap, vec2(fragTexCoord.x, fragTexCoord.y)).rgb;
//Transform normal values to the range -1.0 ... 1.0
normal = normalize(normal * 2.0 - 1.0);
normal = normalize(normal*2.0 - 1.0);
//Transform the normal from tangent-space to world-space for lighting calculation
normal = normalize(normal * TBN);
normal = normalize(normal*TBN);
}
else
{
normal = normalize(fragNormal);
}
vec4 tint = colDiffuse * fragColor;
vec4 tint = colDiffuse*fragColor;
vec3 lightColor = vec3(1.0, 1.0, 1.0);
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 lightDot = lightColor * NdotL;
vec3 lightDot = lightColor*NdotL;
float specCo = 0.0;
@ -54,9 +54,9 @@ void main()
specular += specCo;
finalColor = (texelColor * ((tint + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
finalColor += texelColor * (vec4(1.0, 1.0, 1.0, 1.0) / 40.0) * tint;
finalColor = (texelColor*((tint + vec4(specular, 1.0))*vec4(lightDot, 1.0)));
finalColor += texelColor*(vec4(1.0, 1.0, 1.0, 1.0)/40.0)*tint;
// Gamma correction
gl_FragColor = pow(finalColor, vec4(1.0 / 2.2));
gl_FragColor = pow(finalColor, vec4(1.0/2.2));
}

View File

@ -27,15 +27,15 @@ mat3 inverse(mat3 m)
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;
float b01 = a22*a11 - a12*a21;
float b11 = -a22*a10 + a12*a20;
float b21 = a21*a10 - a11*a20;
float det = a00 * b01 + a01 * b11 + a02 * b21;
float det = a00*b01 + a01*b11 + a02*b21;
return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11),
b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10),
b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det;
}
// https://github.com/glslify/glsl-transpose
@ -49,21 +49,21 @@ mat3 transpose(mat3 m)
void main()
{
// Compute binormal from vertex normal and tangent. W component is the tangent handedness
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz) * vertexTangent.w;
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz)*vertexTangent.w;
// Compute fragment normal based on normal transformations
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
// Compute fragment position based on model transformations
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
//Create TBN matrix for transforming the normal map values from tangent-space to world-space
fragNormal = normalize(normalMatrix * vertexNormal);
fragNormal = normalize(normalMatrix*vertexNormal);
vec3 fragTangent = normalize(normalMatrix * vertexTangent.xyz);
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal) * fragNormal);
vec3 fragTangent = normalize(normalMatrix*vertexTangent.xyz);
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal);
vec3 fragBinormal = normalize(normalMatrix * vertexBinormal);
vec3 fragBinormal = normalize(normalMatrix*vertexBinormal);
fragBinormal = cross(fragNormal, fragTangent);
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
@ -72,5 +72,5 @@ void main()
fragTexCoord = vertexTexCoord;
gl_Position = mvp * vec4(vertexPosition, 1.0);
gl_Position = mvp*vec4(vertexPosition, 1.0);
}

View File

@ -13,15 +13,15 @@ uniform ivec3 palette[colors];
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture(texture0, fragTexCoord) * fragColor;
vec4 texelColor = texture(texture0, fragTexCoord)*fragColor;
// Convert the (normalized) texel color RED component (GB would work, too)
// to the palette index by scaling up from [0, 1] to [0, 255].
int index = int(texelColor.r * 255.0);
// to the palette index by scaling up from [0, 1] to [0, 255]
int index = int(texelColor.r*255.0);
ivec3 color = palette[index];
// Calculate final fragment color. Note that the palette color components
// are defined in the range [0, 255] and need to be normalized to [0, 1]
// for OpenGL to work.
gl_FragColor = vec4(color / 255.0, texelColor.a);
// for OpenGL to work
gl_FragColor = vec4(color/255.0, texelColor.a);
}

View File

@ -52,7 +52,7 @@ mat3 transpose(mat3 m)
void main()
{
// Compute binormal from vertex normal and tangent
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz) * vertexTangent.w;
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz)*vertexTangent.w;
// Compute fragment normal based on normal transformations
mat3 normalMatrix = transpose(inverse(mat3(matModel)));

View File

@ -30,7 +30,7 @@ uniform vec2 resolution;
// SOFTWARE.
// A list of useful distance function to simple primitives, and an example on how to
// do some interesting boolean operations, repetition and displacement.
// do some interesting boolean operations, repetition and displacement
//
// More info here: http://www.iquilezles.org/www/articles/distfunctions/distfunctions.htm
@ -38,38 +38,38 @@ uniform vec2 resolution;
//------------------------------------------------------------------
float sdPlane( vec3 p )
float sdPlane(vec3 p)
{
return p.y;
}
float sdSphere( vec3 p, float s )
float sdSphere(vec3 p, float s)
{
return length(p)-s;
}
float sdBox( vec3 p, vec3 b )
float sdBox(vec3 p, vec3 b)
{
vec3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
float sdEllipsoid( in vec3 p, in vec3 r )
float sdEllipsoid(in vec3 p, in vec3 r)
{
return (length( p/r ) - 1.0) * min(min(r.x,r.y),r.z);
return (length(p/r) - 1.0)*min(min(r.x,r.y),r.z);
}
float udRoundBox( vec3 p, vec3 b, float r )
float udRoundBox(vec3 p, vec3 b, float r)
{
return length(max(abs(p)-b,0.0))-r;
}
float sdTorus( vec3 p, vec2 t )
float sdTorus(vec3 p, vec2 t)
{
return length( vec2(length(p.xz)-t.x,p.y) )-t.y;
return length(vec2(length(p.xz)-t.x,p.y))-t.y;
}
float sdHexPrism( vec3 p, vec2 h )
float sdHexPrism(vec3 p, vec2 h)
{
vec3 q = abs(p);
#if 0
@ -81,24 +81,24 @@ float sdHexPrism( vec3 p, vec2 h )
#endif
}
float sdCapsule( vec3 p, vec3 a, vec3 b, float r )
float sdCapsule(vec3 p, vec3 a, vec3 b, float r)
{
vec3 pa = p-a, ba = b-a;
float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length( pa - ba*h ) - r;
float h = clamp(dot(pa,ba)/dot(ba,ba), 0.0, 1.0);
return length(pa - ba*h) - r;
}
float sdEquilateralTriangle( in vec2 p )
float sdEquilateralTriangle( in vec2 p)
{
const float k = sqrt(3.0);
p.x = abs(p.x) - 1.0;
p.y = p.y + 1.0/k;
if( p.x + k*p.y > 0.0 ) p = vec2( p.x - k*p.y, -k*p.x - p.y )/2.0;
p.x += 2.0 - 2.0*clamp( (p.x+2.0)/2.0, 0.0, 1.0 );
if (p.x + k*p.y > 0.0) p = vec2(p.x - k*p.y, -k*p.x - p.y)/2.0;
p.x += 2.0 - 2.0*clamp((p.x+2.0)/2.0, 0.0, 1.0);
return -length(p)*sign(p.y);
}
float sdTriPrism( vec3 p, vec2 h )
float sdTriPrism(vec3 p, vec2 h)
{
vec3 q = abs(p);
float d1 = q.z-h.y;
@ -113,95 +113,95 @@ float sdTriPrism( vec3 p, vec2 h )
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
}
float sdCylinder( vec3 p, vec2 h )
float sdCylinder(vec3 p, vec2 h)
{
vec2 d = abs(vec2(length(p.xz),p.y)) - h;
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdCone( in vec3 p, in vec3 c )
float sdCone(in vec3 p, in vec3 c)
{
vec2 q = vec2( length(p.xz), p.y );
vec2 q = vec2(length(p.xz), p.y);
float d1 = -q.y-c.z;
float d2 = max( dot(q,c.xy), q.y);
float d2 = max(dot(q,c.xy), q.y);
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
}
float sdConeSection( in vec3 p, in float h, in float r1, in float r2 )
float sdConeSection(in vec3 p, in float h, in float r1, in float r2)
{
float d1 = -p.y - h;
float q = p.y - h;
float si = 0.5*(r1-r2)/h;
float d2 = max( sqrt( dot(p.xz,p.xz)*(1.0-si*si)) + q*si - r2, q );
float d2 = max(sqrt(dot(p.xz,p.xz)*(1.0-si*si)) + q*si - r2, q);
return length(max(vec2(d1,d2),0.0)) + min(max(d1,d2), 0.);
}
float sdPryamid4(vec3 p, vec3 h ) // h = { cos a, sin a, height }
float sdPryamid4(vec3 p, vec3 h) // h = { cos a, sin a, height }
{
// Tetrahedron = Octahedron - Cube
float box = sdBox( p - vec3(0,-2.0*h.z,0), vec3(2.0*h.z) );
float box = sdBox(p - vec3(0,-2.0*h.z,0), vec3(2.0*h.z));
float d = 0.0;
d = max( d, abs( dot(p, vec3( -h.x, h.y, 0 )) ));
d = max( d, abs( dot(p, vec3( h.x, h.y, 0 )) ));
d = max( d, abs( dot(p, vec3( 0, h.y, h.x )) ));
d = max( d, abs( dot(p, vec3( 0, h.y,-h.x )) ));
d = max(d, abs(dot(p, vec3(-h.x, h.y, 0))));
d = max(d, abs(dot(p, vec3( h.x, h.y, 0))));
d = max(d, abs(dot(p, vec3( 0, h.y, h.x))));
d = max(d, abs(dot(p, vec3( 0, h.y,-h.x))));
float octa = d - h.z;
return max(-box,octa); // Subtraction
}
float length2( vec2 p )
float length2(vec2 p)
{
return sqrt( p.x*p.x + p.y*p.y );
return sqrt(p.x*p.x + p.y*p.y);
}
float length6( vec2 p )
float length6(vec2 p)
{
p = p*p*p; p = p*p;
return pow( p.x + p.y, 1.0/6.0 );
return pow(p.x + p.y, 1.0/6.0);
}
float length8( vec2 p )
float length8(vec2 p)
{
p = p*p; p = p*p; p = p*p;
return pow( p.x + p.y, 1.0/8.0 );
return pow(p.x + p.y, 1.0/8.0);
}
float sdTorus82( vec3 p, vec2 t )
float sdTorus82(vec3 p, vec2 t)
{
vec2 q = vec2(length2(p.xz)-t.x,p.y);
return length8(q)-t.y;
}
float sdTorus88( vec3 p, vec2 t )
float sdTorus88(vec3 p, vec2 t)
{
vec2 q = vec2(length8(p.xz)-t.x,p.y);
return length8(q)-t.y;
}
float sdCylinder6( vec3 p, vec2 h )
float sdCylinder6(vec3 p, vec2 h)
{
return max( length6(p.xz)-h.x, abs(p.y)-h.y );
return max(length6(p.xz)-h.x, abs(p.y)-h.y);
}
//------------------------------------------------------------------
float opS( float d1, float d2 )
float opS(float d1, float d2)
{
return max(-d2,d1);
}
vec2 opU( vec2 d1, vec2 d2 )
vec2 opU(vec2 d1, vec2 d2)
{
return (d1.x<d2.x) ? d1 : d2;
}
vec3 opRep( vec3 p, vec3 c )
vec3 opRep(vec3 p, vec3 c)
{
return mod(p,c)-0.5*c;
}
vec3 opTwist( vec3 p )
vec3 opTwist(vec3 p)
{
float c = cos(10.0*p.y+10.0);
float s = sin(10.0*p.y+10.0);
@ -211,110 +211,110 @@ vec3 opTwist( vec3 p )
//------------------------------------------------------------------
vec2 map( in vec3 pos )
vec2 map(in vec3 pos)
{
vec2 res = opU( vec2( sdPlane( pos), 1.0 ),
vec2( sdSphere( pos-vec3( 0.0,0.25, 0.0), 0.25 ), 46.9 ) );
res = opU( res, vec2( sdBox( pos-vec3( 1.0,0.25, 0.0), vec3(0.25) ), 3.0 ) );
res = opU( res, vec2( udRoundBox( pos-vec3( 1.0,0.25, 1.0), vec3(0.15), 0.1 ), 41.0 ) );
res = opU( res, vec2( sdTorus( pos-vec3( 0.0,0.25, 1.0), vec2(0.20,0.05) ), 25.0 ) );
res = opU( res, vec2( sdCapsule( pos,vec3(-1.3,0.10,-0.1), vec3(-0.8,0.50,0.2), 0.1 ), 31.9 ) );
res = opU( res, vec2( sdTriPrism( pos-vec3(-1.0,0.25,-1.0), vec2(0.25,0.05) ),43.5 ) );
res = opU( res, vec2( sdCylinder( pos-vec3( 1.0,0.30,-1.0), vec2(0.1,0.2) ), 8.0 ) );
res = opU( res, vec2( sdCone( pos-vec3( 0.0,0.50,-1.0), vec3(0.8,0.6,0.3) ), 55.0 ) );
res = opU( res, vec2( sdTorus82( pos-vec3( 0.0,0.25, 2.0), vec2(0.20,0.05) ),50.0 ) );
res = opU( res, vec2( sdTorus88( pos-vec3(-1.0,0.25, 2.0), vec2(0.20,0.05) ),43.0 ) );
res = opU( res, vec2( sdCylinder6( pos-vec3( 1.0,0.30, 2.0), vec2(0.1,0.2) ), 12.0 ) );
res = opU( res, vec2( sdHexPrism( pos-vec3(-1.0,0.20, 1.0), vec2(0.25,0.05) ),17.0 ) );
res = opU( res, vec2( sdPryamid4( pos-vec3(-1.0,0.15,-2.0), vec3(0.8,0.6,0.25) ),37.0 ) );
res = opU( res, vec2( opS( udRoundBox( pos-vec3(-2.0,0.2, 1.0), vec3(0.15),0.05),
sdSphere( pos-vec3(-2.0,0.2, 1.0), 0.25)), 13.0 ) );
res = opU( res, vec2( opS( sdTorus82( pos-vec3(-2.0,0.2, 0.0), vec2(0.20,0.1)),
sdCylinder( opRep( vec3(atan(pos.x+2.0,pos.z)/6.2831, pos.y, 0.02+0.5*length(pos-vec3(-2.0,0.2, 0.0))), vec3(0.05,1.0,0.05)), vec2(0.02,0.6))), 51.0 ) );
res = opU( res, vec2( 0.5*sdSphere( pos-vec3(-2.0,0.25,-1.0), 0.2 ) + 0.03*sin(50.0*pos.x)*sin(50.0*pos.y)*sin(50.0*pos.z), 65.0 ) );
res = opU( res, vec2( 0.5*sdTorus( opTwist(pos-vec3(-2.0,0.25, 2.0)),vec2(0.20,0.05)), 46.7 ) );
res = opU( res, vec2( sdConeSection( pos-vec3( 0.0,0.35,-2.0), 0.15, 0.2, 0.1 ), 13.67 ) );
res = opU( res, vec2( sdEllipsoid( pos-vec3( 1.0,0.35,-2.0), vec3(0.15, 0.2, 0.05) ), 43.17 ) );
vec2 res = opU(vec2(sdPlane( pos), 1.0),
vec2(sdSphere( pos-vec3(0.0,0.25, 0.0), 0.25), 46.9));
res = opU(res, vec2(sdBox( pos-vec3(1.0,0.25, 0.0), vec3(0.25)), 3.0));
res = opU(res, vec2(udRoundBox( pos-vec3(1.0,0.25, 1.0), vec3(0.15), 0.1), 41.0));
res = opU(res, vec2(sdTorus( pos-vec3(0.0,0.25, 1.0), vec2(0.20,0.05)), 25.0));
res = opU(res, vec2(sdCapsule( pos,vec3(-1.3,0.10,-0.1), vec3(-0.8,0.50,0.2), 0.1 ), 31.9));
res = opU(res, vec2(sdTriPrism( pos-vec3(-1.0,0.25,-1.0), vec2(0.25,0.05)),43.5));
res = opU(res, vec2(sdCylinder( pos-vec3(1.0,0.30,-1.0), vec2(0.1,0.2)), 8.0));
res = opU(res, vec2(sdCone( pos-vec3(0.0,0.50,-1.0), vec3(0.8,0.6,0.3)), 55.0));
res = opU(res, vec2(sdTorus82( pos-vec3(0.0,0.25, 2.0), vec2(0.20,0.05)),50.0));
res = opU(res, vec2(sdTorus88( pos-vec3(-1.0,0.25, 2.0), vec2(0.20,0.05)),43.0));
res = opU(res, vec2(sdCylinder6(pos-vec3(1.0,0.30, 2.0), vec2(0.1,0.2)), 12.0));
res = opU(res, vec2(sdHexPrism( pos-vec3(-1.0,0.20, 1.0), vec2(0.25,0.05)),17.0));
res = opU(res, vec2(sdPryamid4( pos-vec3(-1.0,0.15,-2.0), vec3(0.8,0.6,0.25)),37.0));
res = opU(res, vec2(opS(udRoundBox( pos-vec3(-2.0,0.2, 1.0), vec3(0.15),0.05),
sdSphere( pos-vec3(-2.0,0.2, 1.0), 0.25)), 13.0));
res = opU(res, vec2(opS(sdTorus82( pos-vec3(-2.0,0.2, 0.0), vec2(0.20,0.1)),
sdCylinder( opRep(vec3(atan(pos.x+2.0,pos.z)/6.2831, pos.y, 0.02+0.5*length(pos-vec3(-2.0,0.2, 0.0))), vec3(0.05,1.0,0.05)), vec2(0.02,0.6))), 51.0));
res = opU(res, vec2(0.5*sdSphere( pos-vec3(-2.0,0.25,-1.0), 0.2) + 0.03*sin(50.0*pos.x)*sin(50.0*pos.y)*sin(50.0*pos.z), 65.0));
res = opU(res, vec2(0.5*sdTorus(opTwist(pos-vec3(-2.0,0.25, 2.0)),vec2(0.20,0.05)), 46.7));
res = opU(res, vec2(sdConeSection(pos-vec3(0.0,0.35,-2.0), 0.15, 0.2, 0.1), 13.67));
res = opU(res, vec2(sdEllipsoid(pos-vec3(1.0,0.35,-2.0), vec3(0.15, 0.2, 0.05)), 43.17));
return res;
}
vec2 castRay( in vec3 ro, in vec3 rd )
vec2 castRay(in vec3 ro, in vec3 rd)
{
float tmin = 0.2;
float tmax = 30.0;
#if 1
// bounding volume
float tp1 = (0.0-ro.y)/rd.y; if( tp1>0.0 ) tmax = min( tmax, tp1 );
float tp2 = (1.6-ro.y)/rd.y; if( tp2>0.0 ) { if( ro.y>1.6 ) tmin = max( tmin, tp2 );
else tmax = min( tmax, tp2 ); }
float tp1 = (0.0-ro.y)/rd.y; if (tp1>0.0) tmax = min(tmax, tp1);
float tp2 = (1.6-ro.y)/rd.y; if (tp2>0.0) { if (ro.y>1.6) tmin = max(tmin, tp2);
else tmax = min(tmax, tp2); }
#endif
float t = tmin;
float m = -1.0;
for( int i=0; i<64; i++ )
for (int i=0; i<64; i++)
{
float precis = 0.0005*t;
vec2 res = map( ro+rd*t );
if( res.x<precis || t>tmax ) break;
vec2 res = map(ro+rd*t);
if (res.x<precis || t>tmax) break;
t += res.x;
m = res.y;
}
if( t>tmax ) m=-1.0;
return vec2( t, m );
if (t>tmax) m=-1.0;
return vec2(t, m);
}
float calcSoftshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax )
float calcSoftshadow(in vec3 ro, in vec3 rd, in float mint, in float tmax)
{
float res = 1.0;
float t = mint;
for( int i=0; i<16; i++ )
for (int i=0; i<16; i++)
{
float h = map( ro + rd*t ).x;
res = min( res, 8.0*h/t );
t += clamp( h, 0.02, 0.10 );
if( h<0.001 || t>tmax ) break;
float h = map(ro + rd*t).x;
res = min(res, 8.0*h/t);
t += clamp(h, 0.02, 0.10);
if (h<0.001 || t>tmax) break;
}
return clamp( res, 0.0, 1.0 );
return clamp(res, 0.0, 1.0);
}
vec3 calcNormal( in vec3 pos )
vec3 calcNormal(in vec3 pos)
{
vec2 e = vec2(1.0,-1.0)*0.5773*0.0005;
return normalize( e.xyy*map( pos + e.xyy ).x +
e.yyx*map( pos + e.yyx ).x +
e.yxy*map( pos + e.yxy ).x +
e.xxx*map( pos + e.xxx ).x );
return normalize(e.xyy*map(pos + e.xyy).x +
e.yyx*map(pos + e.yyx).x +
e.yxy*map(pos + e.yxy).x +
e.xxx*map(pos + e.xxx).x);
/*
vec3 eps = vec3( 0.0005, 0.0, 0.0 );
vec3 eps = vec3(0.0005, 0.0, 0.0);
vec3 nor = vec3(
map(pos+eps.xyy).x - map(pos-eps.xyy).x,
map(pos+eps.yxy).x - map(pos-eps.yxy).x,
map(pos+eps.yyx).x - map(pos-eps.yyx).x );
map(pos+eps.yyx).x - map(pos-eps.yyx).x);
return normalize(nor);
*/
}
float calcAO( in vec3 pos, in vec3 nor )
float calcAO(in vec3 pos, in vec3 nor)
{
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
for (int i=0; i<5; i++)
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos ).x;
vec3 aopos = nor*hr + pos;
float dd = map(aopos).x;
occ += -(dd-hr)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );
return clamp(1.0 - 3.0*occ, 0.0, 1.0);
}
// http://iquilezles.org/www/articles/checkerfiltering/checkerfiltering.htm
float checkersGradBox( in vec2 p )
float checkersGradBox(in vec2 p)
{
// filter kernel
vec2 w = fwidth(p) + 0.001;
@ -324,43 +324,43 @@ float checkersGradBox( in vec2 p )
return 0.5 - 0.5*i.x*i.y;
}
vec3 render( in vec3 ro, in vec3 rd )
vec3 render(in vec3 ro, in vec3 rd)
{
vec3 col = vec3(0.7, 0.9, 1.0) +rd.y*0.8;
vec2 res = castRay(ro,rd);
float t = res.x;
float m = res.y;
if( m>-0.5 )
if (m>-0.5)
{
vec3 pos = ro + t*rd;
vec3 nor = calcNormal( pos );
vec3 ref = reflect( rd, nor );
vec3 nor = calcNormal(pos);
vec3 ref = reflect(rd, nor);
// material
col = 0.45 + 0.35*sin( vec3(0.05,0.08,0.10)*(m-1.0) );
if( m<1.5 )
col = 0.45 + 0.35*sin(vec3(0.05,0.08,0.10)*(m-1.0));
if (m<1.5)
{
float f = checkersGradBox( 5.0*pos.xz );
float f = checkersGradBox(5.0*pos.xz);
col = 0.3 + f*vec3(0.1);
}
// lighting
float occ = calcAO( pos, nor );
vec3 lig = normalize( vec3(cos(-0.4 * runTime), sin(0.7 * runTime), -0.6) );
vec3 hal = normalize( lig-rd );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
float dom = smoothstep( -0.1, 0.1, ref.y );
float fre = pow( clamp(1.0+dot(nor,rd),0.0,1.0), 2.0 );
float occ = calcAO(pos, nor);
vec3 lig = normalize(vec3(cos(-0.4*runTime), sin(0.7*runTime), -0.6));
vec3 hal = normalize(lig-rd);
float amb = clamp(0.5+0.5*nor.y, 0.0, 1.0);
float dif = clamp(dot(nor, lig), 0.0, 1.0);
float bac = clamp(dot(nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0)*clamp(1.0-pos.y,0.0,1.0);
float dom = smoothstep(-0.1, 0.1, ref.y);
float fre = pow(clamp(1.0+dot(nor,rd),0.0,1.0), 2.0);
dif *= calcSoftshadow( pos, lig, 0.02, 2.5 );
dom *= calcSoftshadow( pos, ref, 0.02, 2.5 );
dif *= calcSoftshadow(pos, lig, 0.02, 2.5);
dom *= calcSoftshadow(pos, ref, 0.02, 2.5);
float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0)*
float spe = pow(clamp(dot(nor, hal), 0.0, 1.0),16.0)*
dif *
(0.04 + 0.96*pow( clamp(1.0+dot(hal,rd),0.0,1.0), 5.0 ));
(0.04 + 0.96*pow(clamp(1.0+dot(hal,rd),0.0,1.0), 5.0));
vec3 lin = vec3(0.0);
lin += 1.30*dif*vec3(1.00,0.80,0.55);
@ -371,51 +371,51 @@ vec3 render( in vec3 ro, in vec3 rd )
col = col*lin;
col += 10.00*spe*vec3(1.00,0.90,0.70);
col = mix( col, vec3(0.8,0.9,1.0), 1.0-exp( -0.0002*t*t*t ) );
col = mix(col, vec3(0.8,0.9,1.0), 1.0-exp(-0.0002*t*t*t));
}
return vec3( clamp(col,0.0,1.0) );
return vec3(clamp(col,0.0,1.0));
}
mat3 setCamera( in vec3 ro, in vec3 ta, float cr )
mat3 setCamera(in vec3 ro, in vec3 ta, float cr)
{
vec3 cw = normalize(ta-ro);
vec3 cp = vec3(sin(cr), cos(cr),0.0);
vec3 cu = normalize( cross(cw,cp) );
vec3 cv = normalize( cross(cu,cw) );
return mat3( cu, cv, cw );
vec3 cu = normalize(cross(cw,cp));
vec3 cv = normalize(cross(cu,cw));
return mat3(cu, cv, cw);
}
void main()
{
vec3 tot = vec3(0.0);
#if AA>1
for( int m=0; m<AA; m++ )
for( int n=0; n<AA; n++ )
for (int m=0; m<AA; m++)
for (int n=0; n<AA; n++)
{
// pixel coordinates
vec2 o = vec2(float(m),float(n)) / float(AA) - 0.5;
vec2 o = vec2(float(m),float(n))/float(AA) - 0.5;
vec2 p = (-resolution.xy + 2.0*(gl_FragCoord.xy+o))/resolution.y;
#else
vec2 p = (-resolution.xy + 2.0*gl_FragCoord.xy)/resolution.y;
#endif
// RAY: Camera is provided from raylib
//vec3 ro = vec3( -0.5+3.5*cos(0.1*time + 6.0*mo.x), 1.0 + 2.0*mo.y, 0.5 + 4.0*sin(0.1*time + 6.0*mo.x) );
//vec3 ro = vec3(-0.5+3.5*cos(0.1*time + 6.0*mo.x), 1.0 + 2.0*mo.y, 0.5 + 4.0*sin(0.1*time + 6.0*mo.x));
vec3 ro = viewEye;
vec3 ta = viewCenter;
// camera-to-world transformation
mat3 ca = setCamera( ro, ta, 0.0 );
mat3 ca = setCamera(ro, ta, 0.0);
// ray direction
vec3 rd = ca * normalize( vec3(p.xy,2.0) );
vec3 rd = ca*normalize(vec3(p.xy,2.0));
// render
vec3 col = render( ro, rd );
vec3 col = render(ro, rd);
// gamma
col = pow( col, vec3(0.4545) );
col = pow(col, vec3(0.4545));
tot += col;
#if AA>1
@ -423,5 +423,5 @@ void main()
tot /= float(AA*AA);
#endif
gl_FragColor = vec4( tot, 1.0 );
gl_FragColor = vec4(tot, 1.0);
}

View File

@ -1,7 +1,7 @@
// Note: SDF by Iñigo Quilez is licensed under MIT License
#version 120
// NOTE: SDF by Iñigo Quilez, licensed under MIT License
// Input vertex attributes (from vertex shader)
varying vec2 fragTexCoord;
varying vec4 fragColor;

View File

@ -33,7 +33,7 @@ void main()
fragColor = color;
*/
// Scanlines method 2
float globalPos = (fragTexCoord.y + offset) * frequency;
float globalPos = (fragTexCoord.y + offset)*frequency;
float wavePos = cos((fract(globalPos) - 0.5)*3.14);
vec4 color = texture2D(texture0, fragTexCoord);

View File

@ -52,7 +52,7 @@ void main()
vec2 sampleCoords = fragPosLightSpace.xy;
float curDepth = fragPosLightSpace.z;
// Slope-scale depth bias: depth biasing reduces "shadow acne" artifacts, where dark stripes appear all over the scene.
// Slope-scale depth bias: depth biasing reduces "shadow acne" artifacts, where dark stripes appear all over the scene
// The solution is adding a small bias to the depth
// In this case, the bias is proportional to the slope of the surface, relative to the light
float bias = max(0.0008*(1.0 - dot(normal, l)), 0.00008);
@ -61,8 +61,8 @@ void main()
// PCF (percentage-closer filtering) algorithm:
// Instead of testing if just one point is closer to the current point,
// we test the surrounding points as well.
// This blurs shadow edges, hiding aliasing artifacts.
// we test the surrounding points as well
// This blurs shadow edges, hiding aliasing artifacts
vec2 texelSize = vec2(1.0/float(shadowMapResolution));
for (int x = -1; x <= 1; x++)
{

View File

@ -18,10 +18,10 @@ void main()
vec4 horizEdge = vec4(0.0);
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0;
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y ))*2.0;
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y ))*2.0;
horizEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y ))*2.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y ))*2.0;
horizEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0;
vec4 vertEdge = vec4(0.0);