Trying to fix shading

This commit is contained in:
Dynamitos
2023-12-03 00:29:02 +01:00
parent 6c156d3bc2
commit 178f48120d
12 changed files with 115 additions and 45 deletions
+2 -2
View File
@@ -71,8 +71,8 @@ string(TOLOWER ${SLANG_ROOT}/bin/windows-x64/release SLANG_BINARY_DIR)
ExternalProject_Add(slang-build
SOURCE_DIR ${SLANG_ROOT}
BINARY_DIR ${SLANG_ROOT}
CONFIGURE_COMMAND ${SLANG_ROOT}/premake.bat vs2019 --file=${SLANG_ROOT}/premake5.lua gmake --arch=x64 --deps=true
BUILD_COMMAND msbuild -p:PlatformToolset=v143 -p:Configuration=Release -p:Platform=x64 build/visual-studio/slang/slang.vcxproj
CONFIGURE_COMMAND ${SLANG_ROOT}/premake.bat vs2019 --file=${SLANG_ROOT}/premake5.lua --arch=x64 --deps=true
BUILD_COMMAND msbuild -p:PlatformToolset=v143 -p:Configuration=Release -p:Platform=x64 slang.sln
INSTALL_COMMAND ""
)
elseif(UNIX)
+1 -1
+2 -2
View File
@@ -25,12 +25,12 @@ struct BlinnPhong : IBRDF
float3 evaluate(float3x3 tbn, float3 viewDir_WS, float3 lightDir_WS, float3 lightColor)
{
float3 normal_WS = mul(tbn, normal);
float3 normal_WS = mul(normal, tbn);
float diffuse = max(dot(normal_WS, lightDir_WS), 0);
float3 h = lightDir_WS + viewDir_WS;
float specular = dot(normal_WS, h);
return baseColor * (diffuse + specular) * lightColor;
return (viewDir_WS + float3(1, 1, 1)) / 2;//baseColor * (diffuse + specular) * lightColor;
}
};
+1 -1
View File
@@ -28,7 +28,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.tbn, params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz);
return brdf.evaluate(params.tbn, params.viewDir_WS, normalize(lightDir_WS), colorRange.xyz);
}
bool insidePlane(Plane plane)
+1 -1
View File
@@ -39,7 +39,7 @@ struct FragmentParameter
LightingParameter result;
result.tbn = float3x3(normalize(tangent_WS), normalize(biTangent_WS), normalize(normal_WS));
result.position_WS = position_WS;
result.viewDir_WS = viewDir_WS;
result.viewDir_WS = normalize(viewDir_WS);
return result;
}
};
+9 -5
View File
@@ -130,12 +130,8 @@ void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset)
if(obj.is_string())
{
std::string str = obj.get<std::string>();
if (expressions.find([&str, &expressions](const OShaderExpression& exp) {return exp->key == str; }) != expressions.end())
{
return str;
}
OConstantExpression c = new ConstantExpression(str, ExpressionType::UNKNOWN);
std::string name = std::format("Const{0}", auxKey++);
std::string name = std::format("const_{0}", auxKey++);
c->key = name;
expressions.add(std::move(c));
return name;
@@ -151,6 +147,14 @@ void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset)
{
auto& obj = param.value();
std::string exp = obj["exp"].get<std::string>();
if (exp.compare("Const") == 0)
{
OConstantExpression p = new ConstantExpression();
std::string name = std::format("{0}", key++);
p->key = name;
p->expr = obj["value"];
expressions.add(std::move(p));
}
if(exp.compare("Add") == 0)
{
OAddExpression p = new AddExpression();
+82 -27
View File
@@ -48,11 +48,14 @@ void MeshLoader::loadMaterials(const aiScene* scene, const std::string& baseName
{
aiMaterial* material = scene->mMaterials[i];
json matCode;
std::string materialName = std::format("{0}{1}{2}", baseName, material->GetName().C_Str(), char(i+'a'));
std::string materialName = std::format("{0}{1}{2}", baseName, material->GetName().C_Str(), i);
materialName.erase(std::remove(materialName.begin(), materialName.end(), '.'), materialName.end()); // dots break adding the .asset extension later
matCode["name"] = materialName;
matCode["profile"] = "BlinnPhong"; //TODO: other shading models
aiString texPath;
uint32 baseColorIndex = 0;
uint32 normalIndex = 0;
int32 codeIndex = -1;
if(material->GetTexture(aiTextureType_DIFFUSE, 0, &texPath) == AI_SUCCESS)
{
auto texFilename = std::filesystem::path(texPath.C_Str()).stem();
@@ -60,13 +63,11 @@ void MeshLoader::loadMaterials(const aiScene* scene, const std::string& baseName
{
matCode["code"].push_back(
{
{ "exp", "BRDF" },
{ "profile", "BlinnPhong" },
{ "values", {
{"baseColor", "float3(1, 1, 1)"}
}}
{ "exp", "Const" },
{ "value", "float3(1, 1, 1)"}
}
);
baseColorIndex = ++codeIndex;
}
else
{
@@ -88,46 +89,100 @@ void MeshLoader::loadMaterials(const aiScene* scene, const std::string& baseName
{ "exp", "Sample" },
{ "texture", "diffuseTexture" },
{ "sampler", "diffuseSampler" },
{ "coords", "input.texCoords[0]"}
{ "coords", "input.texCoords"}
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Swizzle" },
{ "target", 0 },
{ "target", codeIndex },
{ "comp", json::array({0, 1, 2}) },
}
);
matCode["code"].push_back(
{
{ "exp", "BRDF" },
{ "profile", "BlinnPhong" },
{ "values", {
{"baseColor", 1}
}}
}
);
baseColorIndex = ++codeIndex;
}
}
else
{
matCode["code"].push_back(
{
{ "exp", "BRDF" },
{ "profile", "BlinnPhong" },
{ "values", {
{"baseColor", "input.vertexColor.xyz"}
}}
{ "exp", "Const" },
{ "value", "input.vertexColor.xyz" }
}
);
}
if(material->GetTexture(aiTextureType_SPECULAR, 0, &texPath) == AI_SUCCESS)
{
baseColorIndex = ++codeIndex;
}
if(material->GetTexture(aiTextureType_NORMALS, 0, &texPath) == AI_SUCCESS)
{
auto texFilename = std::filesystem::path(texPath.C_Str()).stem();
AssetImporter::importTexture(TextureImportArgs{
.filePath = meshDirectory / texPath.C_Str(),
.importPath = importPath,
});
matCode["params"]["normalTexture"] =
{
{"type", "Texture2D"},
{"default", texFilename.string()}
};
matCode["params"]["normalSampler"] =
{
{"type", "Sampler"}
};
matCode["code"].push_back(
{
{ "exp", "Sample" },
{ "texture", "normalTexture" },
{ "sampler", "normalSampler" },
{ "coords", "input.texCoords" }
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Swizzle" },
{ "target", codeIndex },
{ "comp", json::array({0, 1, 2}) },
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Mul" },
{ "lhs", "2" },
{ "rhs", codeIndex },
}
);
++codeIndex;
matCode["code"].push_back(
{
{ "exp", "Sub" },
{ "lhs", codeIndex },
{ "rhs", "float3(1, 1, 1)"},
}
);
normalIndex = ++codeIndex;
}
else
{
matCode["code"].push_back(
{
{ "exp", "Const" },
{ "value", "float3(0, 0, 1)" }
}
);
normalIndex = ++codeIndex;
}
matCode["code"].push_back(
{
{ "exp", "BRDF" },
{ "profile", "BlinnPhong" },
{ "values", {
{ "baseColor", baseColorIndex },
{ "normal", normalIndex },
}}
}
);
std::string outMatFilename = materialName.append(".json");
std::ofstream outMatFile = std::ofstream(meshDirectory / outMatFilename);
outMatFile << std::setw(4) << matCode;
@@ -181,7 +236,7 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const Array<PMaterialIns
for (uint32 i = 0; i < mesh->mNumVertices; ++i)
{
positions[i] = Vector(mesh->mVertices[i].x, mesh->mVertices[i].y, mesh->mVertices[i].z);
texCoords[i] = Vector2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].x);
texCoords[i] = Vector2(mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y);
normals[i] = Vector(mesh->mNormals[i].x, mesh->mNormals[i].y, mesh->mNormals[i].z);
tangents[i] = Vector(mesh->mTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z);
biTangents[i] = Vector(mesh->mBitangents[i].x, mesh->mBitangents[i].y, mesh->mBitangents[i].z);
+1 -1
View File
@@ -21,7 +21,7 @@ struct Camera
}
Vector getCameraPosition() const
{
return cameraPos;
return -viewMatrix[3];
}
void mouseMove(float deltaX, float deltaY);
void mouseScroll(float x);
+10 -3
View File
@@ -137,7 +137,7 @@ public:
assert(_data != nullptr);
for (size_type i = 0; i < size; ++i)
{
_data[i] = value;
std::allocator_traits<allocator_type>::construct(allocator, &_data[i], value);
}
}
constexpr explicit Array(size_type size, const allocator_type& alloc = allocator_type())
@@ -147,9 +147,16 @@ public:
{
_data = allocateArray(size);
assert(_data != nullptr);
for (size_type i = 0; i < size; ++i)
if constexpr (std::is_integral_v<T> || std::is_floating_point_v<T>)
{
std::allocator_traits<allocator_type>::construct(allocator, &_data[i]);
std::memset(_data, 0, size * sizeof(T));
}
else
{
for (size_type i = 0; i < size; ++i)
{
std::allocator_traits<allocator_type>::construct(allocator, &_data[i]);
}
}
}
constexpr Array(std::initializer_list<T> init, const allocator_type& alloc = allocator_type())
+1 -1
View File
@@ -418,7 +418,7 @@ Gfx::PDescriptorSet DescriptorPool::allocateDescriptorSet()
{
nextAlloc = new DescriptorPool(graphics, layout);
}
std::cout << "Out of descriptors, forwarding" << std::endl;
//std::cout << "Out of descriptors, forwarding" << std::endl;
return nextAlloc->allocateDescriptorSet();
//throw std::logic_error("Out of descriptor sets");
}
+4
View File
@@ -16,6 +16,10 @@ double Gfx::getCurrentFrameDelta()
void glfwKeyCallback(GLFWwindow* handle, int key, int, int action, int modifier)
{
if (key == -1)
{
return;
}
Window* window = (Window*)glfwGetWindowUserPointer(handle);
window->keyPress((KeyCode)key, (InputAction)action, (KeyModifier)modifier);
}
+1 -1
View File
@@ -346,7 +346,7 @@ std::string MulExpression::evaluate(Map<std::string, std::string>& varState) con
{
std::string varName = std::format("exp_{}", key);
varState[key] = varName;
return std::format("let {} = mul({}, {});\n", varName, varState[inputs.at("lhs").source], varState[inputs.at("rhs").source]);
return std::format("let {} = {} * {};\n", varName, varState[inputs.at("lhs").source], varState[inputs.at("rhs").source]);
}
void MulExpression::save(ArchiveBuffer& buffer) const