Improving Material shader code generation
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
#include "Graphics/Graphics.h"
|
||||
#include "MeshAsset.h"
|
||||
#include "Graphics/Mesh.h"
|
||||
#include "Graphics/StaticMeshVertexInput.h"
|
||||
#include "AssetRegistry.h"
|
||||
#include "Material/Material.h"
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <stb_image_write.h>
|
||||
#include <assimp/config.h>
|
||||
@@ -27,7 +29,8 @@ MeshLoader::~MeshLoader()
|
||||
|
||||
void MeshLoader::importAsset(const std::filesystem::path &path)
|
||||
{
|
||||
futures.add(std::async(std::launch::async, &MeshLoader::import, this, path));
|
||||
//futures.add(std::async(std::launch::async, &MeshLoader::import, this, path));
|
||||
import(path);
|
||||
}
|
||||
|
||||
void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials, Gfx::PGraphics graphics)
|
||||
@@ -52,7 +55,7 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
|
||||
matCode["params"]["diffuseTexture"] =
|
||||
{
|
||||
{"type", "Texture2D"},
|
||||
{"default", texFilename}
|
||||
{"default", texPath.C_Str()}
|
||||
};
|
||||
code.push_back("result.baseColor = diffuseTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
|
||||
}
|
||||
@@ -62,7 +65,7 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
|
||||
matCode["params"]["specularTexture"] =
|
||||
{
|
||||
{"type", "Texture2D"},
|
||||
{"default", texFilename}
|
||||
{"default", texPath.C_Str()}
|
||||
};
|
||||
code.push_back("result.specular = specularTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
|
||||
}
|
||||
@@ -72,7 +75,7 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
|
||||
matCode["params"]["normalTexture"] =
|
||||
{
|
||||
{"type", "Texture2D"},
|
||||
{"default", texFilename}
|
||||
{"default", texPath.C_Str()}
|
||||
};
|
||||
code.push_back("float3 bumpMapNormal = normalTexture.Sample(textureSampler, geometry.texCoord).xyz;\n");
|
||||
code.push_back("bumpMapNormal = 2.0 * bumpMapNormal - float3(1.0f, 1.0f, 1.0f);\n");
|
||||
@@ -80,10 +83,15 @@ void loadMaterials(const aiScene* scene, Array<PMaterialAsset>& globalMaterials,
|
||||
}
|
||||
code.push_back("return result;\n");
|
||||
matCode["code"] = code;
|
||||
std::ofstream outMatFile("testMat.asset");
|
||||
std::string outMatFilename = matCode["name"].get<std::string>().append(".asset");
|
||||
std::ofstream outMatFile = AssetRegistry::createWriteStream(outMatFilename);
|
||||
outMatFile << std::setw(4) << matCode;
|
||||
outMatFile.flush();
|
||||
PMaterial asset = new Material("testMat.asset");
|
||||
outMatFile.close();
|
||||
//TODO: let the material loader handle this instead
|
||||
std::cout << matCode["name"] << std::endl;
|
||||
AssetRegistry::importFile(outMatFilename);
|
||||
PMaterialAsset asset = AssetRegistry::findMaterial(outMatFilename);
|
||||
globalMaterials[i] = asset;
|
||||
}
|
||||
}
|
||||
@@ -108,7 +116,7 @@ void loadToBuffer(Array<Vector>& buffer, const aiVector3D* sourceData, uint32 si
|
||||
buffer[i] = Vector(sourceData[i].x, sourceData[i].y, sourceData[i].z);
|
||||
}
|
||||
}
|
||||
Gfx::VertexStream createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::PGraphics graphics)
|
||||
VertexStreamComponent createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::PGraphics graphics)
|
||||
{
|
||||
Array<Vector> buffer(size);
|
||||
for(uint32 i = 0; i < size; ++i)
|
||||
@@ -121,12 +129,10 @@ Gfx::VertexStream createVertexStream(uint32 size, aiVector3D* sourceData, Gfx::P
|
||||
vbInfo.resourceData.data = (uint8 *)buffer.data();
|
||||
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
|
||||
vbInfo.resourceData.size = buffer.size();
|
||||
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
|
||||
auto stream = Gfx::VertexStream(vbInfo.vertexSize, 0, false, vertexBuffer);
|
||||
stream.addVertexElement(Gfx::VertexElement(0, Gfx::SE_FORMAT_R32G32_SFLOAT, 0));
|
||||
return stream;
|
||||
const Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
|
||||
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32B32_SFLOAT);
|
||||
}
|
||||
Gfx::VertexStream createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::PGraphics graphics)
|
||||
VertexStreamComponent createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::PGraphics graphics)
|
||||
{
|
||||
Array<Vector2> buffer(size);
|
||||
for(uint32 i = 0; i < size; ++i)
|
||||
@@ -140,9 +146,7 @@ Gfx::VertexStream createVertexStream(uint32 size, aiVector2D* sourceData, Gfx::P
|
||||
vbInfo.resourceData.owner = Gfx::QueueType::DEDICATED_TRANSFER;
|
||||
vbInfo.resourceData.size = buffer.size();
|
||||
Gfx::PVertexBuffer vertexBuffer = graphics->createVertexBuffer(vbInfo);
|
||||
auto stream = Gfx::VertexStream(vbInfo.vertexSize, 0, false, vertexBuffer);
|
||||
stream.addVertexElement(Gfx::VertexElement(0, Gfx::SE_FORMAT_R32G32B32_SFLOAT, 0));
|
||||
return stream;
|
||||
return VertexStreamComponent(vertexBuffer, 0, vbInfo.vertexSize, Gfx::SE_FORMAT_R32G32_SFLOAT);
|
||||
}
|
||||
void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Array<PMaterialAsset> materials, Gfx::PGraphics graphics)
|
||||
{
|
||||
@@ -150,32 +154,29 @@ void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Array<PM
|
||||
{
|
||||
aiMesh *mesh = scene->mMeshes[meshIndex];
|
||||
|
||||
MeshDescription description;
|
||||
Gfx::PVertexDeclaration declaration = new Gfx::VertexDeclaration();
|
||||
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
|
||||
description.layout.add(Gfx::VertexAttribute::POSITION);
|
||||
PStaticMeshVertexInput vertexShaderInput = new StaticMeshVertexInput(std::string(mesh->mName.C_Str()));
|
||||
VertexStreamComponent positionStream = createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics);
|
||||
vertexShaderInput->setPositionStream(positionStream);
|
||||
|
||||
for(uint32 i = 0; i < MAX_TEX_CHANNELS; ++i)
|
||||
for(uint32 i = 0; i < MAX_TEXCOORDS; ++i)
|
||||
{
|
||||
if(mesh->HasTextureCoords(i))
|
||||
{
|
||||
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mTextureCoords[i], graphics));
|
||||
description.layout.add(Gfx::VertexAttribute::TEXCOORD);
|
||||
VertexStreamComponent texCoordStream = createVertexStream(mesh->mNumVertices, mesh->mTextureCoords[i], graphics);
|
||||
vertexShaderInput->setTexCoordStream(i, texCoordStream);
|
||||
}
|
||||
}
|
||||
if(mesh->HasNormals())
|
||||
{
|
||||
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mNormals, graphics));
|
||||
description.layout.add(Gfx::VertexAttribute::NORMAL);
|
||||
VertexStreamComponent normalStream = createVertexStream(mesh->mNumVertices, mesh->mNormals, graphics);
|
||||
vertexShaderInput->setTangentXStream(normalStream);
|
||||
}
|
||||
if(mesh->HasTangentsAndBitangents())
|
||||
{
|
||||
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
|
||||
declaration->addVertexStream(createVertexStream(mesh->mNumVertices, mesh->mVertices, graphics));
|
||||
description.layout.add(Gfx::VertexAttribute::TANGENT);
|
||||
description.layout.add(Gfx::VertexAttribute::BITANGENT);
|
||||
//TODO: use bitangent to calculate sign for 4th coordinate of tangentstream
|
||||
VertexStreamComponent tangentStream = createVertexStream(mesh->mNumVertices, mesh->mTangents, graphics);
|
||||
vertexShaderInput->setTangentZStream(tangentStream);
|
||||
}
|
||||
description.declaration = declaration;
|
||||
|
||||
Array<uint32> indices(mesh->mNumFaces * 3);
|
||||
for (uint32 faceIndex = 0; faceIndex < mesh->mNumFaces; ++faceIndex)
|
||||
@@ -192,7 +193,7 @@ void loadGlobalMeshes(const aiScene* scene, Array<PMesh>& globalMeshes, Array<PM
|
||||
Gfx::PIndexBuffer indexBuffer = graphics->createIndexBuffer(idxInfo);
|
||||
indexBuffer->transferOwnership(Gfx::QueueType::GRAPHICS);
|
||||
|
||||
globalMeshes[meshIndex] = new Mesh(description, indexBuffer);
|
||||
globalMeshes[meshIndex] = new Mesh(vertexShaderInput, indexBuffer);
|
||||
globalMeshes[meshIndex]->referencedMaterial = materials[mesh->mMaterialIndex];
|
||||
}
|
||||
}
|
||||
@@ -247,23 +248,27 @@ void MeshLoader::import(const std::filesystem::path &path)
|
||||
aiProcess_FindDegenerates |
|
||||
aiProcess_EmbedTextures);
|
||||
const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
|
||||
Array<PMesh> globalMeshes(scene->mNumMeshes);
|
||||
|
||||
Array<PMaterialAsset> globalMaterials(scene->mNumMaterials);
|
||||
loadMaterials(scene, globalMaterials, graphics);
|
||||
loadGlobalMeshes(scene, globalMeshes, globalMaterials, graphics);
|
||||
loadTextures(scene, graphics, path);
|
||||
loadMaterials(scene, globalMaterials, graphics);
|
||||
|
||||
Array<PMesh> globalMeshes(scene->mNumMeshes);
|
||||
loadGlobalMeshes(scene, globalMeshes, globalMaterials, graphics);
|
||||
|
||||
|
||||
List<aiNode *> meshNodes;
|
||||
findMeshRoots(scene->mRootNode, meshNodes);
|
||||
std::filesystem::path filePath = path.filename();
|
||||
filePath.replace_extension("asset");
|
||||
PMeshAsset meshAsset = new MeshAsset(filePath.generic_string());
|
||||
for (auto meshNode : meshNodes)
|
||||
{
|
||||
std::string fileName = std::string("arissa").append(".asset");
|
||||
PMeshAsset meshAsset = new MeshAsset(fileName);
|
||||
for(uint32 i = 0; i < meshNode->mNumMeshes; ++i)
|
||||
{
|
||||
meshAsset->addMesh(globalMeshes[meshNode->mMeshes[i]]);
|
||||
}
|
||||
meshAsset->save();
|
||||
AssetRegistry::get().meshes[meshAsset->getFileName()] = meshAsset;
|
||||
}
|
||||
meshAsset->save();
|
||||
AssetRegistry::get().registerMesh(meshAsset);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user