2024-07-08 13:46:49 +02:00
|
|
|
import Common;
|
|
|
|
|
import MaterialParameter;
|
|
|
|
|
import LightEnv;
|
2024-07-10 21:07:10 +02:00
|
|
|
import Scene;
|
|
|
|
|
import RayTracingData;
|
2024-09-27 15:57:37 +02:00
|
|
|
import StaticMeshVertexData;
|
2024-07-15 17:55:22 +02:00
|
|
|
import Material;
|
|
|
|
|
import MATERIAL_FILE_NAME;
|
2024-07-08 13:46:49 +02:00
|
|
|
|
|
|
|
|
// simplification: all BLAS only have 1 geometry
|
|
|
|
|
|
2025-01-29 16:15:48 +01:00
|
|
|
/*vec3 nextEventEstimation(vec3 accmat, vec3 w, vec3 x, vec3 nl, float kt, bool useAtt, vec3 rnd) {
|
|
|
|
|
uint triId = 0;
|
|
|
|
|
uint sphereId = 0;
|
|
|
|
|
ShadingParams paramsls;
|
|
|
|
|
Material matls;
|
|
|
|
|
vec3 result = vec3(0);
|
|
|
|
|
// Direct Illumination: Next Event Estimation over any present lights
|
|
|
|
|
for (int i = spheres.length(); i-->0;) {
|
|
|
|
|
Sphere ls = spheres[i];
|
|
|
|
|
if (all(equal(ls.e, vec3(0)))) continue; // skip non-emissive spheres
|
|
|
|
|
vec3 xc = ls.geo.xyz - x;
|
|
|
|
|
vec3 sw = normalize(xc), su = normalize(cross((abs(sw.x)>.1 ? vec3(0,1,0) : vec3(1,0,0)), sw)), sv = cross(sw,su);
|
|
|
|
|
float cos_a_max = sqrt(float(1 - ls.geo.w*ls.geo.w / dot(xc,xc)));
|
|
|
|
|
float cos_a = 1 - rnd.x + rnd.x*cos_a_max, sin_a = sqrt(1 - cos_a*cos_a);
|
|
|
|
|
float phi = 2 * pi * rnd.y;
|
|
|
|
|
vec3 l = normalize(su*cos(phi)*sin_a + sv*sin(phi)*sin_a + sw*cos_a); // sampled direction towards light
|
|
|
|
|
if (intersect(Ray(x,l), matls, paramsls, sphereId, triId) && sphereId == i) { // test if shadow ray hits this light source
|
|
|
|
|
float omega = 2 * pi * (1-cos_a_max);
|
|
|
|
|
if(useAtt) {
|
|
|
|
|
float tau = exp(-kt * length(x - paramsls.x));
|
|
|
|
|
result += max(dot(l,nl),0) * ls.e * omega * tau;
|
|
|
|
|
} else {
|
|
|
|
|
result += accmat / pi * max(dot(l,nl),0) * ls.e * omega; // brdf term obj.c.xyz already in accmat, 1/pi for brdf
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(int i = meshLights.length(); i-->0;) {
|
|
|
|
|
MeshDescriptor mesh = meshes[meshLights[i]];
|
|
|
|
|
for(int j = 0; j < mesh.numIndices; j+=3) {
|
|
|
|
|
vec3 A = vec3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 0]]);
|
|
|
|
|
vec3 B = vec3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 1]]);
|
|
|
|
|
vec3 C = vec3(vertices[mesh.vertexOffset + indices[mesh.indexOffset + j + 2]]);
|
|
|
|
|
float b1 = 1 - sqrt(rnd.x);
|
|
|
|
|
float b2 = (1 - rnd.y) * sqrt(rnd.x);
|
|
|
|
|
float b3 = rnd.y * sqrt(rnd.x);
|
|
|
|
|
vec3 P = A * b1 + B * b2 + C * b3;
|
|
|
|
|
vec3 omega = P - x;
|
|
|
|
|
vec3 l = normalize(omega);
|
|
|
|
|
float v = 0.f;
|
|
|
|
|
if(intersect(Ray(x,l), matls, paramsls, sphereId, triId) && triId == j) {
|
|
|
|
|
v = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
vec3 e1 = C - A;
|
|
|
|
|
vec3 e2 = B - A;
|
|
|
|
|
float area = length(cross(e1, e2)) / 2;
|
|
|
|
|
float rayLen = length(omega);
|
|
|
|
|
float cosTheta = dot(nl, l);
|
|
|
|
|
float cosThetaDash = dot(paramsls.n, -l);
|
|
|
|
|
float factor = area * (cosThetaDash / (rayLen * rayLen));
|
|
|
|
|
if(useAtt) {
|
|
|
|
|
float tau = phase(w, l) * exp(-kt * length(x - paramsls.x));
|
|
|
|
|
result += tau * matls.e * max(cosTheta, 0) * factor;
|
|
|
|
|
} else {
|
|
|
|
|
result += accmat * (matls.e * max(cosTheta, 0) * factor) / pi;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const float eps = 1e-5;
|
2024-07-08 13:46:49 +02:00
|
|
|
[shader("closesthit")]
|
2024-07-12 13:33:52 +02:00
|
|
|
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
|
2024-07-08 13:46:49 +02:00
|
|
|
{
|
2024-12-31 10:40:03 +01:00
|
|
|
hitValue.hit = true;
|
|
|
|
|
// todo: replace with anyhit shader
|
|
|
|
|
if(hitValue.anyHit)
|
|
|
|
|
return;
|
2024-07-15 17:55:22 +02:00
|
|
|
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
|
2024-07-08 13:46:49 +02:00
|
|
|
|
2024-07-15 17:55:22 +02:00
|
|
|
InstanceData inst = pScene.instances[InstanceID()];
|
|
|
|
|
MeshData m = pScene.meshData[InstanceID()];
|
2024-07-12 13:33:52 +02:00
|
|
|
|
2024-07-15 17:55:22 +02:00
|
|
|
// offset into the index buffer
|
|
|
|
|
uint indexOffset = m.firstIndex;
|
|
|
|
|
// added to indices to reference correct part of global mesh pool
|
|
|
|
|
uint vertexOffset = pScene.meshletInfos[m.meshletOffset].indicesOffset;
|
|
|
|
|
|
|
|
|
|
uint vertexIndex0 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 0];
|
|
|
|
|
uint vertexIndex1 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 1];
|
|
|
|
|
uint vertexIndex2 = vertexOffset + pRayTracingParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 2];
|
|
|
|
|
|
|
|
|
|
VertexAttributes attr0 = pVertexData.getAttributes(vertexIndex0);
|
|
|
|
|
VertexAttributes attr1 = pVertexData.getAttributes(vertexIndex1);
|
|
|
|
|
VertexAttributes attr2 = pVertexData.getAttributes(vertexIndex2);
|
|
|
|
|
|
2024-12-27 17:06:43 +01:00
|
|
|
FragmentParameter f0 = attr0.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
|
|
|
|
|
FragmentParameter f1 = attr1.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
|
|
|
|
|
FragmentParameter f2 = attr2.getParameter(inst.transformMatrix, inst.inverseTransformMatrix);
|
2024-07-15 17:55:22 +02:00
|
|
|
|
|
|
|
|
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
|
|
|
|
|
|
|
|
|
MaterialParameter materialParams = params.getMaterialParameter();
|
2024-12-31 10:40:03 +01:00
|
|
|
LightingParameter lightingParams = params.getLightingParameter();
|
|
|
|
|
lightingParams.viewDir_WS = -WorldRayDirection();
|
2024-12-25 14:59:08 +01:00
|
|
|
let brdf = Material.prepare(materialParams);
|
2024-07-16 16:44:56 +02:00
|
|
|
|
2025-01-02 14:28:31 +01:00
|
|
|
float3 normal_WS = normalize(mul(params.getTangentToWorld(), brdf.normal));
|
2025-01-29 16:15:48 +01:00
|
|
|
float3 normalLight_WS = dot(normal_WS,WorldRayDirection())<0 ? normal_WS : -normal_WS;
|
|
|
|
|
float3 intersection_WS = params.position_WS;
|
2024-12-31 10:40:03 +01:00
|
|
|
|
|
|
|
|
hitValue.depth++;
|
2025-01-29 16:15:48 +01:00
|
|
|
float3 rnd = rand01(hitValue.rndSeed);
|
2024-12-31 10:40:03 +01:00
|
|
|
//float kt = ka + ks;
|
|
|
|
|
//float s = -log(rnd.z) / kt;
|
|
|
|
|
//float3 xs = r.o + s * r.d;
|
|
|
|
|
//if (s < t) {
|
|
|
|
|
// float p = kt * rnd.z;
|
|
|
|
|
// if (depth > 5) {
|
|
|
|
|
// if (rnd.z >= p) break;
|
|
|
|
|
// else accmat /= p;
|
|
|
|
|
// }
|
|
|
|
|
// float3 ldirect = nextEventEstimation(accmat, r.d, xs, -r.d, kt, true, rnd);
|
|
|
|
|
// accrad += (fogEmm + ks * ldirect) / kt;
|
|
|
|
|
// accmat *= ks / kt;
|
|
|
|
|
// rayDesc.Origin = xs;
|
|
|
|
|
// rayDesc.Direction = float3(
|
|
|
|
|
// cos(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
|
|
|
|
|
// sin(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y),
|
|
|
|
|
// rnd.y
|
|
|
|
|
// );
|
|
|
|
|
// continue;
|
|
|
|
|
//}
|
|
|
|
|
|
2025-01-29 16:15:48 +01:00
|
|
|
//float p = max(max(brdf.baseColor.x, brdf.baseColor.y), brdf.baseColor.z);
|
2024-12-31 10:40:03 +01:00
|
|
|
//if(hitValue.depth > 5) {
|
|
|
|
|
// if (rnd.z >= p) return;
|
|
|
|
|
// else hitValue.accmat /= p;
|
|
|
|
|
//}
|
|
|
|
|
|
2025-01-29 16:15:48 +01:00
|
|
|
hitValue.light += brdf.emissive * hitValue.emissive + brdf.evaluateAmbient();
|
2024-12-31 10:40:03 +01:00
|
|
|
//-- Ideal DIFFUSE reflection
|
|
|
|
|
//if(bool(useNEE)) {
|
|
|
|
|
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
|
|
|
|
|
//}
|
|
|
|
|
for(uint i = 0; i < pLightEnv.numDirectionalLights; ++i) {
|
2025-01-02 14:28:31 +01:00
|
|
|
float3 x = intersection_WS;
|
2024-12-31 10:40:03 +01:00
|
|
|
float3 l = -pLightEnv.directionalLights[i].direction.xyz;
|
2025-01-02 14:28:31 +01:00
|
|
|
RayDesc rayDesc;
|
|
|
|
|
rayDesc.TMax = 10000.0f;
|
2025-01-29 16:15:48 +01:00
|
|
|
rayDesc.TMin = eps;
|
2024-12-31 10:40:03 +01:00
|
|
|
rayDesc.Origin = x;
|
|
|
|
|
rayDesc.Direction = l;
|
|
|
|
|
RayPayload payload;
|
|
|
|
|
payload.depth = hitValue.depth;
|
|
|
|
|
payload.emissive = 1;
|
|
|
|
|
payload.anyHit = true;
|
2025-01-29 16:15:48 +01:00
|
|
|
payload.hit = false;
|
|
|
|
|
payload.rndSeed = hitValue.rndSeed;
|
2025-01-02 14:28:31 +01:00
|
|
|
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
|
2024-12-31 10:40:03 +01:00
|
|
|
|
|
|
|
|
// we have missed all geometry, so directional light is affecting us
|
2025-01-29 16:15:48 +01:00
|
|
|
//if(!payload.hit) {
|
|
|
|
|
//hitValue.light += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
|
|
|
|
//}
|
2024-12-31 10:40:03 +01:00
|
|
|
}
|
2025-01-02 14:28:31 +01:00
|
|
|
for(uint i = 0; i < pLightEnv.numPointLights; ++i) {
|
|
|
|
|
RayPayload payload;
|
2025-01-29 16:15:48 +01:00
|
|
|
payload.rndSeed = hitValue.rndSeed;
|
2025-01-02 14:28:31 +01:00
|
|
|
float3 x = intersection_WS;
|
|
|
|
|
float3 l = pLightEnv.pointLights[i].position_WS.xyz - intersection_WS;
|
|
|
|
|
if(length(l) > pLightEnv.pointLights[i].colorRange.w) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
RayDesc rayDesc;
|
|
|
|
|
rayDesc.TMax = 1.0f;
|
2025-01-29 16:15:48 +01:00
|
|
|
rayDesc.TMin = eps;
|
2025-01-02 14:28:31 +01:00
|
|
|
rayDesc.Origin = x;
|
|
|
|
|
rayDesc.Direction = l;
|
|
|
|
|
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
|
|
|
|
|
|
|
|
|
|
// hitting only after the light
|
|
|
|
|
if(!payload.hit) {
|
2025-01-29 16:15:48 +01:00
|
|
|
hitValue.light += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
|
2025-01-02 14:28:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-12-31 10:40:03 +01:00
|
|
|
// Indirect Illumination: cosine-weighted importance sampling
|
|
|
|
|
if(hitValue.depth < 12) {
|
|
|
|
|
float r1 = 2 * PI * rnd.x, r2 = rnd.y, r2s = sqrt(r2);
|
|
|
|
|
float3 w = normalLight_WS;
|
2025-01-29 16:15:48 +01:00
|
|
|
float3 u = normalize(cross(abs(w.x)>0.1 ? float3(0,1,0) : float3(1,0,0), w));
|
2024-12-31 10:40:03 +01:00
|
|
|
float3 v = cross(w,u);
|
2025-01-02 14:28:31 +01:00
|
|
|
RayDesc rayDesc;
|
|
|
|
|
rayDesc.TMax = 10000.0f;
|
2025-01-29 16:15:48 +01:00
|
|
|
rayDesc.TMin = eps;
|
2025-01-02 14:28:31 +01:00
|
|
|
rayDesc.Origin = intersection_WS;
|
2024-12-31 10:40:03 +01:00
|
|
|
rayDesc.Direction = normalize(u*cos(r1)*r2s + v * sin(r1)*r2s + w * sqrt(1 - r2));
|
|
|
|
|
RayPayload payload;
|
|
|
|
|
payload.light = float3(0);
|
2025-01-29 16:15:48 +01:00
|
|
|
payload.emissive = 1;
|
|
|
|
|
payload.hit = false;
|
|
|
|
|
//payload.emissive = 0; // in the next bounce, consider reflective part only!
|
|
|
|
|
payload.depth = hitValue.depth;
|
2024-12-31 10:40:03 +01:00
|
|
|
payload.anyHit = false;
|
2025-01-29 16:15:48 +01:00
|
|
|
payload.rndSeed = hitValue.rndSeed + 1;
|
2025-01-02 14:28:31 +01:00
|
|
|
TraceRay(pRayTracingParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload);
|
2025-01-29 16:15:48 +01:00
|
|
|
float bias = dot(normalLight_WS, rayDesc.Direction);
|
|
|
|
|
hitValue.light += brdf.evaluate(params.getTangentToWorld(), -WorldRayDirection(), rayDesc.Direction, payload.light / bias);
|
2024-12-31 10:40:03 +01:00
|
|
|
}
|
2024-07-08 13:46:49 +02:00
|
|
|
}
|