Commit 9d3ced97 by Kai Westerkamp

temp interface rework

parent 720b4285
// Fill out your copyright notice in the Description page of Project Settings.
#include "MasterTestProject.h"
#include "ImageDownloader.h"
void UImageDownloader::GetTextureForMaterial(UMaterialInstanceDynamic *Material, FName TextureName, FString & baseUrl, FString & Url)
{
_Material = Material;
_TextureName = TextureName;
}
void UImageDownloader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
EImageFormat::Type format = GetImageTypeByExtension(FPaths::GetExtension(Request->GetURL()));
bool isValid;
int32 width, height;
UTexture2D* diffTexture = UImageDownloader::LoadTexture2D(Response->GetContent().GetData(), Response->GetContent().Num(), format, isValid, width, height);
if (isValid)
_Material->SetTextureParameterValue(_TextureName, diffTexture);
}
EImageFormat::Type UImageDownloader::GetImageTypeByExtension(const FString& extension) {
EImageFormat::Type format = EImageFormat::Invalid;
if (extension.EndsWith(TEXT("jpg"), ESearchCase::IgnoreCase)) {
format = EImageFormat::JPEG;
}
else if (extension.EndsWith(TEXT("png"), ESearchCase::IgnoreCase)) {
format = EImageFormat::PNG;
}
else if (extension.EndsWith(TEXT("bmp"), ESearchCase::IgnoreCase)) {
format = EImageFormat::BMP;
}
else if (extension.EndsWith(TEXT("ico"), ESearchCase::IgnoreCase)) {
format = EImageFormat::ICO;
}
else if (extension.EndsWith(TEXT("exr"), ESearchCase::IgnoreCase)) {
format = EImageFormat::EXR;
}
else if (extension.EndsWith(TEXT("icns"), ESearchCase::IgnoreCase)) {
format = EImageFormat::ICNS;
}
return format;
}
UTexture2D* UImageDownloader::LoadTexture2D(const uint8* RawFileData, int32 lenght, EImageFormat::Type Format, bool& IsValid, int32& Width, int32& Height) {
IsValid = false;
UTexture2D* LoadedT2D = NULL;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(Format);
//Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData, lenght))
{
const TArray<uint8>* UncompressedBGRA = NULL;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
//Valid?
if (!LoadedT2D) 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;
return LoadedT2D;
}
\ No newline at end of file
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "HttpDownloader.h"
#include "ImageUtils.h"
#include "ImageWrapper.h" //requires "ImageWrapper" in public dependencies in build CS
#include "ImageDownloader.generated.h"
/**
*
*/
UCLASS()
class MASTERTESTPROJECT_API UImageDownloader : public UHttpDownloader
{
GENERATED_BODY()
private:
UMaterialInstanceDynamic *_Material;
FName _TextureName;
public:
void GetTextureForMaterial(UMaterialInstanceDynamic* Material, FName TextureName, FString &baseUrl, FString &Url);
virtual void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
static UTexture2D* LoadTexture2D(const uint8* RawFileData, int32 lenght, EImageFormat::Type Format, bool& IsValid, int32& Width, int32& Height);
static EImageFormat::Type GetImageTypeByExtension(const FString& extension);
};
...@@ -20,7 +20,7 @@ void UTileDownloader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponseP ...@@ -20,7 +20,7 @@ void UTileDownloader::OnResponseReceived(FHttpRequestPtr Request, FHttpResponseP
UE_LOG(TILES, Warning, TEXT("Parsed Tilset: %s Scucess: %s "), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False")); UE_LOG(TILES, Warning, TEXT("Parsed Tilset: %s Scucess: %s "), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
} }
else { else {
//Response->GetContent UTilesetUtil::parseTile(Response->GetContent(), Request->GetURL());
UE_LOG(TILES, Error, TEXT("Parsed Tile: %s Scucess: %s "), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False")); UE_LOG(TILES, Error, TEXT("Parsed Tile: %s Scucess: %s "), *Request->GetURL(), bWasSuccessful ? TEXT("True") : TEXT("False"));
} }
......
...@@ -29,6 +29,20 @@ FTileset* UTilesetUtil::parseTileset(FString JsonString, FString BaseURL) ...@@ -29,6 +29,20 @@ FTileset* UTilesetUtil::parseTileset(FString JsonString, FString BaseURL)
return nullptr; return nullptr;
} }
void UTilesetUtil::parseTile(const TArray<uint8> data, FString BaseURL)
{
TileHeader *header = (TileHeader*) data.GetData();
UE_LOG(TILES, Log, TEXT("Magic: %s Version %d lenght %d, array %d "), *header->getMagicAsFString(), header->version, header->byteLength, data.Num());
if (header->magic[0] == 'b' && header->magic[1] == '3' && header->magic[2] == 'd' && header->magic[3] == 'm') {
Batch3DModelHeader *b3dmheader = (Batch3DModelHeader*) data.GetData();
UE_LOG(TILES, Log, TEXT("Batch %d %d %d"), b3dmheader->batchtable.batchTableJSONByteLength, b3dmheader->batchtable.batchTableBinaryByteLength, b3dmheader->batchLength);
}
}
void UTilesetUtil::parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile, FTileset* parent) { void UTilesetUtil::parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile, FTileset* parent) {
bool success = FJsonObjectConverter::JsonObjectToUStruct<FTile>(json.ToSharedRef(), targetTile, 0, 0); bool success = FJsonObjectConverter::JsonObjectToUStruct<FTile>(json.ToSharedRef(), targetTile, 0, 0);
......
...@@ -159,11 +159,40 @@ struct FTileset ...@@ -159,11 +159,40 @@ struct FTileset
} }
}; };
//struct TileHeader { struct TileHeader {
// uchar[4] magic; unsigned char magic[4];
// unit32 version; uint32 version;
// unit32 byteLength; uint32 byteLength;
//};
FString getMagicAsFString() {
char ansiiData[5]; //A temp buffer for the data
memcpy(ansiiData, magic, 4); //Assumes bytesRead is always smaller than 1024 bytes
ansiiData[4] = 0; //Add null terminator
return ANSI_TO_TCHAR(ansiiData); //Convert to FString
}
};
struct BatchTableHeader {
uint32 batchTableJSONByteLength;
uint32 batchTableBinaryByteLength;
};
struct BinaryGLTFHeader {
unsigned char magic[4];
uint32 version;
uint32 length;
uint32 contentLength;
uint32 contentFormat;
};
struct Batch3DModelHeader {
TileHeader general;
BatchTableHeader batchtable;
uint32 batchLength;
};
/** /**
...@@ -176,7 +205,7 @@ class MASTERTESTPROJECT_API UTilesetUtil : public UObject ...@@ -176,7 +205,7 @@ class MASTERTESTPROJECT_API UTilesetUtil : public UObject
public: public:
static FTileset* parseTileset(FString JsonString, FString BaseURL); static FTileset* parseTileset(FString JsonString, FString BaseURL);
static void parseTile(TArray<uint8> data, FString BaseURL); static void parseTile(const TArray<uint8> data, FString BaseURL);
private: private:
static void parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile, FTileset* parent); static void parseTile(TSharedPtr<FJsonObject> json, FTile *targetTile, FTileset* parent);
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include <time.h> #include <time.h>
#include "ProceduralEntity.h" #include "ProceduralEntity.h"
#include "ImageDownloader.h"
#include <assimp/DefaultLogger.hpp> #include <assimp/DefaultLogger.hpp>
#include <assimp/Logger.hpp> #include <assimp/Logger.hpp>
...@@ -48,7 +49,7 @@ void AProceduralEntity::PostActorCreated() { ...@@ -48,7 +49,7 @@ void AProceduralEntity::PostActorCreated() {
if (!IsTemplate(RF_Transient)) { if (!IsTemplate(RF_Transient)) {
UE_LOG(GLTF, Warning, TEXT("File Path: %s"), *_filePath); UE_LOG(GLTF, Warning, TEXT("File Path: %s"), *_filePath);
std::string filename(TCHAR_TO_UTF8(*_filePath)); std::string filename(TCHAR_TO_UTF8(*_filePath));
loadModel(filename); // loadModel(filename);
} }
} }
...@@ -83,7 +84,7 @@ void AProceduralEntity::Tick(float DeltaTime) ...@@ -83,7 +84,7 @@ void AProceduralEntity::Tick(float DeltaTime)
_lastModifiedTime = (int)buf.st_mtime; _lastModifiedTime = (int)buf.st_mtime;
UE_LOG(GLTF, Warning, TEXT("Reloading model. %s"), *_filePath); UE_LOG(GLTF, Warning, TEXT("Reloading model. %s"), *_filePath);
loadModel(sfilename); //loadModel(filename);
} }
} }
} }
...@@ -238,11 +239,18 @@ void AProceduralEntity::processNode(aiNode* node, const aiScene* scene, FMatrix ...@@ -238,11 +239,18 @@ void AProceduralEntity::processNode(aiNode* node, const aiScene* scene, FMatrix
} }
} }
void AProceduralEntity::loadModel(std::string path) { void AProceduralEntity::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
loadModel(Response.GetContent());
}
void AProceduralEntity::loadModel(TArray<uint8> data) {
Assimp::Importer importer; Assimp::Importer importer;
const aiScene* scene = importer.ReadFileFromMemory(data.GetData(), data.Num(), path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals);
//UE_LOG(GLTF, Warning, TEXT("START LOGGING")); //UE_LOG(GLTF, Warning, TEXT("START LOGGING"));
const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals); //const aiScene* scene = importer.ReadFile(path, aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenNormals);
if (!scene) { if (!scene) {
UE_LOG(GLTF, Error, TEXT("Failed, Scene is empty: %s"), *FString(importer.GetErrorString())); UE_LOG(GLTF, Error, TEXT("Failed, Scene is empty: %s"), *FString(importer.GetErrorString()));
...@@ -297,11 +305,11 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -297,11 +305,11 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
if (aiTexture->mHeight == 0) { // If mHeight = 0 this is a pointer to a memory buffer of size mWidth containing the compressed texture data. Good luck, have fun! if (aiTexture->mHeight == 0) { // If mHeight = 0 this is a pointer to a memory buffer of size mWidth containing the compressed texture data. Good luck, have fun!
FString extension = FString(aiTexture->achFormatHint); FString extension = FString(aiTexture->achFormatHint);
EImageFormat::Type format = GetImageTypeByExtension(extension); EImageFormat::Type format = UImageDownloader::GetImageTypeByExtension(extension);
bool isValid; bool isValid;
int32 width, height; int32 width, height;
UTexture2D* diffTexture = LoadTexture2D((uint8*)aiTexture->pcData, aiTexture->mWidth, format, isValid, width, height); UTexture2D* diffTexture = UImageDownloader::LoadTexture2D((uint8*)aiTexture->pcData, aiTexture->mWidth, format, isValid, width, height);
if (isValid) if (isValid)
newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture); newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture);
...@@ -318,21 +326,8 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -318,21 +326,8 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
} }
else { else {
FString dirPath, temp, right; UImageDownloader *downloader = NewObject<UImageDownloader>(UImageDownloader::StaticClass());
if (!_filePath.Split(TEXT("\\"), &temp, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd)) { downloader->GetTextureForMaterial(newMaterial, FName("DiffuseTexture"), _filePath, fileName);
temp = _filePath;
}
if (!temp.Split(TEXT("/"), &dirPath, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd)) {
dirPath = temp;
}
//UE_LOG(GLTF, Warning, TEXT("Diffuse Texture:\"%s\" \"%s\""), *dirPath, *fileName);
bool isValid;
int32 width, height;
UTexture2D* diffTexture = LoadTexture2D_FromFile(dirPath + "\\" + fileName, isValid, width, height);
if (isValid)
newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture);
} }
} }
} }
...@@ -370,80 +365,3 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -370,80 +365,3 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
} }
} }
EImageFormat::Type AProceduralEntity::GetImageTypeByExtension(const FString& extension) {
EImageFormat::Type format = EImageFormat::Invalid;
if (extension.EndsWith(TEXT("jpg"), ESearchCase::IgnoreCase)) {
format = EImageFormat::JPEG;
}
else if (extension.EndsWith(TEXT("png"), ESearchCase::IgnoreCase)) {
format = EImageFormat::PNG;
}
else if (extension.EndsWith(TEXT("bmp"), ESearchCase::IgnoreCase)) {
format = EImageFormat::BMP;
}
else if (extension.EndsWith(TEXT("ico"), ESearchCase::IgnoreCase)) {
format = EImageFormat::ICO;
}
else if (extension.EndsWith(TEXT("exr"), ESearchCase::IgnoreCase)) {
format = EImageFormat::EXR;
}
else if (extension.EndsWith(TEXT("icns"), ESearchCase::IgnoreCase)) {
format = EImageFormat::ICNS;
}
return format;
}
//https://wiki.unrealengine.com/Rama%27s_Vertex_Snap_Editor_Plugin#Plugin_Download
UTexture2D* AProceduralEntity::LoadTexture2D_FromFile(const FString& FullFilePath, bool& IsValid, int32& Width, int32& Height)
{
EImageFormat::Type format = GetImageTypeByExtension(FPaths::GetExtension(FullFilePath));
//Load From File
TArray<uint8> RawFileData;
if (!FFileHelper::LoadFileToArray(RawFileData, *FullFilePath)) return NULL;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return LoadTexture2D(RawFileData.GetData(), RawFileData.Num(), format, IsValid, Width, Height);
}
UTexture2D* AProceduralEntity::LoadTexture2D(uint8* RawFileData, int32 lenght, EImageFormat::Type Format, bool& IsValid, int32& Width, int32& Height) {
IsValid = false;
UTexture2D* LoadedT2D = NULL;
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(Format);
//Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData, lenght))
{
const TArray<uint8>* UncompressedBGRA = NULL;
if (ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, UncompressedBGRA))
{
LoadedT2D = UTexture2D::CreateTransient(ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), PF_B8G8R8A8);
//Valid?
if (!LoadedT2D) 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;
return LoadedT2D;
}
...@@ -10,12 +10,13 @@ ...@@ -10,12 +10,13 @@
#include "ImageUtils.h" #include "ImageUtils.h"
#include "ImageWrapper.h" //requires "ImageWrapper" in public dependencies in build CS #include "ImageWrapper.h" //requires "ImageWrapper" in public dependencies in build CS
#include "HttpDownloader.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "ProceduralMeshComponent.h" #include "ProceduralMeshComponent.h"
#include "ProceduralEntity.generated.h" #include "ProceduralEntity.generated.h"
UCLASS() UCLASS()
class AProceduralEntity : public AActor class AProceduralEntity : public AActor, public UHttpDownloader
{ {
GENERATED_BODY() GENERATED_BODY()
...@@ -52,6 +53,8 @@ public: ...@@ -52,6 +53,8 @@ public:
//UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Materials") //UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Materials")
//UTexture2D* diffTexture; //UTexture2D* diffTexture;
virtual void OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful);
private: private:
int32 _selectedVertex; int32 _selectedVertex;
int32 _meshCurrentlyProcessed; int32 _meshCurrentlyProcessed;
...@@ -83,13 +86,8 @@ private: ...@@ -83,13 +86,8 @@ private:
void processMesh(aiMesh* mesh, const aiScene* scene, FMatrix modelTransform); void processMesh(aiMesh* mesh, const aiScene* scene, FMatrix modelTransform);
void processNode(aiNode* node, const aiScene* scene, FMatrix modelTransform); void processNode(aiNode* node, const aiScene* scene, FMatrix modelTransform);
void loadModel(std::string path); void loadModel(TArray<uint8> data);
UMaterialInterface *GetMaterialForIndex(unsigned int index, const aiScene* scene); UMaterialInterface *GetMaterialForIndex(unsigned int index, const aiScene* scene);
UTexture2D* LoadTexture2D_FromFile(const FString& FullFilePath, bool& IsValid, int32& Width, int32& Height);
UTexture2D* LoadTexture2D(uint8* RawFileData, int32 lenght, EImageFormat::Type Format, bool& IsValid, int32& Width, int32& Height);
EImageFormat::Type GetImageTypeByExtension(const FString& extension);
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment