poc submesh position export

This commit is contained in:
ackwell 2023-12-27 16:17:39 +11:00
parent ed283afe2c
commit b7472f722e
2 changed files with 55 additions and 18 deletions

View file

@ -1,3 +1,4 @@
using Lumina.Extensions;
using OtterGui.Tasks; using OtterGui.Tasks;
using Penumbra.GameData.Files; using Penumbra.GameData.Files;
using SharpGLTF.Geometry; using SharpGLTF.Geometry;
@ -45,39 +46,75 @@ public sealed class ModelManager : SingleTaskQueue, IDisposable
return task; return task;
} }
public Task ExportToGltf(/* MdlFile mdl, */string path) public Task ExportToGltf(MdlFile mdl, string path)
=> Enqueue(new ExportToGltfAction(path)); => Enqueue(new ExportToGltfAction(mdl, path));
private class ExportToGltfAction : IAction private class ExportToGltfAction : IAction
{ {
private readonly MdlFile _mdl;
private readonly string _path; private readonly string _path;
public ExportToGltfAction(string path) public ExportToGltfAction(MdlFile mdl, string path)
{ {
_mdl = mdl;
_path = path; _path = path;
} }
public void Execute(CancellationToken token) public void Execute(CancellationToken token)
{ {
var mesh = new MeshBuilder<VertexPosition>("mesh"); var meshBuilder = new MeshBuilder<VertexPosition>("mesh");
var material1 = new MaterialBuilder() var material = new MaterialBuilder()
.WithDoubleSide(true) .WithDoubleSide(true)
.WithMetallicRoughnessShader() .WithMetallicRoughnessShader()
.WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 0, 1)); .WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 1, 1, 1));
var primitive1 = mesh.UsePrimitive(material1);
primitive1.AddTriangle(new VertexPosition(-10, 0, 0), new VertexPosition(10, 0, 0), new VertexPosition(0, 10, 0)); // lol, lmao even
primitive1.AddTriangle(new VertexPosition(10, 0, 0), new VertexPosition(-10, 0, 0), new VertexPosition(0, -10, 0)); var meshIndex = 2;
var lod = 0;
var material2 = new MaterialBuilder()
.WithDoubleSide(true) var mesh = _mdl.Meshes[meshIndex];
.WithMetallicRoughnessShader() var submesh = _mdl.SubMeshes[mesh.SubMeshIndex]; // just first for now
.WithChannelParam(KnownChannel.BaseColor, KnownProperty.RGBA, new Vector4(1, 0, 1, 1));
var primitive2 = mesh.UsePrimitive(material2); var positionVertexElement = _mdl.VertexDeclarations[meshIndex].VertexElements
primitive2.AddQuadrangle(new VertexPosition(-5, 0, 3), new VertexPosition(0, -5, 3), new VertexPosition(5, 0, 3), new VertexPosition(0, 5, 3)); .Where(decl => decl.Usage == 0 /* POSITION */)
.First();
// reading in the entire indices list
var dataReader = new BinaryReader(new MemoryStream(_mdl.RemainingData));
dataReader.Seek(_mdl.IndexOffset[lod]);
var indices = dataReader.ReadStructuresAsArray<ushort>((int)_mdl.IndexBufferSize[lod] / sizeof(ushort));
// read in verts for this mesh
var baseOffset = _mdl.VertexOffset[lod] + mesh.VertexBufferOffset[positionVertexElement.Stream] + positionVertexElement.Offset;
var vertices = new List<VertexPosition>();
for (var vertexIndex = 0; vertexIndex < mesh.VertexCount; vertexIndex++)
{
dataReader.Seek(baseOffset + vertexIndex * mesh.VertexBufferStride[positionVertexElement.Stream]);
// todo handle type
vertices.Add(new VertexPosition(
dataReader.ReadSingle(),
dataReader.ReadSingle(),
dataReader.ReadSingle()
));
}
// build a primitive for the submesh
var primitiveBuilder = meshBuilder.UsePrimitive(material);
// they're all tri list
for (var indexOffset = 0; indexOffset < submesh.IndexCount; indexOffset += 3)
{
var index = indexOffset + submesh.IndexOffset;
primitiveBuilder.AddTriangle(
vertices[indices[index + 0]],
vertices[indices[index + 1]],
vertices[indices[index + 2]]
);
}
var scene = new SceneBuilder(); var scene = new SceneBuilder();
scene.AddRigidMesh(mesh, Matrix4x4.Identity); scene.AddRigidMesh(meshBuilder, Matrix4x4.Identity);
var model = scene.ToGltf2(); var model = scene.ToGltf2();
model.SaveGLTF(_path); model.SaveGLTF(_path);

View file

@ -38,7 +38,7 @@ public partial class ModEditWindow
if (ImGuiUtil.DrawDisabledButton("bingo bango", Vector2.Zero, "description", _pendingIo)) if (ImGuiUtil.DrawDisabledButton("bingo bango", Vector2.Zero, "description", _pendingIo))
{ {
_pendingIo = true; _pendingIo = true;
var task = _models.ExportToGltf("C:\\Users\\ackwell\\blender\\gltf-tests\\bingo.gltf"); var task = _models.ExportToGltf(file, "C:\\Users\\ackwell\\blender\\gltf-tests\\bingo.gltf");
task.ContinueWith(_ => _pendingIo = false); task.ContinueWith(_ => _pendingIo = false);
} }