Created
December 29, 2018 21:13
-
-
Save keenanwoodall/5d8ddbe6a38f26c7a3d19311b0c64703 to your computer and use it in GitHub Desktop.
Example script showing how to deform a mesh using a NativeCurve.
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 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