Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Model randomly is rendered wrong. #8

Closed
Fransferdy opened this issue Mar 11, 2022 · 14 comments
Closed

[bug] Model randomly is rendered wrong. #8

Fransferdy opened this issue Mar 11, 2022 · 14 comments

Comments

@Fransferdy
Copy link
Contributor

Reopening here since I realized I wrote the issue in the wrong place.

Hello, thanks for providing this plugin. After testing for a while, I think the issue is with the plugin, but if it is not I would be glad to know how this is being used elsewhere.
So I started to use this plugin 4 days ago, I have been working to make a model procedurally.
At the end of generation the function save the Model as a test.off file, call the plugin GenerateSkeletalMesh and return the result, which is then loaded in an Actor via blueprints. The blueprint run at Event Begin Play.
The output teste.off file is always correct, you can load it in any tool to check that it is the same everytime, but the output from GenerateSkeletalMesh randomly changes. The function calculates the normals, tangents, uvs, set colors to (0,0,0) and flipnormals to false, set every vertex to the 0 bone.
My function outputs a cylinder/capsule model.
Sometimes the model is rendered correctly, but many times it is rendered in very weird patterns. On my side the result output is always the same, but in the engine the end result changes from play to play in bizarre ways.
You can see the code and the teste.off file here:
I also ported the plugin to UE5 (but the same issues happens in Ue4.27)
Can visualize the teste.off file here:https://3dviewer.net/
https://github.com/Fransferdy/creaturecreator

Screenshot (11)
Screenshot (12)
Screenshot (13)
Screenshot (14)
Screenshot (15)
Screenshot (16)

@Fransferdy
Copy link
Contributor Author

As a way of testing, I saved the output of my model generator function, and every 1 second passed that output to the plugin, everytime the rendered model varies, like the pictures above.

@Fransferdy
Copy link
Contributor Author

New Info, this only happens in the editor, in a packaged game it does not happen.

@AndreaCatania
Copy link
Owner

Hmm this is really interesting. So the same input renders a different output each time.

Can you please test this using UE4.27? This is an useful information to have. If it's confirmed an only UE5 issue then we should check how the data is layed out in UE5 or if we need to populate also another buffer.

@Fransferdy
Copy link
Contributor Author

Fransferdy commented Mar 15, 2022

Yes, it is happening in Ue 4.27 as well:
I recorded a video to show the issue:
https://youtu.be/jchqYdma8I8
These are the blueprints:
(Every second call GenerateBlob)
ue4 blueprints

Also, this is the code that loads the skeletal mesh:
(here GenerateMyBlob(); creates the surface and adds it to the class instance member meshSurfaces, it only happens once, every subsequent call to GenerateBlob() just uses the already created meshSurface)

USkeletalMesh* USkeletalMeshLoader::GenerateBlob(USkeleton* BaseSkeleton, TArray<UMaterialInterface*> mats) {

	UE_LOG(LogTemp, Warning, TEXT("Meshes: %d"), meshSurfaces.Num());
	if (meshSurfaces.Num() == 0)
	{
		GenerateMyBlob();
	}
	USkeletalMesh* skeletalMesh = NewObject<USkeletalMesh>();
	USkeleton* skeleton = NewObject<USkeleton>();
	buildSkeleton((FReferenceSkeleton&)skeleton->GetReferenceSkeleton(), skeleton);

	skeletalMesh->SetRefSkeleton(skeleton->GetReferenceSkeleton());
	skeletalMesh->SetSkeleton(skeleton);

	FRuntimeSkeletalMeshGenerator::GenerateSkeletalMesh(
		skeletalMesh,
		meshSurfaces,
		mats
	);

	return skeletalMesh;
}

@Fransferdy
Copy link
Contributor Author

Fransferdy commented Mar 15, 2022

Not sure if it could have to do with hardware, but my machine is running Windows 10, with an i9 10900F (20 cores), GTX 1060 6GB and 32 GB ram.

@Fransferdy
Copy link
Contributor Author

Fransferdy commented Mar 15, 2022

