| | 1 | | using System.Linq; |
| | 2 | | using Unity.Collections; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Profiling; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | namespace GPUSkinning |
| | 8 | | { |
| | 9 | | public interface IGPUSkinning |
| | 10 | | { |
| | 11 | | Renderer renderer { get; } |
| | 12 | |
|
| | 13 | | void Update(); |
| | 14 | |
|
| | 15 | | void Prepare(SkinnedMeshRenderer skr, bool encodeBindPoses = false); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public static class GPUSkinningUtils |
| | 19 | | { |
| | 20 | | /// <summary> |
| | 21 | | /// This must be done once per SkinnedMeshRenderer before animating. |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="skr"></param> |
| | 24 | | public static void EncodeBindPosesIntoMesh(SkinnedMeshRenderer skr) |
| | 25 | | { |
| | 26 | | Mesh sharedMesh = skr.sharedMesh; |
| | 27 | | int vertexCount = sharedMesh.vertexCount; |
| | 28 | |
|
| | 29 | | NativeArray<Vector4> bone01data = new NativeArray<Vector4>(vertexCount, Allocator.Temp, NativeArrayOptions.U |
| | 30 | | NativeArray<Vector4> bone23data = new NativeArray<Vector4>(vertexCount, Allocator.Temp, NativeArrayOptions.U |
| | 31 | |
|
| | 32 | | var bonesPerVertex = sharedMesh.GetBonesPerVertex(); |
| | 33 | | var boneWeightsNative = sharedMesh.GetAllBoneWeights(); |
| | 34 | | var boneIndex = 0; |
| | 35 | |
|
| | 36 | | for (int i = 0; i < vertexCount; i++) |
| | 37 | | { |
| | 38 | | byte boneCount = bonesPerVertex[i]; |
| | 39 | |
|
| | 40 | | if (boneCount == 0) |
| | 41 | | { |
| | 42 | | bone01data[i] = new Vector4(0, 0, 0, 0); |
| | 43 | | bone23data[i] = new Vector4(0, 0, 0, 0); |
| | 44 | | } |
| | 45 | | else if (boneCount == 1) |
| | 46 | | { |
| | 47 | | var bw0 = boneWeightsNative[boneIndex]; |
| | 48 | | bone01data[i] = new Vector4(bw0.boneIndex, bw0.weight, 0, 0); |
| | 49 | | bone23data[i] = new Vector4(0, 0, 0, 0); |
| | 50 | | } |
| | 51 | | else if (boneCount == 2) |
| | 52 | | { |
| | 53 | | var bw0 = boneWeightsNative[boneIndex]; |
| | 54 | | var bw1 = boneWeightsNative[boneIndex + 1]; |
| | 55 | | bone01data[i] = new Vector4(bw0.boneIndex, bw0.weight, bw1.boneIndex, bw1.weight); |
| | 56 | | bone23data[i] = new Vector4(0, 0, 0, 0); |
| | 57 | | } |
| | 58 | | else if (boneCount == 3) |
| | 59 | | { |
| | 60 | | var bw0 = boneWeightsNative[boneIndex]; |
| | 61 | | var bw1 = boneWeightsNative[boneIndex + 1]; |
| | 62 | | var bw2 = boneWeightsNative[boneIndex + 2]; |
| | 63 | | bone01data[i] = new Vector4(bw0.boneIndex, bw0.weight, bw1.boneIndex, bw1.weight); |
| | 64 | | bone23data[i] = new Vector4(bw2.boneIndex, bw2.weight, 0, 0); |
| | 65 | | } |
| | 66 | | else if (boneCount >= 4) |
| | 67 | | { |
| | 68 | | var bw0 = boneWeightsNative[boneIndex]; |
| | 69 | | var bw1 = boneWeightsNative[boneIndex + 1]; |
| | 70 | | var bw2 = boneWeightsNative[boneIndex + 2]; |
| | 71 | | var bw3 = boneWeightsNative[boneIndex + 3]; |
| | 72 | | bone01data[i] = new Vector4(bw0.boneIndex, bw0.weight, bw1.boneIndex, bw1.weight); |
| | 73 | | bone23data[i] = new Vector4(bw2.boneIndex, bw2.weight, bw3.boneIndex, bw3.weight); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | boneIndex += boneCount; |
| | 77 | | } |
| | 78 | |
|
| | 79 | | sharedMesh.SetTangents(bone01data); |
| | 80 | | sharedMesh.SetUVs(1, bone23data); |
| | 81 | |
|
| | 82 | | bone01data.Dispose(); |
| | 83 | | bone23data.Dispose(); |
| | 84 | | } |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public class SimpleGPUSkinning : IGPUSkinning |
| | 88 | | { |
| 1 | 89 | | private static readonly int BONE_MATRICES = Shader.PropertyToID("_Matrices"); |
| 1 | 90 | | private static readonly int BIND_POSES = Shader.PropertyToID("_BindPoses"); |
| 1 | 91 | | private static readonly int RENDERER_WORLD_INVERSE = Shader.PropertyToID("_WorldInverse"); |
| | 92 | |
|
| 14 | 93 | | public Renderer renderer { get; private set; } |
| | 94 | |
|
| | 95 | | private Transform[] bones; |
| | 96 | | private Matrix4x4[] boneMatrices; |
| | 97 | |
|
| | 98 | | public void Prepare(SkinnedMeshRenderer skr, bool encodeBindPoses = false) |
| | 99 | | { |
| 1 | 100 | | if (encodeBindPoses) |
| 0 | 101 | | GPUSkinningUtils.EncodeBindPosesIntoMesh(skr); |
| | 102 | |
|
| 1 | 103 | | boneMatrices = new Matrix4x4[skr.bones.Length]; |
| | 104 | |
|
| 1 | 105 | | GameObject go = skr.gameObject; |
| | 106 | |
|
| 1 | 107 | | if (!go.TryGetComponent(out MeshFilter meshFilter)) |
| 1 | 108 | | meshFilter = go.AddComponent<MeshFilter>(); |
| | 109 | |
|
| 1 | 110 | | meshFilter.sharedMesh = skr.sharedMesh; |
| | 111 | |
|
| 1 | 112 | | renderer = go.AddComponent<MeshRenderer>(); |
| 1 | 113 | | renderer.enabled = skr.enabled; |
| 1 | 114 | | renderer.sharedMaterials = skr.sharedMaterials; |
| | 115 | |
|
| 4 | 116 | | foreach (Material material in renderer.sharedMaterials) |
| | 117 | | { |
| 1 | 118 | | material.SetMatrixArray(BIND_POSES, skr.sharedMesh.bindposes.ToArray()); |
| 1 | 119 | | material.EnableKeyword("_GPU_SKINNING"); |
| | 120 | | } |
| | 121 | |
|
| 1 | 122 | | bones = skr.bones; |
| 1 | 123 | | renderer.localBounds = new Bounds(new Vector3(0, 2, 0), new Vector3(1, 3, 1)); |
| 1 | 124 | | UpdateMatrices(); |
| | 125 | |
|
| 1 | 126 | | Object.Destroy(skr); |
| 1 | 127 | | } |
| | 128 | |
|
| | 129 | | public void Update() |
| | 130 | | { |
| 1 | 131 | | if (!renderer.gameObject.activeInHierarchy) |
| 0 | 132 | | return; |
| | 133 | |
|
| 1 | 134 | | UpdateMatrices(); |
| 1 | 135 | | } |
| | 136 | |
|
| | 137 | | private void UpdateMatrices() |
| | 138 | | { |
| 2 | 139 | | int bonesLength = bones.Length; |
| | 140 | |
|
| 252 | 141 | | for (int i = 0; i < bonesLength; i++) |
| | 142 | | { |
| 124 | 143 | | Transform bone = bones[i]; |
| 124 | 144 | | boneMatrices[i] = bone.localToWorldMatrix; |
| | 145 | | } |
| | 146 | |
|
| 8 | 147 | | for (int index = 0; index < renderer.sharedMaterials.Length; index++) |
| | 148 | | { |
| 2 | 149 | | Material material = renderer.sharedMaterials[index]; |
| 2 | 150 | | material.SetMatrix(RENDERER_WORLD_INVERSE, renderer.transform.worldToLocalMatrix); |
| 2 | 151 | | material.SetMatrixArray(BONE_MATRICES, boneMatrices); |
| | 152 | | } |
| 2 | 153 | | } |
| | 154 | | } |
| | 155 | | } |