using UnityEngine; using Unity.Collections; using Unity.Mathematics; public class DynamicMeshData : System.IDisposable { #region FIELDS & PROPERTIES public Mesh mesh { get; private set; } public NativeArray vertices { get; private set; } Vector3[] _vertices = null; public NativeArray normals { get; private set; } Vector3[] _normals = null; public NativeArray uv { get; private set; } Vector2[] _uv = null; public NativeArray uv2 { get; private set; } Vector2[] _uv2 = null; public NativeArray tangents { get; private set; } Vector4[] _tangents = null; public NativeArray triangles { get; private set; } int[] _triangles = null; public NativeArray colors { get; private set; } Color[] _colors = null; public int numTriangles => numVertices/3; public int numVertices => vertices.Length; #endregion #region CONSTRUCTORS public DynamicMeshData ( Mesh mesh ) { this.mesh = mesh; vertices = mesh.vertices.GetNativeArray(); triangles = mesh.triangles.GetNativeArray(); _normals = mesh.normals; if( _normals.Length!=0 ) normals = _normals.GetNativeArray(); _uv = mesh.uv; if( _uv.Length!=0 ) uv = _uv.GetNativeArray(); _uv2 = mesh.uv2; if( _uv2.Length!=0 ) uv2 = _uv2.GetNativeArray(); _tangents = mesh.tangents; if( _tangents.Length!=0 ) tangents = _tangents.GetNativeArray(); _colors = mesh.colors; if( colors!=null ) this.colors = _colors.GetNativeArray(); } public DynamicMeshData ( Mesh mesh , Vector3[] vertices , int[] triangles , Vector3[] normals = null , Vector2[] uv = null , Vector2[] uv2 = null , Vector4[] tangents = null , Color[] colors = null ) { this.mesh = mesh; _vertices = vertices; if( vertices!=null ) this.vertices = vertices.GetNativeArray(); _triangles = triangles; if( triangles!=null ) this.triangles = triangles.GetNativeArray(); _normals = normals; if( normals!=null ) this.normals = normals.GetNativeArray(); _uv = uv; if( uv!=null ) this.uv = uv.GetNativeArray(); _uv2 = uv2; if( uv2!=null ) this.uv2 = uv2.GetNativeArray(); _tangents = tangents; if( tangents!=null ) this.tangents = tangents.GetNativeArray(); _colors = colors; if( colors!=null ) this.colors = colors.GetNativeArray(); } public DynamicMeshData ( Mesh mesh , NativeArray vertices , NativeArray triangles , NativeArray normals = default(NativeArray) , NativeArray uv = default(NativeArray) , NativeArray uv2 = default(NativeArray) , NativeArray tangents = default(NativeArray) , NativeArray colors = default(NativeArray) ) { this.mesh = mesh; if( vertices.IsCreated ) { _vertices = new Vector3[ vertices.Length ]; this.vertices = vertices; } if( triangles.IsCreated ) { _triangles = new int[ triangles.Length ]; this.triangles = triangles; } if( normals.IsCreated ) { _normals = new Vector3[ normals.Length ]; this.normals = normals; } if( uv.IsCreated ) { _uv = new Vector2[ uv.Length ]; this.uv = uv; } if( uv2.IsCreated ) { _uv2 = new Vector2[ uv2.Length ]; this.uv2 = uv2; } if( tangents.IsCreated ) { _tangents = new Vector4[ tangents.Length ]; this.tangents = tangents; } if( colors.IsCreated ) { _colors = new Color[ colors.Length ]; this.colors = colors; } } #endregion #region DESTRUCTORS ~DynamicMeshData () => Dispose(); #endregion #region PUBLIC METHODS public void VerticesToMesh () { vertices.MemCpy( _vertices ); mesh.vertices = _vertices; } public void TrianglesToMesh () { triangles.MemCpy( _triangles ); mesh.triangles = _triangles; } public void NormalsToMesh () { normals.MemCpy( _normals ); mesh.normals = _normals; } public void UVToMesh () { uv.MemCpy( _uv ); mesh.uv = _uv; } public void UV2ToMesh () { uv2.MemCpy( _uv2 ); mesh.uv2 = _uv2; } public void ColorToMesh () { colors.MemCpy( _colors ); mesh.colors = _colors; } #endregion #region IDISPOSABLE public void Dispose () { if( vertices.IsCreated ) vertices.Dispose(); if( triangles.IsCreated ) triangles.Dispose(); if( normals.IsCreated ) normals.Dispose(); if( uv.IsCreated ) uv.Dispose(); if( uv2.IsCreated ) uv2.Dispose(); if( tangents.IsCreated ) tangents.Dispose(); if( colors.IsCreated ) colors.Dispose(); } #endregion }