forked from keenanwoodall/Deform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformOffsetDeformer.cs
52 lines (46 loc) · 1.3 KB
/
TransformOffsetDeformer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using UnityEngine;
using Unity.Jobs;
using Unity.Burst;
using Unity.Collections;
using Unity.Mathematics;
using static Unity.Mathematics.math;
namespace Deform
{
[Deformer (Name = "Transform Offset", Description = "Offsets the position, rotation and scale of a mesh", Type = typeof (TransformOffsetDeformer))]
public class TransformOffsetDeformer : Deformer
{
public Transform Target
{
get
{
if (target == null)
target = transform;
return target;
}
set => target = value;
}
[SerializeField, HideInInspector]
private Transform target;
public override DataFlags DataFlags => DataFlags.Vertices;
public override JobHandle Process (MeshData data, JobHandle dependency = default)
{
var dataTargetTransform = data.Target.GetTransform ();
var matrix = Matrix4x4.TRS (Target.position, Target.rotation, Target.lossyScale);
return new TransformJob
{
matrix = matrix,
vertices = data.DynamicNative.VertexBuffer
}.Schedule (data.Length, 256, dependency);
}
[BurstCompile (CompileSynchronously = COMPILE_SYNCHRONOUSLY)]
private struct TransformJob : IJobParallelFor
{
public float4x4 matrix;
public NativeArray<float3> vertices;
public void Execute (int index)
{
vertices[index] = mul (matrix, float4 (vertices[index], 1f)).xyz;
}
}
}
}