yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
c97166aed
master
1implementing fcpw; 2__include ray; 3__include math_constants; 4__include interaction; 5__include bounding_volumes; 6 7public float3x3 extractLinearTransform(float3x4 t) 8{ 9 return float3x3(t[0].xyz, t[1].xyz, t[2].xyz); 10} 11 12public float3 extractTranslation(float3x4 t) 13{ 14 return float3(t[0][3], t[1][3], t[2][3]); 15} 16 17public float3 transformPoint(float3x4 t, float3 p) 18{ 19 return mul(t, float4(p, 1.0)).xyz; 20} 21 22public Ray transformRay(float3x4 t, Ray r) 23{ 24 float3 o = transformPoint(t, r.o); 25 float3 d = transformPoint(t, r.o + r.d * (r.tMax < FLT_MAX ? r.tMax : 1.0)) - o; 26 float dNorm = length(d); 27 28 return Ray(o, d / dNorm, r.tMax < FLT_MAX ? dNorm : FLT_MAX); 29} 30 31public BoundingSphere transformSphere(float3x4 t, BoundingSphere s) 32{ 33 float3 c = transformPoint(t, s.c); 34 float r2 = FLT_MAX; 35 if (s.r2 < FLT_MAX) 36 { 37 float3 d = transformPoint(t, s.c + float3(sqrt(s.r2), 0.0, 0.0)) - c; 38 r2 = dot(d, d); 39 } 40 41 return BoundingSphere(c, r2); 42} 43 44public void transformInteraction(float3x4 t, float3x4 tInv, float3 x, 45 bool overwriteDistance, inout Interaction i) 46{ 47 float3 p = transformPoint(t, i.p); 48 float3 n = normalize(mul(transpose(extractLinearTransform(tInv)), i.n)); 49 50 i.p = p; 51 i.n = n; 52 if (overwriteDistance) 53 { 54 i.d = length(p - x); 55 } 56}