138 lines
3.4 KiB
C#
138 lines
3.4 KiB
C#
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using Godot;
|
||
|
using System;
|
||
|
|
||
|
|
||
|
|
||
|
namespace Rokojori
|
||
|
{
|
||
|
public class MeshGeometry
|
||
|
{
|
||
|
public List<Vector3> vertices;
|
||
|
public List<int> indices;
|
||
|
|
||
|
public List<Vector3> normals;
|
||
|
public List<Vector2> uvs;
|
||
|
public List<Vector2> uv2s;
|
||
|
public List<Color> colors;
|
||
|
|
||
|
|
||
|
public void Initialize( bool normals = true, bool uvs = true, bool colors = false, bool uvs2 = false)
|
||
|
{
|
||
|
this.vertices = new List<Vector3>();
|
||
|
this.indices = new List<int>();
|
||
|
|
||
|
this.normals = normals ? new List<Vector3>() : null;
|
||
|
this.uvs = uvs ? new List<Vector2>() : null;
|
||
|
this.uv2s = uvs2 ? new List<Vector2>() : null;
|
||
|
this.colors = colors ? new List<Color>() : null;
|
||
|
}
|
||
|
|
||
|
public void AddTriangle(
|
||
|
Vector3 va, Vector3 vb, Vector3 vc,
|
||
|
Vector3 na, Vector3 nb, Vector3 nc,
|
||
|
Vector2 uva, Vector2 uvb, Vector2 uvc
|
||
|
)
|
||
|
{
|
||
|
var index = vertices.Count;
|
||
|
Lists.Add( vertices, va, vb, vc );
|
||
|
Lists.Add( normals, na, nb, nc );
|
||
|
Lists.Add( uvs, uva, uvb, uvc );
|
||
|
Lists.Add( indices, index, index + 1, index + 2 );
|
||
|
}
|
||
|
|
||
|
public void AddQuad(
|
||
|
Vector3 va, Vector3 vb, Vector3 vc, Vector3 vd,
|
||
|
Vector3 na, Vector3 nb, Vector3 nc, Vector3 nd,
|
||
|
Vector2 uva, Vector2 uvb, Vector2 uvc, Vector2 uvd
|
||
|
)
|
||
|
{
|
||
|
AddTriangle( va, vb, vc, na, nb, nc, uva, uvb, uvc );
|
||
|
AddTriangle( vc, vd, va, nc, nd, na, uvc, uvd, uva );
|
||
|
}
|
||
|
|
||
|
public void AddTriangle(
|
||
|
Vector3 va, Vector3 vb, Vector3 vc,
|
||
|
Vector2 uva, Vector2 uvb, Vector2 uvc
|
||
|
)
|
||
|
{
|
||
|
var n = Vector3.Up;
|
||
|
|
||
|
AddTriangle( va, vb, vc,
|
||
|
n, n, n,
|
||
|
uva, uvb, uvc );
|
||
|
}
|
||
|
|
||
|
public void AddQuad(
|
||
|
Vector3 va, Vector3 vb, Vector3 vc, Vector3 vd,
|
||
|
Vector2 uva, Vector2 uvb, Vector2 uvc, Vector2 uvd
|
||
|
)
|
||
|
{
|
||
|
|
||
|
AddTriangle( va, vb, vc, uva, uvb, uvc );
|
||
|
AddTriangle( vc, vd, va, uvc, uvd, uva );
|
||
|
}
|
||
|
|
||
|
public void AddTriangle( Vector3 va, Vector3 vb, Vector3 vc )
|
||
|
{
|
||
|
var n = Vector3.Up;
|
||
|
var uv = Vector2.Zero;
|
||
|
|
||
|
AddTriangle( va, vb, vc, n, n, n, uv, uv, uv );
|
||
|
}
|
||
|
|
||
|
public void AddQuad( Vector3 va, Vector3 vb, Vector3 vc, Vector3 vd )
|
||
|
{
|
||
|
AddTriangle( va, vb, vc );
|
||
|
AddTriangle( vc, vd, va );
|
||
|
}
|
||
|
|
||
|
|
||
|
public ArrayMesh Generate( Mesh.PrimitiveType type = Mesh.PrimitiveType.Triangles, ArrayMesh arrayMesh = null )
|
||
|
{
|
||
|
if ( arrayMesh == null )
|
||
|
{
|
||
|
arrayMesh = new ArrayMesh();
|
||
|
}
|
||
|
|
||
|
var surfaceArray = new Godot.Collections.Array();
|
||
|
surfaceArray.Resize( (int) Mesh.ArrayType.Max );
|
||
|
|
||
|
if ( vertices != null )
|
||
|
{
|
||
|
surfaceArray[ (int) Mesh.ArrayType.Vertex ] = vertices.ToArray();
|
||
|
}
|
||
|
|
||
|
if ( normals != null )
|
||
|
{
|
||
|
surfaceArray[ (int) Mesh.ArrayType.Normal ] = normals.ToArray();
|
||
|
}
|
||
|
|
||
|
if ( uvs != null )
|
||
|
{
|
||
|
surfaceArray[ (int) Mesh.ArrayType.TexUV ] = uvs.ToArray();
|
||
|
}
|
||
|
|
||
|
if ( uv2s != null )
|
||
|
{
|
||
|
surfaceArray[ (int) Mesh.ArrayType.TexUV2 ] = uv2s.ToArray();
|
||
|
}
|
||
|
|
||
|
if ( colors != null )
|
||
|
{
|
||
|
surfaceArray[ (int) Mesh.ArrayType.Color ] = colors.ToArray();
|
||
|
}
|
||
|
|
||
|
if ( indices != null )
|
||
|
{
|
||
|
surfaceArray[ (int) Mesh.ArrayType.Index ] = indices.ToArray();
|
||
|
}
|
||
|
|
||
|
arrayMesh.AddSurfaceFromArrays( Mesh.PrimitiveType.Triangles, surfaceArray );
|
||
|
|
||
|
return arrayMesh;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|