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