private void LateUpdate()
        {
            if (m_CurrentSprite != spriteRenderer.sprite)
            {
                m_CurrentSprite = spriteRenderer.sprite;
                DeactivateSkinning();
            }

            if (!spriteRenderer.enabled || !isValid)
            {
                DeactivateSkinning();
                return;
            }

            UpdateBounds();

            var bindPoses         = m_CurrentSprite.GetBindPoses();
            var boneWeights       = m_CurrentSprite.GetBoneWeights();
            var outputVertices    = spriteRenderer.GetDeformableVertices();
            var transformMatrices = new NativeArray <Matrix4x4>(m_BoneTransforms.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            for (int i = 0; i < m_BoneTransforms.Length; ++i)
            {
                transformMatrices[i] = m_BoneTransforms[i].localToWorldMatrix;
            }

            var deformJobHandle = SpriteSkinUtility.Deform(m_CurrentSprite.GetVertexAttribute <Vector3>(VertexAttribute.Position), boneWeights, transform.worldToLocalMatrix, bindPoses, transformMatrices, outputVertices);

            spriteRenderer.UpdateDeformableBuffer(deformJobHandle);
        }
        internal static Vector3[] Bake(this SpriteSkin spriteSkin)
        {
            if (!spriteSkin.isValid)
            {
                throw new Exception("Bake error: invalid SpriteSkin");
            }

            var sprite            = spriteSkin.spriteRenderer.sprite;
            var boneTransforms    = spriteSkin.boneTransforms;
            var bindPoses         = sprite.GetBindPoses();
            var boneWeights       = sprite.GetBoneWeights();
            var outputVertices    = new NativeArray <Vector3>(sprite.GetVertexCount(), Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var transformMatrices = new NativeArray <Matrix4x4>(boneTransforms.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var vertices          = sprite.GetVertexAttribute <Vector3>(VertexAttribute.Position);

            if (vertices.Length == 0 || transformMatrices.Length == 0 || boneWeights.Length == 0 || outputVertices.Length == 0)
            {
                return(new Vector3[0]);
            }
            for (int i = 0; i < boneTransforms.Length; ++i)
            {
                transformMatrices[i] = boneTransforms[i].localToWorldMatrix;
            }

            var jobHandle = SpriteSkinUtility.Deform(vertices, boneWeights, Matrix4x4.identity, bindPoses, transformMatrices, outputVertices);

            jobHandle.Complete();

            var result = outputVertices.ToArray();

            outputVertices.Dispose();

            return(result);
        }
        void LateUpdate()
        {
#if ENABLE_ENTITIES
            if (entitiesEnabled)
            {
                return;
            }
#endif
            if (m_CurrentDeformSprite != sprite)
            {
                DeactivateSkinning();
                m_CurrentDeformSprite = sprite;
            }
            if (isValid)
            {
                var inputVertices = deformedVertices;
                var transformHash = SpriteSkinUtility.CalculateTransformHash(this);
                if (inputVertices.Length > 0 && m_TransformsHash != transformHash)
                {
                    SpriteSkinUtility.Deform(sprite, gameObject.transform.worldToLocalMatrix, boneTransforms, ref inputVertices);
                    SpriteSkinUtility.UpdateBounds(this);
                    InternalEngineBridge.SetDeformableBuffer(spriteRenderer, inputVertices);
                    m_TransformsHash      = transformHash;
                    m_CurrentDeformSprite = sprite;
                }
            }
        }
        private void LateUpdate()
        {
#if UNITY_EDITOR
            if (!Application.isPlaying && spriteRenderer.sprite != null)
            {
                var assetTimeStamp = UnityEditor.AssetImporter.GetAtPath(UnityEditor.AssetDatabase.GetAssetPath(spriteRenderer.sprite)).assetTimeStamp;
                if (m_AssetTimeStamp != assetTimeStamp)
                {
                    m_AssetTimeStamp = assetTimeStamp;
                    DeactivateSkinning();
                }
            }
#endif

            if (m_CurrentSprite != spriteRenderer.sprite)
            {
                m_CurrentSprite = spriteRenderer.sprite;
                DeactivateSkinning();
            }

            if (!spriteRenderer.enabled || !isValid)
            {
                DeactivateSkinning();
                return;
            }

            UpdateBoundsIfNeeded();

            int hashCode = this.CalculateTransformHash();
            if (hashCode == m_TransformHash)
            {
                return;
            }

            var bindPoses         = m_CurrentSprite.GetBindPoses();
            var boneWeights       = m_CurrentSprite.GetBoneWeights();
            var outputVertices    = spriteRenderer.GetDeformableVertices();
            var transformMatrices = new NativeArray <Matrix4x4>(m_BoneTransforms.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            for (int i = 0; i < m_BoneTransforms.Length; ++i)
            {
                transformMatrices[i] = m_BoneTransforms[i].localToWorldMatrix;
            }

            var deformJobHandle = SpriteSkinUtility.Deform(m_CurrentSprite.GetVertexAttribute <Vector3>(VertexAttribute.Position), boneWeights, transform.worldToLocalMatrix, bindPoses, transformMatrices, outputVertices);

            m_BoundsHandle = SpriteSkinUtility.CalculateBounds(outputVertices, m_MinMax, deformJobHandle);
            spriteRenderer.UpdateDeformableBuffer(m_BoundsHandle);

            m_TransformHash     = hashCode;
            m_NeedsUpdateBounds = true;

#if UNITY_EDITOR
            if (m_SpriteBones != null && m_SpriteBones.Length != bindPoses.Length)
            {
                m_SpriteBones = null;
            }
#endif
        }
            public void Execute(int i)
            {
                var rootInv            = localToWorldArray[i].Value;
                var boneTransforms     = boneTransformArray[entities[i]].Reinterpret <float4x4>().AsNativeArray();
                var deformableVertices = deformedArray[entities[i]].Reinterpret <float3>().AsNativeArray();

                SpriteSkinUtility.Deform(rootInv, vertices, boneWeights, boneTransforms, bindPoses, deformableVertices);
            }
Exemple #6
0
 internal bool GetSpriteSkinBatchData(ref SpriteSkinBatchData data)
 {
     if (m_CurrentDeformSprite != sprite)
     {
         DeactivateSkinning();
         m_CurrentDeformSprite = sprite;
     }
     if (isValid)
     {
         Profiler.BeginSample("SpriteSkin.UpdateBounds");
         SpriteSkinUtility.UpdateBounds(this);
         Profiler.EndSample();
         data.sprite        = sprite;
         data.boneTransform = boneTransforms;
         Profiler.BeginSample("SpriteSkin.worldToLocalMatrix");
         data.worldToLocalMatrix = gameObject.transform.worldToLocalMatrix;
         Profiler.EndSample();
         return(true);
     }
     return(false);
 }