Tangent space works again

This commit is contained in:
Dynamitos
2024-12-30 23:47:24 +01:00
parent f3b6ed31dc
commit 0fb5320a30
9 changed files with 27 additions and 27 deletions
+2 -2
View File
@@ -34,7 +34,7 @@ struct Phong : IBRDF
float3 r = 2 * (nDotL) * normal_WS - lightDir_WS; float3 r = 2 * (nDotL) * normal_WS - lightDir_WS;
float rDotV = dot(r, viewDir_WS); float rDotV = dot(r, viewDir_WS);
return lightColor * (baseColor * max(nDotL, 0.0));// + specular * pow(max(rDotV, 0.0), max(shininess, 1))); return lightColor * (baseColor * max(nDotL, 0.0)) + specular * pow(max(rDotV, 0.0), max(shininess, 1));
} }
float3 evaluateAmbient() float3 evaluateAmbient()
@@ -77,7 +77,7 @@ struct BlinnPhong : IBRDF
float3 h = normalize(lightDir_WS + viewDir_WS); float3 h = normalize(lightDir_WS + viewDir_WS);
float specular = pow(saturate(dot(normal_WS, h)), shininess); float specular = pow(saturate(dot(normal_WS, h)), shininess);
return (baseColor * diffuse * lightColor) + (specularColor * specular); return (baseColor * diffuse * lightColor);// + (specularColor * specular);
} }
float3 evaluateAmbient() float3 evaluateAmbient()
+1 -1
View File
@@ -14,7 +14,7 @@ struct DirectionalLight : ILightEnv
float3 illuminate<B:IBRDF>(LightingParameter params, B brdf) float3 illuminate<B:IBRDF>(LightingParameter params, B brdf)
{ {
float3 dir_WS = normalize(direction.xyz); float3 dir_WS = -normalize(direction.xyz);
return brdf.evaluate(params.tangentToWorld, params.viewDir_WS, dir_WS, color.xyz); return brdf.evaluate(params.tangentToWorld, params.viewDir_WS, dir_WS, color.xyz);
} }
}; };
+8 -5
View File
@@ -12,6 +12,7 @@ struct LightingParameter
float3x3 tangentToWorld; float3x3 tangentToWorld;
float3 viewDir_WS; float3 viewDir_WS;
float3 position_WS; float3 position_WS;
float3 normal_WS;
} }
@@ -91,12 +92,15 @@ struct FragmentParameter
result.tangentToWorld = getTangentToWorld(); result.tangentToWorld = getTangentToWorld();
result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS); result.viewDir_WS = normalize(pViewParams.cameraPosition_WS.xyz - position_WS);
result.position_WS = position_WS; result.position_WS = position_WS;
result.normal_WS = normalize(normal_WS);
return result; return result;
} }
float3x3 getTangentToWorld() float3x3 getTangentToWorld()
{ {
return float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS)); // in theory, transposing would make this a world-to-tangent matrix, but because we are building the matrix ourselves
// and something with matrix layouts is working the opposite direction, we need to transpose here
return transpose(float3x3(tangent_WS, biTangent_WS, normal_WS));
} }
#endif #endif
static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords) static FragmentParameter interpolate(FragmentParameter f0, FragmentParameter f1, FragmentParameter f2, float3 barycentricCoords)
@@ -138,10 +142,9 @@ struct VertexAttributes
FragmentParameter result; FragmentParameter result;
result.position_CS = clipPos; result.position_CS = clipPos;
#ifndef POS_ONLY #ifndef POS_ONLY
float3x3 normalMatrix = transpose(float3x3(inverseTransformMatrix)); result.tangent_WS = normalize(mul(transformMatrix, float4(tangent_MS, 0)).xyz);
result.tangent_WS = mul(normalMatrix, tangent_MS); result.biTangent_WS = normalize(mul(transformMatrix, float4(biTangent_MS, 0)).xyz);
result.biTangent_WS = mul(normalMatrix, biTangent_MS); result.normal_WS = normalize(mul(transformMatrix, float4(normal_MS, 0)).xyz);
result.normal_WS = mul(normalMatrix, normal_MS);
result.vertexColor = vertexColor; result.vertexColor = vertexColor;
result.position_WS = worldPos.xyz; result.position_WS = worldPos.xyz;
result.texCoords0 = float4(texCoords[0], texCoords[1]); result.texCoords0 = float4(texCoords[0], texCoords[1]);
+3 -2
View File
@@ -1,10 +1,11 @@
import VertexData; import VertexData;
import Common; import Common;
import Scene; import Scene;
import MaterialParameter;
struct SkinnedMeshVertexData : VertexData struct SkinnedMeshVertexData
{ {
SkinnedMeshVertexAttributes getAttributes(uint index, float4x4 transform) VertexInput getAttributes(uint index, float4x4 transform)
{ {
StaticMeshVertexAttributes attr; StaticMeshVertexAttributes attr;
float4x4 boneTransform = float4x4(1.0f); float4x4 boneTransform = float4x4(1.0f);
+7 -7
View File
@@ -372,12 +372,6 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array<PTextureAsset>&
aiShadingMode mode; aiShadingMode mode;
material->Get(AI_MATKEY_SHADING_MODEL, mode); material->Get(AI_MATKEY_SHADING_MODEL, mode);
switch (mode) { switch (mode) {
case aiShadingMode_Blinn:
brdf.profile = "BlinnPhong";
brdf.variables["specularColor"] = outputSpecular;
brdf.variables["ambient"] = outputAmbient;
brdf.variables["shininess"] = outputShininess;
break;
case aiShadingMode_Phong: case aiShadingMode_Phong:
brdf.profile = "Phong"; brdf.profile = "Phong";
brdf.variables["specular"] = outputSpecular; brdf.variables["specular"] = outputSpecular;
@@ -387,7 +381,6 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array<PTextureAsset>&
case aiShadingMode_Toon: case aiShadingMode_Toon:
brdf.profile = "CelShading"; brdf.profile = "CelShading";
break; break;
default:
case aiShadingMode_CookTorrance: case aiShadingMode_CookTorrance:
brdf.profile = "CookTorrance"; brdf.profile = "CookTorrance";
brdf.variables["roughness"] = outputRoughness; brdf.variables["roughness"] = outputRoughness;
@@ -396,6 +389,13 @@ void MeshLoader::loadMaterials(const aiScene* scene, const Array<PTextureAsset>&
brdf.variables["ambientOcclusion"] = outputAmbient; brdf.variables["ambientOcclusion"] = outputAmbient;
} }
break; break;
default:
case aiShadingMode_Blinn:
brdf.profile = "BlinnPhong";
brdf.variables["specularColor"] = outputSpecular;
brdf.variables["ambient"] = outputAmbient;
brdf.variables["shininess"] = outputShininess;
break;
}; };
uint32 twoSided = false; uint32 twoSided = false;
float opacity = 1.0f; float opacity = 1.0f;
-7
View File
@@ -96,13 +96,6 @@ int main() {
.filePath = sourcePath / "import/textures/skybox.jpg", .filePath = sourcePath / "import/textures/skybox.jpg",
.type = TextureImportType::TEXTURE_CUBEMAP, .type = TextureImportType::TEXTURE_CUBEMAP,
}); });
AssetImporter::importTexture(TextureImportArgs{
.filePath = sourcePath / "import/textures/brickwall.jpg",
});
AssetImporter::importTexture(TextureImportArgs{
.filePath = sourcePath / "import/textures/brickwall_normal.jpg",
.type = TextureImportType::TEXTURE_NORMAL,
});
// AssetImporter::importMesh(MeshImportArgs{ // AssetImporter::importMesh(MeshImportArgs{
// .filePath = sourcePath / "import/models/ship.fbx", // .filePath = sourcePath / "import/models/ship.fbx",
// .importPath = "ship", // .importPath = "ship",
+1 -1
View File
@@ -196,7 +196,7 @@ void BasePass::render() {
}, },
.rasterizationState = .rasterizationState =
{ {
.cullMode = Gfx::SE_CULL_MODE_NONE, .cullMode = Gfx::SE_CULL_MODE_BACK_BIT,
}, },
.colorBlend = .colorBlend =
{ {
+4 -1
View File
@@ -77,7 +77,10 @@ class VertexData {
const Array<TransparentDraw>& getTransparentData() const { return transparentData; } const Array<TransparentDraw>& getTransparentData() const { return transparentData; }
const Array<Gfx::PBottomLevelAS>& getRayTracingData() const { return rayTracingScene; } const Array<Gfx::PBottomLevelAS>& getRayTracingData() const { return rayTracingScene; }
const MeshData& getMeshData(MeshId id) const { return meshData[id]; } const MeshData& getMeshData(MeshId id) const { return meshData[id]; }
void registerBottomLevelAccelerationStructure(Gfx::PBottomLevelAS blas) { dataToBuild.add(blas); } void registerBottomLevelAccelerationStructure(Gfx::PBottomLevelAS blas) {
assert(blas != nullptr);
dataToBuild.add(blas);
}
uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; } uint64 getIndicesOffset(uint32 meshletIndex) { return meshlets[meshletIndex].indicesOffset; }
uint64 getNumInstances() const { return instanceData.size(); } uint64 getNumInstances() const { return instanceData.size(); }
static List<VertexData*> getList(); static List<VertexData*> getList();
+1 -1
View File
@@ -142,7 +142,7 @@ void Seele::beginCompilation(const ShaderCompilationInfo& info, SlangCompileTarg
for (size_t i = 0; i < signature->getParameterCount(); ++i) { for (size_t i = 0; i < signature->getParameterCount(); ++i) {
auto param = signature->getParameterByIndex(i); auto param = signature->getParameterByIndex(i);
layout->addMapping(param->getName(), param->getBindingIndex()); layout->addMapping(param->getName(), param->getBindingIndex());
std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl; //std::cout << param->getName() << ": " << param->getBindingIndex() << std::endl;
} }
// workaround // workaround