-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImportStaticMesh.cpp
302 lines (233 loc) · 9.73 KB
/
ImportStaticMesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "ImportStaticMesh.hpp"
#include "ImportMaterial.hpp"
#include "ImportPath.hpp"
#include "ResourceManifest.hpp"
#include <Components/BsCRenderable.h>
#include <FileSystem/BsFileSystem.h>
#include <RenderAPI/BsVertexDataDesc.h>
#include <Resources/BsBuiltinResources.h>
#include <Resources/BsResources.h>
#include <Scene/BsPrefab.h>
#include <Scene/BsSceneObject.h>
#include <vdfs/fileIndex.h>
#include <zenload/zCMesh.h>
#include <zenload/zCProgMeshProto.h>
using namespace bs;
struct StaticMeshVertex
{
Vector3 position;
Vector3 normal;
Vector2 texCoord;
uint32_t color;
Vector3 tangent;
Vector3 bitangent;
};
static HPrefab cacheStaticMesh(const bs::String& originalFileName, HMesh mesh,
const Vector<HMaterial>& materials);
static SPtr<VertexDataDesc> makeVertexDataDescForZenLibVertex();
static MESH_DESC meshDescForPackedMesh(const ZenLoad::PackedMesh& packedMesh);
static Vector<StaticMeshVertex> transformVertices(const ZenLoad::PackedMesh& packedMesh);
static void fillMeshDataFromPackedMesh(HMesh target, const Vector<StaticMeshVertex>& vertices,
const ZenLoad::PackedMesh& packedMesh);
static void transferVertices(SPtr<MeshData> target, const Vector<StaticMeshVertex>& vertices);
static void transferIndices(SPtr<MeshData> target, const ZenLoad::PackedMesh& packedMesh);
// - Implementation --------------------------------------------------------------------------------
BsZenLib::Res::HMeshWithMaterials BsZenLib::ImportAndCacheStaticMesh(
const bs::String& originalFileName, const VDFS::FileIndex& vdfs)
{
BS_LOG(Info, Uncategorized, "Caching Static Mesh: " + originalFileName);
HMesh mesh = ImportAndCacheStaticMeshGeometry(originalFileName, vdfs);
if (!mesh)
{
BS_LOG(Warning, Uncategorized, "Load Failed (Mesh): " + originalFileName);
return {};
}
Vector<HMaterial> materials = ImportAndCacheStaticMeshMaterials(originalFileName, vdfs);
if (materials.empty())
{
BS_LOG(Warning, Uncategorized, "Load Failed (Materials): " + originalFileName);
return {};
}
Res::HMeshWithMaterials combined = Res::MeshWithMaterials::create(mesh, materials);
if (!combined)
{
BS_LOG(Warning, Uncategorized, "Load Failed (Combined): " + originalFileName);
return {};
}
const bool overwrite = true;
gResources().save(combined, GothicPathToCachedStaticMesh(originalFileName), overwrite);
AddToResourceManifest(combined, GothicPathToCachedStaticMesh(originalFileName));
return combined;
}
BsZenLib::Res::HMeshWithMaterials BsZenLib::ImportAndCacheStaticMesh(
const bs::String& originalFileName, const ZenLoad::PackedMesh& packedMesh,
const VDFS::FileIndex& vdfs)
{
BS_LOG(Info, Uncategorized, "Caching Static Mesh: " + originalFileName);
HMesh mesh = ImportAndCacheStaticMeshGeometry(originalFileName, packedMesh);
if (!mesh)
{
BS_LOG(Warning, Uncategorized, "Load Failed (Mesh): " + originalFileName);
return {};
}
Vector<HMaterial> materials =
ImportAndCacheStaticMeshMaterials(originalFileName, packedMesh, vdfs);
if (materials.empty())
{
BS_LOG(Warning, Uncategorized, "Load Failed (Materials): " + originalFileName);
return {};
}
Res::HMeshWithMaterials combined = Res::MeshWithMaterials::create(mesh, materials);
if (!combined)
{
BS_LOG(Warning, Uncategorized, "Load Failed (Combined): " + originalFileName);
return {};
}
const bool overwrite = true;
gResources().save(combined, GothicPathToCachedStaticMesh(originalFileName), overwrite);
AddToResourceManifest(combined, GothicPathToCachedStaticMesh(originalFileName));
return combined;
}
BsZenLib::Res::HMeshWithMaterials BsZenLib::LoadCachedStaticMesh(const bs::String& originalFileName)
{
return gResources().load<Res::MeshWithMaterials>(GothicPathToCachedStaticMesh(originalFileName));
}
bool BsZenLib::HasCachedStaticMesh(const bs::String& originalFileName)
{
return FileSystem::isFile(GothicPathToCachedStaticMesh(originalFileName));
}
HMesh BsZenLib::ImportAndCacheStaticMeshGeometry(const bs::String& originalFileName,
const VDFS::FileIndex& vdfs)
{
bs::String withoutExt = originalFileName.substr(0, originalFileName.find_last_of('.'));
bs::String compiledExt = withoutExt + ".MRM";
ZenLoad::zCProgMeshProto progMesh(compiledExt.c_str(), vdfs);
if (progMesh.getNumSubmeshes() == 0) return {};
ZenLoad::PackedMesh packedMesh;
progMesh.packMesh(packedMesh, 0.01f);
return ImportAndCacheStaticMeshGeometry(originalFileName, packedMesh);
}
bs::HMesh BsZenLib::ImportAndCacheStaticMeshGeometry(const bs::String& originalFileName,
const ZenLoad::PackedMesh& packedMesh)
{
HMesh mesh = ImportStaticMeshGeometry(packedMesh);
Path path = GothicPathToCachedStaticMesh(originalFileName + ".mesh");
if (!mesh) return {};
mesh->setName(originalFileName);
const bool overwrite = true;
gResources().save(mesh, path, overwrite);
AddToResourceManifest(mesh, path);
return mesh;
}
Vector<HMaterial> BsZenLib::ImportAndCacheStaticMeshMaterials(const bs::String& originalFileName,
const VDFS::FileIndex& vdfs)
{
bs::String withoutExt = originalFileName.substr(0, originalFileName.find_last_of('.'));
bs::String compiledExt = withoutExt + ".MRM";
ZenLoad::zCProgMeshProto progMesh(compiledExt.c_str(), vdfs);
if (progMesh.getNumSubmeshes() == 0) return {};
ZenLoad::PackedMesh packedMesh;
progMesh.packMesh(packedMesh, 0.01f);
return ImportAndCacheStaticMeshMaterials(originalFileName, packedMesh, vdfs);
}
Vector<HMaterial> BsZenLib::ImportAndCacheStaticMeshMaterials(const bs::String& originalFileName,
const ZenLoad::PackedMesh& packedMesh,
const VDFS::FileIndex& vdfs)
{
HShader shader = gBuiltinResources().getBuiltinShader(BuiltinShader::Standard);
Vector<HMaterial> materials;
for (size_t i = 0; i < packedMesh.subMeshes.size(); i++)
{
// TODO: Transfer more properties
const auto& originalMaterial = packedMesh.subMeshes[i].material;
HMaterial loaded;
String materialCacheFile = BuildMaterialNameForSubmesh(originalFileName, (UINT32)i);
if (HasCachedMaterial(materialCacheFile))
{
loaded = LoadCachedMaterial(materialCacheFile);
}
else
{
loaded = ImportAndCacheMaterialWithTextures(materialCacheFile, originalMaterial, vdfs);
}
materials.push_back(loaded);
}
return materials;
}
HMesh BsZenLib::ImportStaticMeshGeometry(const ZenLoad::PackedMesh& packedMesh)
{
MESH_DESC desc = meshDescForPackedMesh(packedMesh);
HMesh mesh = Mesh::create(desc);
Vector<StaticMeshVertex> vertices = transformVertices(packedMesh);
fillMeshDataFromPackedMesh(mesh, vertices, packedMesh);
return mesh;
}
static MESH_DESC meshDescForPackedMesh(const ZenLoad::PackedMesh& packedMesh)
{
MESH_DESC desc = {};
for (const auto& submesh : packedMesh.subMeshes)
{
desc.subMeshes.emplace_back();
desc.subMeshes.back().drawOp = DrawOperationType::DOT_TRIANGLE_LIST;
desc.subMeshes.back().indexCount = (UINT32)submesh.indices.size();
desc.subMeshes.back().indexOffset = desc.numIndices;
desc.numIndices += (UINT32)submesh.indices.size();
}
desc.indexType = IndexType::IT_32BIT;
desc.numVertices = (UINT32)packedMesh.vertices.size();
desc.vertexDesc = makeVertexDataDescForZenLibVertex();
desc.usage = MU_CPUCACHED; // To create our physics mesh later
return desc;
}
static SPtr<VertexDataDesc> makeVertexDataDescForZenLibVertex()
{
SPtr<VertexDataDesc> vertexDataDesc = VertexDataDesc::create();
vertexDataDesc->addVertElem(VET_FLOAT3, VES_POSITION);
vertexDataDesc->addVertElem(VET_FLOAT3, VES_NORMAL);
vertexDataDesc->addVertElem(VET_FLOAT2, VES_TEXCOORD);
vertexDataDesc->addVertElem(VET_COLOR, VES_COLOR);
vertexDataDesc->addVertElem(VET_FLOAT3, VES_TANGENT);
vertexDataDesc->addVertElem(VET_FLOAT3, VES_BITANGENT);
return vertexDataDesc;
}
static void fillMeshDataFromPackedMesh(HMesh target, const Vector<StaticMeshVertex>& vertices,
const ZenLoad::PackedMesh& packedMesh)
{
// Allocate a buffer big enough to hold what we specified in the MESH_DESC
SPtr<MeshData> meshData = target->allocBuffer();
transferVertices(meshData, vertices);
transferIndices(meshData, packedMesh);
target->writeData(meshData, false);
}
static Vector<StaticMeshVertex> transformVertices(const ZenLoad::PackedMesh& packedMesh)
{
Vector<StaticMeshVertex> v;
for (const ZenLoad::WorldVertex& oldVertex : packedMesh.vertices)
{
StaticMeshVertex newVertex = {};
newVertex.position = Vector3(oldVertex.Position.x, oldVertex.Position.y, oldVertex.Position.z);
newVertex.normal = Vector3(oldVertex.Normal.x, oldVertex.Normal.y, oldVertex.Normal.z);
newVertex.texCoord = Vector2(oldVertex.TexCoord.x, oldVertex.TexCoord.y);
newVertex.color = oldVertex.Color;
newVertex.tangent = Vector3();
newVertex.bitangent = Vector3();
v.push_back(newVertex);
}
return v;
}
static void transferVertices(SPtr<MeshData> target, const Vector<StaticMeshVertex>& vertices)
{
assert(target->getNumVertices() == vertices.size());
UINT8* pVertices = target->getElementData(VES_POSITION);
memcpy(pVertices, vertices.data(), sizeof(StaticMeshVertex) * vertices.size());
}
static void transferIndices(SPtr<MeshData> target, const ZenLoad::PackedMesh& packedMesh)
{
UINT32* pIndices = target->getIndices32();
size_t writtenSoFar = 0;
for (const auto& submesh : packedMesh.subMeshes)
{
memcpy(&pIndices[writtenSoFar], submesh.indices.data(), sizeof(UINT32) * submesh.indices.size());
writtenSoFar += submesh.indices.size();
}
}