private static Mesh GenerateUvBoneWeightedMesh(SkinnedMeshRenderer smr, GpuInstancedAnimationBoneExport export) { var mesh = Object.Instantiate(smr.sharedMesh); var boneSets = smr.sharedMesh.boneWeights; var boneIndexes = boneSets.Select(x => new Vector4(x.boneIndex0, x.boneIndex1, x.boneIndex2, x.boneIndex3)).ToList(); var boneWeights = boneSets.Select(x => new Vector4(x.weight0, x.weight1, x.weight2, x.weight3)).ToList(); mesh.SetUVs(2, boneIndexes); mesh.SetUVs(3, boneWeights); if (export != null) { var boneHeights = boneSets.Select(x => new Vector4(export.GetBoneHeightWeight(x.boneIndex0), export.GetBoneHeightWeight(x.boneIndex1), export.GetBoneHeightWeight(x.boneIndex2), export.GetBoneHeightWeight(x.boneIndex3))).ToList(); mesh.SetUVs(4, boneHeights); } return(mesh); }
private static GameObject GenerateMeshRendererObject(GameObject targetObject, Mesh mesh, Material material, IEnumerable <AnimationClip> clips, SkinnedMeshRenderer smr) { var go = new GameObject(); go.name = targetObject.name; var animation = go.AddComponent <GpuInstancedAnimation>(); GpuInstancedAnimationBoneExport animationBoneExport = targetObject.GetComponent <GpuInstancedAnimationBoneExport>(); if (animationBoneExport != null && animationBoneExport.bones != null && animationBoneExport.bones.Count > 0) { List <GpuInstancedAnimationBone> animationBones = new List <GpuInstancedAnimationBone>(); List <GpuInstancedAnimationBoneFrame> animationBoneFrames = new List <GpuInstancedAnimationBoneFrame>(); for (int i = 0; i < smr.bones.Length; ++i) { var bone = smr.bones[i]; for (int j = 0; j < animationBoneExport.bones.Count; ++j) { if (animationBoneExport.bones[j] == bone) { animationBones.Add(new GpuInstancedAnimationBone { boneName = bone.gameObject.name, index = j, blendWeight = animationBoneExport.GetBoneHeightWeight(j) }); animation.boneFrames.Add(new GpuInstancedAnimationBoneFrame { localPosition = targetObject.transform.InverseTransformPoint(bone.position), localForward = targetObject.transform.InverseTransformVector(bone.forward), }); } } } animation.bones = animationBones; animation.boneFrames = animationBoneFrames; } var animationClips = new List <GpuInstancedAnimationClip>(); var currentClipFrames = 0; foreach (var clip in clips) { var frameCount = (int)(clip.length * GpuInstancedAnimation.TargetFrameRate); var startFrame = currentClipFrames + 1; var endFrame = startFrame + frameCount - 1; animationClips.Add(new GpuInstancedAnimationClip(clip.name, startFrame, endFrame, frameCount, clip.isLooping ? GpuInstancedAnimationClip.WrapMode.Loop : GpuInstancedAnimationClip.WrapMode.Once)); currentClipFrames = endFrame; if (animationBoneExport != null && animationBoneExport.bones != null && animationBoneExport.bones.Count > 0) { var totalFrames = (int)(clip.length * GpuInstancedAnimation.TargetFrameRate); foreach (var frame in Enumerable.Range(0, totalFrames)) { clip.SampleAnimation(targetObject, (float)frame / GpuInstancedAnimation.TargetFrameRate); for (int i = 0; i < smr.bones.Length; ++i) { var bone = smr.bones[i]; for (int j = 0; j < animationBoneExport.bones.Count; ++j) { if (animationBoneExport.bones[j] == bone) { animation.boneFrames.Add(new GpuInstancedAnimationBoneFrame { localPosition = targetObject.transform.InverseTransformPoint(bone.position), localForward = targetObject.transform.InverseTransformVector(bone.forward), }); } } } } } } animation.mesh = mesh; animation.material = material; animation.animationClips = animationClips; return(go); }