Commit 5b57cf19 by Kai Westerkamp

Added Node Transfom

parent 3cacd365
...@@ -20,7 +20,7 @@ public class MasterTestProject : ModuleRules ...@@ -20,7 +20,7 @@ public class MasterTestProject : ModuleRules
public MasterTestProject(TargetInfo Target) public MasterTestProject(TargetInfo Target)
{ {
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","ImageWrapper", "RawMesh", "ProceduralMeshComponent" }); PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "ImageWrapper", "RawMesh", "ProceduralMeshComponent" });
PrivateDependencyModuleNames.AddRange(new string[] { }); PrivateDependencyModuleNames.AddRange(new string[] { });
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <assimp/Logger.hpp> #include <assimp/Logger.hpp>
// Sets default values // Sets default values
AProceduralEntity::AProceduralEntity() AProceduralEntity::AProceduralEntity()
: _requiresFullRecreation(true), _meshCurrentlyProcessed(0) : _requiresFullRecreation(true), _meshCurrentlyProcessed(0)
...@@ -80,6 +81,7 @@ void AProceduralEntity::Tick(float DeltaTime) ...@@ -80,6 +81,7 @@ void AProceduralEntity::Tick(float DeltaTime)
else { else {
if (_lastModifiedTime == 0 || (int)buf.st_mtime > _lastModifiedTime) { if (_lastModifiedTime == 0 || (int)buf.st_mtime > _lastModifiedTime) {
_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(sfilename);
} }
...@@ -92,8 +94,8 @@ void AProceduralEntity::loadModelFromBlueprint() { ...@@ -92,8 +94,8 @@ void AProceduralEntity::loadModelFromBlueprint() {
} }
/* ASSIMP IMPORT */ /* ASSIMP IMPORT */
void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) { void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene, FMatrix modelTransform) {
UE_LOG(GLTF, Warning, TEXT("Processing mesh")); //UE_LOG(GLTF, Warning, TEXT("Processing mesh"));
// the very first time this method runs, we'll need to create the empty arrays // the very first time this method runs, we'll need to create the empty arrays
// we can't really do that in the class constructor because we don't know how many meshes we'll read, and this data can change between imports // we can't really do that in the class constructor because we don't know how many meshes we'll read, and this data can change between imports
...@@ -122,12 +124,19 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) { ...@@ -122,12 +124,19 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) {
_indices[_meshCurrentlyProcessed].Empty(); _indices[_meshCurrentlyProcessed].Empty();
//} //}
FMatrix normalMatrix = modelTransform.Inverse().GetTransposed();
bool isIdentity = modelTransform.Equals(FMatrix::Identity);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) { for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
FVector vertex, normal; FVector4 vertex;
FVector normal;
// process vertex positions, normals and UVs // process vertex positions, normals and UVs
vertex.X = mesh->mVertices[i].x; vertex.X = mesh->mVertices[i].x;
vertex.Y = mesh->mVertices[i].y; vertex.Y = mesh->mVertices[i].y;
vertex.Z = mesh->mVertices[i].z; vertex.Z = mesh->mVertices[i].z;
vertex.W = 1.0;
normal.X = mesh->mNormals[i].x; normal.X = mesh->mNormals[i].x;
normal.Y = mesh->mNormals[i].y; normal.Y = mesh->mNormals[i].y;
...@@ -143,8 +152,23 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) { ...@@ -143,8 +152,23 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) {
else { else {
_uvs[_meshCurrentlyProcessed].Add(FVector2D(0.f, 0.f)); _uvs[_meshCurrentlyProcessed].Add(FVector2D(0.f, 0.f));
} }
_vertices[_meshCurrentlyProcessed].Add(vertex);
_normals[_meshCurrentlyProcessed].Add(normal); if (isIdentity) {
_vertices[_meshCurrentlyProcessed].Add(vertex);
_normals[_meshCurrentlyProcessed].Add(normal);
}
else {
FVector4 pos4 = modelTransform.TransformFVector4(vertex);
FVector dehomogenisert = FVector(pos4 / pos4.W);
_vertices[_meshCurrentlyProcessed].Add(dehomogenisert);
//noremalen brauchen keine translation
_normals[_meshCurrentlyProcessed].Add(normalMatrix.TransformVector(normal));
}
} }
if (_requiresFullRecreation) { if (_requiresFullRecreation) {
...@@ -159,11 +183,11 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) { ...@@ -159,11 +183,11 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) {
// we can finally either update or create the mesh // we can finally either update or create the mesh
if (_requiresFullRecreation) { if (_requiresFullRecreation) {
UE_LOG(GLTF, Warning, TEXT("Full recreation")); //UE_LOG(GLTF, Warning, TEXT("Full recreation"));
_mesh->CreateMeshSection(_meshCurrentlyProcessed, _vertices[_meshCurrentlyProcessed], _indices[_meshCurrentlyProcessed], _normals[_meshCurrentlyProcessed], _uvs[_meshCurrentlyProcessed], _vertexColors[_meshCurrentlyProcessed], _tangents[_meshCurrentlyProcessed], true); _mesh->CreateMeshSection(_meshCurrentlyProcessed, _vertices[_meshCurrentlyProcessed], _indices[_meshCurrentlyProcessed], _normals[_meshCurrentlyProcessed], _uvs[_meshCurrentlyProcessed], _vertexColors[_meshCurrentlyProcessed], _tangents[_meshCurrentlyProcessed], true);
} }
else { else {
UE_LOG(GLTF, Warning, TEXT("Update")); // UE_LOG(GLTF, Warning, TEXT("Update"));
_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]);
} }
...@@ -173,33 +197,51 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) { ...@@ -173,33 +197,51 @@ void AProceduralEntity::processMesh(aiMesh* mesh, const aiScene* scene) {
_mesh->SetMaterial(_meshCurrentlyProcessed, material); _mesh->SetMaterial(_meshCurrentlyProcessed, material);
UE_LOG(GLTF, Warning, TEXT("Done creating the mesh")); //UE_LOG(GLTF, Warning, TEXT("Done creating the mesh"));
_requiresFullRecreation = false; _requiresFullRecreation = false;
} }
void AProceduralEntity::processNode(aiNode* node, const aiScene* scene) { void AProceduralEntity::processNode(aiNode* node, const aiScene* scene, FMatrix modelTransform) {
UE_LOG(GLTF, Warning, TEXT("Processing a node")); //UE_LOG(GLTF, Warning, TEXT("Processing a node"));
FMatrix child = FMatrix();
aiVector3D nodeScale, nodePos;
aiQuaternion nodeQuat;
node->mTransformation.Decompose(nodeScale, nodeQuat, nodePos);
FQuat lightQuat = FQuat(nodeQuat.x, nodeQuat.y, nodeQuat.z, nodeQuat.w);
FRotator lightRot = FRotator::MakeFromEuler(lightQuat.Euler());
FVector trans = FVector(nodePos.x, nodePos.y, nodePos.z);
FVector scale = FVector(nodeScale.x, nodeScale.y, nodeScale.z);
FTransform LocalTransform = FTransform(lightRot, trans, scale);
//UE_LOG(GLTF, Warning, TEXT("Transform: Rot: %s Trans: %s Scale: %s "),*lightRot.ToString(), * trans.ToString(), *scale.ToString() );
FMatrix absoluteTransform = LocalTransform.ToMatrixWithScale() * modelTransform;
// process all the node's meshes // process all the node's meshes
for (uint32 i = 0; i < node->mNumMeshes; i++) { for (uint32 i = 0; i < node->mNumMeshes; i++) {
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]]; aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
UE_LOG(GLTF, Warning, TEXT("New mesh in the node %i"), mesh->mNumVertices); //UE_LOG(GLTF, Warning, TEXT("New mesh in the node %i"), mesh->mNumVertices);
_numVertecies += mesh->mNumVertices; _numVertecies += mesh->mNumVertices;
processMesh(mesh, scene); processMesh(mesh, scene, absoluteTransform);
++_meshCurrentlyProcessed; ++_meshCurrentlyProcessed;
} }
// do the same for all of its children // do the same for all of its children
for (uint32 i = 0; i < node->mNumChildren; i++) { for (uint32 i = 0; i < node->mNumChildren; i++) {
UE_LOG(GLTF, Warning, TEXT("New child in the node")); //UE_LOG(GLTF, Warning, TEXT("New child in the node"));
processNode(node->mChildren[i], scene); processNode(node->mChildren[i], scene, absoluteTransform);
} }
} }
void AProceduralEntity::loadModel(std::string path) { void AProceduralEntity::loadModel(std::string path) {
Assimp::Importer importer; Assimp::Importer importer;
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) {
...@@ -211,21 +253,19 @@ void AProceduralEntity::loadModel(std::string path) { ...@@ -211,21 +253,19 @@ void AProceduralEntity::loadModel(std::string path) {
wchar_t buffer[260]; wchar_t buffer[260];
GetModuleFileName(NULL, buffer, MAX_PATH); GetModuleFileName(NULL, buffer, MAX_PATH);
UE_LOG(GLTF, Error, TEXT("Curretn PAth: %s"), *FString(buffer)); UE_LOG(GLTF, Error, TEXT("Curretn Path: %s"), *FString(buffer));
return; return;
} }
UE_LOG(GLTF, Warning, TEXT("Has MAterials: %s"), scene->HasMaterials() ? TEXT("True") : TEXT("False"));
double start = FPlatformTime::Seconds(); double start = FPlatformTime::Seconds();
_numVertecies = 0; _numVertecies = 0;
_meshCurrentlyProcessed = 0; _meshCurrentlyProcessed = 0;
processNode(scene->mRootNode, scene); processNode(scene->mRootNode, scene, FMatrix::Identity);
double end = FPlatformTime::Seconds(); double end = FPlatformTime::Seconds();
UE_LOG(GLTF, Warning, TEXT("Final Number of Vertecies in Scene: %i in %f seconds"), _numVertecies, end - start); UE_LOG(GLTF, Log, TEXT("GLTF loaded: %i Vertecies in %f seconds"), _numVertecies, end - start);
} }
UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, const aiScene* scene){ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, const aiScene* scene){
...@@ -233,21 +273,18 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -233,21 +273,18 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
return *MaterialMap.Find(index); return *MaterialMap.Find(index);
} }
else { else {
UE_LOG(GLTF, Warning, TEXT("Parsing Material %d"),index); //UE_LOG(GLTF, Warning, TEXT("Parsing Material %d"),index);
UMaterialInstanceDynamic* newMaterial = UMaterialInstanceDynamic::Create(MasterMaterialRef, this); UMaterialInstanceDynamic* newMaterial = UMaterialInstanceDynamic::Create(MasterMaterialRef, this);
//UMaterialInterface *newMaterial = UMaterial::GetDefaultMaterial(EMaterialDomain::MD_Surface);
aiMaterial* AMat = scene->mMaterials[index]; aiMaterial* AMat = scene->mMaterials[index];
aiColor3D color(0.f, 0.f, 0.f); aiColor3D color(0.f, 0.f, 0.f);
if (AI_SUCCESS == AMat->Get(AI_MATKEY_COLOR_DIFFUSE, color)) { if (AI_SUCCESS == AMat->Get(AI_MATKEY_COLOR_DIFFUSE, color)) {
UE_LOG(GLTF, Warning, TEXT("Material Property: %s, %d %d %d"), TEXT("AI_MATKEY_COLOR_DIFFUSE"), color.r, color.g, color.b); UE_LOG(GLTF, Warning, TEXT("Material Property: %s, %d %d %d not Implemented"), TEXT("AI_MATKEY_COLOR_DIFFUSE"), color.r, color.g, color.b);
} }
else {
UE_LOG(GLTF, Warning, TEXT("Material Property: %s, Failed"), TEXT("AI_MATKEY_COLOR_DIFFUSE"));
}
if (AMat->GetTextureCount(aiTextureType_DIFFUSE) > 0) { if (AMat->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
...@@ -257,17 +294,6 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -257,17 +294,6 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
FString fileName = FString(Path.C_Str()); FString fileName = FString(Path.C_Str());
FString dirPath, temp , right;
if (!_filePath.Split(TEXT("\\"), &temp, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd)) {
temp = _filePath;
}
if (!temp.Split(TEXT("/"), &dirPath, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd)) {
dirPath = temp;
}
if (Path.data[0] == '*') { //embedded if (Path.data[0] == '*') { //embedded
int32 textureIndex = FCString::Atoi(*fileName.RightChop(1)); int32 textureIndex = FCString::Atoi(*fileName.RightChop(1));
//TODO "(note Atoi is unsafe; no way to indicate errors)" from Unreal Wiki //TODO "(note Atoi is unsafe; no way to indicate errors)" from Unreal Wiki
...@@ -275,7 +301,7 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -275,7 +301,7 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
if (textureIndex >= 0) { if (textureIndex >= 0) {
aiTexture *aiTexture = scene->mTextures[textureIndex]; aiTexture *aiTexture = scene->mTextures[textureIndex];
UE_LOG(GLTF, Warning, TEXT("Importing Texture with Index \"%s\" %d (%d|%d)"), *fileName, textureIndex, aiTexture->mWidth, aiTexture->mHeight); // UE_LOG(GLTF, Warning, TEXT("Importing Texture with Index \"%s\" %d (%d|%d)"), *fileName, textureIndex, aiTexture->mWidth, aiTexture->mHeight);
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);
...@@ -283,9 +309,9 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -283,9 +309,9 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
bool isValid; bool isValid;
int32 width, height; int32 width, height;
UTexture2D* diffTexture = LoadTexture2D((uint8*)aiTexture->pcData, aiTexture->mWidth, format, isValid, width, height); diffTexture = LoadTexture2D((uint8*)aiTexture->pcData, aiTexture->mWidth, format, isValid, width, height);
if (isValid) //if (isValid)
newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture); newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture);
} }
...@@ -300,29 +326,27 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index, ...@@ -300,29 +326,27 @@ UMaterialInterface *AProceduralEntity::GetMaterialForIndex(unsigned int index,
} }
else { else {
UE_LOG(GLTF, Warning, TEXT("Diffuse Texture:\"%s\" \"%s\" %d"), *dirPath, *fileName, Path.length); FString dirPath, temp, right;
if (!_filePath.Split(TEXT("\\"), &temp, &right, ESearchCase::IgnoreCase, ESearchDir::FromEnd)) {
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; bool isValid;
int32 width, height; int32 width, height;
UTexture2D* diffTexture = LoadTexture2D_FromFile(dirPath + "\\" + fileName, isValid, width, height); diffTexture = LoadTexture2D_FromFile(dirPath + "\\" + fileName, isValid, width, height);
if (isValid) //if (isValid)
newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture); newMaterial->SetTextureParameterValue(FName("DiffuseTexture"), diffTexture);
} }
MaterialMap.Add(index, newMaterial); MaterialMap.Add(index, newMaterial);
} }
else {
UE_LOG(GLTF, Warning, TEXT("No Diffuse Texture"));
}
} }
return newMaterial; return newMaterial;
} }
} }
...@@ -372,6 +396,8 @@ UTexture2D* AProceduralEntity::LoadTexture2D(uint8* RawFileData, int32 lenght, E ...@@ -372,6 +396,8 @@ UTexture2D* AProceduralEntity::LoadTexture2D(uint8* RawFileData, int32 lenght, E
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper")); IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(Format); IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(Format);
//Create T2D! //Create T2D!
if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData, lenght)) if (ImageWrapper.IsValid() && ImageWrapper->SetCompressed(RawFileData, lenght))
{ {
...@@ -401,4 +427,4 @@ UTexture2D* AProceduralEntity::LoadTexture2D(uint8* RawFileData, int32 lenght, E ...@@ -401,4 +427,4 @@ UTexture2D* AProceduralEntity::LoadTexture2D(uint8* RawFileData, int32 lenght, E
// Success! // Success!
IsValid = true; IsValid = true;
return LoadedT2D; return LoadedT2D;
} }
\ No newline at end of file
...@@ -46,6 +46,8 @@ public: ...@@ -46,6 +46,8 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Materials") UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Materials")
TMap<int32, UMaterialInterface *> MaterialMap; TMap<int32, UMaterialInterface *> MaterialMap;
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = "Materials")
UTexture2D* diffTexture;
private: private:
int32 _selectedVertex; int32 _selectedVertex;
...@@ -76,8 +78,8 @@ private: ...@@ -76,8 +78,8 @@ private:
void ChangeAddModifier(); void ChangeAddModifier();
void ChangeVertex(FVector dv);*/ void ChangeVertex(FVector dv);*/
void processMesh(aiMesh* mesh, const aiScene* scene); void processMesh(aiMesh* mesh, const aiScene* scene, FMatrix modelTransform);
void processNode(aiNode* node, const aiScene* scene); void processNode(aiNode* node, const aiScene* scene, FMatrix modelTransform);
void loadModel(std::string path); void loadModel(std::string path);
UMaterialInterface *GetMaterialForIndex(unsigned int index, const aiScene* scene); UMaterialInterface *GetMaterialForIndex(unsigned int index, const aiScene* scene);
...@@ -85,4 +87,6 @@ private: ...@@ -85,4 +87,6 @@ private:
UTexture2D* LoadTexture2D(uint8* RawFileData, int32 lenght, EImageFormat::Type Format, 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); 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