Hi everyone!
I have loaded a Texture2D from image file successfully, and then how to create a material using this texture? I try to create a UMaterialInstanceDynamic and use SetTextureParameterValue() to change texture, but it did not work, it still shows the texture of the parent material.
In my RuntimeMeshTest.h:
UPROPERTY(VisibleAnywhere)
UMaterialInterface* MiniMapmatInst;
UPROPERTY(VisibleAnywhere)
UMaterialInstanceDynamic* sectionMaterial;
In my RuntimeMeshTest.cpp:
void ARuntimeMeshTest::setmaterial()
{
//Load image file to UTexture2D
bool isValid;
int32 texWidth, texHeight;
FString path = "C:/Users/a/Documents/RuntimeMeshLoader/image/u_f001_t005_Industry_001.jpg";
UTexture2D* textureParam = importmesh.LoadTexture2DFromFile(path, isValid, texWidth, texHeight);
//Set material
MiniMapmatInst = LoadObject<UMaterialInterface>(NULL, TEXT("Material'/Game/test/NewMaterial.NewMaterial'"));
sectionMaterial = UMaterialInstanceDynamic::Create(MiniMapmatInst, nullptr);
sectionMaterial->SetTextureParameterValue(FName("MiniMapTex"), textureParam);
}
u_f001_t005_Industry_001.jpg:
"Materialâ/Game/test/NewMaterial.NewMaterialâ:
In ue4:
It still shows the texture of the parent material.
ALL my code:
//RuntimeMeshTest.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "RuntimeMeshComponent.h"
#include "Providers/RuntimeMeshProviderStatic.h"
#include "ImportMesh.h"
#include "RuntimeMeshTest.generated.h"
UCLASS()
class IMPORT_3DMODEL_API ARuntimeMeshTest : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ARuntimeMeshTest();
UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
URuntimeMeshComponent* RuntimeMeshComponent;
UPROPERTY(VisibleAnywhere)
UMaterialInterface* MiniMapmatInst;
UPROPERTY(VisibleAnywhere)
UMaterialInstanceDynamic* sectionMaterial;
UPROPERTY(VisibleAnywhere)
URuntimeMeshProviderStatic* StaticProvider;
ImportMesh importmesh;
TArray<FVector> Positions;
TArray<FLinearColor> Colors;
TArray<int32> Triangles;
TArray<FVector> Normals;
TArray<FVector2D> TexCoords;
TArray<FRuntimeMeshTangent> Tangents;
UTexture2D* textureParam2;
void setmaterial();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
//RuntimeMeshTest.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "RuntimeMeshTest.h"
// Sets default values
ARuntimeMeshTest::ARuntimeMeshTest()
{
// 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;
RuntimeMeshComponent = CreateDefaultSubobject<URuntimeMeshComponent>(TEXT("RuntimeMeshComponent"));
RootComponent = RuntimeMeshComponent;
}
void ARuntimeMeshTest::setmaterial()
{
bool isValid;
int32 texWidth, texHeight;
FString path = "C:/Users/a/Documents/RuntimeMeshLoader/image/u_f001_t005_Industry_001.jpg";
UTexture2D* textureParam = importmesh.LoadTexture2DFromFile(path, isValid, texWidth, texHeight);
MiniMapmatInst = LoadObject<UMaterialInterface>(NULL, TEXT("Material'/Game/test/NewMaterial.NewMaterial'"));
sectionMaterial = UMaterialInstanceDynamic::Create(MiniMapmatInst, nullptr);
sectionMaterial->SetTextureParameterValue(FName("MiniMapTex"), textureParam);
//sectionMaterial->SetScalarParameterValue(FName("MiniMapTex12"), 1);
}
// Called when the game starts or when spawned
void ARuntimeMeshTest::BeginPlay()
{
Super::BeginPlay();
setmaterial();
StaticProvider = NewObject<URuntimeMeshProviderStatic>(this, TEXT("StaticProvider"));
RuntimeMeshComponent->Initialize(StaticProvider);
StaticProvider->SetupMaterialSlot(0, TEXT("Material"), sectionMaterial);
// This creates 3 positions for a triangle
Positions={ FVector(0, -50, 0), FVector(0, 0, 100), FVector(0, 50, 0) };
// This creates 3 vertex colors
//Colors = { FLinearColor(255.f, 255.f, 255.f,0.f),FLinearColor(255.f, 255.f, 255.f,0.f),FLinearColor(255.f, 255.f, 255.f,0.f) };
// This indexes our simple triangle
Triangles = { 0, 1, 2 };
TexCoords = { FVector2D(0, 0) ,FVector2D(1, 0) ,FVector2D(0, 1) };
StaticProvider->CreateSectionFromComponents(0, 0, 0, Positions, Triangles, Normals, TexCoords, Colors, Tangents, ERuntimeMeshUpdateFrequency::Infrequent, true);
}
// Called every frame
void ARuntimeMeshTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}