diff --git a/Examples/Text/FontSdf.cs b/Examples/Text/FontSdf.cs index fb1ca29..3b01a9c 100644 --- a/Examples/Text/FontSdf.cs +++ b/Examples/Text/FontSdf.cs @@ -41,7 +41,7 @@ public class FontSdf // Loading font data from memory data // Parameters > font size: 16, no chars array provided (0), chars count: 95 (autogenerate chars array) - fontDefault.Glyphs = LoadFontData(fileData, (int)fileSize, 16, null, 95, FontType.Default); + fontDefault.Glyphs = LoadFontData(fileData, (int)fileSize, 16, null, 95, FontType.Default, &fontDefault.GlyphCount); // Parameters > chars count: 95, font size: 16, chars padding in image: 4 px, pack method: 0 (default) Image atlas = GenImageFontAtlas(fontDefault.Glyphs, &fontDefault.Recs, 95, 16, 4, 0); fontDefault.Texture = LoadTextureFromImage(atlas); @@ -52,7 +52,7 @@ public class FontSdf fontSDF.BaseSize = 16; fontSDF.GlyphCount = 95; // Parameters > font size: 16, no chars array provided (0), chars count: 0 (defaults to 95) - fontSDF.Glyphs = LoadFontData(fileData, (int)fileSize, 16, null, 0, FontType.Sdf); + fontSDF.Glyphs = LoadFontData(fileData, (int)fileSize, 16, null, 0, FontType.Sdf, &fontDefault.GlyphCount); // Parameters > chars count: 95, font size: 16, chars padding in image: 0 px, pack method: 1 (Skyline algorythm) atlas = GenImageFontAtlas(fontSDF.Glyphs, &fontSDF.Recs, 95, 16, 0, 1); fontSDF.Texture = LoadTextureFromImage(atlas); diff --git a/Examples/resources/shaders/glsl100/base.fs b/Examples/resources/shaders/glsl100/base.fs index 6a8d44e..681f9cc 100644 --- a/Examples/resources/shaders/glsl100/base.fs +++ b/Examples/resources/shaders/glsl100/base.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl100/base.vs b/Examples/resources/shaders/glsl100/base.vs index 32e8399..ad164a2 100644 --- a/Examples/resources/shaders/glsl100/base.vs +++ b/Examples/resources/shaders/glsl100/base.vs @@ -13,7 +13,7 @@ uniform mat4 mvp; varying vec2 fragTexCoord; varying vec4 fragColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl100/bloom.fs b/Examples/resources/shaders/glsl100/bloom.fs index 673e011..e0c2ecf 100644 --- a/Examples/resources/shaders/glsl100/bloom.fs +++ b/Examples/resources/shaders/glsl100/bloom.fs @@ -10,11 +10,11 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here -const vec2 size = vec2(800, 450); // render size -const float samples = 5.0; // pixels per axis; higher = bigger glow, worse performance -const float quality = 2.5; // lower = smaller glow, better quality +const vec2 size = vec2(800, 450); // Framebuffer size +const float samples = 5.0; // Pixels per axis; higher = bigger glow, worse performance +const float quality = 2.5; // Defines size factor: Lower = smaller glow, better quality void main() { diff --git a/Examples/resources/shaders/glsl100/blur.fs b/Examples/resources/shaders/glsl100/blur.fs index 2fef571..44ae894 100644 --- a/Examples/resources/shaders/glsl100/blur.fs +++ b/Examples/resources/shaders/glsl100/blur.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; diff --git a/Examples/resources/shaders/glsl100/cross_hatching.fs b/Examples/resources/shaders/glsl100/cross_hatching.fs index d978de8..8ec684c 100644 --- a/Examples/resources/shaders/glsl100/cross_hatching.fs +++ b/Examples/resources/shaders/glsl100/cross_hatching.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float hatchOffsetY = 5.0; float lumThreshold01 = 0.9; diff --git a/Examples/resources/shaders/glsl100/cross_stitching.fs b/Examples/resources/shaders/glsl100/cross_stitching.fs index a7a348d..a098794 100644 --- a/Examples/resources/shaders/glsl100/cross_stitching.fs +++ b/Examples/resources/shaders/glsl100/cross_stitching.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; @@ -23,8 +23,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)); @@ -38,11 +38,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); } diff --git a/Examples/resources/shaders/glsl100/cubes_panning.fs b/Examples/resources/shaders/glsl100/cubes_panning.fs index 9e5eab0..ea05cad 100644 --- a/Examples/resources/shaders/glsl100/cubes_panning.fs +++ b/Examples/resources/shaders/glsl100/cubes_panning.fs @@ -16,7 +16,7 @@ float angle = 0.0; vec2 VectorRotateTime(vec2 v, float speed) { float time = uTime*speed; - float localTime = fract(time); // The time domain this works on is 1 sec. + float localTime = fract(time); // The time domain this works on is 1 sec if ((localTime >= 0.0) && (localTime < 0.25)) angle = 0.0; else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4.0*sin(2.0*PI*localTime - PI/2.0); diff --git a/Examples/resources/shaders/glsl100/dream_vision.fs b/Examples/resources/shaders/glsl100/dream_vision.fs index e361626..27b70ee 100644 --- a/Examples/resources/shaders/glsl100/dream_vision.fs +++ b/Examples/resources/shaders/glsl100/dream_vision.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { @@ -34,4 +34,4 @@ void main() color = color/9.5; gl_FragColor = color; -} \ No newline at end of file +} \ No newline at end of file diff --git a/Examples/resources/shaders/glsl100/eratosthenes.fs b/Examples/resources/shaders/glsl100/eratosthenes.fs index 0d5fcc5..739d7b9 100644 --- a/Examples/resources/shaders/glsl100/eratosthenes.fs +++ b/Examples/resources/shaders/glsl100/eratosthenes.fs @@ -7,12 +7,12 @@ precision mediump float; The Sieve of Eratosthenes -- a simple shader by ProfJski An early prime number sieve: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes - The screen is divided into a square grid of boxes, each representing an integer value. - Each integer is tested to see if it is a prime number. Primes are colored white. - Non-primes are colored with a color that indicates the smallest factor which evenly divdes our integer. + The screen is divided into a square grid of boxes, each representing an integer value + Each integer is tested to see if it is a prime number. Primes are colored white + Non-primes are colored with a color that indicates the smallest factor which evenly divdes our integer - You can change the scale variable to make a larger or smaller grid. - Total number of integers displayed = scale squared, so scale = 100 tests the first 10,000 integers. + You can change the scale variable to make a larger or smaller grid + Total number of integers displayed = scale squared, so scale = 100 tests the first 10,000 integers WARNING: If you make scale too large, your GPU may bog down! @@ -38,7 +38,7 @@ vec4 Colorizer(float counter, float maxSize) void main() { vec4 color = vec4(1.0); - float scale = 1000.0; // Makes 100x100 square grid. Change this variable to make a smaller or larger grid. + float scale = 1000.0; // Makes 100x100 square grid. Change this variable to make a smaller or larger grid float value = scale*floor(fragTexCoord.y*scale) + floor(fragTexCoord.x*scale); // Group pixels into boxes representing integer values int valuei = int(value); diff --git a/Examples/resources/shaders/glsl100/fisheye.fs b/Examples/resources/shaders/glsl100/fisheye.fs index c8ca0bb..9c136a9 100644 --- a/Examples/resources/shaders/glsl100/fisheye.fs +++ b/Examples/resources/shaders/glsl100/fisheye.fs @@ -10,29 +10,29 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here 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 { diff --git a/Examples/resources/shaders/glsl100/fog.fs b/Examples/resources/shaders/glsl100/fog.fs index c3d0e17..e043b6e 100644 --- a/Examples/resources/shaders/glsl100/fog.fs +++ b/Examples/resources/shaders/glsl100/fog.fs @@ -12,7 +12,7 @@ varying vec3 fragNormal; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_LIGHTS 4 #define LIGHT_DIRECTIONAL 0 diff --git a/Examples/resources/shaders/glsl100/grayscale.fs b/Examples/resources/shaders/glsl100/grayscale.fs index 0c01fc5..58cc1ed 100644 --- a/Examples/resources/shaders/glsl100/grayscale.fs +++ b/Examples/resources/shaders/glsl100/grayscale.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl100/hybrid_raster.fs b/Examples/resources/shaders/glsl100/hybrid_raster.fs index fa6c0ea..4f4f4a6 100644 --- a/Examples/resources/shaders/glsl100/hybrid_raster.fs +++ b/Examples/resources/shaders/glsl100/hybrid_raster.fs @@ -1,5 +1,7 @@ #version 100 + #extension GL_EXT_frag_depth : enable // Extension required for writing depth + precision mediump float; // Precision required for OpenGL ES2 (WebGL) varying vec2 fragTexCoord; @@ -11,6 +13,7 @@ uniform vec4 colDiffuse; void main() { vec4 texelColor = texture2D(texture0, fragTexCoord); + gl_FragColor = texelColor*colDiffuse*fragColor; - gl_FragDepthEXT = gl_FragCoord.z; + gl_FragDepthEXT = gl_FragCoord.z; } \ No newline at end of file diff --git a/Examples/resources/shaders/glsl100/hybrid_raymarch.fs b/Examples/resources/shaders/glsl100/hybrid_raymarch.fs index aa38316..f13902d 100644 --- a/Examples/resources/shaders/glsl100/hybrid_raymarch.fs +++ b/Examples/resources/shaders/glsl100/hybrid_raymarch.fs @@ -1,8 +1,11 @@ #version 100 + #extension GL_EXT_frag_depth : enable //Extension required for writing depth #extension GL_OES_standard_derivatives : enable //Extension used for fwidth() -precision mediump float; // Precision required for OpenGL ES2 (WebGL) +#define ZERO 0 + +precision mediump float; // Precision required for OpenGL ES2 (WebGL) // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; @@ -17,23 +20,22 @@ uniform vec3 camPos; uniform vec3 camDir; uniform vec2 screenCenter; -#define ZERO 0 - -// https://learnopengl.com/Advanced-OpenGL/Depth-testing -float CalcDepth(in vec3 rd, in float Idist){ +// SRC: https://learnopengl.com/Advanced-OpenGL/Depth-testing +float CalcDepth(in vec3 rd, in float Idist) +{ float local_z = dot(normalize(camDir),rd)*Idist; return (1.0/(local_z) - 1.0/0.01)/(1.0/1000.0 -1.0/0.01); } -// https://iquilezles.org/articles/distfunctions/ -float sdHorseshoe( in vec3 p, in vec2 c, in float r, in float le, vec2 w ) +// SRC: https://iquilezles.org/articles/distfunctions/ +float sdHorseshoe(in vec3 p, in vec2 c, in float r, in float le, vec2 w) { p.x = abs(p.x); float l = length(p.xy); p.xy = mat2(-c.x, c.y, c.y, c.x)*p.xy; p.xy = vec2((p.y>0.0 || p.x>0.0)?p.x:l*sign(-c.x), - (p.x>0.0)?p.y:l ); + (p.x>0.0)?p.y:l); p.xy = vec2(p.x,abs(p.y-r))-vec2(le,0.0); vec2 q = vec2(length(max(p.xy,0.0)) + min(0.0,max(p.x,p.y)),p.z); @@ -44,67 +46,70 @@ float sdHorseshoe( in vec3 p, in vec2 c, in float r, in float le, vec2 w ) // r = sphere's radius // h = cutting's plane's position // t = thickness -float sdSixWayCutHollowSphere( vec3 p, float r, float h, float t ) +float sdSixWayCutHollowSphere(vec3 p, float r, float h, float t) { // Six way symetry Transformation vec3 ap = abs(p); - if(ap.x < max(ap.y, ap.z)){ - if(ap.y < ap.z) ap.xz = ap.zx; + if (ap.x < max(ap.y, ap.z)){ + if (ap.y < ap.z) ap.xz = ap.zx; else ap.xy = ap.yx; } - vec2 q = vec2( length(ap.yz), ap.x ); + vec2 q = vec2(length(ap.yz), ap.x); float w = sqrt(r*r-h*h); - return ((h*q.x0.0 ) + if (tp1>0.0) { - tmax = min( tmax, tp1 ); - res = vec2( tp1, 1.0 ); + tmax = min(tmax, tp1); + res = vec2(tp1, 1.0); } float t = tmin; - for( int i=0; i<70 ; i++ ) + for (int i=0; i<70 ; i++) { - if(t>tmax) break; - vec2 h = map( ro+rd*t ); - if( abs(h.x)<(0.0001*t) ) + if (t>tmax) break; + vec2 h = map(ro+rd*t); + if (abs(h.x) < (0.0001*t)) { res = vec2(t,h.y); break; @@ -117,54 +122,54 @@ vec2 raycast( in vec3 ro, in vec3 rd ){ // https://iquilezles.org/articles/rmshadows -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) { // bounding volume - float tp = (0.8-ro.y)/rd.y; if( tp>0.0 ) tmax = min( tmax, tp ); + float tp = (0.8-ro.y)/rd.y; if (tp>0.0) tmax = min(tmax, tp); float res = 1.0; float t = mint; - for( int i=ZERO; i<24; i++ ) + for (int i = ZERO; i < 24; i++) { - float h = map( ro + rd*t ).x; + float h = map(ro + rd*t).x; float s = clamp(8.0*h/t,0.0,1.0); - res = min( res, s ); - t += clamp( h, 0.01, 0.2 ); - if( res<0.004 || t>tmax ) break; + res = min(res, s); + t += clamp(h, 0.01, 0.2); + if (res<0.004 || t>tmax) break; } - res = clamp( res, 0.0, 1.0 ); + res = clamp(res, 0.0, 1.0); return res*res*(3.0-2.0*res); } // https://iquilezles.org/articles/normalsSDF -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); } // https://iquilezles.org/articles/nvscene2008/rwwtt.pdf -float calcAO( in vec3 pos, in vec3 nor ) +float calcAO(in vec3 pos, in vec3 nor) { - float occ = 0.0; + float occ = 0.0; float sca = 1.0; - for( int i=ZERO; i<5; i++ ) + for (int i = ZERO; i < 5; i++) { float h = 0.01 + 0.12*float(i)/4.0; - float d = map( pos + h*nor ).x; + float d = map(pos + h*nor).x; occ += (h-d)*sca; sca *= 0.95; - if( occ>0.35 ) break; + if (occ>0.35) break; } - return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y); + return clamp(1.0 - 3.0*occ, 0.0, 1.0)*(0.5+0.5*nor.y); } // https://iquilezles.org/articles/checkerfiltering -float checkersGradBox( in vec2 p ) +float checkersGradBox(in vec2 p) { // filter kernel vec2 w = fwidth(p) + 0.001; @@ -175,7 +180,7 @@ float checkersGradBox( in vec2 p ) } // https://www.shadertoy.com/view/tdS3DG -vec4 render( in vec3 ro, in vec3 rd) +vec4 render(in vec3 ro, in vec3 rd) { // background vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3; @@ -183,37 +188,37 @@ vec4 render( in vec3 ro, in vec3 rd) // raycast scene vec2 res = raycast(ro,rd); float t = res.x; - float m = res.y; - if( m>-0.5 ) + float m = res.y; + if (m>-0.5) { vec3 pos = ro + t*rd; - vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos ); - vec3 ref = reflect( rd, nor ); + vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal(pos); + vec3 ref = reflect(rd, nor); // material - col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) ); + col = 0.2 + 0.2*sin(m*2.0 + vec3(0.0,1.0,2.0)); float ks = 1.0; - if( m<1.5 ) + if (m<1.5) { - float f = checkersGradBox( 3.0*pos.xz); + float f = checkersGradBox(3.0*pos.xz); col = 0.15 + f*vec3(0.05); ks = 0.4; } // lighting - float occ = calcAO( pos, nor ); + float occ = calcAO(pos, nor); - vec3 lin = vec3(0.0); + vec3 lin = vec3(0.0); // sun { - vec3 lig = normalize( vec3(-0.5, 0.4, -0.6) ); - vec3 hal = normalize( lig-rd ); - float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); - //if( dif>0.0001 ) - dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); - float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0); + vec3 lig = normalize(vec3(-0.5, 0.4, -0.6)); + vec3 hal = normalize(lig-rd); + float dif = clamp(dot(nor, lig), 0.0, 1.0); + //if (dif>0.0001) + dif *= calcSoftshadow(pos, lig, 0.02, 2.5); + float spe = pow(clamp(dot(nor, hal), 0.0, 1.0),16.0); spe *= dif; spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0); //spe *= 0.04+0.96*pow(clamp(1.0-sqrt(0.5*(1.0-dot(rd,lig))),0.0,1.0),5.0); @@ -222,38 +227,39 @@ vec4 render( in vec3 ro, in vec3 rd) } // sky { - float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); + float dif = sqrt(clamp(0.5+0.5*nor.y, 0.0, 1.0)); dif *= occ; - float spe = smoothstep( -0.2, 0.2, ref.y ); + float spe = smoothstep(-0.2, 0.2, ref.y); spe *= dif; - spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 ); - //if( spe>0.001 ) - spe *= calcSoftshadow( pos, ref, 0.02, 2.5 ); + spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0); + //if (spe>0.001) + spe *= calcSoftshadow(pos, ref, 0.02, 2.5); lin += col*0.60*dif*vec3(0.40,0.60,1.15); lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks; } // back { - float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); + float dif = clamp(dot(nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0)*clamp(1.0-pos.y,0.0,1.0); dif *= occ; - lin += col*0.55*dif*vec3(0.25,0.25,0.25); + lin += col*0.55*dif*vec3(0.25,0.25,0.25); } // sss { float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0); dif *= occ; - lin += col*0.25*dif*vec3(1.00,1.00,1.00); + lin += col*0.25*dif*vec3(1.00,1.00,1.00); } - col = lin; + col = lin; - col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); + col = mix(col, vec3(0.7,0.7,0.9), 1.0-exp(-0.0001*t*t*t)); } - return vec4(vec3( clamp(col,0.0,1.0) ),t); + return vec4(vec3(clamp(col,0.0,1.0)),t); } -vec3 CalcRayDir(vec2 nCoord){ +vec3 CalcRayDir(vec2 nCoord) +{ vec3 horizontal = normalize(cross(camDir,vec3(.0 , 1.0, .0))); vec3 vertical = normalize(cross(horizontal,camDir)); return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y); @@ -261,11 +267,11 @@ vec3 CalcRayDir(vec2 nCoord){ mat3 setCamera() { - vec3 cw = normalize(camDir); - vec3 cp = vec3(0.0, 1.0 ,0.0); - vec3 cu = normalize( cross(cw,cp) ); - vec3 cv = ( cross(cu,cw) ); - return mat3( cu, cv, cw ); + vec3 cw = normalize(camDir); + vec3 cp = vec3(0.0, 1.0 ,0.0); + vec3 cu = normalize(cross(cw,cp)); + vec3 cv = (cross(cu,cw)); + return mat3(cu, cv, cw); } void main() @@ -275,14 +281,15 @@ void main() // focal length float fl = length(camDir); - vec3 rd = ca * normalize( vec3(nCoord,fl) ); + vec3 rd = ca*normalize(vec3(nCoord,fl)); vec3 color = vec3(nCoord/2.0 + 0.5, 0.0); float depth = gl_FragCoord.z; { - vec4 res = render( camPos - vec3(0.0, 0.0, 0.0) , rd ); + vec4 res = render(camPos - vec3(0.0, 0.0, 0.0) , rd); color = res.xyz; depth = CalcDepth(rd,res.w); } + gl_FragColor = vec4(color , 1.0); - gl_FragDepthEXT = depth; + gl_FragDepthEXT = depth; } \ No newline at end of file diff --git a/Examples/resources/shaders/glsl100/julia_set.fs b/Examples/resources/shaders/glsl100/julia_set.fs index 44d0834..a6f3979 100644 --- a/Examples/resources/shaders/glsl100/julia_set.fs +++ b/Examples/resources/shaders/glsl100/julia_set.fs @@ -6,59 +6,58 @@ precision mediump float; varying vec2 fragTexCoord; varying vec4 fragColor; -uniform vec2 screenDims; // Dimensions of the screen uniform vec2 c; // c.x = real, c.y = imaginary component. Equation done is z^2 + c -uniform vec2 offset; // Offset of the scale. -uniform float zoom; // Zoom of the scale. +uniform vec2 offset; // Offset of the scale +uniform float zoom; // Zoom of the scale // NOTE: Maximum number of shader for-loop iterations depend on GPU, // for example, on RasperryPi for this examply only supports up to 60 -const int MAX_ITERATIONS = 48; // Max iterations to do +const int maxIterations = 255; // Max iterations to do. +const float colorCycles = 1.0; // Number of times the color palette repeats. // Square a complex number vec2 ComplexSquare(vec2 z) { - return vec2( - z.x * z.x - z.y * z.y, - z.x * z.y * 2.0 - ); + return vec2(z.x*z.x - z.y*z.y, z.x*z.y*2.0); } // Convert Hue Saturation Value (HSV) color into RGB vec3 Hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); + vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz)*6.0 - K.www); + return c.z*mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } void main() { /********************************************************************************************** - Julia sets use a function z^2 + c, where c is a constant. - This function is iterated until the nature of the point is determined. + Julia sets use a function z^2 + c, where c is a constant + This function is iterated until the nature of the point is determined If the magnitude of the number becomes greater than 2, then from that point onward - the number will get bigger and bigger, and will never get smaller (tends towards infinity). - 2^2 = 4, 4^2 = 8 and so on. - So at 2 we stop iterating. + the number will get bigger and bigger, and will never get smaller (tends towards infinity) + 2^2 = 4, 4^2 = 8 and so on + So at 2 we stop iterating - If the number is below 2, we keep iterating. + If the number is below 2, we keep iterating But when do we stop iterating if the number is always below 2 (it converges)? - That is what MAX_ITERATIONS is for. - Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can - then map to a color. + That is what maxIterations is for + Then we can divide the iterations by the maxIterations value to get a normalized value + that we can then map to a color - We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. - And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + We use dot product (z.x*z.x + z.y*z.y) to determine the magnitude (length) squared + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power) *************************************************************************************************/ // The pixel coordinates are scaled so they are on the mandelbrot scale // NOTE: fragTexCoord already comes as normalized screen coordinates but offset must be normalized before scaling and zoom - vec2 z = vec2((fragTexCoord.x + offset.x/screenDims.x)*2.5/zoom, (fragTexCoord.y + offset.y/screenDims.y)*1.5/zoom); + vec2 z = vec2((fragTexCoord.x - 0.5)*2.5, (fragTexCoord.y - 0.5)*1.5)/zoom; + z.x += offset.x; + z.y += offset.y; int iter = 0; - for (int iterations = 0; iterations < 60; iterations++) + for (int iterations = 0; iterations < maxIterations; iterations++) { z = ComplexSquare(z) + c; // Iterate function if (dot(z, z) > 4.0) break; @@ -66,18 +65,18 @@ void main() iter = iterations; } - // Another few iterations decreases errors in the smoothing calculation. - // See http://linas.org/art-gallery/escape/escape.html for more information. + // Another few iterations decreases errors in the smoothing calculation + // See http://linas.org/art-gallery/escape/escape.html for more information z = ComplexSquare(z) + c; z = ComplexSquare(z) + c; - // This last part smooths the color (again see link above). + // This last part smooths the color (again see link above) float smoothVal = float(iter) + 1.0 - (log(log(length(z)))/log(2.0)); - // Normalize the value so it is between 0 and 1. - float norm = smoothVal/float(MAX_ITERATIONS); + // Normalize the value so it is between 0 and 1 + float norm = smoothVal/float(maxIterations); - // If in set, color black. 0.999 allows for some float accuracy error. + // If in set, color black. 0.999 allows for some float accuracy error if (norm > 0.999) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0); - else gl_FragColor = vec4(Hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0); + else gl_FragColor = vec4(Hsv2rgb(vec3(norm*colorCycles, 1.0, 1.0)), 1.0); } diff --git a/Examples/resources/shaders/glsl100/lighting.fs b/Examples/resources/shaders/glsl100/lighting.fs index 7367161..e36eb7a 100644 --- a/Examples/resources/shaders/glsl100/lighting.fs +++ b/Examples/resources/shaders/glsl100/lighting.fs @@ -12,18 +12,12 @@ varying vec3 fragNormal; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_LIGHTS 4 #define LIGHT_DIRECTIONAL 0 #define LIGHT_POINT 1 -struct MaterialProperty { - vec3 color; - int useSampler; - sampler2D sampler; -}; - struct Light { int enabled; int type; @@ -46,6 +40,8 @@ void main() vec3 viewD = normalize(viewPos - fragPosition); vec3 specular = vec3(0.0); + vec4 tint = colDiffuse*fragColor; + // NOTE: Implement here your fragment shader code for (int i = 0; i < MAX_LIGHTS; i++) @@ -73,7 +69,7 @@ void main() } } - vec4 finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0))); + vec4 finalColor = (texelColor*((tint + vec4(specular, 1.0))*vec4(lightDot, 1.0))); finalColor += texelColor*(ambient/10.0); // Gamma correction diff --git a/Examples/resources/shaders/glsl100/lighting.vs b/Examples/resources/shaders/glsl100/lighting.vs index 5245c61..577ba92 100644 --- a/Examples/resources/shaders/glsl100/lighting.vs +++ b/Examples/resources/shaders/glsl100/lighting.vs @@ -16,7 +16,7 @@ varying vec2 fragTexCoord; varying vec4 fragColor; varying vec3 fragNormal; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // https://github.com/glslify/glsl-inverse mat3 inverse(mat3 m) diff --git a/Examples/resources/shaders/glsl100/lighting_instancing.vs b/Examples/resources/shaders/glsl100/lighting_instancing.vs index eb47bb9..59d7304 100644 --- a/Examples/resources/shaders/glsl100/lighting_instancing.vs +++ b/Examples/resources/shaders/glsl100/lighting_instancing.vs @@ -18,7 +18,7 @@ varying vec2 fragTexCoord; varying vec4 fragColor; varying vec3 fragNormal; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl100/mask.fs b/Examples/resources/shaders/glsl100/mask.fs index 2071062..2136ec1 100644 --- a/Examples/resources/shaders/glsl100/mask.fs +++ b/Examples/resources/shaders/glsl100/mask.fs @@ -12,7 +12,7 @@ uniform sampler2D mask; uniform vec4 colDiffuse; uniform int frame; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl100/palette_switch.fs b/Examples/resources/shaders/glsl100/palette_switch.fs index 25b4963..a2dcdb9 100644 --- a/Examples/resources/shaders/glsl100/palette_switch.fs +++ b/Examples/resources/shaders/glsl100/palette_switch.fs @@ -2,7 +2,7 @@ precision mediump float; -const int colors = 8; +const int MAX_INDEXED_COLORS = 8; // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; @@ -10,7 +10,8 @@ varying vec4 fragColor; // Input uniform values uniform sampler2D texture0; -uniform ivec3 palette[colors]; +uniform ivec3 palette[MAX_INDEXED_COLORS]; +//uniform sampler2D palette; // Alternative to ivec3, palette provided as a 256x1 texture void main() { @@ -18,13 +19,13 @@ void main() vec4 texelColor = texture2D(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]. + // to the palette index by scaling up from [0..1] to [0..255] int index = int(texelColor.r*255.0); ivec3 color = ivec3(0); // NOTE: On GLSL 100 we are not allowed to index a uniform array by a variable value, - // a constantmust be used, so this logic... + // a constant must be used, so this logic... if (index == 0) color = palette[0]; else if (index == 1) color = palette[1]; else if (index == 2) color = palette[2]; @@ -34,8 +35,9 @@ void main() else if (index == 6) color = palette[6]; else if (index == 7) color = palette[7]; + //gl_FragColor = texture2D(palette, texelColor.xy); // Alternative to ivec3 + // 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. + // are defined in the range [0..255] and need to be normalized to [0..1] gl_FragColor = vec4(float(color.x)/255.0, float(color.y)/255.0, float(color.z)/255.0, texelColor.a); } diff --git a/Examples/resources/shaders/glsl100/pixelizer.fs b/Examples/resources/shaders/glsl100/pixelizer.fs index dae63df..1190d48 100644 --- a/Examples/resources/shaders/glsl100/pixelizer.fs +++ b/Examples/resources/shaders/glsl100/pixelizer.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; diff --git a/Examples/resources/shaders/glsl100/posterization.fs b/Examples/resources/shaders/glsl100/posterization.fs index f7060e5..8eabb15 100644 --- a/Examples/resources/shaders/glsl100/posterization.fs +++ b/Examples/resources/shaders/glsl100/posterization.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float gamma = 0.6; float numColors = 8.0; diff --git a/Examples/resources/shaders/glsl100/predator.fs b/Examples/resources/shaders/glsl100/predator.fs index e0c0a9d..45a373e 100644 --- a/Examples/resources/shaders/glsl100/predator.fs +++ b/Examples/resources/shaders/glsl100/predator.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl100/raymarching.fs b/Examples/resources/shaders/glsl100/raymarching.fs index d58d9d3..58cd410 100644 --- a/Examples/resources/shaders/glsl100/raymarching.fs +++ b/Examples/resources/shaders/glsl100/raymarching.fs @@ -1,9 +1,9 @@ #version 100 -precision mediump float; - #extension GL_OES_standard_derivatives : enable +precision mediump float; + // Input vertex attributes (from vertex shader) varying vec2 fragTexCoord; varying vec4 fragColor; @@ -34,46 +34,46 @@ 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 -#define AA 1 // make this 1 is your machine is too slow +#define AA 1 // make this 1 if your machine is too slow //------------------------------------------------------------------ -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 @@ -85,24 +85,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; @@ -117,95 +117,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.x0.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.xtmax ) break; + vec2 res = map(ro+rd*t); + if (res.xtmax) 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; @@ -328,43 +328,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); @@ -375,51 +375,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; m1 @@ -427,5 +427,5 @@ void main() tot /= float(AA*AA); #endif - gl_FragColor = vec4( tot, 1.0 ); + gl_FragColor = vec4(tot, 1.0); } diff --git a/Examples/resources/shaders/glsl100/scanlines.fs b/Examples/resources/shaders/glsl100/scanlines.fs index 74c9c31..3d55b3f 100644 --- a/Examples/resources/shaders/glsl100/scanlines.fs +++ b/Examples/resources/shaders/glsl100/scanlines.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float offset = 0.0; float frequency = 450.0/3.0; @@ -35,7 +35,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); diff --git a/Examples/resources/shaders/glsl100/sobel.fs b/Examples/resources/shaders/glsl100/sobel.fs index 6468b09..e0eb061 100644 --- a/Examples/resources/shaders/glsl100/sobel.fs +++ b/Examples/resources/shaders/glsl100/sobel.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here vec2 resolution = vec2(800.0, 450.0); void main() @@ -20,10 +20,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); diff --git a/Examples/resources/shaders/glsl100/swirl.fs b/Examples/resources/shaders/glsl100/swirl.fs index ec6c664..b775e31 100644 --- a/Examples/resources/shaders/glsl100/swirl.fs +++ b/Examples/resources/shaders/glsl100/swirl.fs @@ -10,7 +10,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values should be passed from code const float renderWidth = 800.0; @@ -42,5 +42,5 @@ void main() tc += center; vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;; - gl_FragColor = vec4(color.rgb, 1.0);; + gl_FragColor = vec4(color.rgb, 1.0); } diff --git a/Examples/resources/shaders/glsl100/wave.fs b/Examples/resources/shaders/glsl100/wave.fs index 50c4e02..00097f2 100644 --- a/Examples/resources/shaders/glsl100/wave.fs +++ b/Examples/resources/shaders/glsl100/wave.fs @@ -10,10 +10,8 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -uniform float secondes; - +uniform float seconds; uniform vec2 size; - uniform float freqX; uniform float freqY; uniform float ampX; @@ -21,16 +19,17 @@ uniform float ampY; uniform float speedX; uniform float speedY; -void main() { - float pixelWidth = 1.0 / size.x; - float pixelHeight = 1.0 / size.y; - float aspect = pixelHeight / pixelWidth; +void main() +{ + float pixelWidth = 1.0/size.x; + float pixelHeight = 1.0/size.y; + float aspect = pixelHeight/pixelWidth; float boxLeft = 0.0; float boxTop = 0.0; vec2 p = fragTexCoord; - p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; - p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + p.x += cos((fragTexCoord.y - boxTop)*freqX/(pixelWidth*750.0) + (seconds*speedX))*ampX*pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft)*freqY*aspect/(pixelHeight*750.0) + (seconds*speedY))*ampY*pixelHeight; gl_FragColor = texture2D(texture0, p)*colDiffuse*fragColor; } diff --git a/Examples/resources/shaders/glsl120/base.fs b/Examples/resources/shaders/glsl120/base.fs index 50781ad..1d9f7a8 100644 --- a/Examples/resources/shaders/glsl120/base.fs +++ b/Examples/resources/shaders/glsl120/base.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here uniform vec2 resolution = vec2(800, 450); void main() diff --git a/Examples/resources/shaders/glsl120/base.vs b/Examples/resources/shaders/glsl120/base.vs index 08a61e3..4669179 100644 --- a/Examples/resources/shaders/glsl120/base.vs +++ b/Examples/resources/shaders/glsl120/base.vs @@ -13,7 +13,7 @@ uniform mat4 mvp; varying vec2 fragTexCoord; varying vec4 fragColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl120/bloom.fs b/Examples/resources/shaders/glsl120/bloom.fs index b8c4495..2e8bbda 100644 --- a/Examples/resources/shaders/glsl120/bloom.fs +++ b/Examples/resources/shaders/glsl120/bloom.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here const vec2 size = vec2(800, 450); // Framebuffer size const float samples = 5.0; // Pixels per axis; higher = bigger glow, worse performance diff --git a/Examples/resources/shaders/glsl120/blur.fs b/Examples/resources/shaders/glsl120/blur.fs index cf66d87..a0691d4 100644 --- a/Examples/resources/shaders/glsl120/blur.fs +++ b/Examples/resources/shaders/glsl120/blur.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; diff --git a/Examples/resources/shaders/glsl120/cross_hatching.fs b/Examples/resources/shaders/glsl120/cross_hatching.fs index 14b7e2e..0513af5 100644 --- a/Examples/resources/shaders/glsl120/cross_hatching.fs +++ b/Examples/resources/shaders/glsl120/cross_hatching.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float hatchOffsetY = 5.0; float lumThreshold01 = 0.9; diff --git a/Examples/resources/shaders/glsl120/cross_stitching.fs b/Examples/resources/shaders/glsl120/cross_stitching.fs index 0e0cb7c..e3ad341 100644 --- a/Examples/resources/shaders/glsl120/cross_stitching.fs +++ b/Examples/resources/shaders/glsl120/cross_stitching.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; @@ -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); } diff --git a/Examples/resources/shaders/glsl120/dream_vision.fs b/Examples/resources/shaders/glsl120/dream_vision.fs index 03f0291..873ba3f 100644 --- a/Examples/resources/shaders/glsl120/dream_vision.fs +++ b/Examples/resources/shaders/glsl120/dream_vision.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { @@ -32,4 +32,4 @@ void main() color = color/9.5; gl_FragColor = color; -} \ No newline at end of file +} \ No newline at end of file diff --git a/Examples/resources/shaders/glsl120/fisheye.fs b/Examples/resources/shaders/glsl120/fisheye.fs index 5cc57f0..b88c5b7 100644 --- a/Examples/resources/shaders/glsl120/fisheye.fs +++ b/Examples/resources/shaders/glsl120/fisheye.fs @@ -8,29 +8,29 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here 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 { diff --git a/Examples/resources/shaders/glsl120/fog.fs b/Examples/resources/shaders/glsl120/fog.fs index 63af6c4..1842a2b 100644 --- a/Examples/resources/shaders/glsl120/fog.fs +++ b/Examples/resources/shaders/glsl120/fog.fs @@ -10,7 +10,7 @@ varying vec3 fragNormal; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_LIGHTS 4 #define LIGHT_DIRECTIONAL 0 diff --git a/Examples/resources/shaders/glsl120/grayscale.fs b/Examples/resources/shaders/glsl120/grayscale.fs index de48f6b..20e52bc 100644 --- a/Examples/resources/shaders/glsl120/grayscale.fs +++ b/Examples/resources/shaders/glsl120/grayscale.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl120/lighting.fs b/Examples/resources/shaders/glsl120/lighting.fs index d9cfb44..ef0dd65 100644 --- a/Examples/resources/shaders/glsl120/lighting.fs +++ b/Examples/resources/shaders/glsl120/lighting.fs @@ -10,18 +10,12 @@ varying vec3 fragNormal; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_LIGHTS 4 #define LIGHT_DIRECTIONAL 0 #define LIGHT_POINT 1 -struct MaterialProperty { - vec3 color; - int useSampler; - sampler2D sampler; -}; - struct Light { int enabled; int type; @@ -44,6 +38,8 @@ void main() vec3 viewD = normalize(viewPos - fragPosition); vec3 specular = vec3(0.0); + vec4 tint = colDiffuse*fragColor; + // NOTE: Implement here your fragment shader code for (int i = 0; i < MAX_LIGHTS; i++) @@ -71,7 +67,7 @@ void main() } } - vec4 finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0))); + vec4 finalColor = (texelColor*((tint + vec4(specular, 1.0))*vec4(lightDot, 1.0))); finalColor += texelColor*(ambient/10.0); // Gamma correction diff --git a/Examples/resources/shaders/glsl120/lighting.vs b/Examples/resources/shaders/glsl120/lighting.vs index b114093..cd00675 100644 --- a/Examples/resources/shaders/glsl120/lighting.vs +++ b/Examples/resources/shaders/glsl120/lighting.vs @@ -16,7 +16,7 @@ varying vec2 fragTexCoord; varying vec4 fragColor; varying vec3 fragNormal; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // https://github.com/glslify/glsl-inverse mat3 inverse(mat3 m) diff --git a/Examples/resources/shaders/glsl120/palette_switch.fs b/Examples/resources/shaders/glsl120/palette_switch.fs index ab3f79c..9c60379 100644 --- a/Examples/resources/shaders/glsl120/palette_switch.fs +++ b/Examples/resources/shaders/glsl120/palette_switch.fs @@ -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); } diff --git a/Examples/resources/shaders/glsl120/pixelizer.fs b/Examples/resources/shaders/glsl120/pixelizer.fs index 8f5e4f1..ff44051 100644 --- a/Examples/resources/shaders/glsl120/pixelizer.fs +++ b/Examples/resources/shaders/glsl120/pixelizer.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; diff --git a/Examples/resources/shaders/glsl120/posterization.fs b/Examples/resources/shaders/glsl120/posterization.fs index 445c925..0839e54 100644 --- a/Examples/resources/shaders/glsl120/posterization.fs +++ b/Examples/resources/shaders/glsl120/posterization.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float gamma = 0.6; float numColors = 8.0; diff --git a/Examples/resources/shaders/glsl120/predator.fs b/Examples/resources/shaders/glsl120/predator.fs index 2198696..ab9e235 100644 --- a/Examples/resources/shaders/glsl120/predator.fs +++ b/Examples/resources/shaders/glsl120/predator.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl120/scanlines.fs b/Examples/resources/shaders/glsl120/scanlines.fs index 520daa5..c9abc78 100644 --- a/Examples/resources/shaders/glsl120/scanlines.fs +++ b/Examples/resources/shaders/glsl120/scanlines.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float offset = 0.0; float frequency = 450.0/3.0; @@ -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); diff --git a/Examples/resources/shaders/glsl120/sobel.fs b/Examples/resources/shaders/glsl120/sobel.fs index 8c74a6a..c17cfd8 100644 --- a/Examples/resources/shaders/glsl120/sobel.fs +++ b/Examples/resources/shaders/glsl120/sobel.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here vec2 resolution = vec2(800.0, 450.0); void main() @@ -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); diff --git a/Examples/resources/shaders/glsl120/swirl.fs b/Examples/resources/shaders/glsl120/swirl.fs index 7b3dd2f..01d394e 100644 --- a/Examples/resources/shaders/glsl120/swirl.fs +++ b/Examples/resources/shaders/glsl120/swirl.fs @@ -8,7 +8,7 @@ varying vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values should be passed from code const float renderWidth = 800; diff --git a/Examples/resources/shaders/glsl330/base.fs b/Examples/resources/shaders/glsl330/base.fs index 6b50062..abf5dec 100644 --- a/Examples/resources/shaders/glsl330/base.fs +++ b/Examples/resources/shaders/glsl330/base.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { @@ -20,6 +20,9 @@ void main() // NOTE: Implement here your fragment shader code - finalColor = texelColor*colDiffuse; + // final color is the color from the texture + // times the tint color (colDiffuse) + // times the fragment color (interpolated vertex color) + finalColor = texelColor*colDiffuse*fragColor; } diff --git a/Examples/resources/shaders/glsl330/base.vs b/Examples/resources/shaders/glsl330/base.vs index 8cc2abb..be2809e 100644 --- a/Examples/resources/shaders/glsl330/base.vs +++ b/Examples/resources/shaders/glsl330/base.vs @@ -13,7 +13,7 @@ uniform mat4 mvp; out vec2 fragTexCoord; out vec4 fragColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl330/bloom.fs b/Examples/resources/shaders/glsl330/bloom.fs index 56eadb5..cd0e862 100644 --- a/Examples/resources/shaders/glsl330/bloom.fs +++ b/Examples/resources/shaders/glsl330/bloom.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here const vec2 size = vec2(800, 450); // Framebuffer size const float samples = 5.0; // Pixels per axis; higher = bigger glow, worse performance diff --git a/Examples/resources/shaders/glsl330/blur.fs b/Examples/resources/shaders/glsl330/blur.fs index 8809f71..7d575f7 100644 --- a/Examples/resources/shaders/glsl330/blur.fs +++ b/Examples/resources/shaders/glsl330/blur.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800; diff --git a/Examples/resources/shaders/glsl330/cross_hatching.fs b/Examples/resources/shaders/glsl330/cross_hatching.fs index 276eabd..10756b3 100644 --- a/Examples/resources/shaders/glsl330/cross_hatching.fs +++ b/Examples/resources/shaders/glsl330/cross_hatching.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float hatchOffsetY = 5.0; float lumThreshold01 = 0.9; diff --git a/Examples/resources/shaders/glsl330/cross_stitching.fs b/Examples/resources/shaders/glsl330/cross_stitching.fs index 02be861..7c4bc9b 100644 --- a/Examples/resources/shaders/glsl330/cross_stitching.fs +++ b/Examples/resources/shaders/glsl330/cross_stitching.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800.0; @@ -25,8 +25,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)); @@ -40,11 +40,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 = texture(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4; + else c = texture(tex, tlPos*vec2(1.0/renderWidth, 1.0/renderHeight))*1.4; } else { - if (invert == 1) c = texture(tex, tlPos * vec2(1.0/renderWidth, 1.0/renderHeight)) * 1.4; + if (invert == 1) c = texture(tex, tlPos*vec2(1.0/renderWidth, 1.0/renderHeight))*1.4; else c = vec4(0.0, 0.0, 0.0, 1.0); } diff --git a/Examples/resources/shaders/glsl330/cubes_panning.fs b/Examples/resources/shaders/glsl330/cubes_panning.fs index 1d75e4e..d58b7b1 100644 --- a/Examples/resources/shaders/glsl330/cubes_panning.fs +++ b/Examples/resources/shaders/glsl330/cubes_panning.fs @@ -17,7 +17,7 @@ float angle = 0.0; vec2 VectorRotateTime(vec2 v, float speed) { float time = uTime*speed; - float localTime = fract(time); // The time domain this works on is 1 sec. + float localTime = fract(time); // The time domain this works on is 1 sec if ((localTime >= 0.0) && (localTime < 0.25)) angle = 0.0; else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4*sin(2*PI*localTime - PI/2); diff --git a/Examples/resources/shaders/glsl330/dream_vision.fs b/Examples/resources/shaders/glsl330/dream_vision.fs index c7aa854..b3da235 100644 --- a/Examples/resources/shaders/glsl330/dream_vision.fs +++ b/Examples/resources/shaders/glsl330/dream_vision.fs @@ -7,7 +7,7 @@ out vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { @@ -31,4 +31,4 @@ void main() color = color/9.5; fragColor = color; -} \ No newline at end of file +} \ No newline at end of file diff --git a/Examples/resources/shaders/glsl330/eratosthenes.fs b/Examples/resources/shaders/glsl330/eratosthenes.fs index 644e38d..3a5b7fe 100644 --- a/Examples/resources/shaders/glsl330/eratosthenes.fs +++ b/Examples/resources/shaders/glsl330/eratosthenes.fs @@ -5,12 +5,12 @@ The Sieve of Eratosthenes -- a simple shader by ProfJski An early prime number sieve: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes - The screen is divided into a square grid of boxes, each representing an integer value. - Each integer is tested to see if it is a prime number. Primes are colored white. - Non-primes are colored with a color that indicates the smallest factor which evenly divdes our integer. + The screen is divided into a square grid of boxes, each representing an integer value + Each integer is tested to see if it is a prime number. Primes are colored white + Non-primes are colored with a color that indicates the smallest factor which evenly divides our integer - You can change the scale variable to make a larger or smaller grid. - Total number of integers displayed = scale squared, so scale = 100 tests the first 10,000 integers. + You can change the scale variable to make a larger or smaller grid + Total number of integers displayed = scale squared, so scale = 100 tests the first 10,000 integers WARNING: If you make scale too large, your GPU may bog down! @@ -39,7 +39,7 @@ vec4 Colorizer(float counter, float maxSize) void main() { vec4 color = vec4(1.0); - float scale = 1000.0; // Makes 100x100 square grid. Change this variable to make a smaller or larger grid. + float scale = 1000.0; // Makes 100x100 square grid, change this variable to make a smaller or larger grid int value = int(scale*floor(fragTexCoord.y*scale)+floor(fragTexCoord.x*scale)); // Group pixels into boxes representing integer values if ((value == 0) || (value == 1) || (value == 2)) finalColor = vec4(1.0); diff --git a/Examples/resources/shaders/glsl330/fisheye.fs b/Examples/resources/shaders/glsl330/fisheye.fs index bb03a61..be81aeb 100644 --- a/Examples/resources/shaders/glsl330/fisheye.fs +++ b/Examples/resources/shaders/glsl330/fisheye.fs @@ -7,29 +7,29 @@ out vec4 fragColor; uniform sampler2D texture0; uniform vec4 colDiffuse; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here 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); - 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 { diff --git a/Examples/resources/shaders/glsl330/fog.fs b/Examples/resources/shaders/glsl330/fog.fs index 445cca3..ac0132f 100644 --- a/Examples/resources/shaders/glsl330/fog.fs +++ b/Examples/resources/shaders/glsl330/fog.fs @@ -13,7 +13,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_LIGHTS 4 #define LIGHT_DIRECTIONAL 0 @@ -37,6 +37,7 @@ struct Light { uniform Light lights[MAX_LIGHTS]; uniform vec4 ambient; uniform vec3 viewPos; +uniform vec4 fogColor; uniform float fogDensity; void main() @@ -77,10 +78,6 @@ void main() // Fog calculation float dist = length(viewPos - fragPosition); - // these could be parameters... - const vec4 fogColor = vec4(0.5, 0.5, 0.5, 1.0); - //const float fogDensity = 0.16; - // Exponential fog float fogFactor = 1.0/exp((dist*fogDensity)*(dist*fogDensity)); diff --git a/Examples/resources/shaders/glsl330/grayscale.fs b/Examples/resources/shaders/glsl330/grayscale.fs index dead6ec..c03f2ae 100644 --- a/Examples/resources/shaders/glsl330/grayscale.fs +++ b/Examples/resources/shaders/glsl330/grayscale.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl330/hybrid_raster.fs b/Examples/resources/shaders/glsl330/hybrid_raster.fs index 53db2a9..a31920b 100644 --- a/Examples/resources/shaders/glsl330/hybrid_raster.fs +++ b/Examples/resources/shaders/glsl330/hybrid_raster.fs @@ -1,14 +1,22 @@ #version 330 +// Input vertex attributes (from vertex shader) in vec2 fragTexCoord; in vec4 fragColor; +// Input uniform values uniform sampler2D texture0; uniform vec4 colDiffuse; +// Output fragment color +out vec4 finalColor; + +// NOTE: Add your custom variables here + void main() { - vec4 texelColor = texture2D(texture0, fragTexCoord); - gl_FragColor = texelColor*colDiffuse*fragColor; - gl_FragDepth = gl_FragCoord.z; + vec4 texelColor = texture(texture0, fragTexCoord); + + finalColor = texelColor*colDiffuse*fragColor; + gl_FragDepth = finalColor.z; } \ No newline at end of file diff --git a/Examples/resources/shaders/glsl330/hybrid_raymarch.fs b/Examples/resources/shaders/glsl330/hybrid_raymarch.fs index 8814775..48fba9a 100644 --- a/Examples/resources/shaders/glsl330/hybrid_raymarch.fs +++ b/Examples/resources/shaders/glsl330/hybrid_raymarch.fs @@ -1,5 +1,7 @@ # version 330 +#define ZERO 0 + // Input vertex attributes (from vertex shader) in vec2 fragTexCoord; in vec4 fragColor; @@ -13,96 +15,98 @@ uniform vec3 camPos; uniform vec3 camDir; uniform vec2 screenCenter; -#define ZERO 0 +// Output fragment color +out vec4 finalColor; // https://learnopengl.com/Advanced-OpenGL/Depth-testing -float CalcDepth(in vec3 rd, in float Idist){ +float CalcDepth(in vec3 rd, in float Idist) +{ float local_z = dot(normalize(camDir),rd)*Idist; return (1.0/(local_z) - 1.0/0.01)/(1.0/1000.0 -1.0/0.01); } // https://iquilezles.org/articles/distfunctions/ -float sdHorseshoe( in vec3 p, in vec2 c, in float r, in float le, vec2 w ) +float sdHorseshoe(in vec3 p, in vec2 c, in float r, in float le, vec2 w) { p.x = abs(p.x); float l = length(p.xy); - p.xy = mat2(-c.x, c.y, - c.y, c.x)*p.xy; - p.xy = vec2((p.y>0.0 || p.x>0.0)?p.x:l*sign(-c.x), - (p.x>0.0)?p.y:l ); - p.xy = vec2(p.x,abs(p.y-r))-vec2(le,0.0); + p.xy = mat2(-c.x, c.y, c.y, c.x)*p.xy; + p.xy = vec2(((p.y > 0.0) || (p.x > 0.0))? p.x : l*sign(-c.x), (p.x>0.0)? p.y : l); + p.xy = vec2(p.x, abs(p.y - r)) - vec2(le, 0.0); - vec2 q = vec2(length(max(p.xy,0.0)) + min(0.0,max(p.x,p.y)),p.z); + vec2 q = vec2(length(max(p.xy, 0.0)) + min(0.0, max(p.x, p.y)), p.z); vec2 d = abs(q) - w; - return min(max(d.x,d.y),0.0) + length(max(d,0.0)); + return min(max(d.x, d.y), 0.0) + length(max(d, 0.0)); } // r = sphere's radius // h = cutting's plane's position // t = thickness -float sdSixWayCutHollowSphere( vec3 p, float r, float h, float t ) +float sdSixWayCutHollowSphere(vec3 p, float r, float h, float t) { // Six way symetry Transformation vec3 ap = abs(p); - if(ap.x < max(ap.y, ap.z)){ - if(ap.y < ap.z) ap.xz = ap.zx; + if (ap.x < max(ap.y, ap.z)) + { + if (ap.y < ap.z) ap.xz = ap.zx; else ap.xy = ap.yx; } - vec2 q = vec2( length(ap.yz), ap.x ); - + vec2 q = vec2(length(ap.yz), ap.x); float w = sqrt(r*r-h*h); - return ((h*q.x0.0 ) + if (tp1 > 0.0) { - tmax = min( tmax, tp1 ); - res = vec2( tp1, 1.0 ); + tmax = min(tmax, tp1); + res = vec2(tp1, 1.0); } float t = tmin; - for( int i=0; i<70 ; i++ ) + for (int i = 0; i < 70 ; i++) { - if(t>tmax) break; - vec2 h = map( ro+rd*t ); - if( abs(h.x)<(0.0001*t) ) + if (t > tmax) break; + vec2 h = map(ro + rd*t); + if (abs(h.x )< (0.0001*t)) { - res = vec2(t,h.y); + res = vec2(t, h.y); break; } t += h.x; @@ -111,67 +115,68 @@ vec2 raycast( in vec3 ro, in vec3 rd ){ return res; } - // https://iquilezles.org/articles/rmshadows -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) { // bounding volume - float tp = (0.8-ro.y)/rd.y; if( tp>0.0 ) tmax = min( tmax, tp ); + float tp = (0.8 - ro.y)/rd.y; if (tp > 0.0) tmax = min(tmax, tp); float res = 1.0; float t = mint; - for( int i=ZERO; i<24; i++ ) + for (int i = ZERO; i < 24; i++) { - float h = map( ro + rd*t ).x; - float s = clamp(8.0*h/t,0.0,1.0); - res = min( res, s ); - t += clamp( h, 0.01, 0.2 ); - if( res<0.004 || t>tmax ) break; + float h = map(ro + rd*t).x; + float s = clamp(8.0*h/t, 0.0, 1.0); + res = min(res, s); + t += clamp(h, 0.01, 0.2); + if ((res < 0.004) || (t > tmax)) break; } - res = clamp( res, 0.0, 1.0 ); + + res = clamp(res, 0.0, 1.0); + return res*res*(3.0-2.0*res); } - // https://iquilezles.org/articles/normalsSDF -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 ); + 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); } // https://iquilezles.org/articles/nvscene2008/rwwtt.pdf -float calcAO( in vec3 pos, in vec3 nor ) +float calcAO(in vec3 pos, in vec3 nor) { - float occ = 0.0; + float occ = 0.0; float sca = 1.0; - for( int i=ZERO; i<5; i++ ) + for (int i = ZERO; i < 5; i++) { float h = 0.01 + 0.12*float(i)/4.0; - float d = map( pos + h*nor ).x; + float d = map(pos + h*nor).x; occ += (h-d)*sca; sca *= 0.95; - if( occ>0.35 ) break; + if (occ>0.35) break; } - return clamp( 1.0 - 3.0*occ, 0.0, 1.0 ) * (0.5+0.5*nor.y); + + return clamp(1.0 - 3.0*occ, 0.0, 1.0)*(0.5+0.5*nor.y); } // https://iquilezles.org/articles/checkerfiltering -float checkersGradBox( in vec2 p ) +float checkersGradBox(in vec2 p) { // filter kernel vec2 w = fwidth(p) + 0.001; // analytical integral (box filter) - vec2 i = 2.0*(abs(fract((p-0.5*w)*0.5)-0.5)-abs(fract((p+0.5*w)*0.5)-0.5))/w; + vec2 i = 2.0*(abs(fract((p - 0.5*w)*0.5)-0.5) - abs(fract((p + 0.5*w)*0.5) - 0.5))/w; // xor pattern - return 0.5 - 0.5*i.x*i.y; + return (0.5 - 0.5*i.x*i.y); } // https://www.shadertoy.com/view/tdS3DG -vec4 render( in vec3 ro, in vec3 rd) +vec4 render(in vec3 ro, in vec3 rd) { // background vec3 col = vec3(0.7, 0.7, 0.9) - max(rd.y,0.0)*0.3; @@ -179,77 +184,78 @@ vec4 render( in vec3 ro, in vec3 rd) // raycast scene vec2 res = raycast(ro,rd); float t = res.x; - float m = res.y; - if( m>-0.5 ) + float m = res.y; + if (m > -0.5) { vec3 pos = ro + t*rd; - vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal( pos ); - vec3 ref = reflect( rd, nor ); + vec3 nor = (m<1.5) ? vec3(0.0,1.0,0.0) : calcNormal(pos); + vec3 ref = reflect(rd, nor); // material - col = 0.2 + 0.2*sin( m*2.0 + vec3(0.0,1.0,2.0) ); + col = 0.2 + 0.2*sin(m*2.0 + vec3(0.0,1.0,2.0)); float ks = 1.0; - if( m<1.5 ) + if (m < 1.5) { - float f = checkersGradBox( 3.0*pos.xz); + float f = checkersGradBox(3.0*pos.xz); col = 0.15 + f*vec3(0.05); ks = 0.4; } // lighting - float occ = calcAO( pos, nor ); + float occ = calcAO(pos, nor); - vec3 lin = vec3(0.0); + vec3 lin = vec3(0.0); // sun { - vec3 lig = normalize( vec3(-0.5, 0.4, -0.6) ); - vec3 hal = normalize( lig-rd ); - float dif = clamp( dot( nor, lig ), 0.0, 1.0 ); - //if( dif>0.0001 ) - dif *= calcSoftshadow( pos, lig, 0.02, 2.5 ); - float spe = pow( clamp( dot( nor, hal ), 0.0, 1.0 ),16.0); + vec3 lig = normalize(vec3(-0.5, 0.4, -0.6)); + vec3 hal = normalize(lig-rd); + float dif = clamp(dot(nor, lig), 0.0, 1.0); + //if (dif>0.0001) + dif *= calcSoftshadow(pos, lig, 0.02, 2.5); + float spe = pow(clamp(dot(nor, hal), 0.0, 1.0),16.0); spe *= dif; spe *= 0.04+0.96*pow(clamp(1.0-dot(hal,lig),0.0,1.0),5.0); //spe *= 0.04+0.96*pow(clamp(1.0-sqrt(0.5*(1.0-dot(rd,lig))),0.0,1.0),5.0); lin += col*2.20*dif*vec3(1.30,1.00,0.70); - lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks; + lin += 5.00*spe*vec3(1.30,1.00,0.70)*ks; } // sky { - float dif = sqrt(clamp( 0.5+0.5*nor.y, 0.0, 1.0 )); + float dif = sqrt(clamp(0.5+0.5*nor.y, 0.0, 1.0)); dif *= occ; - float spe = smoothstep( -0.2, 0.2, ref.y ); + float spe = smoothstep(-0.2, 0.2, ref.y); spe *= dif; - spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0 ); - //if( spe>0.001 ) - spe *= calcSoftshadow( pos, ref, 0.02, 2.5 ); + spe *= 0.04+0.96*pow(clamp(1.0+dot(nor,rd),0.0,1.0), 5.0); + //if (spe>0.001) + spe *= calcSoftshadow(pos, ref, 0.02, 2.5); lin += col*0.60*dif*vec3(0.40,0.60,1.15); lin += 2.00*spe*vec3(0.40,0.60,1.30)*ks; } // back { - float dif = clamp( dot( nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0); + float dif = clamp(dot(nor, normalize(vec3(0.5,0.0,0.6))), 0.0, 1.0)*clamp(1.0-pos.y,0.0,1.0); dif *= occ; - lin += col*0.55*dif*vec3(0.25,0.25,0.25); + lin += col*0.55*dif*vec3(0.25,0.25,0.25); } // sss { float dif = pow(clamp(1.0+dot(nor,rd),0.0,1.0),2.0); dif *= occ; - lin += col*0.25*dif*vec3(1.00,1.00,1.00); + lin += col*0.25*dif*vec3(1.00,1.00,1.00); } - col = lin; + col = lin; - col = mix( col, vec3(0.7,0.7,0.9), 1.0-exp( -0.0001*t*t*t ) ); + col = mix(col, vec3(0.7,0.7,0.9), 1.0-exp(-0.0001*t*t*t)); } - return vec4(vec3( clamp(col,0.0,1.0) ),t); + return vec4(vec3(clamp(col,0.0,1.0)),t); } -vec3 CalcRayDir(vec2 nCoord){ +vec3 CalcRayDir(vec2 nCoord) +{ vec3 horizontal = normalize(cross(camDir,vec3(.0 , 1.0, .0))); vec3 vertical = normalize(cross(horizontal,camDir)); return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y); @@ -257,11 +263,11 @@ vec3 CalcRayDir(vec2 nCoord){ mat3 setCamera() { - vec3 cw = normalize(camDir); - vec3 cp = vec3(0.0, 1.0 ,0.0); - vec3 cu = normalize( cross(cw,cp) ); - vec3 cv = ( cross(cu,cw) ); - return mat3( cu, cv, cw ); + vec3 cw = normalize(camDir); + vec3 cp = vec3(0.0, 1.0 ,0.0); + vec3 cu = normalize(cross(cw,cp)); + vec3 cv = (cross(cu,cw)); + return mat3(cu, cv, cw); } void main() @@ -271,14 +277,15 @@ void main() // focal length float fl = length(camDir); - vec3 rd = ca * normalize( vec3(nCoord,fl) ); + vec3 rd = ca*normalize(vec3(nCoord,fl)); vec3 color = vec3(nCoord/2.0 + 0.5, 0.0); float depth = gl_FragCoord.z; { - vec4 res = render( camPos - vec3(0.0, 0.0, 0.0) , rd ); + vec4 res = render(camPos - vec3(0.0, 0.0, 0.0) , rd); color = res.xyz; depth = CalcDepth(rd,res.w); } - gl_FragColor = vec4(color , 1.0); - gl_FragDepth = depth; + + finalColor = vec4(color , 1.0); + gl_FragDepth = depth; } \ No newline at end of file diff --git a/Examples/resources/shaders/glsl330/julia_set.fs b/Examples/resources/shaders/glsl330/julia_set.fs index c5ee0da..a3e5bec 100644 --- a/Examples/resources/shaders/glsl330/julia_set.fs +++ b/Examples/resources/shaders/glsl330/julia_set.fs @@ -7,75 +7,74 @@ in vec4 fragColor; // Output fragment color out vec4 finalColor; -uniform vec2 screenDims; // Dimensions of the screen uniform vec2 c; // c.x = real, c.y = imaginary component. Equation done is z^2 + c -uniform vec2 offset; // Offset of the scale. -uniform float zoom; // Zoom of the scale. +uniform vec2 offset; // Offset of the scale +uniform float zoom; // Zoom of the scale -const int MAX_ITERATIONS = 255; // Max iterations to do. +const int maxIterations = 255; // Max iterations to do +const float colorCycles = 2.0; // Number of times the color palette repeats. Can show higher detail for higher iteration numbers // Square a complex number vec2 ComplexSquare(vec2 z) { - return vec2( - z.x * z.x - z.y * z.y, - z.x * z.y * 2.0 - ); + return vec2(z.x*z.x - z.y*z.y, z.x*z.y*2.0); } // Convert Hue Saturation Value (HSV) color into RGB vec3 Hsv2rgb(vec3 c) { - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); + vec4 K = vec4(1.0, 2.0/3.0, 1.0/3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz)*6.0 - K.www); + return c.z*mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); } void main() { /********************************************************************************************** - Julia sets use a function z^2 + c, where c is a constant. - This function is iterated until the nature of the point is determined. + Julia sets use a function z^2 + c, where c is a constant + This function is iterated until the nature of the point is determined If the magnitude of the number becomes greater than 2, then from that point onward - the number will get bigger and bigger, and will never get smaller (tends towards infinity). - 2^2 = 4, 4^2 = 8 and so on. - So at 2 we stop iterating. + the number will get bigger and bigger, and will never get smaller (tends towards infinity) + 2^2 = 4, 4^2 = 8 and so on + So at 2 we stop iterating - If the number is below 2, we keep iterating. + If the number is below 2, we keep iterating But when do we stop iterating if the number is always below 2 (it converges)? - That is what MAX_ITERATIONS is for. - Then we can divide the iterations by the MAX_ITERATIONS value to get a normalized value that we can - then map to a color. + That is what maxIterations is for + Then we can divide the iterations by the maxIterations value to get a normalized value + that we can then map to a color - We use dot product (z.x * z.x + z.y * z.y) to determine the magnitude (length) squared. - And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power). + We use dot product (z.x*z.x + z.y*z.y) to determine the magnitude (length) squared + And once the magnitude squared is > 4, then magnitude > 2 is also true (saves computational power) *************************************************************************************************/ // The pixel coordinates are scaled so they are on the mandelbrot scale // NOTE: fragTexCoord already comes as normalized screen coordinates but offset must be normalized before scaling and zoom - vec2 z = vec2((fragTexCoord.x + offset.x/screenDims.x)*2.5/zoom, (fragTexCoord.y + offset.y/screenDims.y)*1.5/zoom); + vec2 z = vec2((fragTexCoord.x - 0.5f)*2.5, (fragTexCoord.y - 0.5)*1.5)/zoom; + z.x += offset.x; + z.y += offset.y; int iterations = 0; - for (iterations = 0; iterations < MAX_ITERATIONS; iterations++) + for (iterations = 0; iterations < maxIterations; iterations++) { z = ComplexSquare(z) + c; // Iterate function if (dot(z, z) > 4.0) break; } - // Another few iterations decreases errors in the smoothing calculation. - // See http://linas.org/art-gallery/escape/escape.html for more information. + // Another few iterations decreases errors in the smoothing calculation + // See http://linas.org/art-gallery/escape/escape.html for more information z = ComplexSquare(z) + c; z = ComplexSquare(z) + c; - // This last part smooths the color (again see link above). + // This last part smooths the color (again see link above) float smoothVal = float(iterations) + 1.0 - (log(log(length(z)))/log(2.0)); - // Normalize the value so it is between 0 and 1. - float norm = smoothVal/float(MAX_ITERATIONS); + // Normalize the value so it is between 0 and 1 + float norm = smoothVal/float(maxIterations); - // If in set, color black. 0.999 allows for some float accuracy error. + // If in set, color black. 0.999 allows for some float accuracy error if (norm > 0.999) finalColor = vec4(0.0, 0.0, 0.0, 1.0); - else finalColor = vec4(Hsv2rgb(vec3(norm, 1.0, 1.0)), 1.0); + else finalColor = vec4(Hsv2rgb(vec3(norm*colorCycles, 1.0, 1.0)), 1.0); } diff --git a/Examples/resources/shaders/glsl330/lighting.fs b/Examples/resources/shaders/glsl330/lighting.fs index 58845c8..b9abfed 100644 --- a/Examples/resources/shaders/glsl330/lighting.fs +++ b/Examples/resources/shaders/glsl330/lighting.fs @@ -3,7 +3,7 @@ // Input vertex attributes (from vertex shader) in vec3 fragPosition; in vec2 fragTexCoord; -//in vec4 fragColor; +in vec4 fragColor; in vec3 fragNormal; // Input uniform values @@ -13,18 +13,12 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_LIGHTS 4 #define LIGHT_DIRECTIONAL 0 #define LIGHT_POINT 1 -struct MaterialProperty { - vec3 color; - int useSampler; - sampler2D sampler; -}; - struct Light { int enabled; int type; @@ -47,6 +41,8 @@ void main() vec3 viewD = normalize(viewPos - fragPosition); vec3 specular = vec3(0.0); + vec4 tint = colDiffuse*fragColor; + // NOTE: Implement here your fragment shader code for (int i = 0; i < MAX_LIGHTS; i++) @@ -74,8 +70,8 @@ void main() } } - finalColor = (texelColor*((colDiffuse + vec4(specular, 1.0))*vec4(lightDot, 1.0))); - finalColor += texelColor*(ambient/10.0)*colDiffuse; + finalColor = (texelColor*((tint + vec4(specular, 1.0))*vec4(lightDot, 1.0))); + finalColor += texelColor*(ambient/10.0)*tint; // Gamma correction finalColor = pow(finalColor, vec4(1.0/2.2)); diff --git a/Examples/resources/shaders/glsl330/lighting.vs b/Examples/resources/shaders/glsl330/lighting.vs index f8ec45f..ad154b2 100644 --- a/Examples/resources/shaders/glsl330/lighting.vs +++ b/Examples/resources/shaders/glsl330/lighting.vs @@ -17,7 +17,7 @@ out vec2 fragTexCoord; out vec4 fragColor; out vec3 fragNormal; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl330/lighting_instancing.vs b/Examples/resources/shaders/glsl330/lighting_instancing.vs index 6775a2e..6473ccf 100644 --- a/Examples/resources/shaders/glsl330/lighting_instancing.vs +++ b/Examples/resources/shaders/glsl330/lighting_instancing.vs @@ -18,19 +18,16 @@ out vec2 fragTexCoord; out vec4 fragColor; out vec3 fragNormal; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { - // Compute MVP for current instance - mat4 mvpi = mvp*instanceTransform; - // Send vertex attributes to fragment shader - fragPosition = vec3(mvpi*vec4(vertexPosition, 1.0)); + fragPosition = vec3(instanceTransform*vec4(vertexPosition, 1.0)); fragTexCoord = vertexTexCoord; - //fragColor = vertexColor; + fragColor = vec4(1.0); fragNormal = normalize(vec3(matNormal*vec4(vertexNormal, 1.0))); - // Calculate final vertex position - gl_Position = mvpi*vec4(vertexPosition, 1.0); + // Calculate final vertex position, note that we multiply mvp by instanceTransform + gl_Position = mvp*instanceTransform*vec4(vertexPosition, 1.0); } diff --git a/Examples/resources/shaders/glsl330/lightmap.fs b/Examples/resources/shaders/glsl330/lightmap.fs index 827473d..0685e84 100644 --- a/Examples/resources/shaders/glsl330/lightmap.fs +++ b/Examples/resources/shaders/glsl330/lightmap.fs @@ -19,5 +19,5 @@ void main() vec4 texelColor = texture(texture0, fragTexCoord); vec4 texelColor2 = texture(texture1, fragTexCoord2); - finalColor = texelColor * texelColor2; + finalColor = texelColor*texelColor2; } diff --git a/Examples/resources/shaders/glsl330/overdraw.fs b/Examples/resources/shaders/glsl330/overdraw.fs index d1a4b6a..af2fed3 100644 --- a/Examples/resources/shaders/glsl330/overdraw.fs +++ b/Examples/resources/shaders/glsl330/overdraw.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl330/palette_switch.fs b/Examples/resources/shaders/glsl330/palette_switch.fs index 7c8a488..9bd77e8 100644 --- a/Examples/resources/shaders/glsl330/palette_switch.fs +++ b/Examples/resources/shaders/glsl330/palette_switch.fs @@ -1,6 +1,6 @@ #version 330 -const int colors = 8; +const int MAX_INDEXED_COLORS = 8; // Input fragment attributes (from fragment shader) in vec2 fragTexCoord; @@ -8,7 +8,8 @@ in vec4 fragColor; // Input uniform values uniform sampler2D texture0; -uniform ivec3 palette[colors]; +uniform ivec3 palette[MAX_INDEXED_COLORS]; +//uniform sampler2D palette; // Alternative to ivec3, palette provided as a 256x1 texture // Output fragment color out vec4 finalColor; @@ -16,15 +17,17 @@ out vec4 finalColor; void main() { // Texel color fetching from texture sampler + // NOTE: The texel is actually the GRAYSCALE index color 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]. + // to the palette index by scaling up from [0..1] to [0..255] int index = int(texelColor.r*255.0); ivec3 color = palette[index]; + //finalColor = texture(palette, texelColor.xy); // Alternative to ivec3 + // 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. + // are defined in the range [0..255] and need to be normalized to [0..1] finalColor = vec4(color/255.0, texelColor.a); } diff --git a/Examples/resources/shaders/glsl330/pbr.fs b/Examples/resources/shaders/glsl330/pbr.fs index d7544d3..ceaead3 100644 --- a/Examples/resources/shaders/glsl330/pbr.fs +++ b/Examples/resources/shaders/glsl330/pbr.fs @@ -64,16 +64,16 @@ vec3 SchlickFresnel(float hDotV,vec3 refl) float GgxDistribution(float nDotH,float roughness) { - float a = roughness * roughness * roughness * roughness; - float d = nDotH * nDotH * (a - 1.0) + 1.0; - d = PI * d * d; - return a / max(d,0.0000001); + float a = roughness*roughness*roughness*roughness; + float d = nDotH*nDotH*(a - 1.0) + 1.0; + d = PI*d*d; + return (a/max(d,0.0000001)); } float GeomSmith(float nDotV,float nDotL,float roughness) { float r = roughness + 1.0; - float k = r*r / 8.0; + float k = r*r/8.0; float ik = 1.0 - k; float ggx1 = nDotV/(nDotV*ik + k); float ggx2 = nDotL/(nDotL*ik + k); @@ -84,14 +84,14 @@ vec3 ComputePBR() { vec3 albedo = texture(albedoMap,vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb; albedo = vec3(albedoColor.x*albedo.x, albedoColor.y*albedo.y, albedoColor.z*albedo.z); - + float metallic = clamp(metallicValue, 0.0, 1.0); float roughness = clamp(roughnessValue, 0.0, 1.0); float ao = clamp(aoValue, 0.0, 1.0); - + if (useTexMRA == 1) { - vec4 mra = texture(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y))*useTexMRA; + vec4 mra = texture(mraMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)); metallic = clamp(mra.r + metallicValue, 0.04, 1.0); roughness = clamp(mra.g + roughnessValue, 0.04, 1.0); ao = (mra.b + aoValue)*0.5; @@ -108,10 +108,10 @@ vec3 ComputePBR() vec3 V = normalize(viewPos - fragPosition); vec3 emissive = vec3(0); - emissive = (texture(emissiveMap, vec2(fragTexCoord.x*tiling.x+offset.x, fragTexCoord.y*tiling.y+offset.y)).rgb).g * emissiveColor.rgb*emissivePower * useTexEmissive; + emissive = (texture(emissiveMap, vec2(fragTexCoord.x*tiling.x + offset.x, fragTexCoord.y*tiling.y + offset.y)).rgb).g*emissiveColor.rgb*emissivePower*useTexEmissive; // return N;//vec3(metallic,metallic,metallic); - // if dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity + // If dia-electric use base reflectivity of 0.04 otherwise ut is a metal use albedo as base reflectivity vec3 baseRefl = mix(vec3(0.04), albedo.rgb, metallic); vec3 lightAccum = vec3(0.0); // Acumulate lighting lum @@ -133,19 +133,19 @@ vec3 ComputePBR() vec3 F = SchlickFresnel(hDotV, baseRefl); // Fresnel proportion of specular reflectance vec3 spec = (D*G*F)/(4.0*nDotV*nDotL); - + // Difuse and spec light can't be above 1.0 // kD = 1.0 - kS diffuse component is equal 1.0 - spec comonent vec3 kD = vec3(1.0) - F; - + // Mult kD by the inverse of metallnes, only non-metals should have diffuse light kD *= 1.0 - metallic; lightAccum += ((kD*albedo.rgb/PI + spec)*radiance*nDotL)*lights[i].enabled; // Angle of light has impact on result } - + vec3 ambientFinal = (ambientColor + albedo)*ambient*0.5; - - return ambientFinal + lightAccum*ao + emissive; + + return (ambientFinal + lightAccum*ao + emissive); } void main() @@ -154,7 +154,7 @@ void main() // HDR tonemapping color = pow(color, color + vec3(1.0)); - + // Gamma correction color = pow(color, vec3(1.0/2.2)); diff --git a/Examples/resources/shaders/glsl330/pbr.vs b/Examples/resources/shaders/glsl330/pbr.vs index 94b0062..0fc7888 100644 --- a/Examples/resources/shaders/glsl330/pbr.vs +++ b/Examples/resources/shaders/glsl330/pbr.vs @@ -4,7 +4,7 @@ in vec3 vertexPosition; in vec2 vertexTexCoord; in vec3 vertexNormal; -in vec3 vertexTangent; +in vec4 vertexTangent; in vec4 vertexColor; // Input uniform values @@ -26,17 +26,17 @@ const float normalOffset = 0.1; void main() { // Compute binormal from vertex normal and tangent - vec3 vertexBinormal = cross(vertexNormal, vertexTangent); - + 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.0f)); + fragPosition = vec3(matModel*vec4(vertexPosition, 1.0)); fragTexCoord = vertexTexCoord*2.0; fragNormal = normalize(normalMatrix*vertexNormal); - vec3 fragTangent = normalize(normalMatrix*vertexTangent); + vec3 fragTangent = normalize(normalMatrix*vertexTangent.xyz); fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal)*fragNormal); vec3 fragBinormal = normalize(normalMatrix*vertexBinormal); fragBinormal = cross(fragNormal, fragTangent); @@ -45,4 +45,4 @@ void main() // Calculate final vertex position gl_Position = mvp*vec4(vertexPosition, 1.0); -} \ No newline at end of file +} diff --git a/Examples/resources/shaders/glsl330/pixelizer.fs b/Examples/resources/shaders/glsl330/pixelizer.fs index cf8aec4..2f0838b 100644 --- a/Examples/resources/shaders/glsl330/pixelizer.fs +++ b/Examples/resources/shaders/glsl330/pixelizer.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800; diff --git a/Examples/resources/shaders/glsl330/posterization.fs b/Examples/resources/shaders/glsl330/posterization.fs index cf84585..1a2bde0 100644 --- a/Examples/resources/shaders/glsl330/posterization.fs +++ b/Examples/resources/shaders/glsl330/posterization.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here float gamma = 0.6; float numColors = 8.0; diff --git a/Examples/resources/shaders/glsl330/predator.fs b/Examples/resources/shaders/glsl330/predator.fs index c0db7b3..090b046 100644 --- a/Examples/resources/shaders/glsl330/predator.fs +++ b/Examples/resources/shaders/glsl330/predator.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here void main() { diff --git a/Examples/resources/shaders/glsl330/raymarching.fs b/Examples/resources/shaders/glsl330/raymarching.fs index 6a9eb45..67446cd 100644 --- a/Examples/resources/shaders/glsl330/raymarching.fs +++ b/Examples/resources/shaders/glsl330/raymarching.fs @@ -33,7 +33,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 @@ -41,38 +41,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 @@ -84,24 +84,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; @@ -116,95 +116,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.x0.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.xtmax ) break; + vec2 res = map(ro+rd*t); + if (res.xtmax) 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; @@ -327,43 +327,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); @@ -374,51 +374,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; m1 @@ -426,5 +426,5 @@ void main() tot /= float(AA*AA); #endif - finalColor = vec4( tot, 1.0 ); + finalColor = vec4(tot, 1.0); } diff --git a/Examples/resources/shaders/glsl330/scanlines.fs b/Examples/resources/shaders/glsl330/scanlines.fs index 2c4c6fd..77d8f63 100644 --- a/Examples/resources/shaders/glsl330/scanlines.fs +++ b/Examples/resources/shaders/glsl330/scanlines.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values must be passed from code const float renderWidth = 800; @@ -39,7 +39,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); // Texel color fetching from texture sampler diff --git a/Examples/resources/shaders/glsl330/sobel.fs b/Examples/resources/shaders/glsl330/sobel.fs index f76e9ca..a8005cb 100644 --- a/Examples/resources/shaders/glsl330/sobel.fs +++ b/Examples/resources/shaders/glsl330/sobel.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here uniform vec2 resolution = vec2(800, 450); void main() @@ -20,22 +20,22 @@ void main() float y = 1.0/resolution.y; 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 + 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 + y))*1.0; + horizEdge -= texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0; + horizEdge -= texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y ))*2.0; + horizEdge -= texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0; + horizEdge += texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0; + horizEdge += texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y ))*2.0; + horizEdge += texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0; vec4 vertEdge = vec4(0.0); - vertEdge -= texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0; - vertEdge -= texture2D(texture0, vec2(fragTexCoord.x , fragTexCoord.y - y))*2.0; - vertEdge -= texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0; - vertEdge += texture2D(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0; - vertEdge += texture2D(texture0, vec2(fragTexCoord.x , fragTexCoord.y + y))*2.0; - vertEdge += texture2D(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0; + vertEdge -= texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y - y))*1.0; + vertEdge -= texture(texture0, vec2(fragTexCoord.x , fragTexCoord.y - y))*2.0; + vertEdge -= texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y - y))*1.0; + vertEdge += texture(texture0, vec2(fragTexCoord.x - x, fragTexCoord.y + y))*1.0; + vertEdge += texture(texture0, vec2(fragTexCoord.x , fragTexCoord.y + y))*2.0; + vertEdge += texture(texture0, vec2(fragTexCoord.x + x, fragTexCoord.y + y))*1.0; vec3 edge = sqrt((horizEdge.rgb*horizEdge.rgb) + (vertEdge.rgb*vertEdge.rgb)); - finalColor = vec4(edge, texture2D(texture0, fragTexCoord).a); + finalColor = vec4(edge, texture(texture0, fragTexCoord).a); } \ No newline at end of file diff --git a/Examples/resources/shaders/glsl330/spotlight.fs b/Examples/resources/shaders/glsl330/spotlight.fs index 02cddf4..b3e89eb 100644 --- a/Examples/resources/shaders/glsl330/spotlight.fs +++ b/Examples/resources/shaders/glsl330/spotlight.fs @@ -7,7 +7,7 @@ in vec4 fragColor; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here #define MAX_SPOTS 3 @@ -53,7 +53,7 @@ void main() else { if (d < spots[fi].inner) alpha = 0.0; - else alpha = (d - spots[fi].inner) / (spots[fi].radius - spots[fi].inner); + else alpha = (d - spots[fi].inner)/(spots[fi].radius - spots[fi].inner); } } diff --git a/Examples/resources/shaders/glsl330/swirl.fs b/Examples/resources/shaders/glsl330/swirl.fs index bb0732c..804b958 100644 --- a/Examples/resources/shaders/glsl330/swirl.fs +++ b/Examples/resources/shaders/glsl330/swirl.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -// NOTE: Add here your custom variables +// NOTE: Add your custom variables here // NOTE: Render size values should be passed from code const float renderWidth = 800; @@ -41,7 +41,7 @@ void main() } tc += center; - vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;; + vec4 color = texture(texture0, tc/texSize)*colDiffuse*fragColor;; finalColor = vec4(color.rgb, 1.0);; } \ No newline at end of file diff --git a/Examples/resources/shaders/glsl330/wave.fs b/Examples/resources/shaders/glsl330/wave.fs index 43efee2..be07ccd 100644 --- a/Examples/resources/shaders/glsl330/wave.fs +++ b/Examples/resources/shaders/glsl330/wave.fs @@ -11,7 +11,7 @@ uniform vec4 colDiffuse; // Output fragment color out vec4 finalColor; -uniform float secondes; +uniform float seconds; uniform vec2 size; @@ -22,16 +22,17 @@ uniform float ampY; uniform float speedX; uniform float speedY; -void main() { - float pixelWidth = 1.0 / size.x; - float pixelHeight = 1.0 / size.y; - float aspect = pixelHeight / pixelWidth; +void main() +{ + float pixelWidth = 1.0/size.x; + float pixelHeight = 1.0/size.y; + float aspect = pixelHeight/pixelWidth; float boxLeft = 0.0; float boxTop = 0.0; vec2 p = fragTexCoord; - p.x += cos((fragTexCoord.y - boxTop) * freqX / ( pixelWidth * 750.0) + (secondes * speedX)) * ampX * pixelWidth; - p.y += sin((fragTexCoord.x - boxLeft) * freqY * aspect / ( pixelHeight * 750.0) + (secondes * speedY)) * ampY * pixelHeight; + p.x += cos((fragTexCoord.y - boxTop)*freqX/(pixelWidth*750.0) + (seconds*speedX))*ampX*pixelWidth; + p.y += sin((fragTexCoord.x - boxLeft)*freqY*aspect/(pixelHeight*750.0) + (seconds*speedY))*ampY*pixelHeight; finalColor = texture(texture0, p)*colDiffuse*fragColor; } diff --git a/Examples/resources/shaders/glsl430/gol.glsl b/Examples/resources/shaders/glsl430/gol.glsl index 62307b7..c5dfe06 100644 --- a/Examples/resources/shaders/glsl430/gol.glsl +++ b/Examples/resources/shaders/glsl430/gol.glsl @@ -32,7 +32,7 @@ void main() neighbourCount += fetchGol(x - 1, y); // Left neighbourCount += fetchGol(x + 1, y); // Right neighbourCount += fetchGol(x - 1, y + 1); // Bottom left - neighbourCount += fetchGol(x, y + 1); // Bottom middle + neighbourCount += fetchGol(x, y + 1); // Bottom middle neighbourCount += fetchGol(x + 1, y + 1); // Bottom right if (neighbourCount == 3) setGol(x, y, 1); diff --git a/Raylib-cs/interop/Raylib.cs b/Raylib-cs/interop/Raylib.cs index 68668d8..4e5e186 100644 --- a/Raylib-cs/interop/Raylib.cs +++ b/Raylib-cs/interop/Raylib.cs @@ -2582,9 +2582,10 @@ public static unsafe partial class Raylib byte* fileData, int dataSize, int fontSize, - int* fontChars, - int glyphCount, - FontType type + int* codepoints, + int codepointsCount, + FontType type, + int* glyphCount ); /// Generate image font atlas using chars info