| 13 | #include "Engine/Core/DeleteMe.h" |
| 14 | |
| 15 | ExportAssetResult AssetExporters::ExportModel(ExportAssetContext& context) |
| 16 | { |
| 17 | auto asset = (ModelBase*)context.Asset.Get(); |
| 18 | auto lock = asset->Storage->LockSafe(); |
| 19 | auto path = GET_OUTPUT_PATH(context, "obj"); |
| 20 | const int32 lodIndex = 0; |
| 21 | |
| 22 | // Fetch chunk with data |
| 23 | const auto chunkIndex = MODEL_LOD_TO_CHUNK_INDEX(lodIndex); |
| 24 | if (asset->LoadChunk(chunkIndex)) |
| 25 | return ExportAssetResult::CannotLoadData; |
| 26 | const auto chunk = asset->GetChunk(chunkIndex); |
| 27 | if (!chunk) |
| 28 | return ExportAssetResult::CannotLoadData; |
| 29 | MemoryReadStream stream(chunk->Get(), chunk->Size()); |
| 30 | FileWriteStream* output = FileWriteStream::Open(path); |
| 31 | if (output == nullptr) |
| 32 | return ExportAssetResult::Error; |
| 33 | DeleteMe<FileWriteStream> outputDeleteMe(output); |
| 34 | |
| 35 | const auto name = StringUtils::GetFileNameWithoutExtension(asset->GetPath()).ToStringAnsi(); |
| 36 | output->WriteText(StringAnsi::Format("# Exported model {0}\n", name.Get())); |
| 37 | |
| 38 | // Extract all meshes |
| 39 | if (asset->GetLODsCount() <= lodIndex) |
| 40 | return ExportAssetResult::Error; |
| 41 | Array<MeshBase*> meshes; |
| 42 | asset->GetMeshes(meshes, lodIndex); |
| 43 | uint32 vertexStart = 1; // OBJ counts vertices from 1 not from 0 |
| 44 | ModelBase::MeshData meshData; |
| 45 | byte meshVersion = stream.ReadByte(); |
| 46 | for (int32 meshIndex = 0; meshIndex < meshes.Count(); meshIndex++) |
| 47 | { |
| 48 | auto mesh = meshes[meshIndex]; |
| 49 | if (asset->LoadMesh(stream, meshVersion, mesh, &meshData)) |
| 50 | return ExportAssetResult::CannotLoadData; |
| 51 | if (meshData.Vertices == 0 || meshData.Triangles == 0) |
| 52 | return ExportAssetResult::Error; |
| 53 | MeshAccessor accessor; |
| 54 | if (accessor.LoadFromMeshData(&meshData)) |
| 55 | return ExportAssetResult::CannotLoadAsset; |
| 56 | output->WriteText(StringAnsi::Format("# Mesh {0}\n", meshIndex)); |
| 57 | |
| 58 | auto positionStream = accessor.Position(); |
| 59 | if (!positionStream.IsValid()) |
| 60 | return ExportAssetResult::Error; |
| 61 | for (uint32 i = 0; i < meshData.Vertices; i++) |
| 62 | { |
| 63 | auto v = positionStream.GetFloat3(i); |
| 64 | output->WriteText(StringAnsi::Format("v {0} {1} {2}\n", v.X, v.Y, v.Z)); |
| 65 | } |
| 66 | output->WriteChar('\n'); |
| 67 | |
| 68 | auto texCoordStream = accessor.TexCoord(); |
| 69 | if (texCoordStream.IsValid()) |
| 70 | { |
| 71 | for (uint32 i = 0; i < meshData.Vertices; i++) |
| 72 | { |
nothing calls this directly
no test coverage detected