protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            m_UniqueSpriteComponents.Clear();
            EntityManager.GetAllUniqueSharedComponentData(m_UniqueSpriteComponents);

            var spriteComponentCount = m_UniqueSpriteComponents.Count;
            var returnHandle         = inputDeps;

            for (var i = 0; i < spriteComponentCount; i++)
            {
                var spriteComponent = m_UniqueSpriteComponents[i];
                var sprite          = spriteComponent.Value;
                var entityCount     = 0;

                if (sprite != null)
                {
                    m_ComponentGroup.SetFilter(spriteComponent);
                    var filteredEntities = m_ComponentGroup.ToEntityArray(Allocator.TempJob);

                    entityCount = filteredEntities.Length;
                    if (entityCount > 0)
                    {
                        var skinJob = new SkinJob
                        {
                            entities           = filteredEntities,
                            vertices           = sprite.GetVertexAttribute <Vector3>(UnityEngine.Rendering.VertexAttribute.Position).SliceWithStride <float3>(),
                            boneWeights        = sprite.GetVertexAttribute <BoneWeight>(UnityEngine.Rendering.VertexAttribute.BlendWeight),
                            bindPoses          = new NativeSlice <Matrix4x4>(sprite.GetBindPoses()).SliceWithStride <float4x4>(),
                            localToWorldArray  = m_ComponentGroup.ToComponentDataArray <WorldToLocal>(Allocator.TempJob),
                            boneTransformArray = GetBufferFromEntity <BoneTransform>(),
                            deformedArray      = GetBufferFromEntity <Vertex>()
                        };

                        returnHandle = skinJob.Schedule(entityCount, 4, returnHandle);
                    }
                    else
                    {
                        filteredEntities.Dispose();
                    }
                }
            }

            var system = World.GetOrCreateSystem <EndPresentationEntityCommandBufferSystem>();

            system.AddJobHandleForProducer(returnHandle);

            return(returnHandle);
        }
Example #2
0
        internal static JobHandle Deform(NativeSlice <Vector3> inputVertices, NativeArray <BoneWeight> boneWeights, Matrix4x4 worldToLocalMatrix,
                                         NativeArray <Matrix4x4> bindPoses, NativeArray <Matrix4x4> transformMatrices, NativeArray <Vector3> outputVertices)
        {
            if (bindPoses.Length != transformMatrices.Length)
            {
                throw new InvalidOperationException("Invalid TransformMatrices array length.");
            }
            if (boneWeights.Length != inputVertices.Length)
            {
                throw new InvalidOperationException("Invalid BoneWeight array length");
            }
            if (outputVertices.Length != inputVertices.Length)
            {
                throw new InvalidOperationException("Invalid output Vertices array length");
            }

            var skinningMatrices = new NativeArray <Matrix4x4>(transformMatrices.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            var boneJob = new BoneJob()
            {
                rootInv   = worldToLocalMatrix,
                bindPoses = bindPoses,
                bones     = transformMatrices,
                output    = skinningMatrices
            };

            var skinJob = new SkinJob()
            {
                influenceCount = 4,
                influences     = boneWeights,
                vertices       = inputVertices,
                bones          = skinningMatrices,
                output         = outputVertices
            };

            return(skinJob.Schedule(boneJob.Schedule()));
        }
Example #3
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            m_Jobs.Clear();
            m_UniqueSpriteComponents.Clear();
            EntityManager.GetAllUniqueSharedComponentData(m_UniqueSpriteComponents);

            var spriteComponentCount = m_UniqueSpriteComponents.Count;
            var entitiesPerSprite    = new NativeArray <int>(spriteComponentCount, Allocator.Temp);

            m_Jobs.Capacity = spriteComponentCount;

            for (var i = 0; i < spriteComponentCount; i++)
            {
                var spriteComponent = m_UniqueSpriteComponents[i];
                var sprite          = spriteComponent.Value;
                var job             = default(SkinJob);
                var entityCount     = 0;

                if (sprite != null)
                {
                    m_ComponentGroup.SetFilter(spriteComponent);
                    var filteredEntities = m_ComponentGroup.ToEntityArray(Allocator.TempJob);

                    entityCount          = filteredEntities.Length;
                    entitiesPerSprite[i] = entityCount;
                    if (entityCount > 0)
                    {
                        job = new SkinJob
                        {
                            entities           = filteredEntities,
                            vertices           = sprite.GetVertexAttribute <Vector3>(UnityEngine.Rendering.VertexAttribute.Position).SliceWithStride <float3>(),
                            boneWeights        = sprite.GetVertexAttribute <BoneWeight>(UnityEngine.Rendering.VertexAttribute.BlendWeight),
                            bindPoses          = new NativeSlice <Matrix4x4>(sprite.GetBindPoses()).SliceWithStride <float4x4>(),
                            localToWorldArray  = m_ComponentGroup.ToComponentDataArray <WorldToLocal>(Allocator.TempJob),
                            boneTransformArray = GetBufferFromEntity <BoneTransform>(),
                            deformedArray      = GetBufferFromEntity <Vertex>()
                        };
                        m_Jobs.Add(job);
                    }
                    else
                    {
                        filteredEntities.Dispose();
                    }
                }
            }


            if (m_Jobs.Count > 0)
            {
                var jobHandles = new NativeArray <JobHandle>(m_Jobs.Count, Allocator.Temp);
                var prevHandle = inputDeps;

                var jobIndex = 0;
                for (var i = 0; i < entitiesPerSprite.Length; ++i)
                {
                    if (entitiesPerSprite[i] > 0)
                    {
                        jobHandles[jobIndex] = m_Jobs[jobIndex].Schedule(entitiesPerSprite[i], 4, prevHandle);
                        prevHandle           = jobHandles[jobIndex];
                        ++jobIndex;
                    }
                }

                var combinedHandle = JobHandle.CombineDependencies(jobHandles);
                jobHandles.Dispose();
                entitiesPerSprite.Dispose();

                var system = World.GetOrCreateSystem <EndPresentationEntityCommandBufferSystem>();
                system.AddJobHandleForProducer(combinedHandle);

                return(combinedHandle);
            }

            return(inputDeps);
        }