using UMA; using Unity.Collections; using Unity.Jobs; using UnityEngine; namespace UMA { public class RecalculationSlot : MonoBehaviour { public float angle = 60f; public string blendShapeNameStartsWith = ""; public void Recalculate(UMAData umaDATA) { #if UMA_BURSTCOMPILE Transform theRenderer = umaDATA.gameObject.transform.Find("UMARenderer"); GameObject rendererGameObject = theRenderer.gameObject; SkinnedMeshRenderer smr = rendererGameObject.GetComponent(); if (smr != null) { NativeArray vertices = new NativeArray(smr.sharedMesh.vertices, Allocator.TempJob); NativeArray normals = new NativeArray(smr.sharedMesh.normals, Allocator.TempJob); NativeArray uvs = new NativeArray(smr.sharedMesh.uv, Allocator.TempJob); NativeArray tangents = new NativeArray(smr.sharedMesh.tangents, Allocator.TempJob); NativeArray triangles = new NativeArray(smr.sharedMesh.triangles, Allocator.TempJob); Vector3[] deltaVertices = new Vector3[vertices.Length]; JobHandle handle = default; int blendShapeCount = smr.sharedMesh.blendShapeCount; for (int shapeIndex = 0; shapeIndex < blendShapeCount; shapeIndex++) { float weight = smr.GetBlendShapeWeight(shapeIndex); if (weight > 0f) { weight /= 100f; //bring the weight into 0-1 range. string blendShapeName = smr.sharedMesh.GetBlendShapeName(shapeIndex); if (blendShapeName.StartsWith(blendShapeNameStartsWith)) { smr.sharedMesh.GetBlendShapeFrameVertices(shapeIndex, 0, deltaVertices, null, null); NativeArray blendShapeDeltaVertices = new NativeArray(deltaVertices, Allocator.TempJob); handle = MeshUtilities.BakeOneFramePositionBlendShape(vertices, blendShapeDeltaVertices, weight, handle); } } } handle = MeshUtilities.RecalculateNormalsTangentsJobified(vertices, normals, uvs, tangents, triangles, angle, handle); handle.Complete(); //We don't need to do this if we know our mesh is a single instance! //Mesh mesh = Instantiate(smr.sharedMesh); Mesh mesh = smr.sharedMesh; //mesh.SetVertices(vertices); //don't update the vertices if the blendshapes are going to continue... mesh.SetNormals(normals); mesh.SetUVs(0, uvs); mesh.SetTangents(tangents); smr.sharedMesh = mesh; vertices.Dispose(); normals.Dispose(); uvs.Dispose(); tangents.Dispose(); triangles.Dispose(); } #endif } } }