- Изменено
Change texture use external texture in UE5
hello guys!
I want to change the texture of an attachment with an external texture.
I passed a UTexture2D from blueprint to cpp. Then use the following code to change the texture.
Attachment *attachment = slot->getAttachment();
RegionAttachment* atta = (RegionAttachment*)attachment;
AtlasPage *page = NULL;
char *name = TCHAR_TO_ANSI(*texture->GetName());
UE_LOG(LogTemp, Log, TEXT("name is:%s"), TCHAR_TO_UTF8(*SlotName));
page = new (__FILE__, __LINE__) AtlasPage(TCHAR_TO_UTF8(*SlotName));
page->setRendererObject(texture);
page->texturePath = TCHAR_TO_UTF8(*texture->GetPathName());
page->width = texture->GetSizeX();
page->height = texture->GetSizeY();
page->uWrap = TextureWrap_ClampToEdge;
page->vWrap = TextureWrap_ClampToEdge;
UE_LOG(LogTemp, Log, TEXT("width is:%d, height is:%d"), page->width, page->height);
AtlasRegion *region = new (__FILE__, __LINE__) AtlasRegion();
region->width = texture->GetSizeX();
region->height = texture->GetSizeY();
region->originalWidth = texture->GetSizeX();
region->originalHeight = texture->GetSizeY();
region->page = page;
region->u = 0;
region->v = 0;
region->u2 = 1;
region->u2 = 1;
region->degrees = 0;
region->offsetX = 0;
region->offsetY = 0;
atta->setRendererObject(region);
atta->updateOffset();
Now the output is the following picture. The head(which I am trying to change) disappears and nothing shows .
I kinda have no idea where to go now.
Could anyone help me with this? Thanks!
Your could is actually pretty good, however, it will eventually be overwritten by this:
https://github.com/EsotericSoftware/spine-runtimes/blob/4.0/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Private/SpineSkeletonRendererComponent.cpp#L269
Sadly, there's currently no way to allow setting a texture on a specific attachment, due to how me have to manage materials and blend modes in UE4/5.
Mario написалYour could is actually pretty good, however, it will eventually be overwritten by this:
https://github.com/EsotericSoftware/spine-runtimes/blob/4.0/spine-ue4/Plugins/SpinePlugin/Source/SpinePlugin/Private/SpineSkeletonRendererComponent.cpp#L269Sadly, there's currently no way to allow setting a texture on a specific attachment, due to how me have to manage materials and blend modes in UE4/5.
Thank you, Mario!
According to the source code you pointed to, I found out a way to do this. I add following modification to my code to force to recreate the material.
skeleton->Atlas->GetAtlas()->getPages().add(page);
skeleton->Atlas->atlasPages.Add(texture);
Now the output is right now as the following screenshot.
In case someone need the same function in the future, I paste the code here. (Maybe need more test for it)
RegionAttachment* atta = (RegionAttachment*)attachment;
auto oldRegion = (AtlasRegion*)atta->getRendererObject();
auto oldPage = oldRegion->page;
AtlasPage *page = NULL;
char *name = TCHAR_TO_ANSI(*texture->GetName());
page = new (__FILE__, __LINE__) AtlasPage(TCHAR_TO_UTF8(*slotName));
page->setRendererObject(texture);
page->name = oldPage->name;
page->texturePath = oldPage->texturePath;
page->width = texture->GetSizeX();
page->height = texture->GetSizeY();
page->uWrap = TextureWrap_ClampToEdge;
page->vWrap = TextureWrap_ClampToEdge;
UE_LOG(LogTemp, Log, TEXT("width is:%d, height is:%d"), page->width, page->height);
UE_LOG(LogTemp, Log, TEXT("old page:width is:%d, height is:%d"), oldPage->width, oldPage->height);
AtlasRegion *region = new (__FILE__, __LINE__) AtlasRegion();
region->width = texture->GetSizeX();
region->height = texture->GetSizeY();
region->originalWidth = texture->GetSizeX();
region->originalHeight = texture->GetSizeY();
region->page = page;
region->u = 0;
region->v = 0;
region->u2 = 1;
region->v2 = 1;
region->degrees = 0;
region->offsetX = 0;
region->offsetY = 0;
skeleton->Atlas->GetAtlas()->getPages().add(page);
skeleton->Atlas->atlasPages.Add(texture);
atta->setRendererObject(region);
atta->setUVs(0, 0, 1, 1, 0);
atta->updateOffset();
Ohhh, I didn't think of modifying the altas itself! Good stuff, thanks for posting it here.