Skip to content

Commit 27c0397

Browse files
committed
Start Project class
Flair::Project now loads meshes into an array and creates a debug material with the "Standard" shader. The GameObject is still created and instantiated from main.cpp. The Project class will serve as a unified place to store all information related to a project.
1 parent 88e5795 commit 27c0397

3 files changed

Lines changed: 142 additions & 97 deletions

File tree

include/project.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include "assimp/scene.h"
4+
5+
#include "UnityEngine/Mesh.hpp"
6+
#include "UnityEngine/Material.hpp"
7+
8+
#include <string>
9+
#include <vector>
10+
11+
namespace Flair {
12+
13+
class Project {
14+
private:
15+
void LoadMeshes(const aiScene* scene);
16+
void LoadMaterials(const aiScene* scene);
17+
18+
public:
19+
std::string name;
20+
std::string author;
21+
std::string description;
22+
23+
std::vector<SafePtrUnity<UnityEngine::Mesh>> meshes;
24+
std::vector<SafePtrUnity<UnityEngine::Material>> materials;
25+
26+
Project();
27+
Project(std::string_view filePath);
28+
29+
void LoadFromFile(std::string_view filePath);
30+
};
31+
32+
}

src/main.cpp

Lines changed: 13 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "UnityEngine/ParticleSystem.hpp"
1212
#include "Window/window.hpp"
1313
#include "Window/createModuleWindows.hpp"
14+
#include "project.hpp"
1415

1516
#include "assimp/shared/assimp/Importer.hpp"
1617
#include "assimp/shared/assimp/Exporter.hpp"
@@ -24,6 +25,7 @@
2425
#include "UnityEngine/Rendering/IndexFormat.hpp"
2526

2627
#include "bsml/shared/Helpers/getters.hpp"
28+
#include "bsml/shared/BSML/MainThreadScheduler.hpp"
2729

2830

