正在載入此比較的其餘部分。

Subdivision.cs

建立於 差異永不過期
50 刪除
636 行
125 新增
620 行
//#define SUBDIVIDEVECTOR4 // used to subdivide meshes in 4D
//#define SUBDIVIDEVECTOR4 // used to subdivide meshes in 4D


using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine;
using UnityEngine.Rendering;


//using System.Linq; -- don't use Linq for performance
//using System.Linq; -- don't use Linq for performance


namespace Torec {
namespace Torec {


// Vector used for position, normal and tangent of a Vertex
// Vector used for position, normal and tangent of a Vertex
#if !SUBDIVIDEVECTOR4
#if !SUBDIVIDEVECTOR4
using Vector = Vector3;
using Vector = Vector3;
#else
#else
using Vector = Vector4; // to subdivide 4D meshes
using Vector = Vector4; // to subdivide 4D meshes
#endif
#endif


/// <summary>
/// <summary>
/// Temporal Vertex object extracted from (inserted to) Mesh data arrays (positions, normals, uvs,..)
/// Temporal Vertex object extracted from (inserted to) Mesh data arrays (positions, normals, uvs,..)
/// </summary>
/// </summary>
[System.Diagnostics.DebuggerDisplay("Vertex: pos {posIndex}. {position}; normal {normal}")]
[System.Diagnostics.DebuggerDisplay("Vertex: pos {posIndex}. {position}; normal {normal}")]
public struct Vertex {
public struct Vertex {
public int posIndex;
public int posIndex;
public Vector position;
public Vector position;
public Vector normal;
public Vector normal;
public Vector tangent;
public Vector tangent;
public Vector2 uv0;
public Vector2 uv0;
public Vector2 uv1;
public Vector2 uv1;
public Vector2 uv2;
public Vector2 uv2;
public Vector2 uv3;
public Vector2 uv3;
public BoneWeight boneWeight; // 頂点属性にBoneWeightを追加
}
}


/// <summary>
/// <summary>
/// Content of Mesh data arrays
/// Content of Mesh data arrays
/// </summary>
/// </summary>
public enum VertexContent {
public enum VertexContent {
none = 0x00,
none = 0x00,
pos = 0x01,
pos = 0x01,
normal = 0x02,
normal = 0x02,
tangent = 0x04,
tangent = 0x04,
uv0 = 0x08,
uv0 = 0x08,
uv1 = 0x10,
uv1 = 0x10,
uv2 = 0x20,
uv2 = 0x20,
uv3 = 0x40,
uv3 = 0x40,
full = 0x7F,
boneWeight = 0x80, // BoneWeight用フラグを追加
full = 0xFF, // フラグを増やしたのでfullも増やす
}
}
public static class VertexContentMethods {
public static class VertexContentMethods {
public static bool HasPosition(this VertexContent c) { return (c & VertexContent.pos) != 0; }
public static bool HasPosition (this VertexContent c) { return (c & VertexContent.pos) != 0; }
public static bool HasNormal (this VertexContent c) { return (c & VertexContent.normal) != 0; }
public static bool HasNormal (this VertexContent c) { return (c & VertexContent.normal) != 0; }
public static bool HasTangent (this VertexContent c) { return (c & VertexContent.tangent) != 0; }
public static bool HasTangent (this VertexContent c) { return (c & VertexContent.tangent) != 0; }
public static bool HasUV0 (this VertexContent c) { return (c & VertexContent.uv0) != 0; }
public static bool HasUV0 (this VertexContent c) { return (c & VertexContent.uv0) != 0; }
public static bool HasUV1 (this VertexContent c) { return (c & VertexContent.uv1) != 0; }
public static bool HasUV1 (this VertexContent c) { return (c & VertexContent.uv1) != 0; }
public static bool HasUV2 (this VertexContent c) { return (c & VertexContent.uv2) != 0; }
public static bool HasUV2 (this VertexContent c) { return (c & VertexContent.uv2) != 0; }
public static bool HasUV3 (this VertexContent c) { return (c & VertexContent.uv3) != 0; }
public static bool HasUV3 (this VertexContent c) { return (c & VertexContent.uv3) != 0; }
public static bool HasBoneWeight(this VertexContent c) { return (c & VertexContent.boneWeight) != 0; } // BoneWeight用メソッドを追加
}
}


public struct Submesh {
public struct Submesh {
public int[][] faces; // [face index] => [face vertex indices]
public int[][] faces; // [face index] => [face vertex indices]
}
}


public struct Face {
public struct Face {
public int submesh;
public int submesh;
public int index; // face index
public int index; // face index
}
}
public struct Edge {
public struct Edge {
public Face face;
public Face face;
public int index; // edge index in a face (0..3)
public int index; // edge index in a face (0..3)
//
//
public static Edge Invalid = new Edge { index = -1 };
public static Edge Invalid = new Edge { index = -1 };
public bool IsValid() { return index != -1; }
public bool IsValid() { return index != -1; }
}
}


public enum EdgeType { // see GetEdgeType(Edge e)
public enum EdgeType { // see GetEdgeType(Edge e)
boundary = -1, // the edge has no opposite neighbor
boundary = -1, // the edge has no opposite neighbor
back = -2, // back edge - skip it to not count the edge twice
back = -2, // back edge - skip it to not count the edge twice
solid = 0, // front solid edge: both edge vertices belong to both faces
solid = 0, // front solid edge: both edge vertices belong to both faces
seam0 = 1, // front seam edge: faces have different uv on this edge
seam0 = 1, // front seam edge: faces have different uv on this edge
seam1 = 2,
seam1 = 2,
}
}


/// <summary>
/// <summary>
/// Extended mesh. Allows to weld vertex positions keeping different uvs. So 'positions.Count' may be less than 'vertexCount'
/// Extended mesh. Allows to weld vertex positions keeping different uvs. So 'positions.Count' may be less than 'vertexCount'
/// </summary>
/// </summary>
public class MeshX
public class MeshX
{
{
// by position index
// by position index
public IList<Vector> positions = null;
public IList<Vector> positions = null;
// by vertex index
// by vertex index
public IList<int> posIndices = null;
public IList<int> posIndices = null;
public IList<Vector> normals = null; // may be per position: if 'normalPerPosition'
public IList<Vector> normals = null; // may be per position: if 'normalPerPosition'
public IList<Vector> tangents = null;
public IList<Vector> tangents = null;
public IList<Vector2> uvs0 = null;
public IList<Vector2> uvs0 = null;
public IList<Vector2> uvs1 = null;
public IList<Vector2> uvs1 = null;
public IList<Vector2> uvs2 = null;
public IList<Vector2> uvs2 = null;
public IList<Vector2> uvs3 = null;
public IList<Vector2> uvs3 = null;
public IList<BoneWeight> boneWeights = null; // BoneWeightリストを追加
//
//

// bindposesを追加
public Matrix4x4[] bindposes = null;

public VertexContent content = VertexContent.none;
public VertexContent content = VertexContent.none;
public bool normalPerPosition = false; // normal per position or per vertex
public bool normalPerPosition = false; // normal per position or per vertex


public string name = null;
public string name = null;


public bool buildMode = false;
public bool buildMode = false;


// faces
// faces
public Submesh[] submeshes = null;
public Submesh[] submeshes = null;


public int vertexCount { get { return posIndices == null ? -1 : posIndices.Count; } }
public int vertexCount { get { return posIndices == null ? -1 : posIndices.Count; } }


// helpers
// helpers
public bool helpersInited { get { return positionVertices != null; } }
public bool helpersInited { get { return positionVertices != null; } }
public int[][] positionVertices = null; // [position index] => [vertex indices]
public int[][] positionVertices = null; // [position index] => [vertex indices]
public Edge[][] vertexEdges = null; // [vertex index] => [face edges started from the vertex]
public Edge[][] vertexEdges = null; // [vertex index] => [face edges started from the vertex]
private Edge[][][] neighbors = null; // [submesh index][face index][face edge index] => neighbor edge
private Edge[][][] neighbors = null; // [submesh index][face index][face edge index] => neighbor edge


//
//
public MeshX() {}
public MeshX() {}