From 86bc613c40939e713f836dbccfb2e8a9a2beabc2 Mon Sep 17 00:00:00 2001 From: rdeioris Date: Fri, 26 May 2023 07:25:20 +0200 Subject: [PATCH] first round of tangents generation fixes --- .../glTFRuntimeParserSkeletalMeshes.cpp | 79 ++++++++++--------- .../Private/glTFRuntimeParserStaticMeshes.cpp | 2 +- Source/glTFRuntime/Public/glTFRuntimeParser.h | 4 + 3 files changed, 46 insertions(+), 39 deletions(-) diff --git a/Source/glTFRuntime/Private/glTFRuntimeParserSkeletalMeshes.cpp b/Source/glTFRuntime/Private/glTFRuntimeParserSkeletalMeshes.cpp index 2a7e2535..da63e326 100644 --- a/Source/glTFRuntime/Private/glTFRuntimeParserSkeletalMeshes.cpp +++ b/Source/glTFRuntime/Private/glTFRuntimeParserSkeletalMeshes.cpp @@ -291,6 +291,8 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRefSkeletalMesh->ResetLODInfo(); + const float TangentsDirection = SkeletalMeshContext->SkeletalMeshConfig.bReverseTangents ? -1 : 1; + #if WITH_EDITOR FSkeletalMeshModel* ImportedResource = SkeletalMeshContext->SkeletalMesh->GetImportedModel(); @@ -436,8 +438,13 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRef 0) { + TangentXW[0] = Primitive.Tangents[Primitive.Indices[i - 2]].W; + TangentXW[1] = Primitive.Tangents[Primitive.Indices[i - 1]].W; + TangentXW[2] = Primitive.Tangents[Primitive.Indices[i]].W; + #if ENGINE_MAJOR_VERSION > 4 Triangle.TangentX[0] = FVector3f(FVector(Primitive.Tangents[Primitive.Indices[i - 2]])); Triangle.TangentX[1] = FVector3f(FVector(Primitive.Tangents[Primitive.Indices[i - 1]])); @@ -453,6 +460,9 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRefSkeletalMeshConfig.bIgnoreEmptyMorphTargets && bSkip) { @@ -613,9 +623,9 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRef 4 ModelVertex.Position = FVector3f(Primitive.Positions[Index]); SkeletalMeshContext->BoundingBox += FVector(ModelVertex.Position) * SkeletalMeshContext->SkeletalMeshConfig.BoundsScale; @@ -719,10 +731,12 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRef 4 + TangentXW = Primitive.Tangents[Index].W; ModelVertex.TangentX = FVector4f(Primitive.Tangents[Index]); #else ModelVertex.TangentX = Primitive.Tangents[Index]; #endif + } else { @@ -749,8 +763,10 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRefStaticVertexBuffers.PositionVertexBuffer.VertexPosition(TotalVertexIndex) = ModelVertex.Position; - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(TotalVertexIndex, ModelVertex.TangentX, ModelVertex.GetTangentY(), ModelVertex.TangentZ); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(TotalVertexIndex, ModelVertex.TangentX, TangentY, ModelVertex.TangentZ); LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexUV(TotalVertexIndex, 0, ModelVertex.TexCoord); TMap& BoneMapInUse = Primitive.OverrideBoneMap.Num() > 0 ? Primitive.OverrideBoneMap : MainBoneMap; @@ -845,13 +861,6 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRef 4 - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex, FVector3f(TangentX0), FVector3f(GetTangentY(TangentZ0, TangentX0)), FVector3f(FVector(TangentZ0))); - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 1, FVector3f(TangentX1), FVector3f(GetTangentY(TangentZ1, TangentX1)), FVector3f(FVector(TangentZ1))); - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 2, FVector3f(TangentX2), FVector3f(GetTangentY(TangentZ2, TangentX2)), FVector3f(FVector(TangentZ2))); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex, FVector3f(TangentX0), FVector3f(TangentY0), FVector3f(FVector(TangentZ0))); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 1, FVector3f(TangentX1), FVector3f(TangentY1), FVector3f(FVector(TangentZ1))); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 2, FVector3f(TangentX2), FVector3f(TangentY2), FVector3f(FVector(TangentZ2))); #else - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex, TangentX0, GetTangentY(TangentZ0, TangentX0), TangentZ0); - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 1, TangentX1, GetTangentY(TangentZ1, TangentX1), TangentZ1); - LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 2, TangentX2, GetTangentY(TangentZ2, TangentX2), TangentZ2); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex, TangentX0, TangentY0, TangentZ0); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 1, TangentX1, TangentY1, TangentZ1); + LodRenderData->StaticVertexBuffers.StaticMeshVertexBuffer.SetVertexTangents(VertexIndex + 2, TangentX2, TangentY2, TangentZ2); #endif } else if (!LOD.bHasNormals) // if we are here we need to reapply normals @@ -1008,10 +1011,10 @@ USkeletalMesh* FglTFRuntimeParser::CreateSkeletalMeshFromLODs(TSharedRefMultiSizeIndexContainer.GetIndexBuffer()->AddItem(Index); } #endif - } + } return SkeletalMeshContext->SkeletalMesh; -} + } USkeletalMesh* FglTFRuntimeParser::FinalizeSkeletalMeshWithLODs(TSharedRef SkeletalMeshContext) { @@ -1270,7 +1273,7 @@ USkeletalMesh* FglTFRuntimeParser::FinalizeSkeletalMeshWithLODs(TSharedRefSkeletalMesh->Skeleton->MergeAllBonesToBoneTree(SkeletalMeshContext->SkeletalMesh); #endif - } +} } else { @@ -1281,7 +1284,7 @@ USkeletalMesh* FglTFRuntimeParser::FinalizeSkeletalMeshWithLODs(TSharedRefSkeletalMesh->Skeleton = SkeletonsCache[SkeletalMeshContext->SkinIndex]; #endif - } + } else { #if ENGINE_MAJOR_VERSION > 4 || ENGINE_MINOR_VERSION > 26 @@ -1358,7 +1361,7 @@ USkeletalMesh* FglTFRuntimeParser::FinalizeSkeletalMeshWithLODs(TSharedRefAggGeom = SourceBodySetup->AggGeom; PhysicsAsset->SkeletalBodySetups.Add(NewBodySetup); } - } + } for (UPhysicsConstraintTemplate* ConstraintTemplate : PhysicsAssetTemplate->ConstraintSetup) { UPhysicsConstraintTemplate* NewConstraint = NewObject(PhysicsAsset, NAME_None, RF_Public); @@ -1366,7 +1369,7 @@ USkeletalMesh* FglTFRuntimeParser::FinalizeSkeletalMeshWithLODs(TSharedRefProfileHandles = ConstraintTemplate->ProfileHandles; PhysicsAsset->ConstraintSetup.Add(NewConstraint); } - } + } for (const TPair& PhysicsBody : SkeletalMeshContext->SkeletalMeshConfig.PhysicsBodies) { if (PhysicsBody.Key.IsEmpty()) @@ -1437,7 +1440,7 @@ USkeletalMesh* FglTFRuntimeParser::FinalizeSkeletalMeshWithLODs(TSharedRefSkeletalMesh; -} + } USkeletalMesh* FglTFRuntimeParser::LoadSkeletalMesh(const int32 MeshIndex, const int32 SkinIndex, const FglTFRuntimeSkeletalMeshConfig & SkeletalMeshConfig) { @@ -1793,7 +1796,7 @@ UAnimSequence* FglTFRuntimeParser::LoadSkeletalAnimation(USkeletalMesh * Skeleta CompressionCodec->Tracks[BoneIndex].ScaleKeys.Add(BonesPoses[BoneIndex].GetScale3D()); #endif - } +} } #else #if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 2 @@ -1827,8 +1830,8 @@ UAnimSequence* FglTFRuntimeParser::LoadSkeletalAnimation(USkeletalMesh * Skeleta #else Pair.Value.PosKeys.Add(BoneTransform.GetLocation()); #endif - } } + } else if (Pair.Value.PosKeys.Num() < NumFrames) { #if ENGINE_MAJOR_VERSION > 4 @@ -1857,7 +1860,7 @@ UAnimSequence* FglTFRuntimeParser::LoadSkeletalAnimation(USkeletalMesh * Skeleta #else Pair.Value.RotKeys.Add(BoneTransform.GetRotation()); #endif - } + } } else if (Pair.Value.RotKeys.Num() < NumFrames) { @@ -1886,7 +1889,7 @@ UAnimSequence* FglTFRuntimeParser::LoadSkeletalAnimation(USkeletalMesh * Skeleta #else Pair.Value.ScaleKeys.Add(BoneTransform.GetScale3D()); #endif - } + } } else if (Pair.Value.ScaleKeys.Num() < NumFrames) { @@ -2199,7 +2202,7 @@ UAnimSequence* FglTFRuntimeParser::CreateAnimationFromPose(USkeletalMesh * Skele CompressionCodec->Tracks[BoneIndex].ScaleKeys.Add(BonesPoses[BoneIndex].GetScale3D()); #endif - } +} } #else #if ENGINE_MAJOR_VERSION >= 5 && ENGINE_MINOR_VERSION >= 2 diff --git a/Source/glTFRuntime/Private/glTFRuntimeParserStaticMeshes.cpp b/Source/glTFRuntime/Private/glTFRuntimeParserStaticMeshes.cpp index 6a3cfac4..a432e21e 100644 --- a/Source/glTFRuntime/Private/glTFRuntimeParserStaticMeshes.cpp +++ b/Source/glTFRuntime/Private/glTFRuntimeParserStaticMeshes.cpp @@ -111,7 +111,7 @@ UStaticMesh* FglTFRuntimeParser::LoadStaticMesh_Internal(TSharedRef SectionMaterialMap; diff --git a/Source/glTFRuntime/Public/glTFRuntimeParser.h b/Source/glTFRuntime/Public/glTFRuntimeParser.h index 014b20fb..d0afc8f0 100644 --- a/Source/glTFRuntime/Public/glTFRuntimeParser.h +++ b/Source/glTFRuntime/Public/glTFRuntimeParser.h @@ -846,6 +846,9 @@ struct FglTFRuntimeSkeletalMeshConfig UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "glTFRuntime") EglTFRuntimeTangentsGenerationStrategy TangentsGenerationStrategy; + UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "glTFRuntime") + bool bReverseTangents; + FglTFRuntimeSkeletalMeshConfig() { CacheMode = EglTFRuntimeCacheMode::ReadWrite; @@ -868,6 +871,7 @@ struct FglTFRuntimeSkeletalMeshConfig bAddVirtualBones = false; NormalsGenerationStrategy = EglTFRuntimeNormalsGenerationStrategy::IfMissing; TangentsGenerationStrategy = EglTFRuntimeTangentsGenerationStrategy::IfMissing; + bReverseTangents = false; } };