I need help on a godot shader
I can't find help in internet anymore!
where are all the help going today?
It's not the first time I make a cylinder intersection code, but somehow I messed this one And can't figure out:
shader_type spatial;
uniform sampler2D text;
void fragment()
{
mat3 TBN = mat3(TANGENT, BINORMAL, NORMAL);
vec3 viewDirTangentSpace = TBN * VIEW;
const float mydepth = 1.0;
vec3 raydir = viewDirTangentSpace;///.5; // ray direction/depth
const vec2 centers = vec2(-0.0,.5);//x and depth
const float radius = 0.3;
const float r2 = radius * radius;
const vec2 UVsurfacePos = vec2(UV.x,0);
//vector: 2d origine to circles center in 2d along vertical depth plane
const vec2 L = centers.xy-UVsurfacePos.xy; const vec2 L2 = L*L;
//dot projection: 2d centers to 2d ray line
vec2 raydir2d = normalize(raydir.xz);
float tca = dot(L,raydir2d);
//2d distances to projected
float distances = abs( (L2.x + L2.y) - (tca) );
float d = sqrt( distances );float d2 = d * d;
//float d2 = distances * distances;
//2d intersections points
float thcdiff = r2 - d2; float thc = sqrt (thcdiff);
vec2 t = vec2(tca +thc, tca -thc);
//get extrusion in y
//vec3 p11 = vec3(UV,0) + raydir * t.x; //first impact column 1
//vec3 p12 = vec3(UV,0) + raydir * t.y; //second impact column 1
vec3 p11 = raydir * t.x; //first impact column 1
vec3 p12 = raydir * t.y; //second impact column 1
//get normal
vec3 N11 = normalize( vec3( (UV+p11.xz - centers),0) );
vec3 N12 = normalize( vec3( -(UV+p12.xz - centers),0) );
//get twist
float twist11 = atan(N11.x, N11.y);
float twist12 = atan(-N12.x, -N12.y);//reverse the N
vec3 result = vec3(0.0); // black color if not intersecting
if( t.x > 0.0 && t.y > 0.0 )
{
result = vec3(1.0, 0.0, 0.0); // red color if intersecting cylinder1
//result = N11; // red color if intersecting cylinder1
}
float ptmin = min(t.x,t.y);
vec2 UVoffset = UV+raydir.xy * ptmin;
ALBEDO = result;
NORMAL = N11.xzy;
ALBEDO = vec3(UVoffset,0);
ALBEDO = texture(text, UVoffset).xyz;
//ALBEDO = vec3(L.x+0.5,0.0,0.0);
//ALBEDO = vec3(twist11+0.05);
//ALBEDO = N12.xzy;
//ALPHA = 0.0;
//NORMAL
}
Technically I use scratch pixel version. BUT I intercept a 2d circles that I'm trying to extrude along X to get axis aligned cylinder.
Then I supposed to find the angle (twist) of the hit to compute the distance to the spiral.
BUT all I get is a blobby mess and nothing usable, which mean the code partially works but don't output correct result, that suggest something is screwy somewhere!