Add initial basic .BIN model support
This commit is contained in:
parent
b12070f982
commit
b5d3a3c275
|
@ -0,0 +1,182 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
|
||||
namespace KeepersCompound.LGS;
|
||||
|
||||
// TODO: Remove all the things that don't actually need to be stored
|
||||
public class ModelFile
|
||||
{
|
||||
public readonly struct BHeader
|
||||
{
|
||||
public string Signature { get; }
|
||||
public int Version { get; }
|
||||
|
||||
public BHeader(BinaryReader reader)
|
||||
{
|
||||
Signature = Encoding.UTF8.GetString(reader.ReadBytes(4)).Replace("\0", string.Empty);
|
||||
Version = reader.ReadInt32();
|
||||
}
|
||||
}
|
||||
|
||||
public readonly struct MHeader
|
||||
{
|
||||
public string Name { get; }
|
||||
public float Radius { get; }
|
||||
public float MaxPolygonRadius { get; }
|
||||
public Vector3 MaxBounds { get; }
|
||||
public Vector3 MinBounds { get; }
|
||||
public Vector3 Center { get; }
|
||||
|
||||
public ushort PolygonCount { get; }
|
||||
public ushort VertexCount { get; }
|
||||
public ushort ParameterCount { get; }
|
||||
public byte MaterialCount { get; }
|
||||
public byte VCallCount { get; }
|
||||
public byte VHotCount { get; }
|
||||
public byte ObjectCount { get; }
|
||||
|
||||
public uint ObjectOffset { get; }
|
||||
public uint MaterialOffset { get; }
|
||||
public uint UvOffset { get; }
|
||||
public uint VHotOffset { get; }
|
||||
public uint VertexOffset { get; }
|
||||
public uint LightOffset { get; }
|
||||
public uint NormalOffset { get; }
|
||||
public uint PolygonOffset { get; }
|
||||
public uint NodeOffset { get; }
|
||||
|
||||
public uint ModelSize { get; }
|
||||
|
||||
public uint AuxMaterialFlags { get; }
|
||||
public uint AuxMaterialOffset { get; }
|
||||
public uint AuxMaterialSize { get; }
|
||||
|
||||
public MHeader(BinaryReader reader, int version)
|
||||
{
|
||||
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(8)).Replace("\0", string.Empty);
|
||||
Name = tmpName[..Math.Min(7, tmpName.Length)];
|
||||
Radius = reader.ReadSingle();
|
||||
MaxPolygonRadius = reader.ReadSingle();
|
||||
MaxBounds = reader.ReadVec3();
|
||||
MinBounds = reader.ReadVec3();
|
||||
Center = reader.ReadVec3();
|
||||
PolygonCount = reader.ReadUInt16();
|
||||
VertexCount = reader.ReadUInt16();
|
||||
ParameterCount = reader.ReadUInt16();
|
||||
MaterialCount = reader.ReadByte();
|
||||
VCallCount = reader.ReadByte();
|
||||
VHotCount = reader.ReadByte();
|
||||
ObjectCount = reader.ReadByte();
|
||||
ObjectOffset = reader.ReadUInt32();
|
||||
MaterialOffset = reader.ReadUInt32();
|
||||
UvOffset = reader.ReadUInt32();
|
||||
VHotOffset = reader.ReadUInt32();
|
||||
VertexOffset = reader.ReadUInt32();
|
||||
LightOffset = reader.ReadUInt32();
|
||||
NormalOffset = reader.ReadUInt32();
|
||||
PolygonOffset = reader.ReadUInt32();
|
||||
NodeOffset = reader.ReadUInt32();
|
||||
ModelSize = reader.ReadUInt32();
|
||||
|
||||
if (version == 4)
|
||||
{
|
||||
AuxMaterialFlags = reader.ReadUInt32();
|
||||
AuxMaterialOffset = reader.ReadUInt32();
|
||||
AuxMaterialSize = reader.ReadUInt32();
|
||||
}
|
||||
else
|
||||
{
|
||||
AuxMaterialFlags = 0;
|
||||
AuxMaterialOffset = 0;
|
||||
AuxMaterialSize = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Polygon
|
||||
{
|
||||
public ushort Index;
|
||||
public ushort Data;
|
||||
public byte Type;
|
||||
public byte VertexCount;
|
||||
public ushort Normal;
|
||||
public float D;
|
||||
public ushort[] VertexIndices;
|
||||
public ushort[] LightIndices;
|
||||
public ushort[] UvIndices;
|
||||
public byte Material;
|
||||
|
||||
public Polygon(BinaryReader reader, int version)
|
||||
{
|
||||
Index = reader.ReadUInt16();
|
||||
Data = reader.ReadUInt16();
|
||||
Type = reader.ReadByte();
|
||||
VertexCount = reader.ReadByte();
|
||||
Normal = reader.ReadUInt16();
|
||||
D = reader.ReadSingle();
|
||||
VertexIndices = new ushort[VertexCount];
|
||||
for (var i = 0; i < VertexCount; i++)
|
||||
{
|
||||
VertexIndices[i] = reader.ReadUInt16();
|
||||
}
|
||||
LightIndices = new ushort[VertexCount];
|
||||
for (var i = 0; i < VertexCount; i++)
|
||||
{
|
||||
LightIndices[i] = reader.ReadUInt16();
|
||||
}
|
||||
UvIndices = new ushort[Type == 0x1B ? VertexCount : 0];
|
||||
for (var i = 0; i < UvIndices.Length; i++)
|
||||
{
|
||||
LightIndices[i] = reader.ReadUInt16();
|
||||
}
|
||||
|
||||
Material = version == 4 ? reader.ReadByte() : (byte)0;
|
||||
}
|
||||
}
|
||||
|
||||
public BHeader BinHeader { get; set; }
|
||||
public MHeader Header { get; set; }
|
||||
public Vector3[] Vertices { get; }
|
||||
public Vector2[] Uvs { get; }
|
||||
public Vector3[] Normals { get; }
|
||||
public Polygon[] Polygons { get; }
|
||||
|
||||
public ModelFile(string filename)
|
||||
{
|
||||
if (!File.Exists(filename)) return;
|
||||
|
||||
using MemoryStream stream = new(File.ReadAllBytes(filename));
|
||||
using BinaryReader reader = new(stream, Encoding.UTF8, false);
|
||||
|
||||
BinHeader = new BHeader(reader);
|
||||
if (BinHeader.Signature != "LGMD") return;
|
||||
|
||||
Header = new MHeader(reader, BinHeader.Version);
|
||||
stream.Seek(Header.VertexOffset, SeekOrigin.Begin);
|
||||
Vertices = new Vector3[Header.VertexCount];
|
||||
for (var i = 0; i < Vertices.Length; i++)
|
||||
{
|
||||
Vertices[i] = reader.ReadVec3();
|
||||
}
|
||||
stream.Seek(Header.UvOffset, SeekOrigin.Begin);
|
||||
Uvs = new Vector2[(Header.VHotOffset - Header.UvOffset) / 8];
|
||||
for (var i = 0; i < Uvs.Length; i++)
|
||||
{
|
||||
Uvs[i] = reader.ReadVec2();
|
||||
}
|
||||
stream.Seek(Header.NormalOffset, SeekOrigin.Begin);
|
||||
Normals = new Vector3[(Header.PolygonOffset - Header.NormalOffset) / 12];
|
||||
for (var i = 0; i < Normals.Length; i++)
|
||||
{
|
||||
Normals[i] = reader.ReadVec3();
|
||||
}
|
||||
stream.Seek(Header.PolygonOffset, SeekOrigin.Begin);
|
||||
Polygons = new Polygon[Header.PolygonCount];
|
||||
for (var i = 0; i < Polygons.Length; i++)
|
||||
{
|
||||
Polygons[i] = new Polygon(reader, BinHeader.Version);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ public class InstallPaths
|
|||
{
|
||||
public string rootPath;
|
||||
public string famPath;
|
||||
public string objPath;
|
||||
public string omsPath;
|
||||
public string fmsPath;
|
||||
|
||||
|
@ -21,6 +22,7 @@ public class InstallPaths
|
|||
|
||||
rootPath = root;
|
||||
famPath = Directory.GetFiles(rootPath, "fam.crf", searchOptions)[0];
|
||||
objPath = Directory.GetFiles(rootPath, "obj.crf", searchOptions)[0];
|
||||
var paths = Directory.GetFiles(rootPath, "install.cfg", searchOptions);
|
||||
if (paths.Length == 0)
|
||||
{
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using KeepersCompound.LGS;
|
||||
using KeepersCompound.TMV.UI;
|
||||
|
||||
namespace KeepersCompound.TMV;
|
||||
|
||||
public partial class Model : Node3D
|
||||
{
|
||||
public override void _Ready()
|
||||
{
|
||||
var modelSelector = GetNode<Control>("%ModelSelector") as ModelSelector;
|
||||
modelSelector.LoadModel += BuildModel;
|
||||
}
|
||||
|
||||
private void BuildModel(string rootPath, string modelPath)
|
||||
{
|
||||
foreach (var node in GetChildren())
|
||||
{
|
||||
node.QueueFree();
|
||||
}
|
||||
|
||||
var modelFile = new ModelFile(modelPath);
|
||||
if (modelFile == null)
|
||||
{
|
||||
GD.Print("UH OH");
|
||||
}
|
||||
|
||||
var vertices = new List<Vector3>();
|
||||
var indices = new List<int>();
|
||||
|
||||
foreach (var v in modelFile.Vertices)
|
||||
{
|
||||
vertices.Add(v.ToGodotVec3());
|
||||
}
|
||||
|
||||
foreach (var poly in modelFile.Polygons)
|
||||
{
|
||||
for (var i = 1; i < poly.VertexCount - 1; i++)
|
||||
{
|
||||
indices.Add(poly.VertexIndices[0]);
|
||||
indices.Add(poly.VertexIndices[i]);
|
||||
indices.Add(poly.VertexIndices[i + 1]);
|
||||
}
|
||||
}
|
||||
|
||||
var array = new Array();
|
||||
array.Resize((int)Mesh.ArrayType.Max);
|
||||
array[(int)Mesh.ArrayType.Vertex] = vertices.ToArray();
|
||||
array[(int)Mesh.ArrayType.Index] = indices.ToArray();
|
||||
|
||||
var mesh = new ArrayMesh();
|
||||
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
|
||||
|
||||
var meshInstance = new MeshInstance3D { Mesh = mesh };
|
||||
AddChild(meshInstance);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
|
||||
namespace KeepersCompound.TMV.UI;
|
||||
|
||||
public partial class ModelSelector : Control
|
||||
{
|
||||
[Signal]
|
||||
public delegate void LoadModelEventHandler(string rootPath, string modelPath);
|
||||
|
||||
private InstallPaths _installPaths;
|
||||
|
||||
private FileDialog _FolderSelect;
|
||||
private LineEdit _FolderPath;
|
||||
private Button _BrowseButton;
|
||||
private ItemList _Models;
|
||||
private Button _LoadButton;
|
||||
private Button _CancelButton;
|
||||
|
||||
private string _extractedObjectsPath = ProjectSettings.GlobalizePath($"user://objects/tmp");
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
// TODO: Load initial folderpath from config and prefil everything
|
||||
|
||||
_FolderSelect = GetNode<FileDialog>("%FolderSelect");
|
||||
_FolderPath = GetNode<LineEdit>("%FolderPath");
|
||||
_BrowseButton = GetNode<Button>("%BrowseButton");
|
||||
_Models = GetNode<ItemList>("%Models");
|
||||
_LoadButton = GetNode<Button>("%LoadButton");
|
||||
_CancelButton = GetNode<Button>("%CancelButton");
|
||||
|
||||
_BrowseButton.Pressed += () => _FolderSelect.Visible = true;
|
||||
_FolderSelect.DirSelected += (string dir) => { _FolderPath.Text = dir; BuildModelList(dir); };
|
||||
_FolderPath.TextSubmitted += BuildModelList;
|
||||
_Models.ItemSelected += (long _) => _LoadButton.Disabled = false;
|
||||
_LoadButton.Pressed += EmitLoadModel;
|
||||
_CancelButton.Pressed += () => Visible = false;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
if (@event is InputEventKey keyEvent && keyEvent.Pressed)
|
||||
{
|
||||
if (keyEvent.Keycode == Key.Escape)
|
||||
{
|
||||
Visible = !Visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void BuildModelList(string path)
|
||||
{
|
||||
_installPaths = new InstallPaths(path);
|
||||
ExtractObjFiles();
|
||||
|
||||
_Models.Clear();
|
||||
_LoadButton.Disabled = true;
|
||||
|
||||
foreach (var m in Directory.GetFiles(_extractedObjectsPath, "*.bin", SearchOption.AllDirectories))
|
||||
{
|
||||
_Models.AddItem(m.TrimPrefix(_extractedObjectsPath));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Move this to a resource manager
|
||||
private void ExtractObjFiles()
|
||||
{
|
||||
var dir = new DirectoryInfo(_extractedObjectsPath);
|
||||
if (dir.Exists)
|
||||
{
|
||||
dir.Delete(true);
|
||||
}
|
||||
|
||||
var zip = ZipFile.OpenRead(_installPaths.objPath);
|
||||
zip.ExtractToDirectory(_extractedObjectsPath);
|
||||
}
|
||||
|
||||
private void EmitLoadModel()
|
||||
{
|
||||
var selected = _Models.GetSelectedItems();
|
||||
if (selected.IsEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var path = _extractedObjectsPath + _Models.GetItemText(selected[0]);
|
||||
EmitSignal(SignalName.LoadModel, _installPaths.rootPath, path);
|
||||
|
||||
Visible = false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
[gd_scene load_steps=5 format=3 uid="uid://iegbijrr5amb"]
|
||||
|
||||
[ext_resource type="Script" path="res://project/code/TMV/Model.cs" id="1_dax7s"]
|
||||
[ext_resource type="Script" path="res://project/code/camera.gd" id="2_ov7rc"]
|
||||
[ext_resource type="PackedScene" uid="uid://c366p056f8dlu" path="res://project/scenes/ui/model_selector.tscn" id="3_ovrmo"]
|
||||
|
||||
[sub_resource type="Environment" id="Environment_e4172"]
|
||||
ssao_enabled = true
|
||||
|
||||
[node name="ModelViewer" type="Node3D"]
|
||||
|
||||
[node name="Model" type="Node3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.110309, 0.187101, -0.461656)
|
||||
script = ExtResource("1_dax7s")
|
||||
|
||||
[node name="Camera3D" type="Camera3D" parent="."]
|
||||
script = ExtResource("2_ov7rc")
|
||||
|
||||
[node name="UI" type="CanvasLayer" parent="."]
|
||||
|
||||
[node name="ModelSelector" parent="UI" instance=ExtResource("3_ovrmo")]
|
||||
unique_name_in_owner = true
|
||||
|
||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||
environment = SubResource("Environment_e4172")
|
|
@ -0,0 +1,99 @@
|
|||
[gd_scene load_steps=3 format=3 uid="uid://c366p056f8dlu"]
|
||||
|
||||
[ext_resource type="Script" path="res://project/code/TMV/UI/ModelSelector.cs" id="1_oy1sl"]
|
||||
|
||||
[sub_resource type="LabelSettings" id="LabelSettings_4v24o"]
|
||||
font_size = 20
|
||||
|
||||
[node name="ModelSelector" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
script = ExtResource("1_oy1sl")
|
||||
|
||||
[node name="FolderSelect" type="FileDialog" parent="."]
|
||||
unique_name_in_owner = true
|
||||
title = "Open a Directory"
|
||||
initial_position = 2
|
||||
size = Vector2i(636, 159)
|
||||
ok_button_text = "Select Current Folder"
|
||||
file_mode = 2
|
||||
access = 2
|
||||
use_native_dialog = true
|
||||
|
||||
[node name="PanelContainer" type="PanelContainer" parent="."]
|
||||
custom_minimum_size = Vector2(640, 0)
|
||||
layout_mode = 1
|
||||
anchors_preset = 8
|
||||
anchor_left = 0.5
|
||||
anchor_top = 0.5
|
||||
anchor_right = 0.5
|
||||
anchor_bottom = 0.5
|
||||
offset_left = -144.0
|
||||
offset_top = -37.0
|
||||
offset_right = 144.0
|
||||
offset_bottom = 37.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="MarginContainer" type="MarginContainer" parent="PanelContainer"]
|
||||
layout_mode = 2
|
||||
theme_override_constants/margin_left = 8
|
||||
theme_override_constants/margin_top = 8
|
||||
theme_override_constants/margin_right = 8
|
||||
theme_override_constants/margin_bottom = 8
|
||||
|
||||
[node name="VBoxContainer" type="VBoxContainer" parent="PanelContainer/MarginContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Title" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
text = "Model Selector"
|
||||
label_settings = SubResource("LabelSettings_4v24o")
|
||||
horizontal_alignment = 1
|
||||
|
||||
[node name="FolderSelect" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Label" type="Label" parent="PanelContainer/MarginContainer/VBoxContainer/FolderSelect"]
|
||||
layout_mode = 2
|
||||
text = "Folder"
|
||||
|
||||
[node name="FolderPath" type="LineEdit" parent="PanelContainer/MarginContainer/VBoxContainer/FolderSelect"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="BrowseButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer/FolderSelect"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Browse"
|
||||
|
||||
[node name="HBoxContainer" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
|
||||
[node name="Models" type="ItemList" parent="PanelContainer/MarginContainer/VBoxContainer/HBoxContainer"]
|
||||
unique_name_in_owner = true
|
||||
custom_minimum_size = Vector2(0, 480)
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 3
|
||||
|
||||
[node name="Buttons" type="HBoxContainer" parent="PanelContainer/MarginContainer/VBoxContainer"]
|
||||
layout_mode = 2
|
||||
size_flags_horizontal = 8
|
||||
|
||||
[node name="LoadButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer/Buttons"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
disabled = true
|
||||
text = "Load"
|
||||
|
||||
[node name="CancelButton" type="Button" parent="PanelContainer/MarginContainer/VBoxContainer/Buttons"]
|
||||
unique_name_in_owner = true
|
||||
layout_mode = 2
|
||||
text = "Cancel"
|
Loading…
Reference in New Issue