Really bad awful model texturing
This commit is contained in:
parent
5aafab148b
commit
20cf0f7292
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Godot;
|
using Godot;
|
||||||
|
@ -36,6 +37,7 @@ public partial class Model : Node3D
|
||||||
MatchCasing = MatchCasing.CaseInsensitive,
|
MatchCasing = MatchCasing.CaseInsensitive,
|
||||||
RecurseSubdirectories = true,
|
RecurseSubdirectories = true,
|
||||||
};
|
};
|
||||||
|
var textures = new List<ImageTexture>();
|
||||||
foreach (var material in modelFile.Materials)
|
foreach (var material in modelFile.Materials)
|
||||||
{
|
{
|
||||||
var paths = Directory.GetFiles(baseDir, material.Name, options);
|
var paths = Directory.GetFiles(baseDir, material.Name, options);
|
||||||
|
@ -44,33 +46,52 @@ public partial class Model : Node3D
|
||||||
var texture = TextureLoader.LoadTexture(paths[0]);
|
var texture = TextureLoader.LoadTexture(paths[0]);
|
||||||
var saveName = material.Name.GetBaseName() + ".png";
|
var saveName = material.Name.GetBaseName() + ".png";
|
||||||
texture.GetImage().SavePng(ProjectSettings.GlobalizePath($"user://debug/{saveName}"));
|
texture.GetImage().SavePng(ProjectSettings.GlobalizePath($"user://debug/{saveName}"));
|
||||||
|
textures.Add(texture);
|
||||||
}
|
}
|
||||||
|
|
||||||
var vertices = new List<Vector3>();
|
var mat = new StandardMaterial3D
|
||||||
var indices = new List<int>();
|
|
||||||
|
|
||||||
foreach (var v in modelFile.Vertices)
|
|
||||||
{
|
{
|
||||||
vertices.Add(v.ToGodotVec3());
|
AlbedoTexture = textures[0]
|
||||||
}
|
};
|
||||||
|
|
||||||
|
// TODO: Support multiple materials and colour based materials
|
||||||
|
var surfaceData = new MeshSurfaceData();
|
||||||
foreach (var poly in modelFile.Polygons)
|
foreach (var poly in modelFile.Polygons)
|
||||||
{
|
{
|
||||||
for (var i = 1; i < poly.VertexCount - 1; i++)
|
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++)
|
||||||
{
|
{
|
||||||
indices.Add(poly.VertexIndices[0]);
|
var vertex = modelFile.Vertices[poly.VertexIndices[i]];
|
||||||
indices.Add(poly.VertexIndices[i]);
|
vertices.Add(vertex.ToGodotVec3());
|
||||||
indices.Add(poly.VertexIndices[i + 1]);
|
if (i < poly.UvIndices.Length)
|
||||||
}
|
{
|
||||||
}
|
var uv = modelFile.Uvs[poly.UvIndices[i]];
|
||||||
|
uvs.Add(new Vector2(uv.X, uv.Y));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var uv = (i % 4) switch
|
||||||
|
{
|
||||||
|
0 => new Vector2(0, 0),
|
||||||
|
1 => new Vector2(0, 1),
|
||||||
|
2 => new Vector2(1, 0),
|
||||||
|
3 => new Vector2(1, 1),
|
||||||
|
_ => throw new NotImplementedException(),
|
||||||
|
};
|
||||||
|
uvs.Add(uv);
|
||||||
|
}
|
||||||
|
|
||||||
var array = new Array();
|
}
|
||||||
array.Resize((int)Mesh.ArrayType.Max);
|
|
||||||
array[(int)Mesh.ArrayType.Vertex] = vertices.ToArray();
|
surfaceData.AddPolygon(vertices, normal, uvs, uvs);
|
||||||
array[(int)Mesh.ArrayType.Index] = indices.ToArray();
|
}
|
||||||
|
var array = surfaceData.BuildSurfaceArray();
|
||||||
|
|
||||||
var mesh = new ArrayMesh();
|
var mesh = new ArrayMesh();
|
||||||
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
|
mesh.AddSurfaceFromArrays(Mesh.PrimitiveType.Triangles, array);
|
||||||
|
mesh.SurfaceSetMaterial(0, mat);
|
||||||
|
|
||||||
var meshInstance = new MeshInstance3D { Mesh = mesh };
|
var meshInstance = new MeshInstance3D { Mesh = mesh };
|
||||||
AddChild(meshInstance);
|
AddChild(meshInstance);
|
||||||
|
|
Loading…
Reference in New Issue