| | 1 | | using System.Linq; |
| | 2 | | using UnityEngine; |
| | 3 | | using Object = UnityEngine.Object; |
| | 4 | |
|
| | 5 | | namespace GPUSkinning |
| | 6 | | { |
| | 7 | | public interface IGPUSkinning |
| | 8 | | { |
| | 9 | | Renderer renderer { get; } |
| | 10 | | void Update(); |
| | 11 | | void Prepare(SkinnedMeshRenderer skr, bool encodeBindPoses = false); |
| | 12 | | } |
| | 13 | |
|
| | 14 | | public static class GPUSkinningUtils |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// This must be done once per SkinnedMeshRenderer before animating. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="skr"></param> |
| | 20 | | public static void EncodeBindPosesIntoMesh(SkinnedMeshRenderer skr) |
| | 21 | | { |
| | 22 | | Mesh sharedMesh = skr.sharedMesh; |
| | 23 | | int vertexCount = sharedMesh.vertexCount; |
| | 24 | | Vector4[] bone01data = new Vector4[vertexCount]; |
| | 25 | | Vector4[] bone23data = new Vector4[vertexCount]; |
| | 26 | |
|
| | 27 | | BoneWeight[] boneWeights = sharedMesh.boneWeights; |
| | 28 | |
|
| | 29 | | for ( int i = 0; i < vertexCount; i ++ ) |
| | 30 | | { |
| | 31 | | BoneWeight boneWeight = boneWeights[i]; |
| | 32 | | bone01data[i].x = boneWeight.boneIndex0; |
| | 33 | | bone01data[i].y = boneWeight.weight0; |
| | 34 | | bone01data[i].z = boneWeight.boneIndex1; |
| | 35 | | bone01data[i].w = boneWeight.weight1; |
| | 36 | |
|
| | 37 | | bone23data[i].x = boneWeight.boneIndex2; |
| | 38 | | bone23data[i].y = boneWeight.weight2; |
| | 39 | | bone23data[i].z = boneWeight.boneIndex3; |
| | 40 | | bone23data[i].w = boneWeight.weight3; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | skr.sharedMesh.tangents = bone01data; |
| | 44 | | skr.sharedMesh.SetUVs(1, bone23data); |
| | 45 | | } |
| | 46 | | } |
| | 47 | |
|
| | 48 | | public class SimpleGPUSkinning : IGPUSkinning |
| | 49 | | { |
| 1 | 50 | | private static readonly int BONE_MATRICES = Shader.PropertyToID("_Matrices"); |
| 1 | 51 | | private static readonly int BIND_POSES = Shader.PropertyToID("_BindPoses"); |
| 1 | 52 | | private static readonly int RENDERER_WORLD_INVERSE = Shader.PropertyToID("_WorldInverse"); |
| | 53 | |
|
| 0 | 54 | | public Renderer renderer { get; private set; } |
| | 55 | |
|
| | 56 | | private Transform[] bones; |
| | 57 | | private Matrix4x4[] boneMatrices; |
| | 58 | |
|
| | 59 | | public void Prepare(SkinnedMeshRenderer skr, bool encodeBindPoses = false) |
| | 60 | | { |
| 1 | 61 | | if ( encodeBindPoses ) |
| 0 | 62 | | GPUSkinningUtils.EncodeBindPosesIntoMesh(skr); |
| | 63 | |
|
| 1 | 64 | | boneMatrices = new Matrix4x4[skr.bones.Length]; |
| | 65 | |
|
| 1 | 66 | | GameObject go = skr.gameObject; |
| | 67 | |
|
| 1 | 68 | | if (!go.TryGetComponent(out MeshFilter meshFilter)) |
| 1 | 69 | | meshFilter = go.AddComponent<MeshFilter>(); |
| | 70 | |
|
| 1 | 71 | | meshFilter.sharedMesh = skr.sharedMesh; |
| | 72 | |
|
| 1 | 73 | | renderer = go.AddComponent<MeshRenderer>(); |
| 1 | 74 | | renderer.enabled = skr.enabled; |
| 1 | 75 | | renderer.sharedMaterials = skr.sharedMaterials; |
| 4 | 76 | | foreach (Material material in renderer.sharedMaterials) |
| | 77 | | { |
| 1 | 78 | | material.SetMatrixArray(BIND_POSES, skr.sharedMesh.bindposes.ToArray()); |
| 1 | 79 | | material.EnableKeyword("_GPU_SKINNING"); |
| | 80 | | } |
| | 81 | |
|
| 1 | 82 | | bones = skr.bones; |
| 1 | 83 | | meshFilter.mesh.bounds = new Bounds(new Vector3(0, 2, 0), new Vector3(1, 3, 1)); |
| 1 | 84 | | UpdateMatrices(); |
| | 85 | |
|
| 1 | 86 | | Object.Destroy(skr); |
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | public void Update() |
| | 90 | | { |
| 1 | 91 | | if (!renderer.gameObject.activeInHierarchy) |
| 0 | 92 | | return; |
| | 93 | |
|
| 1 | 94 | | UpdateMatrices(); |
| 1 | 95 | | } |
| | 96 | |
|
| | 97 | | private void UpdateMatrices() |
| | 98 | | { |
| 2 | 99 | | int bonesLength = bones.Length; |
| 252 | 100 | | for (int i = 0; i < bonesLength; i++) |
| | 101 | | { |
| 124 | 102 | | Transform bone = bones[i]; |
| 124 | 103 | | boneMatrices[i] = bone.localToWorldMatrix; |
| | 104 | | } |
| | 105 | |
|
| 8 | 106 | | for (int index = 0; index < renderer.sharedMaterials.Length; index++) |
| | 107 | | { |
| 2 | 108 | | Material material = renderer.sharedMaterials[index]; |
| 2 | 109 | | material.SetMatrix(RENDERER_WORLD_INVERSE, renderer.transform.worldToLocalMatrix); |
| 2 | 110 | | material.SetMatrixArray(BONE_MATRICES, boneMatrices); |
| | 111 | | } |
| 2 | 112 | | } |
| | 113 | | } |
| | 114 | | } |