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