Created
September 24, 2023 11:25
-
-
Save andybak/38e049a8e58056d8542d9ee67c7957e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Unity.Collections; | |
using Unity.Mathematics; | |
using UnityEngine; | |
using UnityEngine.Rendering; | |
public class MeshAPIScript : MonoBehaviour | |
{ | |
public Material m_material; | |
Mesh m_mesh; | |
NativeArray<float3> m_vertices; | |
void Start() | |
{ | |
m_mesh = new Mesh(); | |
// Set initial vertices | |
var tempVertices = new NativeArray<float3>( | |
new[] | |
{ | |
new float3(-1f,0,-1f), | |
new float3(1f,0,-1f), | |
new float3(1f,0,1f), | |
new float3(-1,0,1), | |
new float3(0,1,0) | |
}, Allocator.Persistent); | |
m_vertices = new NativeArray<float3>(tempVertices, Allocator.Persistent); | |
m_mesh.SetVertexBufferParams(tempVertices.Length, new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32)); | |
m_mesh.SetVertexBufferData(tempVertices, 0, 0, tempVertices.Length); | |
// Set initial indices | |
var tempIndices = new[] | |
{ | |
0,4,1, 1,4,2, 2,4,3, 3,4,0 | |
}; | |
m_mesh.SetIndexBufferParams(tempIndices.Length, IndexFormat.UInt32); | |
m_mesh.SetIndexBufferData(tempIndices, 0, 0, tempIndices.Length); | |
// Set initial sub nesh data | |
SubMeshDescriptor desc = new SubMeshDescriptor(); | |
desc.topology = MeshTopology.Triangles; | |
desc.firstVertex = 0; | |
desc.baseVertex = 0; | |
desc.indexStart = 0; | |
desc.vertexCount = tempVertices.Length; | |
desc.indexCount = tempIndices.Length; | |
desc.bounds = new Bounds(float3.zero, new float3(2, 2, 1)); | |
m_mesh.SetSubMesh(0, desc); | |
} | |
void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.UpArrow)) | |
{ | |
m_vertices[4] += new float3(0, 1, 0); | |
m_mesh.SetVertexBufferData(m_vertices, 0, 0, m_vertices.Length); | |
} | |
if (Input.GetKeyDown(KeyCode.DownArrow)) | |
{ | |
m_vertices[4] -= new float3(0, 1, 0); | |
m_mesh.SetVertexBufferData(m_vertices, 0, 0, m_vertices.Length); | |
} | |
Graphics.DrawMesh(m_mesh, Matrix4x4.identity, m_material, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment