[ue4] Black Spots and Normal Flipping Bug Fixes By CCW Resolve (Old Title : Normal Generation Improvement) (#2096)

* [ue4] Normal Generation Improvement

All the vertex normals now face correct direction. It will prevent some of the flicker in some occasions.

* [ue4] A Huge Rendering Improvement

A really huge upgrade for the rendering.
Now the renderer can resolving CCW of the triangles to remove the black spot of some parts, and it now has much more simple normal generation.
There is no need to restrict the movement of the joint / bone to avoid some ugly black spots being rendered from now, just do it without even thinking about it!
And also the flicking bug that caused by the normal flipping bug has been fixed with it, so now you can use lit material more practically with spine. Try to create some cool scenes with a great light setting!
This commit is contained in:
GGgRain 2022-07-04 20:11:56 +09:00 committed by GitHub
parent 6e659dc08b
commit a85765d4d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -116,7 +116,8 @@ void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent *sk
atlasScreenBlendMaterials.Add(material);
pageToScreenBlendMaterial.Add(currPage, material);
}
} else {
}
else {
pageToNormalBlendMaterial.Empty();
pageToAdditiveBlendMaterial.Empty();
pageToMultiplyBlendMaterial.Empty();
@ -133,7 +134,8 @@ void USpineSkeletonRendererComponent::UpdateRenderer(USpineSkeletonComponent *sk
}
}
UpdateMesh(skeleton->GetSkeleton());
} else {
}
else {
ClearAllMeshSections();
}
}
@ -240,7 +242,8 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
attachmentUvs = regionAttachment->getUVs().buffer();
numVertices = 4;
numIndices = 6;
} else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
}
else if (attachment->getRTTI().isExactly(MeshAttachment::rtti)) {
MeshAttachment* mesh = (MeshAttachment*)attachment;
// Early out if region is invisible
@ -256,7 +259,8 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
attachmentUvs = mesh->getUVs().buffer();
numVertices = mesh->getWorldVerticesLength() >> 1;
numIndices = mesh->getTriangles().size();
} else /* clipping */ {
}
else /* clipping */ {
ClippingAttachment* clip = (ClippingAttachment*)attachment;
clipper.clipStart(*slot, clip);
continue;
@ -346,16 +350,44 @@ void USpineSkeletonRendererComponent::UpdateMesh(Skeleton *Skeleton) {
indices.Add(idx + attachmentIndices[j]);
}
FVector normal = FVector(0, -1, 0);
if (numVertices > 2 &&
FVector::CrossProduct(
vertices[indices[firstIndex + 2]] - vertices[indices[firstIndex]],
vertices[indices[firstIndex + 1]] - vertices[indices[firstIndex]])
.Y > 0.f) {
normal.Y = 1;
//Calculate total triangle to add on this loof.
int TriangleInitialCount = firstIndex / 3;
int TriangleToAddNum = indices.Num() / 3 - TriangleInitialCount;
int FirstVertexIndex = vertices.Num() - numVertices;
//loof through all the triangles and resolve to be reversed if the triangle has winding order as CCW.
for (int j = 0; j < TriangleToAddNum; j++) {
const int TargetTringleIndex = firstIndex + j * 3;
if (FVector::CrossProduct(
vertices[indices[TargetTringleIndex + 2]] - vertices[indices[TargetTringleIndex]],
vertices[indices[TargetTringleIndex + 1]] - vertices[indices[TargetTringleIndex]]).Y < 0.f)
{
const int32 targetVertex = indices[TargetTringleIndex];
indices[TargetTringleIndex] = indices[TargetTringleIndex + 2];
indices[TargetTringleIndex + 2] = targetVertex;
}
}
FVector normal = FVector(0, 1, 0);
//Add normals for vertices.
for (int j = 0; j < numVertices; j++) {
normals.Add(normal);
}
idx += numVertices;