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