Skip to content

Instantly share code, notes, and snippets.

@keenanwoodall
Created December 29, 2018 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keenanwoodall/5d8ddbe6a38f26c7a3d19311b0c64703 to your computer and use it in GitHub Desktop.
Save keenanwoodall/5d8ddbe6a38f26c7a3d19311b0c64703 to your computer and use it in GitHub Desktop.
Example script showing how to deform a mesh using a NativeCurve.
using UnityEngine;
using Unity.Jobs;
using Unity.Burst;
using Unity.Collections;
public class CurveTest : MonoBehaviour
{
public AnimationCurve curve;
private Mesh mesh;
private NativeCurve nativeCurve;
private Vector3[] originalVertices;
private void Awake ()
{
mesh = GetComponent<MeshFilter> ().mesh;
originalVertices = mesh.vertices;
}
private void Update ()
{
nativeCurve.Update (curve, 32);
var job = new CurveTestJob
{
curve = nativeCurve,
vertices = new NativeArray<Vector3> (originalVertices, Allocator.TempJob)
};
job.Schedule (originalVertices.Length, 32).Complete ();
mesh.vertices = job.vertices.ToArray ();
mesh.RecalculateNormals ();
mesh.RecalculateBounds ();
job.vertices.Dispose ();
}
private void OnDisable ()
{
nativeCurve.Dispose ();
}
[BurstCompile]
private struct CurveTestJob : IJobParallelFor
{
public NativeArray<Vector3> vertices;
[ReadOnly]
public NativeCurve curve;
public void Execute (int index)
{
var vertex = vertices[index];
vertex.y += curve.Evaluate (vertex.z);
vertices[index] = vertex;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment