REVIEWED: example: shaders_hybrid_rendering, shaders issues

This commit is contained in:
Ray
2025-11-20 21:33:43 +01:00
parent 90af210712
commit 6820ff61f1
5 changed files with 64 additions and 52 deletions

View File

@ -1,6 +1,7 @@
#version 100 #version 100
#extension GL_EXT_frag_depth : enable // Extension required for writing depth #extension GL_EXT_frag_depth : enable // Extension required for writing depth
precision mediump float; // Precision required for OpenGL ES2 (WebGL) precision mediump float; // Precision required for OpenGL ES2 (WebGL)
varying vec2 fragTexCoord; varying vec2 fragTexCoord;

View File

@ -2,6 +2,9 @@
#extension GL_EXT_frag_depth : enable //Extension required for writing depth #extension GL_EXT_frag_depth : enable //Extension required for writing depth
#extension GL_OES_standard_derivatives : enable //Extension used for fwidth() #extension GL_OES_standard_derivatives : enable //Extension used for fwidth()
#define ZERO 0
precision mediump float; // Precision required for OpenGL ES2 (WebGL) precision mediump float; // Precision required for OpenGL ES2 (WebGL)
// Input vertex attributes (from vertex shader) // Input vertex attributes (from vertex shader)
@ -17,8 +20,6 @@ uniform vec3 camPos;
uniform vec3 camDir; uniform vec3 camDir;
uniform vec2 screenCenter; uniform vec2 screenCenter;
#define ZERO 0
// SRC: https://learnopengl.com/Advanced-OpenGL/Depth-testing // SRC: https://learnopengl.com/Advanced-OpenGL/Depth-testing
float CalcDepth(in vec3 rd, in float Idist) float CalcDepth(in vec3 rd, in float Idist)
{ {
@ -257,7 +258,8 @@ vec4 render(in vec3 ro, in vec3 rd)
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 horizontal = normalize(cross(camDir,vec3(.0 , 1.0, .0)));
vec3 vertical = normalize(cross(horizontal,camDir)); vec3 vertical = normalize(cross(horizontal,camDir));
return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y); return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y);
@ -287,6 +289,7 @@ void main()
color = res.xyz; color = res.xyz;
depth = CalcDepth(rd,res.w); depth = CalcDepth(rd,res.w);
} }
gl_FragColor = vec4(color , 1.0); gl_FragColor = vec4(color , 1.0);
gl_FragDepthEXT = depth; gl_FragDepthEXT = depth;
} }

View File

@ -9,7 +9,7 @@ uniform sampler2D texture0;
uniform vec4 colDiffuse; uniform vec4 colDiffuse;
// Output fragment color // Output fragment color
//out vec4 finalColor; out vec4 finalColor;
// NOTE: Add your custom variables here // NOTE: Add your custom variables here
@ -17,6 +17,6 @@ void main()
{ {
vec4 texelColor = texture(texture0, fragTexCoord); vec4 texelColor = texture(texture0, fragTexCoord);
gl_FragColor = texelColor*colDiffuse*fragColor; finalColor = texelColor*colDiffuse*fragColor;
gl_FragDepth = gl_FragCoord.z; gl_FragDepth = finalColor.z;
} }

View File

