Commit 113e3ce1 by Kai Westerkamp

material

parent f105761f
Subproject commit 110727d81d3f44dc58f8b2c09c8c27e4c1b6a3dd
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "ProceduralEntity.h" #include "ProceduralEntity.h"
#include "Runtime/Engine/Public/DDSLoader.h"
#include <assimp/DefaultLogger.hpp> #include <assimp/DefaultLogger.hpp>
#include <assimp/Logger.hpp> #include <assimp/Logger.hpp>
...@@ -63,6 +64,7 @@ void AProceduralEntity::Tick(float DeltaTime) ...@@ -63,6 +64,7 @@ void AProceduralEntity::Tick(float DeltaTime)
// quick C++11 trick to get a char * from a std::string // quick C++11 trick to get a char * from a std::string
char* filename = &sfilename[0]; char* filename = &sfilename[0];
result = _stat(filename, &buf); result = _stat(filename, &buf);
if (result != 0) { if (result != 0) {
UE_LOG(LogTemp, Warning, TEXT("Problem getting info")); UE_LOG(LogTemp, Warning, TEXT("Problem getting info"));
...@@ -167,6 +169,12 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) { ...@@ -167,6 +169,12 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) {
_mesh->UpdateMeshSection(_meshCurrentlyProcessed, _vertices[_meshCurrentlyProcessed], _normals[_meshCurrentlyProcessed], _uvs[_meshCurrentlyProcessed], _vertexColors[_meshCurrentlyProcessed], _tangents[_meshCurrentlyProcessed]); _mesh->UpdateMeshSection(_meshCurrentlyProcessed, _vertices[_meshCurrentlyProcessed], _normals[_meshCurrentlyProcessed], _uvs[_meshCurrentlyProcessed], _vertexColors[_meshCurrentlyProcessed], _tangents[_meshCurrentlyProcessed]);
} }
//load material
UMaterial *material = GetMaterialForIndex(mesh->mMaterialIndex, scene);
_mesh->SetMaterial(_meshCurrentlyProcessed, material);
UE_LOG(LogTemp, Warning, TEXT("Done creating the mesh")); UE_LOG(LogTemp, Warning, TEXT("Done creating the mesh"));
_requiresFullRecreation = false; _requiresFullRecreation = false;
...@@ -196,18 +204,168 @@ void AProceduralEntity::loadModel(std::string path) { ...@@ -196,18 +204,168 @@ void AProceduralEntity::loadModel(std::string path) {
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(LogTemp, Error, TEXT("Failed, Scene is empty: %s"), *FString(importer.GetErrorString())); UE_LOG(LogTemp, Error, TEXT("Failed, Scene is empty: %s"), *FString(importer.GetErrorString()));
std::string extensions; std::string extensions;
importer.GetExtensionList(extensions); importer.GetExtensionList(extensions);
UE_LOG(LogTemp, Error, TEXT("Extension list: %s"), *FString(extensions.c_str())); UE_LOG(LogTemp, Error, TEXT("Extension list: %s"), *FString(extensions.c_str()));
return; wchar_t buffer[260];
GetModuleFileName(NULL, buffer, MAX_PATH);
UE_LOG(LogTemp, Error, TEXT("Curretn PAth: %s"), *FString(buffer));
return;
} }
UE_LOG(LogTemp, Warning, TEXT("Has MAterials: %s"), scene->HasMaterials() ? TEXT("True") : TEXT("False"));
_meshCurrentlyProcessed = 0; _meshCurrentlyProcessed = 0;
processNode(scene->mRootNode, scene); processNode(scene->mRootNode, scene);
} }
UMaterial *AProceduralEntity::GetMaterialForIndex(unsigned int index, const aiScene* scene){
if (MaterialMap.Contains(index)) {
return *MaterialMap.Find(index);
}
else {
UE_LOG(LogTemp, Warning, TEXT("Parsing Material %d"),index);
UMaterial *newMaterial = UMaterial::GetDefaultMaterial(EMaterialDomain::MD_Surface);
aiMaterial* AMat = scene->mMaterials[index];
aiColor3D color(0.f, 0.f, 0.f);
if (AI_SUCCESS == AMat->Get(AI_MATKEY_COLOR_DIFFUSE, color)) {
UE_LOG(LogTemp, Warning, TEXT("Material Property: %s, %d %d %d"), TEXT("AI_MATKEY_COLOR_DIFFUSE"), color.r, color.g, color.b);
}
else {
UE_LOG(LogTemp, Warning, TEXT("Material Property: %s, Failed"), TEXT("AI_MATKEY_COLOR_DIFFUSE"));
}
if (AMat->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
aiString Path;
if (AMat->GetTexture(aiTextureType_DIFFUSE, 0, &Path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) {
FString fileName = FString(Path.C_Str());
FString dirPath, temp , right;
_filePath.Split(TEXT("\\"), &temp, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd);
temp.Split(TEXT("/"), &dirPath, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd);
UE_LOG(LogTemp, Warning, TEXT("Diffuse Texture: %s %s"),*dirPath, *fileName);
UTexture2D* diffTexture = LoadTexture(dirPath, fileName);
}
else {
UE_LOG(LogTemp, Warning, TEXT("No Diffuse Texture"));
}
}
return newMaterial;
}
}
/** Loads a texture from the data path */
//https://drive.google.com/file/d/0BzqpBc4MrAMXLXN1ZGNzUEJZc1k/edit
//https://forums.unrealengine.com/showthread.php?33446-Creating-UTexture-from-data-in-code-making-it-a-material-and-adding-it-to-a-mesh
UTexture2D* AProceduralEntity::LoadTexture(FString Folder, FString FileName)
{
FString TexturePath = Folder + "\\" + FileName;
UTexture2D* Texture = NULL;
TArray<uint8> FileData;
/* Load DDS texture */
if (FFileHelper::LoadFileToArray(FileData, *TexturePath, 0))
{
FDDSLoadHelper DDSLoadHelper(&FileData[0], FileData.Num());
if (DDSLoadHelper.IsValid2DTexture())
{
int32 NumMips = DDSLoadHelper.ComputeMipMapCount();
EPixelFormat Format = DDSLoadHelper.ComputePixelFormat();
int32 BlockSize = 16;
if (NumMips == 0)
{
NumMips = 1;
}
if (Format == PF_DXT1)
{
BlockSize = 8;
}
/* Create transient texture */
Texture = UTexture2D::CreateTransient(DDSLoadHelper.DDSHeader->dwWidth, DDSLoadHelper.DDSHeader->dwHeight, Format);
Texture->MipGenSettings = TMGS_LeaveExistingMips;
Texture->PlatformData->NumSlices = 1;
Texture->NeverStream = true;
Texture->Rename(*FileName);
/* Get pointer to actual data */
uint8* DataPtr = (uint8*)DDSLoadHelper.GetDDSDataPointer();
uint32 CurrentWidth = DDSLoadHelper.DDSHeader->dwWidth;
uint32 CurrentHeight = DDSLoadHelper.DDSHeader->dwHeight;
/* Iterate through mips */
for (int32 i = 0; i < NumMips; i++)
{
/* Lock to 1x1 as smallest size */
CurrentWidth = (CurrentWidth < 1) ? 1 : CurrentWidth;
CurrentHeight = (CurrentHeight < 1) ? 1 : CurrentHeight;
/* Get number of bytes to read */
int32 NumBytes = CurrentWidth * CurrentHeight * 4;
if (Format == PF_DXT1 || Format == PF_DXT3 || Format == PF_DXT5)
{
/* Compressed formats */
NumBytes = ((CurrentWidth + 3) / 4) * ((CurrentHeight + 3) / 4) * BlockSize;
}
/* Write to existing mip */
if (i < Texture->PlatformData->Mips.Num())
{
FTexture2DMipMap& Mip = Texture->PlatformData->Mips[i];
void* Data = Mip.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(Data, DataPtr, NumBytes);
Mip.BulkData.Unlock();
}
/* Add new mip */
else
{
FTexture2DMipMap* Mip = new(Texture->PlatformData->Mips) FTexture2DMipMap();
Mip->SizeX = CurrentWidth;
Mip->SizeY = CurrentHeight;
Mip->BulkData.Lock(LOCK_READ_WRITE);
Mip->BulkData.Realloc(NumBytes);
Mip->BulkData.Unlock();
void* Data = Mip->BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(Data, DataPtr, NumBytes);
Mip->BulkData.Unlock();
}
/* Set next mip level */
CurrentWidth /= 2;
CurrentHeight /= 2;
DataPtr += NumBytes;
}
Texture->UpdateResource();
}
}
return Texture;
}
\ No newline at end of file
...@@ -51,6 +51,8 @@ private: ...@@ -51,6 +51,8 @@ private:
TArray<TArray<FProcMeshTangent>> _tangents; TArray<TArray<FProcMeshTangent>> _tangents;
TArray<TArray<FColor>> _vertexColors; TArray<TArray<FColor>> _vertexColors;
TMap<int32, UMaterial*> MaterialMap;
//USceneComponent* _rootComp; //USceneComponent* _rootComp;
/* ################################################### */ /* ################################################### */
...@@ -66,4 +68,6 @@ private: ...@@ -66,4 +68,6 @@ private:
void processMesh(aiMesh* mesh, const aiScene* scene); void processMesh(aiMesh* mesh, const aiScene* scene);
void processNode(aiNode* node, const aiScene* scene); void processNode(aiNode* node, const aiScene* scene);
void loadModel(std::string path); void loadModel(std::string path);
UMaterial *GetMaterialForIndex(unsigned int index, const aiScene* scene);
UTexture2D* LoadTexture(FString Folder, FString FileName);
}; };
\ 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