Skip to content

Commit 5ac03e8

Browse files
Began adding support for output of loaded BMD data to other model formats using AssImp.
1 parent 0df337d commit 5ac03e8

File tree

8 files changed

+85
-88
lines changed

8 files changed

+85
-88
lines changed

SuperBMD/source/BMD/INF1.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ private void GetNodesRecursive(Rigging.Bone bone, List<Rigging.Bone> skeleton, S
111111
}
112112
}
113113

114+
public void FillScene(Scene scene, List<Rigging.Bone> flatSkeleton)
115+
{
116+
117+
}
118+
114119
public void Write(EndianBinaryWriter writer, int packetCount, int vertexCount)
115120
{
116121
long start = writer.BaseStream.Position;

SuperBMD/source/BMD/MAT3.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,38 @@ private void FillMaterialDataBlocks()
659659
}
660660
}
661661

662+
public void FillScene(Assimp.Scene scene, TEX1 textures, string fileDir)
663+
{
664+
foreach (Material mat in m_Materials)
665+
{
666+
Assimp.Material assMat = new Assimp.Material();
667+
668+
if (mat.TextureIndices[0] != -1)
669+
{
670+
int texIndex = mat.TextureIndices[0];
671+
string texPath = textures[texIndex].SaveImageToDisk(fileDir);
672+
673+
Assimp.TextureSlot tex = new Assimp.TextureSlot(texPath, Assimp.TextureType.Diffuse, 0,
674+
Assimp.TextureMapping.FromUV, 0, 1.0f, Assimp.TextureOperation.Add,
675+
textures[texIndex].WrapS.ToAssImpWrapMode(), textures[texIndex].WrapT.ToAssImpWrapMode(), 0);
676+
677+
assMat.AddMaterialTexture(ref tex);
678+
}
679+
680+
if (mat.MaterialColors[0] != null)
681+
{
682+
assMat.ColorDiffuse = mat.MaterialColors[0].Value.ToColor4D();
683+
}
684+
685+
if (mat.AmbientColors[0] != null)
686+
{
687+
assMat.ColorAmbient = mat.AmbientColors[0].Value.ToColor4D();
688+
}
689+
690+
scene.Materials.Add(assMat);
691+
}
692+
}
693+
662694
public void Write(EndianBinaryWriter writer)
663695
{
664696
long start = writer.BaseStream.Position;

SuperBMD/source/BMD/SHP1.cs

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -164,91 +164,6 @@ public static SHP1 Create(Scene scene, Dictionary<string, int> boneNames, Vertex
164164
return shp1;
165165
}
166166

167-
private void SetMatrixIndices(Vertex vert, EVP1 envelopes, DRW1 partialWeight, List<int> matrixIndices)
168-
{
169-
vert.SetAttributeIndex(GXVertexAttribute.PositionMatrixIdx, (uint)matrixIndices.Count);
170-
matrixIndices.Add(partialWeight.WeightTypeCheck.Count);
171-
172-
if (vert.VertexWeight.WeightCount > 1)
173-
{
174-
partialWeight.WeightTypeCheck.Add(true);
175-
partialWeight.Indices.Add(envelopes.Weights.Count);
176-
envelopes.Weights.Add(vert.VertexWeight);
177-
}
178-
else
179-
{
180-
partialWeight.WeightTypeCheck.Add(false);
181-
partialWeight.Indices.Add(vert.VertexWeight.BoneIndices[0]);
182-
}
183-
}
184-
185-
private void SetVertexIndices(Mesh mesh, Vertex vert, VertexData vertData, ShapeVertexDescriptor descriptor, int vertIndex)
186-
{
187-
if (descriptor.CheckAttribute(GXVertexAttribute.Position))
188-
{
189-
Vector3D posVec = mesh.Vertices[vertIndex];
190-
uint posIndex = (uint)vertData.Positions.IndexOf(posVec.ToOpenTKVector3());
191-
vert.SetAttributeIndex(GXVertexAttribute.Position, posIndex);
192-
}
193-
if (descriptor.CheckAttribute(GXVertexAttribute.Normal))
194-
{
195-
Vector3D normVec = mesh.Normals[vertIndex];
196-
uint normIndex = (uint)vertData.Normals.IndexOf(normVec.ToOpenTKVector3());
197-
vert.SetAttributeIndex(GXVertexAttribute.Normal, normIndex);
198-
}
199-
200-
for (int color = 0; color < 2; color++)
201-
{
202-
if (descriptor.CheckAttribute(GXVertexAttribute.Color0 + color))
203-
{
204-
Color4D assimpColor = mesh.VertexColorChannels[color][vertIndex];
205-
List<Color> colorData = (List<Color>)vertData.GetAttributeData(GXVertexAttribute.Color0 + color);
206-
uint colIndex = (uint)colorData.IndexOf(assimpColor.ToSuperBMDColorRGBA());
207-
vert.SetAttributeIndex(GXVertexAttribute.Color0 + color, colIndex);
208-
}
209-
}
210-
211-
for (int tex = 0; tex < 8; tex++)
212-
{
213-
if (descriptor.CheckAttribute(GXVertexAttribute.Tex0 + tex))
214-
{
215-
Vector3D texVec = mesh.TextureCoordinateChannels[tex][vertIndex];
216-
List<OpenTK.Vector2> texData = (List<OpenTK.Vector2>)vertData.GetAttributeData(GXVertexAttribute.Tex0 + tex);
217-
uint texIndex = (uint)texData.IndexOf(texVec.ToOpenTKVector2());
218-
vert.SetAttributeIndex(GXVertexAttribute.Tex0 + tex, texIndex);
219-
}
220-
}
221-
}
222-
223-
public void DistributeWeights(EVP1 envelopes, DRW1 partialWeights)
224-
{
225-
foreach (Shape shape in Shapes)
226-
{
227-
foreach (Packet pack in shape.Packets)
228-
{
229-
foreach (Primitive prim in pack.Primitives)
230-
{
231-
foreach (Vertex vert in prim.Vertices)
232-
{
233-
uint drw1Index = vert.GetAttributeIndex(GXVertexAttribute.PositionMatrixIdx);
234-
235-
if (partialWeights.WeightTypeCheck[(int)drw1Index])
236-
{
237-
vert.SetWeight(envelopes.Weights[partialWeights.Indices[(int)drw1Index]]);
238-
}
239-
else
240-
{
241-
Rigging.Weight newWeight = new Rigging.Weight();
242-
newWeight.AddWeight(1.0f, partialWeights.Indices[(int)drw1Index]);
243-
244-
vert.SetWeight(newWeight);
245-
}
246-
}
247-
}
248-
}
249-
}
250-
}
251-
252167
public void Write(EndianBinaryWriter writer)
253168
{
254169
List<Tuple<ShapeVertexDescriptor, int>> descriptorOffsets; // Contains the offsets for each unique vertex descriptor

SuperBMD/source/Materials/BinaryTextureImage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ public void Load(Assimp.TextureSlot texture, string modelDirectory)
236236
texData.Dispose();
237237
}
238238

239-
public void SaveImageToDisk(string outputFile)
239+
public string SaveImageToDisk(string outputFile)
240240
{
241241
string fileName = Path.Combine(outputFile, $"{ Name }.bmp");
242242

@@ -246,6 +246,8 @@ public void SaveImageToDisk(string outputFile)
246246
Directory.CreateDirectory(Path.GetDirectoryName(fileName));
247247
bmp.Save(fileName);
248248
}
249+
250+
return fileName;
249251
}
250252