@ -1,5 +1,7 @@
# version 330 # version 330
#define ZERO 0
// Input vertex attributes (from vertex shader) // Input vertex attributes (from vertex shader)
in vec2 fragTexCoord; in vec2 fragTexCoord;
in vec4 fragColor; in vec4 fragColor;
@ -13,10 +15,12 @@ uniform vec3 camPos;
uniform vec3 camDir; uniform vec3 camDir;
uniform vec2 screenCenter; uniform vec2 screenCenter;
#define ZERO 0 // Output fragment color
out vec4 finalColor;
// https://learnopengl.com/Advanced-OpenGL/Depth-testing // 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; 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); return (1.0/(local_z) - 1.0/0.01)/(1.0/1000.0 -1.0/0.01);
} }
@ -26,10 +30,8 @@ float sdHorseshoe(in vec3 p, in vec2 c, in float r, in float le, vec2 w)
{ {
p.x = abs(p.x); p.x = abs(p.x);
float l = length(p.xy); float l = length(p.xy);
p.xy = mat2(-c.x, c.y, p.xy = mat2(-c.x, c.y, c.y, c.x)*p.xy;
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.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 = 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);
@ -44,17 +46,16 @@ float sdSixWayCutHollowSphere(vec3 p, float r, float h, float t)
{ {
// Six way symetry Transformation // Six way symetry Transformation
vec3 ap = abs(p); vec3 ap = abs(p);
if (ap.x < max(ap.y, ap.z)){ if (ap.x < max(ap.y, ap.z))
{
if (ap.y < ap.z) ap.xz = ap.zx; if (ap.y < ap.z) ap.xz = ap.zx;
else ap.xy = ap.yx; 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); float w = sqrt(r*r-h*h);
return ((h*q.x<w*q.y) ? length(q-vec2(w,h)) : return ((h*q.x < w*q.y)? length(q - vec2(w, h)) : abs(length(q) - r)) - t;
abs(length(q)-r)) - t;
} }
// https://iquilezles.org/articles/boxfunctions // https://iquilezles.org/articles/boxfunctions
@ -65,8 +66,8 @@ vec2 iBox(in vec3 ro, in vec3 rd, in vec3 rad)
vec3 k = abs(m)*rad; vec3 k = abs(m)*rad;
vec3 t1 = -n - k; vec3 t1 = -n - k;
vec3 t2 = -n + k; vec3 t2 = -n + k;
return vec2(max(max(t1.x, t1.y), t1.z),
min(min(t2.x, t2.y), t2.z)); return vec2(max(max(t1.x, t1.y), t1.z), min(min(t2.x, t2.y), t2.z));
} }
vec2 opU(vec2 d1, vec2 d2) vec2 opU(vec2 d1, vec2 d2)
@ -74,14 +75,17 @@ vec2 opU(vec2 d1, vec2 d2)
return (d1.x < d2.x)? d1 : d2; return (d1.x < d2.x)? d1 : d2;
} }
vec2 map(in vec3 pos){ vec2 map(in vec3 pos)
{
vec2 res = vec2(sdHorseshoe(pos - vec3(-1.0, 0.08, 1.0), vec2(cos(1.3), sin(1.3)), 0.2, 0.3, vec2(0.03,0.5)), 11.5); vec2 res = vec2(sdHorseshoe(pos - vec3(-1.0, 0.08, 1.0), vec2(cos(1.3), sin(1.3)), 0.2, 0.3, vec2(0.03,0.5)), 11.5);
res = opU(res, vec2(sdSixWayCutHollowSphere(pos-vec3(0.0, 1.0, 0.0), 4.0, 3.5, 0.5), 4.5)); res = opU(res, vec2(sdSixWayCutHollowSphere(pos-vec3(0.0, 1.0, 0.0), 4.0, 3.5, 0.5), 4.5));
return res; return res;
} }
// https://www.shadertoy.com/view/Xds3zN // https://www.shadertoy.com/view/Xds3zN
vec2 raycast(in vec3 ro, in vec3 rd){ vec2 raycast(in vec3 ro, in vec3 rd)
{
vec2 res = vec2(-1.0, -1.0); vec2 res = vec2(-1.0, -1.0);
float tmin = 1.0; float tmin = 1.0;
@ -111,7 +115,6 @@ vec2 raycast(in vec3 ro, in vec3 rd){
return res; return res;
} }
// https://iquilezles.org/articles/rmshadows // 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)
{ {
@ -126,12 +129,13 @@ float calcSoftshadow(in vec3 ro, in vec3 rd, in float mint, in float tmax)
float s = clamp(8.0*h/t, 0.0, 1.0); float s = clamp(8.0*h/t, 0.0, 1.0);
res = min(res, s); res = min(res, s);
t += clamp(h, 0.01, 0.2); t += clamp(h, 0.01, 0.2);
if (res<0.004 || t>tmax) break; if ((res < 0.004) || (t > tmax)) break;
}
res = clamp(res, 0.0, 1.0);
return res*res*(3.0-2.0*res);
} }
res = clamp(res, 0.0, 1.0);
return res*res*(3.0-2.0*res);
}
// https://iquilezles.org/articles/normalsSDF // https://iquilezles.org/articles/normalsSDF
vec3 calcNormal(in vec3 pos) vec3 calcNormal(in vec3 pos)
@ -156,6 +160,7 @@ float calcAO(in vec3 pos, in vec3 nor)
sca *= 0.95; 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);
} }
@ -167,7 +172,7 @@ float checkersGradBox(in vec2 p)
// analytical integral (box filter) // 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 // 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 // https://www.shadertoy.com/view/tdS3DG
@ -249,7 +254,8 @@ vec4 render(in vec3 ro, in vec3 rd)
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 horizontal = normalize(cross(camDir,vec3(.0 , 1.0, .0)));
vec3 vertical = normalize(cross(horizontal,camDir)); vec3 vertical = normalize(cross(horizontal,camDir));
return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y); return normalize(camDir + horizontal*nCoord.x + vertical*nCoord.y);
@ -279,6 +285,7 @@ void main()
color = res.xyz; color = res.xyz;
depth = CalcDepth(rd,res.w); depth = CalcDepth(rd,res.w);
} }
gl_FragColor = vec4(color , 1.0);
finalColor = vec4(color , 1.0);
gl_FragDepth = depth; gl_FragDepth = depth;
} }

View File

@ -138,6 +138,7 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE); DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
DrawFPS(10, 10); DrawFPS(10, 10);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------