2931
static modloader::ModInfo modInfo{MOD_ID, VERSION, 0};
@@ -47,103 +49,17 @@ MAKE_HOOK_MATCH(AssimpTestHook, &GlobalNamespace::MainMenuViewController::DidAct
4749
std::string file = "/storage/emulated/0/ModData/com.beatgames.beatsaber/Mods/Flair/testCube.glb";
4850
// std::string file = "/storage/emulated/0/ModData/com.beatgames.beatsaber/Mods/Flair/teapot.glb";
4951

50-
Assimp::Importer importer;
51-
52-
const aiScene* scene = importer.ReadFile(file,
53-
aiProcess_Triangulate);
54-
55-
if(scene == nullptr) {
56-
PaperLogger.info("Error!");
57-
PaperLogger.error("{}", importer.GetErrorString());
58-
return;
59-
}
60-
61-
for(int i = 0; i < scene->mNumMeshes; i++) {
62-
aiMesh* mesh = scene->mMeshes[i];
63-
PaperLogger.info("Mesh name: {}", mesh->mName.C_Str());
64-
65-
UnityEngine::Mesh* unityMesh = UnityEngine::Mesh::New_ctor();
66-
unityMesh->set_name(mesh->mName.C_Str());
67-
unityMesh->Clear();
68-
69-
ArrayW<UnityEngine::Vector3> vertices (mesh->mNumVertices);
70-
for(int j = 0; j < mesh->mNumVertices; j++) {
71-
aiVector3D& vertex = mesh->mVertices[j];
72-
PaperLogger.info("Vertex {} | X: {}, Y: {}, Z: {}", j, vertex.x, vertex.y, vertex.z);
73-
vertices[j] = UnityEngine::Vector3(vertex.x, vertex.y, vertex.z);
74-
}
75-
unityMesh->set_vertices(vertices);
76-
77-
ArrayW<int> triangles (mesh->mNumFaces * 3);
78-
for(int j = 0; j < mesh->mNumFaces; j++) {
79-
aiFace& face = mesh->mFaces[j];
80-
for(int k = 0; k < 3; k++) {
81-
PaperLogger.info("Face {} Index {}", j, face.mIndices[k]);
82-
triangles[j * 3 + k] = face.mIndices[k];
83-
}
84-
}
85-
unityMesh->set_triangles(triangles);
86-
87-
if(mesh->mNormals != nullptr) {
88-
ArrayW<UnityEngine::Vector3> normals (mesh->mNumVertices);
89-
for(int j = 0; j < mesh->mNumVertices; j++) {
90-
aiVector3D& normal = mesh->mNormals[j];
91-
PaperLogger.info("Normal {} | X: {}, Y: {}, Z: {}", j, normal.x, normal.y, normal.z);
92-
normals[j] = UnityEngine::Vector3(normal.x, normal.y, normal.z);
93-
}
94-
unityMesh->set_normals(normals);
95-
} else {
96-
unityMesh->RecalculateNormals();
97-
}
98-
99-
if(mesh->mColors[0] != nullptr) {
100-
ArrayW<UnityEngine::Color> colors (mesh->mNumVertices);
101-
for(int j = 0; j < mesh->mNumVertices; j++) {
102-
aiColor4D& color = mesh->mColors[i][j];
103-
PaperLogger.info("Color {} | R: {}, G: {}, B: {}, A: {}", j, color.r, color.g, color.b, color.a);
104-
colors[j] = UnityEngine::Color(color.r, color.g, color.b, color.a);
105-
}
106-
unityMesh->set_colors(colors);
107-
}
108-
109-
110-
111-
UnityEngine::GameObject* testGO = UnityEngine::GameObject::New_ctor("FlairTest");
112-
testGO->get_transform()->set_position(UnityEngine::Vector3(0, 0.5, 0.5));
113-
testGO->get_transform()->set_localScale(UnityEngine::Vector3(0.2, 0.2, 0.2));
114-
115-
UnityEngine::MeshFilter* testFilter = testGO->AddComponent<UnityEngine::MeshFilter*>();
116-
testFilter->set_sharedMesh(unityMesh);
117-
118-
UnityEngine::MeshRenderer* testRenderer = testGO->AddComponent<UnityEngine::MeshRenderer*>();
119-
UnityEngine::Shader* shader = UnityEngine::Resources::FindObjectsOfTypeAll<UnityEngine::Shader*>()->FirstOrDefault([](UnityEngine::Shader* shader){return shader->get_name() == "Standard";});
120-
UnityEngine::Material* material = UnityEngine::Material::New_ctor(shader);
121-
testRenderer->set_sharedMaterial(material);
122-
123-
124-
125-
Assimp::Exporter exporter;
126-
127-
for(int j = 0; j < mesh->mNumVertices; j++) {
128-
aiVector3D& vertex = mesh->mVertices[j];
129-
if( vertex.x == 1 &&
130-
vertex.y == 1 &&
131-
vertex.z == -1
132-
) {
133-
vertex.x = 0;
134-
vertex.y = 0;
135-
vertex.z = 0;
136-
}
137-
}
138-
139-
exporter.Export(scene, "stl", "/storage/emulated/0/ModData/com.beatgames.beatsaber/Mods/Flair/testCube.glb");
140-
141-
std::string_view err = exporter.GetErrorString();
142-
if(!err.empty()) {
143-
PaperLogger.info("Error exporting!");
144-
PaperLogger.error("{}", err);
145-
}
146-
}
52+
Flair::Project project (file);
53+
54+
UnityEngine::GameObject* testGO = UnityEngine::GameObject::New_ctor("FlairTest");
55+
testGO->get_transform()->set_position(UnityEngine::Vector3(0, 0.5, 0.5));
56+
testGO->get_transform()->set_localScale(UnityEngine::Vector3(0.2, 0.2, 0.2));
57+
58+
UnityEngine::MeshFilter* testFilter = testGO->AddComponent<UnityEngine::MeshFilter*>();
59+
testFilter->set_sharedMesh(project.meshes[0].ptr());
60+
61+
UnityEngine::MeshRenderer* testRenderer = testGO->AddComponent<UnityEngine::MeshRenderer*>();
62+
testRenderer->set_sharedMaterial(project.materials[0].ptr());
14763

14864

14965

src/project.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "project.hpp"
2+
#include "main.hpp"
3+
4+
#include "assimp/Importer.hpp"
5+
#include "assimp/scene.h"
6+
#include "assimp/postprocess.h"
7+
#include "assimp/mesh.h"
8+
9+
#include "UnityEngine/GameObject.hpp"
10+
#include "UnityEngine/Vector3.hpp"
11+
#include "UnityEngine/Color.hpp"
12+
#include "UnityEngine/Mesh.hpp"
13+
#include "UnityEngine/Resources.hpp" // DEBUG
14+
15+
using namespace Flair;
16+
using namespace UnityEngine;
17+
18+
Project::Project() {
19+
20+
}
21+
22+
Project::Project(std::string_view filePath) {
23+
LoadFromFile(filePath);
24+
}
25+
26+
void Project::LoadFromFile(std::string_view filePath) {
27+
PaperLogger.info("Loading project from file");
28+
29+
Assimp::Importer importer;
30+
const aiScene* scene = importer.ReadFile(std::string(filePath),
31+
aiProcess_Triangulate
32+
);
33+
34+
if(scene == nullptr) {
35+
PaperLogger.error("{}", importer.GetErrorString());
36+
return;
37+
}
38+
39+
LoadMeshes(scene);
40+
LoadMaterials(scene);
41+
}
42+
43+
void Project::LoadMeshes(const aiScene* scene) {
44+
for(int i = 0; i < scene->mNumMeshes; i++) {
45+
aiMesh* mesh = scene->mMeshes[i];
46+
47+
Mesh* unityMesh = Mesh::New_ctor();
48+
unityMesh->set_name(mesh->mName.C_Str());
49+
unityMesh->Clear();
50+
51+
ArrayW<Vector3> vertices (mesh->mNumVertices);
52+
for(int j = 0; j < mesh->mNumVertices; j++) {
53+
aiVector3D& vertex = mesh->mVertices[j];
54+
vertices[j] = Vector3(vertex.x, vertex.y, vertex.z);
55+
}
56+
unityMesh->set_vertices(vertices);
57+
58+
ArrayW<int> triangles (mesh->mNumFaces * 3);
59+
for(int j = 0; j < mesh->mNumFaces; j++) {
60+
aiFace& face = mesh->mFaces[j];
61+
for(int k = 0; k < 3; k++) {
62+
triangles[j * 3 + k] = face.mIndices[k];
63+
}
64+
}
65+
unityMesh->set_triangles(triangles);
66+
67+
if(mesh->mNormals != nullptr) {
68+
ArrayW<Vector3> normals (mesh->mNumVertices);
69+
for(int j = 0; j < mesh->mNumVertices; j++) {
70+
aiVector3D& normal = mesh->mNormals[j];
71+
normals[j] = Vector3(normal.x, normal.y, normal.z);
72+
}
73+
unityMesh->set_normals(normals);
74+
} else {
75+
unityMesh->RecalculateNormals();
76+
}
77+
78+
if(mesh->mColors[0] != nullptr) {
79+
ArrayW<Color> colors (mesh->mNumVertices);
80+
for(int j = 0; j < mesh->mNumVertices; j++) {
81+
aiColor4D& color = mesh->mColors[i][j];
82+
colors[j] = Color(color.r, color.g, color.b, color.a);
83+
}
84+
unityMesh->set_colors(colors);
85+
}
86+
87+
meshes.push_back(unityMesh);
88+
}
89+
}
90+
91+
void Project::LoadMaterials(const aiScene* scene) {
92+
// DEBUG
93+
Shader* shader = Resources::FindObjectsOfTypeAll<Shader*>()->FirstOrDefault([](Shader* shader){return shader->get_name() == "Standard";});
94+
SafePtrUnity<Material> material = Material::New_ctor(shader);
95+
96+
materials.push_back(material);
97+
}

0 commit comments

Comments
 (0)