Hi everyone,
I am tring to create a materail with texture and apply it to my mesh in C++, my code can be complied, but it seems no material on my mesh, could you help me?The following is my code.
in my LoadTexture2DFromFile of ImportMesh.cpp:
UTexture2D* ImportMesh::LoadTexture2DFromFile(const FString& FullFilePath, bool& IsValid, int32& Width, int32& Height)
{
IsValid = false;
UTexture2D* LoadedT2D = NULL;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
EImageFormat InFormat;
if (FullFilePath.EndsWith(".png"))
{
InFormat = EImageFormat::PNG;
}
else if (FullFilePath.EndsWith(".jpg") || FullFilePath.EndsWith(".jpeg"))
{
InFormat = EImageFormat::JPEG;
UE_LOG(LogTemp, Warning, TEXT("Loadedimage JPEG"));
}
else if (FullFilePath.EndsWith(".bmp"))
{
InFormat = EImageFormat::BMP;
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Loadedimage fail"));
return NULL;
}
TSharedPtr<IImageWrapper> ImageWrapper = ImageWrapperModule.CreateImageWrapper(InFormat);
//Load From File
if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*FullFilePath))
{
UE_LOG(LogTemp, Warning, TEXT("readimage fail"));
return NULL;
}
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath)) { return NULL; }
//Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData.GetData(), RawFileData.Num()))
{
TArray<uint8> UncompressedBGRA;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
//Valid?
if (!LoadedT2D) {
UE_LOG(LogTemp, Warning, TEXT("LoadedT2D fail"));
return NULL;
}
//~~~~~~~~~~~~~~
//Out!
Width = ImageWrapper->GetWidth();
Height = ImageWrapper->GetHeight();
//Copy!
void* TextureData = LoadedT2D->PlatformData->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(TextureData, UncompressedBGRA.GetData(), UncompressedBGRA.Num());
LoadedT2D->PlatformData->Mips[0].BulkData.Unlock();
//Update!
LoadedT2D->UpdateResource();
}
}
// Success!
IsValid = true;
UE_LOG(LogTemp, Warning, TEXT("LoadedT2D success"));
return LoadedT2D;
}
In my ProcMeshTest.h:
public:
UPROPERTY(EditAnywhere, Category = "MyProceduralMesh")
UProceduralMeshComponent* ProcMesh;
UPROPERTY(EditAnywhere)
UMaterialInterface* Material;
In my BeginPlay() of ProcMeshTest.cpp:
void AProcMeshTest::BeginPlay()
{
Super::BeginPlay();
ImportMesh importmesh;
ImportMesh* IMptr = &importmesh;
if (IMptr->openMesh("D:/biye/model/20220122/20211102_0.obj"))
{
for (int32 i = 0; i <= IMptr->_meshCurrentlyProcessed; i++)
{
TArray<FVector> Vertices;
TArray<int32> Indices;
TArray<FVector> Normals;
TArray<FVector2D> UV;
TArray<FVector> Tangents;
TArray<FProcMeshTangent> ProcMeshTangent;
if (IMptr->getSection(i, Vertices, Indices, Normals, UV, Tangents)) {
for (int32 j = 0; j < Tangents.Num(); j++)
{
float X = Tangents[j].X;
float Y = Tangents[j].Y;
float Z = Tangents[j].Z;
ProcMeshTangent.Add(FProcMeshTangent(X, Y, Z));
}
ProcMesh->CreateMeshSection(i, Vertices, Indices, Normals, UV, TArray<FColor>(), ProcMeshTangent, true);
//load texture and set it to materail
bool isValid;
int32 texWidth, texHeight;
FString path = "C:/Users/a/Documents/RuntimeMeshLoader/image/u_f001_t005_Industry_001.jpg";
UTexture2D* textureParam = IMptr->LoadTexture2DFromFile(path, isValid, texWidth, texHeight);
UMaterialInstanceDynamic* sectionMaterial = UMaterialInstanceDynamic::Create(Material, nullptr);
sectionMaterial->SetTextureParameterValue("TextureParam", textureParam);
ProcMesh->SetMaterial(i, sectionMaterial);
}
}
}
}
in my obj file:
in UE4:
The code can be compiled, I create the mesh successfully, but it did not have materail with my texture loading from file.