/// More Updates in the answers below!! ///
Currently i’m having a problem where my mesh isn’t getting any sort of material. I’ve tried alot, and this last bit of code is based from the recent plugin SiggiG posted in the forums. But even like so i can’t get it to work…
This is all supposed to work during runtime.
Right now it looks like this:
I’ve changed the material while it’s running so you can see that it isn’t on the respective mesh.
And the code for that is like this:
FbxActor.h
UCLASS()
class AFbxActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AFbxActor();
bool GenerateActorFromFile(const FString& inFileName);
//void PostEditChangeProperty(struct FPropertyChangedEvent& PropertyChangedEvent);
// Called when the game starts or when spawned
//virtual void BeginPlay() override;
// Called every frame
virtual void Tick(float DeltaSeconds) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Materials)
UMaterialInterface * LeMaterial;
//UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Mesh)
UStaticMeshComponent * StaticMeshObj;
UProceduralMeshComponent * GenMesh;
};
FbxActor.cpp
AFbxActor::AFbxActor() :
StaticMeshObj(NULL),
GenMesh(NULL)
{
RootComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("RootComponent"));
//RootComponent = StaticMeshObj;
GenMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
GenMesh->SetMobility(EComponentMobility::Movable);
GenMesh->AttachTo(RootComponent);
LeMaterial = UMaterial::GetDefaultMaterial(MD_Surface);
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
bool AFbxActor::GenerateActorFromFile(const FString& inFileName) {
UE_LOG(FbxActorLog, Log, TEXT("Generating actor from file %s"), *inFileName);
GenMesh->SetWorldLocation(this->GetActorLocation());
GenMesh->SetWorldRotation(this->GetActorRotation());
GenMesh->SetWorldScale3D(this->GetActorScale3D());
ImpFbx::FFbxImport currFbxImport = ImpFbx::FFbxImport::FFbxImport();
if (!currFbxImport.GetFbxScene(inFileName)) {
UE_LOG(FbxActorLog, Error, TEXT("Error creating scene from file..."));
return false;
}
currFbxImport.DoYourMagic(this->GenMesh, this->RootComponent);
UMaterialInstanceDynamic * InstanceMaterial = UMaterialInstanceDynamic::Create(LeMaterial, this);
GenMesh->SetMaterial(1, InstanceMaterial);
//this->Tick(1);
return true;
}
I have placed the material interface as material, the “GenMesh->AttachTo(RootComponent)” after the set material, but it’s still the same result.
The set material is for index 1, because in the part where i create the mesh section is for index 1 as well.
GenMesh->CreateMeshSection(1, CtrlPointsArray, TrianglesArray, NormalsArray, UVsArray, ColorsArray, TangentsArray, true);
Am I doing something wrong? I have been trying to work around this problem for the whole week… I really don’t see where the problem is. Can someone help me please?
UPDATE:
I’ve discovered that the material is correctly being assigned to the mesh. The problem is that the material does not appear to be placed correctly on the mesh, as in, the edges of the object are not visible.
Here you can see the mesh is the same (the left one is the import with editor, and the one on the right is the runtime import):
and the same material applied:
You can also see the size differences in each picture, on the first one is selected the mesh imported in runtime and on the second is the fbx imported in the editor… don’t know if that might also be affecting something and is actually an error or not…
What might me causing this incorrect placement? Am I missing information in the mesh? Building the mesh wrong? Or maybe it’s in the material?