Adding normal mapping
This commit is contained in:
+17
-20
@@ -2,7 +2,7 @@ import Common;
|
||||
|
||||
interface IBRDF
|
||||
{
|
||||
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 normal_WS, float3 lightColor);
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor);
|
||||
float3 evaluateAmbient();
|
||||
};
|
||||
|
||||
@@ -19,13 +19,12 @@ struct Phong : IBRDF
|
||||
normal = float3(0, 0, 1);
|
||||
}
|
||||
|
||||
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 n, float3 lightColor)
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
|
||||
{
|
||||
float3 normal_TS = normal;
|
||||
float3 normal_WS = n;//normalize(mul(tbn, normal_TS));
|
||||
float3 nDotL = dot(normal_WS, lightDir_WS);
|
||||
float3 r = 2 * (nDotL) * normal_WS - lightDir_WS;
|
||||
float rDotV = dot(r, viewDir_WS);
|
||||
float3 nDotL = dot(normal_TS, lightDir_TS);
|
||||
float3 r = 2 * (nDotL) * normal_TS - lightDir_TS;
|
||||
float rDotV = dot(r, viewDir_TS);
|
||||
|
||||
return lightColor * (baseColor * max(nDotL, 0.0));// + specular * pow(max(rDotV, 0.0), max(shininess, 1)));
|
||||
}
|
||||
@@ -49,12 +48,11 @@ struct BlinnPhong : IBRDF
|
||||
normal = float3(0, 0, 1);
|
||||
}
|
||||
|
||||
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 normal_WS, float3 lightColor)
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
|
||||
{
|
||||
float3 normal_TS = normalize(normal);
|
||||
//float3 normal_WS = normalize(mul(tbn, normal_TS));
|
||||
float diffuse = max(dot(normal_WS, lightDir_WS), 0);
|
||||
float3 h = normalize(lightDir_WS + viewDir_WS);
|
||||
float diffuse = max(dot(normal_TS, lightDir_TS), 0);
|
||||
float3 h = normalize(lightDir_TS + viewDir_TS);
|
||||
float specular = pow(saturate(dot(normal_TS, h)), shininess);
|
||||
|
||||
return (baseColor * diffuse * lightColor) + (specularColor * specular);
|
||||
@@ -76,11 +74,10 @@ struct CelShading : IBRDF
|
||||
normal = float3(0, 0, 1);
|
||||
}
|
||||
|
||||
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 n, float3 lightColor)
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
|
||||
{
|
||||
float3 normal_TS = normalize(normal);
|
||||
float3 normal_WS = normalize(mul(tbn, normal_TS));
|
||||
float nDotL = dot(normal_WS, lightDir_WS);
|
||||
float nDotL = dot(normal_TS, lightDir_TS);
|
||||
float diffuse = max(nDotL, 0);
|
||||
|
||||
float3 darkenedBase = baseColor * 0.8;
|
||||
@@ -148,21 +145,21 @@ struct CookTorrance : IBRDF
|
||||
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
|
||||
}
|
||||
|
||||
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 normal_WS, float3 lightColor)
|
||||
float3 evaluate(float3 viewDir_TS, float3 lightDir_TS, float3 lightColor)
|
||||
{
|
||||
float3 n = normal_WS;//normalize(mul(tbn, normal));
|
||||
float3 h = normalize(lightDir_WS + viewDir_WS);
|
||||
float3 n = normal;//normalize(mul(tbn, normal));
|
||||
float3 h = normalize(lightDir_TS + viewDir_TS);
|
||||
|
||||
float3 F0 = float3(0.04);
|
||||
F0 = lerp(F0, baseColor, metallic);
|
||||
|
||||
float3 F = FresnelSchlick(max(dot(h, viewDir_WS), 0.0), F0);
|
||||
float3 F = FresnelSchlick(max(dot(h, viewDir_TS), 0.0), F0);
|
||||
|
||||
float NDF = TrowbridgeReitzGGX(n, h);
|
||||
float G = Smith(n, viewDir_WS, lightDir_WS);
|
||||
float G = Smith(n, viewDir_TS, lightDir_TS);
|
||||
|
||||
float3 num = NDF * G * F;
|
||||
float denom = 4.0 * max(dot(n, viewDir_WS), 0.0) * max(dot(n, lightDir_WS), 0.0) + 0.000001;
|
||||
float denom = 4.0 * max(dot(n, viewDir_TS), 0.0) * max(dot(n, lightDir_TS), 0.0) + 0.000001;
|
||||
float3 specular = num / denom;
|
||||
|
||||
float3 k_s = F;
|
||||
@@ -170,7 +167,7 @@ struct CookTorrance : IBRDF
|
||||
|
||||
k_d *= 1.0 - metallic;
|
||||
|
||||
float nDotL = max(dot(n, lightDir_WS), 0.0);
|
||||
float nDotL = max(dot(n, lightDir_TS), 0.0);
|
||||
|
||||
float3 result = (k_d * baseColor / PI + specular) * nDotL * lightColor;
|
||||
return result * ambientOcclusion;
|
||||
|
||||
@@ -14,7 +14,8 @@ struct DirectionalLight : ILightEnv
|
||||
|
||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||
{
|
||||
return brdf.evaluate(params.tbn, params.viewDir_WS, -normalize(direction.xyz), params.normal_WS, color.xyz);
|
||||
float3 dir_TS = mul(params.tbn, -normalize(direction.xyz));
|
||||
return brdf.evaluate(params.viewDir_TS, dir_TS, color.xyz);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,10 +26,11 @@ struct PointLight : ILightEnv
|
||||
|
||||
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
|
||||
{
|
||||
float3 lightDir_WS = position_WS.xyz - params.position_WS;
|
||||
float d = length(lightDir_WS);
|
||||
float3 pos_TS = mul(params.tbn, position_WS.xyz);
|
||||
float3 lightDir_TS = pos_TS.xyz - params.position_TS;
|
||||
float d = length(lightDir_TS);
|
||||
float illuminance = max(1 - d / colorRange.w, 0);
|
||||
return illuminance * brdf.evaluate(params.tbn, params.viewDir_WS, normalize(lightDir_WS), normalize(params.normal_WS), colorRange.xyz);
|
||||
return illuminance * brdf.evaluate(params.viewDir_TS, normalize(lightDir_TS), colorRange.xyz);
|
||||
}
|
||||
|
||||
bool insidePlane(Plane plane, float3 position)
|
||||
|
||||
@@ -11,9 +11,9 @@ struct MaterialParameter
|
||||
struct LightingParameter
|
||||
{
|
||||
float3x3 tbn;
|
||||
float3 normal_WS;
|
||||
float3 position_WS;
|
||||
float3 viewDir_WS;
|
||||
float3 normal_TS;
|
||||
float3 position_TS;
|
||||
float3 viewDir_TS;
|
||||
};
|
||||
|
||||
// data passed to fragment shader
|
||||
@@ -42,10 +42,11 @@ struct FragmentParameter
|
||||
LightingParameter getLightingParameter()
|
||||
{
|
||||
LightingParameter result;
|
||||
result.tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
||||
result.position_WS = position_WS;
|
||||
result.viewDir_WS = normalize(cameraPos_WS - position_WS);
|
||||
result.normal_WS = normal_WS;
|
||||
float3x3 tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
|
||||
result.tbn = tbn;
|
||||
result.position_TS = mul(tbn, position_WS);
|
||||
result.viewDir_TS = mul(tbn, normalize(cameraPos_WS - position_WS));
|
||||
result.normal_TS = mul(tbn, normal_WS);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -3,40 +3,55 @@ import MaterialParameter;
|
||||
import LightEnv;
|
||||
import Scene;
|
||||
import RayTracingData;
|
||||
import VertexData;
|
||||
import Material;
|
||||
import MATERIAL_FILE_NAME;
|
||||
|
||||
// simplification: all BLAS only have 1 geometry
|
||||
|
||||
[shader("closesthit")]
|
||||
void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr)
|
||||
{
|
||||
//const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
|
||||
const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y);
|
||||
|
||||
//InstanceData inst = pScene.instances[InstanceID()];
|
||||
//MeshData m = pScene.meshData[InstanceID()];
|
||||
//
|
||||
//// 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);
|
||||
//
|
||||
//FragmentParameter f0 = attr0.getParameter(inst.transformMatrix);
|
||||
//FragmentParameter f1 = attr1.getParameter(inst.transformMatrix);
|
||||
//FragmentParameter f2 = attr2.getParameter(inst.transformMatrix);
|
||||
//
|
||||
//FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
||||
//
|
||||
//CallablePayload callable;
|
||||
//callable.params = params;
|
||||
//
|
||||
//CallShader(InstanceID(), callable);
|
||||
InstanceData inst = pScene.instances[InstanceID()];
|
||||
MeshData m = pScene.meshData[InstanceID()];
|
||||
|
||||
hitValue.color = float3(1, 0, 0);
|
||||
// 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);
|
||||
|
||||
FragmentParameter f0 = attr0.getParameter(inst.transformMatrix);
|
||||
FragmentParameter f1 = attr1.getParameter(inst.transformMatrix);
|
||||
FragmentParameter f2 = attr2.getParameter(inst.transformMatrix);
|
||||
|
||||
FragmentParameter params = FragmentParameter.interpolate(f0, f1, f2, barycentricCoords);
|
||||
|
||||
LightingParameter lightingParams = params.getLightingParameter();
|
||||
MaterialParameter materialParams = params.getMaterialParameter();
|
||||
let brdf = Material.prepare(materialParams);
|
||||
|
||||
float3 result = float3(0, 0, 0);
|
||||
for(int i = 0; i < pLightEnv.numDirectionalLights; ++i)
|
||||
{
|
||||
result += pLightEnv.directionalLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
for(uint i = 0; i < pLightEnv.numPointLights; ++i)
|
||||
{
|
||||
result += pLightEnv.pointLights[i].illuminate(lightingParams, brdf);
|
||||
}
|
||||
result += brdf.evaluateAmbient();
|
||||
// gamma correction
|
||||
result = result / (result + float3(1.0));
|
||||
result = pow(result, float3(1.0/2.2));
|
||||
hitValue.color = getMaterialTextureParameter(0).Sample(getMaterialSamplerParameter(0), params.texCoords[0]).xyz;
|
||||
}
|
||||
Reference in New Issue
Block a user