251253
public Bitmap CreateBitmap()

SuperBMD/source/Model.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Model(Scene scene, string modelDirectory)
114114
vertexCount = VertexData.Attributes.Positions.Count;
115115
}
116116

117-
public void Export(string fileName)
117+
public void ExportBMD(string fileName)
118118
{
119119
using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
120120
{
@@ -141,5 +141,27 @@ public void Export(string fileName)
141141
writer.Write((int)writer.BaseStream.Length);
142142
}
143143
}
144+
145+
public void ExportAssImp(string fileName, string modelType)
146+
{
147+
string outDir = Path.GetDirectoryName(fileName);
148+
149+
Scene outScene = new Scene();
150+
outScene.RootNode = new Node("RootNode");
151+
Assimp.Node geomNode = new Node(Path.GetFileNameWithoutExtension(fileName), outScene.RootNode);
152+
153+
for (int i = 0; i < Shapes.Shapes.Count; i++)
154+
{
155+
geomNode.MeshIndices.Add(i);
156+
}
157+
158+
outScene.RootNode.Children.Add(geomNode);
159+
160+
Materials.FillScene(outScene, Textures, outDir);
161+
162+
163+
AssimpContext cont = new AssimpContext();
164+
cont.ExportFile(outScene, fileName, modelType, PostProcessSteps.ValidateDataStructure);
165+
}
144166
}
145167
}

SuperBMD/source/Util/Color.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public string ToHexString()
165165
return sb.ToString();
166166
}
167167

168+
public Assimp.Color4D ToColor4D()
169+
{
170+
return new Assimp.Color4D(R, G, B, A);
171+
}
172+
168173
public static Color Lerp(Color a, Color b, float t)
169174
{
170175
//t = MathE.ClampNormalized(t);

SuperBMD/source/Util/WrapModeUtility.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,20 @@ public static BinaryTextureImage.WrapModes ToGXWrapMode(this Assimp.TextureWrapM
2626
throw new ArgumentException("mode");
2727
}
2828
}
29+
30+
public static Assimp.TextureWrapMode ToAssImpWrapMode(this BinaryTextureImage.WrapModes mode)
31+
{
32+
switch (mode)
33+
{
34+
case BinaryTextureImage.WrapModes.ClampToEdge:
35+
return TextureWrapMode.Clamp;
36+
case BinaryTextureImage.WrapModes.MirroredRepeat:
37+
return TextureWrapMode.Mirror;
38+
case BinaryTextureImage.WrapModes.Repeat:
39+
return TextureWrapMode.Wrap;
40+
default:
41+
throw new ArgumentException("mode");
42+
}
43+
}
2944
}
3045
}

SuperBMD_UnitTest/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class Program
1212
static void Main(string[] args)
1313
{
1414
Model mod = Model.Load(args[0]);
15-
mod.Export(args[0] + ".bmd");
15+
//mod.ExportBMD(args[0] + ".bmd");
16+
mod.ExportAssImp(args[2], args[1]);
1617
}
1718
}
1819
}

0 commit comments

Comments
 (0)