adding anyhit, doesnt work though

This commit is contained in:
Dynamitos
2025-01-30 23:25:41 +01:00
parent 44b147084b
commit 89570ff792
19 changed files with 136 additions and 70 deletions
-2
View File
@@ -32,7 +32,5 @@ float4 fragmentMain(in FragmentParameter params) : SV_Target
}
result += brdf.evaluateAmbient();
// gamma correction
//result = result / (result + float3(1.0));
//result = pow(result, float3(1.0/2.2));
return float4(result, brdf.getAlpha());
}
+73 -21
View File
@@ -2,10 +2,13 @@ import Common;
interface IBRDF
{
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor);
float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor);
[mutating] void transformNormal(float3x3 tangentToWorld);
float3 getNormal();
float3 getBaseColor();
float3 evaluateAmbient();
float getAlpha();
float3 getTangentNormal();
float3 getEmissive();
};
struct Phong : IBRDF
@@ -29,16 +32,28 @@ struct Phong : IBRDF
emissive = float3(0, 0, 0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{
float3 normal_WS = normalize(mul(tangentToWorld, normal));
float3 normal_WS = normal;
float3 nDotL = dot(normal_WS, lightDir_WS);
float3 r = 2 * (nDotL) * normal_WS - lightDir_WS;
float rDotV = dot(r, viewDir_WS);
return lightColor * (baseColor * max(nDotL, 0.0)) + specular * pow(max(rDotV, 0.0), max(shininess, 1));
}
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient()
{
return ambient;
@@ -47,9 +62,9 @@ struct Phong : IBRDF
{
return alpha;
}
float3 getTangentNormal()
float3 getEmissive()
{
return normal;
return emissive;
}
};
@@ -74,16 +89,28 @@ struct BlinnPhong : IBRDF
emissive = float3(0, 0, 0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{
float3 normal_WS = normalize(mul(tangentToWorld, normal));
float3 normal_WS = normal;
float diffuse = max(dot(normal_WS, lightDir_WS), 0);
float3 h = normalize(lightDir_WS + viewDir_WS);
float specular = pow(saturate(dot(normal_WS, h)), shininess);
return (baseColor * diffuse * lightColor) + (specularColor * specular);
}
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient()
{
return ambient;
@@ -92,9 +119,9 @@ struct BlinnPhong : IBRDF
{
return alpha;
}
float3 getTangentNormal()
float3 getEmissive()
{
return normal;
return emissive;
}
};
@@ -112,10 +139,9 @@ struct CelShading : IBRDF
normal = float3(0, 0, 1);
emissive = float3(0, 0, 0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{
float3 normal_WS = normalize(mul(tangentToWorld, normal));
float3 normal_WS = normal;
float nDotL = dot(normal_WS, lightDir_WS);
float diffuse = max(nDotL, 0);
@@ -129,6 +155,19 @@ struct CelShading : IBRDF
return darkenedBase * lightColor;
}
}
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient()
{
return float3(0, 0, 0);
@@ -137,9 +176,9 @@ struct CelShading : IBRDF
{
return alpha;
}
float3 getTangentNormal()
float3 getEmissive()
{
return normal;
return emissive;
}
};
@@ -197,9 +236,9 @@ struct CookTorrance : IBRDF
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
float3 evaluate(float3x3 tangentToWorld, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
float3 evaluate(float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{
float3 n = normalize(mul(tangentToWorld, normal));
float3 n = normal;
float3 h = normalize(lightDir_WS + viewDir_WS);
float3 F0 = float3(0.04);
@@ -224,6 +263,19 @@ struct CookTorrance : IBRDF
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
return result * ambientOcclusion;
}
[mutating]
void transformNormal(float3x3 tangentToWorld)
{
normal = normalize(mul(tangentToWorld, normal));
}
float3 getNormal()
{
return normal;
}
float3 getBaseColor()
{
return baseColor;
}
float3 evaluateAmbient()
{
return float3(0.03) * baseColor * ambientOcclusion;
@@ -232,8 +284,8 @@ struct CookTorrance : IBRDF
{
return alpha;
}
float3 getTangentNormal()
float3 getEmissive()
{
return normal;
return emissive;
}
};
+2 -2
View File
@@ -15,7 +15,7 @@ struct DirectionalLight : ILightEnv
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{
float3 dir_WS = -normalize(direction.xyz);
return brdf.evaluate(params.tangentToWorld, params.viewDir_WS, dir_WS, color.xyz);
return brdf.evaluate(params.viewDir_WS, dir_WS, color.xyz);
}
};
@@ -29,7 +29,7 @@ struct PointLight : ILightEnv
float3 lightDir_WS = position_WS.xyz - params.position_WS;
float d = length(lightDir_WS);
float illuminance = max(1 - d / colorRange.w, 0);
return illuminance * brdf.evaluate(params.tangentToWorld, params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz);
return illuminance * brdf.evaluate(params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz);
}
bool insidePlane(Plane plane, float3 position)
+2 -2
View File
@@ -5,8 +5,8 @@ import Scene;
//interface IMaterial
//{
// associatedtype BRDF : IBRDF;
// BRDF prepare(MaterialParameter input);
// associatedtype BRDF: IBRDF;
// static BRDF prepare(MaterialParameter input);
//};
struct MaterialResources
{
+2 -2
View File
@@ -2,6 +2,7 @@ import Common;
struct MaterialParameter
{
float3x3 tangentToWorld;
float3 position_WS;
float2 texCoords[MAX_TEXCOORDS];
float3 vertexColor;
@@ -9,7 +10,6 @@ struct MaterialParameter
struct LightingParameter
{
float3x3 tangentToWorld;
float3 viewDir_WS;
float3 position_WS;
}
@@ -72,6 +72,7 @@ struct FragmentParameter
MaterialParameter getMaterialParameter()
{
MaterialParameter result;
result.tangentToWorld = getTangentToWorld();
result.position_WS = position_WS;
result.texCoords[0] = texCoords0.xy;
result.texCoords[1] = texCoords0.zw;
@@ -88,7 +89,6 @@ struct FragmentParameter
LightingParameter getLightingParameter()
{
LightingParameter result;
result.tangentToWorld = getTangentToWorld();
result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS);
result.position_WS = position_WS;
return result;
+4 -4
View File
@@ -6,7 +6,7 @@ struct VertexInput
uint instanceId: SV_InstanceID;
}
interface IVertexData
{
VertexAttributes getAttributes(uint index);
};
//interface IVertexData
//{
// VertexAttributes getAttributes(uint index);
//};
+4 -1
View File
@@ -5,7 +5,7 @@ import MaterialParameter;
import StaticMeshVertexData;
[shader("anyhit")]
void anyhit(inout RayPayload hitvalue, in BuiltInTriangleIntersectionAttributes attr)
void anyhit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
{
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
@@ -31,4 +31,7 @@ void anyhit(inout RayPayload hitvalue, in BuiltInTriangleIntersectionAttributes
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
hitValue.params = params.getLightingParameter();
hitValue.materialParams = params.getMaterialParameter();
AcceptHitAndEndSearch();
}
+12 -9
View File
@@ -83,6 +83,7 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
// todo: replace with anyhit shader
if(hitValue.anyHit)
return;
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
InstanceData inst = pScene.instances[InstanceID()];
@@ -107,14 +108,15 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
MaterialParameter materialParams = params.getMaterialParameter();
LightingParameter lightingParams = params.getLightingParameter();
hitValue.params = params.getLightingParameter();
hitValue.materialParams = params.getMaterialParameter();
LightingParameter lightingParams = hitValue.params;
lightingParams.viewDir_WS = -WorldRayDirection();
let brdf = Material.prepare(materialParams);
let brdf = Material.prepare(hitValue.materialParams);
float3 normal_WS = normalize(mul(params.getTangentToWorld(), brdf.normal));
float3 normal_WS = brdf.getNormal();
float3 normalLight_WS = dot(normal_WS,WorldRayDirection())<0 ? normal_WS : -normal_WS;
float3 intersection_WS = params.position_WS;
float3 intersection_WS = lightingParams.position_WS;
hitValue.depth++;
float3 rnd = rand01(hitValue.rndSeed);
@@ -139,12 +141,12 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
// TraceRay(scene, 0, 0xff, 0, 0, rayDesc);
//}
float p = max(max(brdf.baseColor.x, brdf.baseColor.y), brdf.baseColor.z);
float p = max(max(brdf.getBaseColor().x, brdf.getBaseColor().y), brdf.getBaseColor().z);
if(hitValue.depth > 5) {
if (rnd.z >= p) return;
}
hitValue.light += brdf.emissive * hitValue.emissive + brdf.evaluateAmbient();
hitValue.light += brdf.getEmissive() * hitValue.emissive + brdf.evaluateAmbient();
//-- Ideal DIFFUSE reflection
//if(bool(useNEE)) {
// accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd);
@@ -211,7 +213,8 @@ void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttribu
payload.rndSeed = hitValue.rndSeed + 1;
TraceRay(pRayTracingParams.scene, RAY_FLAG_CULL_BACK_FACING_TRIANGLES, 0xff, 0, 0, 0, rayDesc, payload);
float bias = dot(normalLight_WS, rayDesc.Direction);
hitValue.light += brdf.evaluate(params.getTangentToWorld(), -WorldRayDirection(), rayDesc.Direction, payload.light / bias);
hitValue.light += brdf.evaluate(-WorldRayDirection(), rayDesc.Direction, payload.light / bias);
}
hitValue.light /= p;
//hitValue.light /= p;
}
+3 -6
View File
@@ -1,4 +1,5 @@
import MaterialParameter;
import BRDF;
struct RayTracingParams
{
@@ -12,14 +13,10 @@ struct RayTracingParams
layout(set=5)
ParameterBlock<RayTracingParams> pRayTracingParams;
struct CallablePayload
{
FragmentParameter params;
float3 color;
};
struct RayPayload
{
LightingParameter params;
MaterialParameter materialParams;
float3 light;
float emissive;
uint depth;