Since this issue only happens inside the editor, I tried taking a look in the "WITHEDITOR" sections of the plugin, to see if I could pinpoint where the issue happens, but I didn't understand clearly all these parts, would you care to explain a bit what it is that we have to do differently when inside the editor, versus the packaged game ? I also tried commenting out all those blocks, but that just crashes UE4/5 haha

Also, was this made based on the editor's skeletal mesh FBX importer ?

@AndreaCatania
Copy link
Owner

Essentially the WITH_EDITOR is needed because those functions are not available in production, so it's necessary to not fill those items. If you can give me a sample project, so I can test it locally, I think I can give it a check and try to debug the issue. Otherwise it's a bit difficult to help you.

@Fransferdy
Copy link
Contributor Author

There you go:
SkeletalTest.zip

@LawlerZhang
Copy link

Hi, have you solved this issue? I found that the bug is caused by not initializing local variable ‘Weights’ defined in Line 429 in RuntimeSkeletalMeshGenerator.cpp

@Fransferdy
Copy link
Contributor Author

No, I haven't, did you solve it ? After reading your comment I tried to call Weights.Empy() on line 430, but that didn't fix it.

@AndreaCatania
Copy link
Owner

AndreaCatania commented Apr 22, 2022

Oh I see, so if a vertex doesn't not have any bind information it fails.
@Fransferdy Can you please try to loop the weights here https://github.com/AndreaCatania/RuntimeSkeletalMeshGenerator/blob/main/Source/RuntimeSkeletalMeshGenerator/RuntimeSkeletalMeshGenerator.cpp#L431

and reset all the influences for each weight using this:

Weight.InfluenceWeights[InfluenceIndex] = 0;
Weight.InfluenceBones[InfluenceIndex] = INDEX_NONE;

it should be something like:

for(int i = 0; i < Vertices.Num(); i++)
{
	for (int InfluenceIndex = 0; InfluenceIndex < MaxBoneInfluences; InfluenceIndex++)
	{
		Weights[i].InfluenceWeights[InfluenceIndex] = 0;
		Weights[i].InfluenceBones[InfluenceIndex] = INDEX_NONE;
	}
}

@Fransferdy
Copy link
Contributor Author

Fransferdy commented Apr 22, 2022

I tried that this morning, but it did not solve the issue; Also, in my example all Vertexes are bound to bone 0.
I think the important thing here is that this only happens in the Editor, in a packaged game it doesn't happen, so it must be something related to the importedModel part, or something of the sort. I couldn't find the issue and I haven't tried to solve it in a while now.

TArray<FSkinWeightInfo> Weights;
	Weights.Empty();
	Weights.SetNum(Vertices.Num());

	for (int32 weightIndex = 0; weightIndex < Weights.Num(); weightIndex++)
	{
		FSkinWeightInfo& Weight = Weights[weightIndex];
		for (int InfluenceIndex = 0; InfluenceIndex < MaxBoneInfluences; InfluenceIndex++)
		{
			Weight.InfluenceWeights[InfluenceIndex] = 0;
			Weight.InfluenceBones[InfluenceIndex] = INDEX_NONE;
		}
	}

@LawlerZhang
Copy link

LawlerZhang commented Apr 22, 2022

@Fransferdy I think maybe it should be written like this:

for (int WeightIdx = 0; WeightIdx < Weights.Num(); WeightIdx++)
{
	for (int InfluenceIdx= 0; InfluenceIdx< MAX_TOTAL_INFLUENCES; ++InfluenceIdx)
	{
		Weights[WeightIdx].InfluenceBones[InfluenceIdx] = INDEX_NONE;
		Weights[WeightIdx].InfluenceWeights[InfluenceIdx] = 0;
	}
}

It is not only happens in the Editor, try to use different models. Before fixed, I imported a simple cube in Packaged game, this bug occurred, too(with low possibility).

@Fransferdy
Copy link
Contributor Author

I can't see what exactly is the difference between the code @LawlerZhang posted, and the one I posted (except for this variable: MaxBoneInfluences, so I think this constant is the game changer MAX_TOTAL_INFLUENCES)
Replacing my loop with his fixed the issue !
Thanks for the solution @LawlerZhang !!
I think the source code should be updated with his fix above @AndreaCatania .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants