diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index c38cd3d..136818e 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -125,7 +125,10 @@ struct VertexAttributes { float3 position_MS; #ifndef POS_ONLY - float4 qTangent; + //float4 qTangent; + float3 normal_MS; + float3 tangent_MS; + float3 biTangent_MS; float3 vertexColor; float2 texCoords[MAX_TEXCOORDS]; #endif @@ -138,10 +141,10 @@ struct VertexAttributes FragmentParameter result; result.position_CS = clipPos; #ifndef POS_ONLY - float3x3 tbn = qTangentToMatrix(qTangent); - result.tangent_WS = normalize(mul(transformMatrix, float4(tbn[0], 0)).xyz); - result.biTangent_WS = normalize(mul(transformMatrix, float4(tbn[1], 0)).xyz); - result.normal_WS = normalize(mul(transformMatrix, float4(tbn[2], 0)).xyz); + //float3x3 tbn = qTangentToMatrix(qTangent); + result.tangent_WS = normalize(mul(transformMatrix, float4(tangent_MS, 0)).xyz); + result.biTangent_WS = normalize(mul(transformMatrix, float4(biTangent_MS, 0)).xyz); + result.normal_WS = normalize(mul(transformMatrix, float4(normal_MS, 0)).xyz); result.vertexColor = vertexColor; result.position_WS = worldPos.xyz; result.texCoords0 = float4(texCoords[0], texCoords[1]); diff --git a/res/shaders/lib/StaticMeshVertexData.slang b/res/shaders/lib/StaticMeshVertexData.slang index dc5d23f..f597dc3 100644 --- a/res/shaders/lib/StaticMeshVertexData.slang +++ b/res/shaders/lib/StaticMeshVertexData.slang @@ -14,9 +14,10 @@ struct StaticMeshVertexData VertexAttributes attributes; attributes.position_MS = float3(positions[index * 3 + 0], positions[index * 3 + 1], positions[index * 3 + 2]); #ifndef POS_ONLY - attributes.qTangent = qTangent[index]; - //attributes.tangent_MS = float3(tangents[index * 3 + 0], tangents[index * 3 + 1], tangents[index * 3 + 2]); - //attributes.biTangent_MS = float3(biTangents[index * 3 + 0], biTangents[index * 3 + 1], biTangents[index * 3 + 2]); + //attributes.qTangent = qTangent[index]; + attributes.normal_MS = float3(normals[index * 3 + 0], normals[index * 3 + 1], normals[index * 3 + 2]); + attributes.tangent_MS = float3(tangents[index * 3 + 0], tangents[index * 3 + 1], tangents[index * 3 + 2]); + attributes.biTangent_MS = float3(biTangents[index * 3 + 0], biTangents[index * 3 + 1], biTangents[index * 3 + 2]); for(uint i = 0; i < MAX_TEXCOORDS; ++i) { attributes.texCoords[i] = float2(uint16ToFloat(texCoords[i][index * 2 + 0]), uint16ToFloat(texCoords[i][index * 2 + 1])); @@ -26,9 +27,9 @@ struct StaticMeshVertexData return attributes; } StructuredBuffer positions; - StructuredBuffer qTangent; - //StructuredBuffer tangents; - //StructuredBuffer biTangents; + StructuredBuffer normals; + StructuredBuffer tangents; + StructuredBuffer biTangents; StructuredBuffer color; StructuredBuffer texCoords[MAX_TEXCOORDS]; }; diff --git a/res/shaders/raytracing/ClosestHit.slang b/res/shaders/raytracing/ClosestHit.slang index 0d4a3d9..3c5b2cf 100644 --- a/res/shaders/raytracing/ClosestHit.slang +++ b/res/shaders/raytracing/ClosestHit.slang @@ -71,7 +71,7 @@ import MATERIAL_FILE_NAME; */ -const float eps = 1e-5; +static const float eps = 1e-5; [shader("closesthit")] void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr) { diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 8136c0f..a39e896 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -535,8 +535,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArraymNumVertices); } Array normals(mesh->mNumVertices); - //Array tangents(mesh->mNumVertices); - //Array biTangents(mesh->mNumVertices); + Array tangents(mesh->mNumVertices); + Array biTangents(mesh->mNumVertices); Array colors(mesh->mNumVertices); for (int32 i = 0; i < mesh->mNumVertices; ++i) { @@ -555,9 +555,9 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArraymTangents[i].x, mesh->mTangents[i].y, mesh->mTangents[i].z); biTangent = Vector(mesh->mBitangents[i].x, mesh->mBitangents[i].y, mesh->mBitangents[i].z); } - normals[i] = encodeQTangent(Matrix3(tangent, biTangent, normal)); - //tangents[i] = tangent; - //biTangents[i] = biTangent; + normals[i] = normal; // encodeQTangent(Matrix3(tangent, biTangent, normal)); + tangents[i] = tangent; + biTangents[i] = biTangent; if (mesh->HasVertexColors(0)) { colors[i] = StaticMeshVertexData::ColorType(mesh->mColors[0][i].r * 65535, mesh->mColors[0][i].g * 65535, mesh->mColors[0][i].b * 65535); @@ -571,8 +571,8 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArrayloadTexCoords(offset, i, texCoords[i]); } vertexData->loadNormals(offset, normals); -// vertexData->loadTangents(offset, tangents); -// vertexData->loadBitangents(offset, biTangents); + vertexData->loadTangents(offset, tangents); + vertexData->loadBitangents(offset, biTangents); vertexData->loadColors(offset, colors); Array indices(mesh->mNumFaces * 3); diff --git a/src/Engine/Graphics/StaticMeshVertexData.cpp b/src/Engine/Graphics/StaticMeshVertexData.cpp index f8dfa51..5b07c96 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.cpp +++ b/src/Engine/Graphics/StaticMeshVertexData.cpp @@ -36,16 +36,16 @@ void StaticMeshVertexData::loadNormals(uint64 offset, const Array& d dirty = true; } -//void StaticMeshVertexData::loadTangents(uint64 offset, const Array& data) { -// assert(offset + data.size() <= head); -// std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType)); -// dirty = true; -//} -//void StaticMeshVertexData::loadBitangents(uint64 offset, const Array& data) { -// assert(offset + data.size() <= head); -// std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType)); -// dirty = true; -//} +void StaticMeshVertexData::loadTangents(uint64 offset, const Array& data) { + assert(offset + data.size() <= head); + std::memcpy(tanData.data() + offset, data.data(), data.size() * sizeof(TangentType)); + dirty = true; +} +void StaticMeshVertexData::loadBitangents(uint64 offset, const Array& data) { + assert(offset + data.size() <= head); + std::memcpy(bitData.data() + offset, data.data(), data.size() * sizeof(BiTangentType)); + dirty = true; +} void StaticMeshVertexData::loadColors(uint64 offset, const Array& data) { assert(offset + data.size() <= head); @@ -68,18 +68,18 @@ void StaticMeshVertexData::serializeMesh(MeshId id, uint64 numVertices, ArchiveB } Array pos(numVertices); Array nor(numVertices); - //Array tan(numVertices); - //Array bit(numVertices); + Array tan(numVertices); + Array bit(numVertices); Array col(numVertices); std::memcpy(pos.data(), posData.data() + offset, numVertices * sizeof(PositionType)); std::memcpy(nor.data(), norData.data() + offset, numVertices * sizeof(NormalType)); - //std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType)); - //std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType)); + std::memcpy(tan.data(), tanData.data() + offset, numVertices * sizeof(TangentType)); + std::memcpy(bit.data(), bitData.data() + offset, numVertices * sizeof(BiTangentType)); std::memcpy(col.data(), colData.data() + offset, numVertices * sizeof(ColorType)); Serialization::save(buffer, pos); Serialization::save(buffer, nor); - //Serialization::save(buffer, tan); - //Serialization::save(buffer, bit); + Serialization::save(buffer, tan); + Serialization::save(buffer, bit); Serialization::save(buffer, col); } @@ -98,23 +98,23 @@ uint64 StaticMeshVertexData::deserializeMesh(MeshId id, ArchiveBuffer& buffer) { } Array pos; Array nor; - //Array tan; - //Array bit; + Array tan; + Array bit; Array col; Serialization::load(buffer, pos); Serialization::load(buffer, nor); - //Serialization::load(buffer, tan); - //Serialization::load(buffer, bit); + Serialization::load(buffer, tan); + Serialization::load(buffer, bit); Serialization::load(buffer, col); loadPositions(offset, pos); loadNormals(offset, nor); - //loadTangents(offset, tan); - //loadBitangents(offset, bit); + loadTangents(offset, tan); + loadBitangents(offset, bit); loadColors(offset, col); result += pos.size() * sizeof(PositionType); result += nor.size() * sizeof(NormalType); - //result += tan.size() * sizeof(TangentType); - //result += bit.size() * sizeof(BiTangentType); + result += tan.size() * sizeof(TangentType); + result += bit.size() * sizeof(BiTangentType); result += col.size() * sizeof(ColorType); return result; } @@ -134,17 +134,17 @@ void StaticMeshVertexData::init(Gfx::PGraphics _graphics) { .binding = 2, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, }); - //descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ - // .binding = 3, - // .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, - //}); - //descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ - // .binding = 4, - // .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, - //}); descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 3, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, + }); + descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 4, + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, + }); + descriptorLayout->addDescriptorBinding(Gfx::DescriptorBinding{ + .binding = 5, + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_STORAGE_BUFFER, .descriptorCount = MAX_TEXCOORDS, }); descriptorLayout->create(); @@ -176,8 +176,8 @@ Gfx::PDescriptorSet StaticMeshVertexData::getVertexDataSet() { return descriptor void StaticMeshVertexData::resizeBuffers() { posData.resize(verticesAllocated); norData.resize(verticesAllocated); -// tanData.resize(verticesAllocated); -// bitData.resize(verticesAllocated); + tanData.resize(verticesAllocated); + bitData.resize(verticesAllocated); colData.resize(verticesAllocated); for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { texData[i].resize(verticesAllocated); @@ -202,22 +202,22 @@ void StaticMeshVertexData::updateBuffers() { }, .name = "Normals", }); - //tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{ - // .sourceData = - // { - // .size = verticesAllocated * sizeof(TangentType), - // .data = (uint8*)tanData.data(), - // }, - // .name = "Tangents", - //}); - //biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{ - // .sourceData = - // { - // .size = verticesAllocated * sizeof(BiTangentType), - // .data = (uint8*)bitData.data(), - // }, - // .name = "BiTangents", - //}); + tangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{ + .sourceData = + { + .size = verticesAllocated * sizeof(TangentType), + .data = (uint8*)tanData.data(), + }, + .name = "Tangents", + }); + biTangents = graphics->createShaderBuffer(ShaderBufferCreateInfo{ + .sourceData = + { + .size = verticesAllocated * sizeof(BiTangentType), + .data = (uint8*)bitData.data(), + }, + .name = "BiTangents", + }); colors = graphics->createShaderBuffer(ShaderBufferCreateInfo{ .sourceData = { @@ -240,11 +240,11 @@ void StaticMeshVertexData::updateBuffers() { descriptorSet = descriptorLayout->allocateDescriptorSet(); descriptorSet->updateBuffer(0, 0, positions); descriptorSet->updateBuffer(1, 0, normals); -// descriptorSet->updateBuffer(2, 0, tangents); -// descriptorSet->updateBuffer(3, 0, biTangents); - descriptorSet->updateBuffer(2, 0, colors); + descriptorSet->updateBuffer(2, 0, tangents); + descriptorSet->updateBuffer(3, 0, biTangents); + descriptorSet->updateBuffer(4, 0, colors); for (size_t i = 0; i < MAX_TEXCOORDS; ++i) { - descriptorSet->updateBuffer(3, i, texCoords[i]); + descriptorSet->updateBuffer(5, i, texCoords[i]); } descriptorSet->writeChanges(); } diff --git a/src/Engine/Graphics/StaticMeshVertexData.h b/src/Engine/Graphics/StaticMeshVertexData.h index c2d3722..dafebc8 100644 --- a/src/Engine/Graphics/StaticMeshVertexData.h +++ b/src/Engine/Graphics/StaticMeshVertexData.h @@ -10,9 +10,9 @@ namespace Seele { class StaticMeshVertexData : public VertexData { public: using PositionType = Vector; - using NormalType = Vector4; - //using TangentType = Vector; - //using BiTangentType = Vector; + using NormalType = Vector; + using TangentType = Vector; + using BiTangentType = Vector; using TexCoordType = U16Vector2; using ColorType = U16Vector; @@ -22,8 +22,8 @@ class StaticMeshVertexData : public VertexData { void loadPositions(uint64 offset, const Array& data); void loadTexCoords(uint64 offset, uint64 index, const Array& data); void loadNormals(uint64 offset, const Array& data); - //void loadTangents(uint64 offset, const Array& data); - //void loadBitangents(uint64 offset, const Array& data); + void loadTangents(uint64 offset, const Array& data); + void loadBitangents(uint64 offset, const Array& data); void loadColors(uint64 offset, const Array& data); virtual void serializeMesh(MeshId id, uint64 numVertices, ArchiveBuffer& buffer) override; virtual uint64 deserializeMesh(MeshId id, ArchiveBuffer& buffer) override; @@ -49,8 +49,8 @@ class StaticMeshVertexData : public VertexData { Array posData; Array texData[MAX_TEXCOORDS]; Array norData; -// Array tanData; -// Array bitData; + Array tanData; + Array bitData; Array colData; Gfx::ODescriptorLayout descriptorLayout; Gfx::PDescriptorSet descriptorSet;