How to convert texture 2D to Material?

How to convert texture 2D to Material?
(i just wanna change material from my server i mean by Varest plugin)

how to do that?

Hey there @ganbaa_elmer! So I think one of the best (and more modular) ways to handle this is to make a material that uses a texture2D param for it’s Base color, then create a dynamic material instance, assign the texture you’d like, then assign that material to your mesh. A little bit like this:


Hope this example helps!

1 Like

You can’t convert it directly. You can however add a texture sample node in a material and set it as a parameter. Then you create a Dynamic Material Instance based on that material and set the texture through a parameter

2 Likes

@SupportiveEntity @3dRaven ok thank you
any suggestions about how to change material from server? i mean remote material change?

any example?

Remote as in a streamed material or in game server => client?

@3dRaven i think both of the examples is good for ue4 community.
because someone will read our posts then found good answers from us.
:pray:

for me. i need to transfer base64 image or image.png to change ingame material by Rest API(varest plugin) or something

Well the image download should probably be pushed towards a Client task (no need to eat up server bandwidth).

Is it a function on begin play or is it triggered by an event?

If a trigger then probably just put the task in a custom event with a Muticast replication.
You could also add a variable that indicates the change and add a on_repNotify call and then change the material. There are a couple of ways depending on how important the task is to the end user

i did it by Varest plugin
but its limited string length
i didn’t found any other solutions
if you have other solution please give to us

in static mesh actor blueprint:

test Rest API by Python Flask:
rest.py (2.0 KB)

You can do a do a direct http request in unreal with HttpModule using FHttpModule

void AHttpActor::MyHttpCall(FString url, EHTTPMethod verb)
{
	FString action = UEnum::GetValueAsString<EHTTPMethod>(verb);
	//FString action = "Get";
	TSharedRef<IHttpRequest, ESPMode::ThreadSafe> Request = Http->CreateRequest();
	Request->OnProcessRequestComplete().BindUObject(this, &AHttpActor::OnResponseReceived);	
	Request->SetURL(url);
	Request->SetVerb(action);
	Request->SetHeader(TEXT("User-Agent"), "X-UnrealEngine-Agent");
	Request->SetHeader("Content-Type", TEXT("application/json"));
	Request->ProcessRequest();
}

void AHttpActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	//Create a pointer to hold the json serialized data
	TSharedPtr<FJsonObject> JsonObject;

	//Create a reader pointer to read the json data
	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Response->GetContentAsString());

	//Deserialize the json data given Reader and the actual object to deserialize
	if (FJsonSerializer::Deserialize(Reader, JsonObject))
	{
          FString url = Request.Get()->GetURL();
	
        // process data here decode base 64 etc 
         FString c  = "NeededKey";			
	TSharedPtr<FJsonValue> foundValue  = JsonObject->TryGetField(c);

	}
}

The final processing depends on what is returned by the rest API

1 Like