Compare commits
No commits in common. "05e90c1a3089328c7f82afcb19ed443d1ae3d1a4" and "2365f0993156e850d80b5b8795a0f46518fa6ad2" have entirely different histories.
05e90c1a30
...
2365f09931
|
@ -17,7 +17,6 @@ public class ResourcePathManager
|
|||
|
||||
private bool _initialised = false;
|
||||
private readonly string _extractionPath;
|
||||
private string _fmsDir;
|
||||
private CampaignResources _omResources;
|
||||
private Dictionary<string, CampaignResources> _fmResources;
|
||||
|
||||
|
@ -75,7 +74,6 @@ public class ResourcePathManager
|
|||
objectPathMap = objectPathMap,
|
||||
};
|
||||
_fmResources.Add(campaign, resource);
|
||||
_fmsDir = fmsDir;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -164,65 +162,28 @@ public class ResourcePathManager
|
|||
return null;
|
||||
}
|
||||
|
||||
public string GetObjectPath(string objectName)
|
||||
{
|
||||
if (!_initialised) return null;
|
||||
|
||||
objectName = objectName.ToLower();
|
||||
if (_omResources.objectPathMap.TryGetValue(objectName, out var path))
|
||||
{
|
||||
return path;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetObjectPath(string campaignName, string objectName)
|
||||
{
|
||||
if (!_initialised) return null;
|
||||
|
||||
objectName = objectName.ToLower();
|
||||
if (campaignName == null || campaignName == "")
|
||||
if (_fmResources.TryGetValue(campaignName, out var campaign) &&
|
||||
campaign.objectPathMap.TryGetValue(objectName, out var path))
|
||||
{
|
||||
if (_omResources.objectPathMap.TryGetValue(objectName, out var omPath))
|
||||
{
|
||||
return omPath;
|
||||
}
|
||||
return path;
|
||||
}
|
||||
if (_fmResources.TryGetValue(campaignName, out var campaign))
|
||||
{
|
||||
if (campaign.objectPathMap.TryGetValue(objectName, out var fmPath))
|
||||
{
|
||||
return fmPath;
|
||||
}
|
||||
else if (_omResources.objectPathMap.TryGetValue(objectName, out var omPath))
|
||||
{
|
||||
return omPath;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// TODO: Store these as part of the resources
|
||||
public string GetObjectTexturePath(string campaignName, string textureName)
|
||||
{
|
||||
var options = new EnumerationOptions
|
||||
{
|
||||
MatchCasing = MatchCasing.CaseInsensitive,
|
||||
RecurseSubdirectories = true,
|
||||
};
|
||||
|
||||
textureName = textureName.ToLower();
|
||||
var omDir = Path.Join(_extractionPath, "obj");
|
||||
var omPaths = Directory.GetFiles(omDir, textureName, options);
|
||||
if (campaignName == null || campaignName == "")
|
||||
{
|
||||
if (omPaths.Length > 0)
|
||||
{
|
||||
return omPaths[0];
|
||||
}
|
||||
}
|
||||
else if (_fmResources.TryGetValue(campaignName, out var campaign))
|
||||
{
|
||||
var fmDir = Path.Join(_fmsDir, campaignName);
|
||||
var fmPaths = Directory.GetFiles(Path.Join(fmDir, "obj"), textureName, options);
|
||||
if (fmPaths.Length > 0)
|
||||
{
|
||||
return fmPaths[0];
|
||||
}
|
||||
else if (omPaths.Length > 0)
|
||||
{
|
||||
return omPaths[0];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -164,18 +164,25 @@ public partial class Mission : Node3D
|
|||
|
||||
// Let's try and place an object :)
|
||||
var modelName = modelNameProp.modelName + ".bin";
|
||||
var fmName = FileName.GetBaseDir().GetFile(); // TODO: Doesn't work for OMs
|
||||
var objPath = _installPaths.GetObjectPath(fmName, modelName);
|
||||
objPath ??= _installPaths.GetObjectPath(modelName);
|
||||
|
||||
var pos = brush.position.ToGodotVec3();
|
||||
var rawRot = brush.angle;
|
||||
var rot = new Vector3(rawRot.Y, rawRot.Z, rawRot.X) * 360 / ushort.MaxValue;
|
||||
var scale = scaleProp == null ? Vector3.One : scaleProp.scale.ToGodotVec3(false);
|
||||
var model = ModelLoader.LoadModel(_installPaths, _campaignName, modelName);
|
||||
if (model != null)
|
||||
var model = new Model
|
||||
{
|
||||
model.Position = pos;
|
||||
model.RotationDegrees = rot;
|
||||
model.Scale = scale;
|
||||
AddChild(model);
|
||||
Position = pos,
|
||||
RotationDegrees = rot,
|
||||
Scale = scale,
|
||||
};
|
||||
if (objPath != null)
|
||||
{
|
||||
model.BuildModel("", objPath);
|
||||
}
|
||||
AddChild(model);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ public partial class Model : Node3D
|
|||
}
|
||||
|
||||
// TODO: Remove this disgusting hack. Not only is it a hack, it doesn't support custom models
|
||||
var baseDir = Path.GetDirectoryName(modelPath);
|
||||
var baseDir = ProjectSettings.GlobalizePath($"user://objects/tmp");
|
||||
var options = new EnumerationOptions
|
||||
{
|
||||
MatchCasing = MatchCasing.CaseInsensitive,
|
||||
|
|
|
@ -1,104 +0,0 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Godot;
|
||||
using KeepersCompound.LGS;
|
||||
|
||||
namespace KeepersCompound.TMV;
|
||||
|
||||
public static class ModelLoader
|
||||
{
|
||||
public static MeshInstance3D LoadModel(ResourcePathManager pathManager, string campaignName, string modelName)
|
||||
{
|
||||
var modelPath = pathManager.GetObjectPath(campaignName, modelName);
|
||||
if (modelPath == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var modelFile = new ModelFile(modelPath);
|
||||
if (modelFile == null)
|
||||
{
|
||||
GD.Print($"Failed to load model file: {modelPath}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var materials = new List<StandardMaterial3D>();
|
||||
foreach (var material in modelFile.Materials)
|
||||
{
|
||||
if (material.Type == 0)
|
||||
{
|
||||
var path = pathManager.GetObjectTexturePath(campaignName, material.Name);
|
||||
if (path == null)
|
||||
{
|
||||
GD.Print($"Failed to load model texture: {material.Name}");
|
||||
continue;
|
||||
}
|
||||
|
||||
materials.Add(new StandardMaterial3D
|
||||
{
|
||||
AlbedoTexture = TextureLoader.LoadTexture(path)
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
var b = (material.Handle) & 0xff;
|
||||
var g = (material.Handle >> 8) & 0xff;
|
||||
var r = (material.Handle >> 16) & 0xff;
|
||||
var colour = new Color(r / 255.0f, g / 255.0f, b / 255.0f, 1.0f);
|
||||
materials.Add(new StandardMaterial3D
|
||||
{
|
||||
AlbedoColor = colour
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var surfaceDataMap = new Dictionary<int, MeshSurfaceData>();
|
||||
foreach (var poly in modelFile.Polygons)
|
||||
{
|
||||
var vertices = new List<Vector3>();
|
||||
var normal = modelFile.Normals[poly.Normal].ToGodotVec3();
|
||||
var uvs = new List<Vector2>();
|
||||
for (var i = 0; i < poly.VertexCount; i++)
|
||||
{
|
||||
var vertex = modelFile.Vertices[poly.VertexIndices[i]];
|
||||
vertices.Add(vertex.ToGodotVec3());
|
||||
if (i < poly.UvIndices.Length)
|
||||
{
|
||||
var uv = modelFile.Uvs[poly.UvIndices[i]];
|
||||
uvs.Add(new Vector2(uv.X, uv.Y));
|
||||
}
|
||||
else
|
||||
{
|
||||
uvs.Add(Vector2.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
if (!surfaceDataMap.ContainsKey(poly.Data))
|
||||
{
|
||||
surfaceDataMap.Add(poly.Data, new MeshSurfaceData());
|
||||
}
|
||||
|
||||
surfaceDataMap[poly.Data].AddPolygon(vertices, normal, uvs, uvs);
|
||||
}
|
||||
|
||||
var mesh = new ArrayMesh();
|
||||
foreach (var (materialId, surfaceData) in surfaceDataMap)
|
||||
{
|
||||
var array = surfaceData.BuildSurfaceArray();
|
||||
var surfaceIdx = mesh.GetSurfaceCount();
|
||||
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
|
||||
for (var i = 0; i < materials.Count; i++)
|
||||
{
|
||||
var m = modelFile.Materials[i];
|
||||
if (m.Slot == materialId)
|
||||
{
|
||||
mesh.SurfaceSetMaterial(surfaceIdx, materials[i]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var pos = -modelFile.Header.Center.ToGodotVec3();
|
||||
var meshInstance = new MeshInstance3D { Mesh = mesh, Position = pos };
|
||||
return meshInstance;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue