Пример #1
0
            protected override void OnUpdate()
            {
                k_ChangesMarker.Begin();

                bool changeDetected = false;

                // Detect removed SharedMeshes
                Entities
                .WithNone <RenderMesh, DeformedEntity>()
                .WithStructuralChanges()
                .ForEach((Entity e, SharedMeshData meshData) =>
                {
                    Parent.MeshHashToInstanceCount[meshData.RenderMeshHash]--;
                    var count = Parent.MeshHashToInstanceCount[meshData.RenderMeshHash];

                    // Clean-up if this was the last instance
                    if (count == 0)
                    {
                        Parent.MeshHashToInstanceCount.Remove(meshData.RenderMeshHash);
                        m_MeshHashToSharedMeshData.Remove(meshData.RenderMeshHash);
#if ENABLE_COMPUTE_DEFORMATIONS
                        Parent.m_MeshHashToRenderMesh.Remove(meshData.RenderMeshHash);

                        totalSharedVertexCount     -= meshData.VertexCount;
                        totalBlendshapeVertexCount -= meshData.HasBlendShapes ? meshData.BlendShapeVertexCount : 0;
                        totalBoneWeightCount       -= meshData.HasSkinning ? meshData.BoneInfluencesCount : 0;
#endif
                    }

#if ENABLE_COMPUTE_DEFORMATIONS
                    if (meshData.HasBlendShapes)
                    {
                        EntityManager.RemoveComponent <BlendWeightBufferIndex>(e);
                    }
#endif

                    if (meshData.HasSkinning)
                    {
                        EntityManager.RemoveComponent <SkinMatrixBufferIndex>(e);
                    }

                    EntityManager.RemoveComponent <SharedMeshData>(e);
#if ENABLE_COMPUTE_DEFORMATIONS
                    EntityManager.RemoveComponent <DeformedMeshIndex>(e);
#endif

                    changeDetected = true;
                })
                .Run();

                // Detect new SharedMeshes
                Entities
                .WithAll <DeformedEntity>()
                .WithNone <SharedMeshData>()
                .WithStructuralChanges()
                .ForEach((Entity e, RenderMesh renderMesh) =>
                {
                    if (m_MeshHashToSharedMeshData.TryGetValue(renderMesh.GetHashCode(), out var meshData))
                    {
#if ENABLE_COMPUTE_DEFORMATIONS
                        EntityManager.AddComponent <DeformedMeshIndex>(e);

                        if (meshData.HasBlendShapes)
                        {
                            EntityManager.AddComponent <BlendWeightBufferIndex>(e);
                        }
#endif

                        if (meshData.HasSkinning)
                        {
                            EntityManager.AddComponent <SkinMatrixBufferIndex>(e);
                        }

                        EntityManager.AddSharedComponentData(e, meshData);
                        Parent.MeshHashToInstanceCount[meshData.RenderMeshHash]++;
                    }
                    else
                    {
                        var mesh     = renderMesh.mesh;
                        var meshHash = renderMesh.GetHashCode();
                        meshData     = new SharedMeshData
                        {
                            VertexCount     = mesh.vertexCount,
                            BlendShapeCount = mesh.blendShapeCount,
#if ENABLE_COMPUTE_DEFORMATIONS
                            BlendShapeVertexCount = mesh.blendShapeCount > 0 ? Parent.BlendShapeBufferManager.CalculateSparseBlendShapeVertexCount(mesh) : 0,
#else
                            BlendShapeVertexCount = 0,
#endif
                            BoneCount           = mesh.bindposes.Length,
                            BoneInfluencesCount = mesh.GetAllBoneWeights().Length,
                            RenderMeshHash      = meshHash,
                        };

#if ENABLE_COMPUTE_DEFORMATIONS
                        EntityManager.AddComponent <DeformedMeshIndex>(e);

                        if (meshData.HasBlendShapes)
                        {
                            EntityManager.AddComponent <BlendWeightBufferIndex>(e);
                        }
#endif

                        if (meshData.HasSkinning)
                        {
                            EntityManager.AddComponent <SkinMatrixBufferIndex>(e);
                        }

                        EntityManager.AddSharedComponentData(e, meshData);

                        Parent.MeshHashToInstanceCount.Add(meshHash, 1);
                        m_MeshHashToSharedMeshData.Add(meshHash, meshData);
#if ENABLE_COMPUTE_DEFORMATIONS
                        Parent.m_MeshHashToRenderMesh.Add(meshHash, renderMesh);

                        totalSharedVertexCount     += meshData.VertexCount;
                        totalBlendshapeVertexCount += meshData.HasBlendShapes ? meshData.BlendShapeVertexCount : 0;
                        totalBoneWeightCount       += meshData.HasSkinning ? meshData.BoneInfluencesCount : 0;

                        // A new shared mesh was detected. Force the buffers to be uploaded.
                        Parent.m_RebuildSharedMeshBuffers = true;
#endif
                    }

                    changeDetected = true;
                }).Run();

#if ENABLE_COMPUTE_DEFORMATIONS
                // Sanity check for desired SharedMesh data sizes
                Debug.Assert(totalSharedVertexCount >= 0);
                Debug.Assert(totalBlendshapeVertexCount >= 0);
                Debug.Assert(totalBoneWeightCount >= 0);
#endif

                k_ChangesMarker.End();

                // Early exit if no changes are detected
                if (!changeDetected)
                {
                    return;
                }

                k_ResizeMarker.Begin();

#if ENABLE_COMPUTE_DEFORMATIONS
                Parent.m_RebuildSharedMeshBuffers |= Parent.MeshBufferManager.ResizeSharedMeshBuffersIfRequired(totalSharedVertexCount);
                Parent.m_RebuildSharedMeshBuffers |= Parent.SkinningBufferManager.ResizeSharedBufferIfRequired(totalBoneWeightCount, totalSharedVertexCount);
                Parent.m_RebuildSharedMeshBuffers |= Parent.BlendShapeBufferManager.ResizeSharedBufferIfRequired(totalBlendshapeVertexCount, totalSharedVertexCount);
#endif

                k_ResizeMarker.End();
                k_OutputBuffer.Begin();

                k_OutputCountBuffer.Begin();
#if ENABLE_COMPUTE_DEFORMATIONS
                int deformedMeshVertexCount = 0;
                int blendShapeWeightCount   = 0;
#endif
                int skinMatrixCount = 0;

                m_SharedMeshQuery.CompleteDependency();

                Parent.UniqueSharedMeshData.Clear();
                EntityManager.GetAllUniqueSharedComponentData(Parent.UniqueSharedMeshData);
                foreach (var meshData in Parent.UniqueSharedMeshData)
                {
                    if (meshData.RenderMeshHash == 0)
                    {
                        continue;
                    }

                    int instanceCount = Parent.MeshHashToInstanceCount[meshData.RenderMeshHash];
#if ENABLE_COMPUTE_DEFORMATIONS
                    deformedMeshVertexCount += instanceCount * meshData.VertexCount;

                    if (meshData.HasBlendShapes)
                    {
                        blendShapeWeightCount += instanceCount * meshData.BlendShapeCount;
                    }
#endif

                    if (meshData.HasSkinning)
                    {
                        skinMatrixCount += instanceCount * meshData.BoneCount;
                    }
                }

#if ENABLE_COMPUTE_DEFORMATIONS
                totalDeformedVertexCount   = deformedMeshVertexCount;
                totalBlendShapeWeightCount = blendShapeWeightCount;
#endif
                totalSkinMatrixCount = skinMatrixCount;

                k_OutputCountBuffer.End();

                k_OutputResizeBuffer.Begin();

                Parent.SkinningBufferManager.ResizePassBufferIfRequired(totalSkinMatrixCount);
#if ENABLE_COMPUTE_DEFORMATIONS
                Parent.BlendShapeBufferManager.ResizePassBufferIfRequired(totalBlendshapeVertexCount);
                Parent.MeshBufferManager.ResizeAndPushDeformMeshBuffersIfRequired(totalDeformedVertexCount);
#endif
                k_OutputResizeBuffer.End();

                // Force the DeformedMesh layout to be updated.
                // As either; an deformed mesh has been removed, or a new one has been added.
                // Both result in a shift of indices.
                Parent.m_RebuildDeformedMeshBuffers = true;

                k_OutputBuffer.End();
            }
        protected override void OnUpdate()
        {
            m_ResizeBuffersSystem.Update();

#if ENABLE_COMPUTE_DEFORMATIONS
            k_PushSharedMeshData.Begin();

            if (m_RebuildSharedMeshBuffers)
            {
                m_PushSharedMeshDataSystem.Update();
            }

            k_PushSharedMeshData.End();
#endif

            if (m_RebuildDeformedMeshBuffers)
            {
                k_MaterialPropertyUpdate.Begin();

                // Layout Deformed Meshes in buffer
#if ENABLE_COMPUTE_DEFORMATIONS
                MeshHashToDeformedMeshIndex.Clear();
                MeshHashToBlendWeightIndex.Clear();
                uint deformedMeshOffset = 0;
                int  blendShapeOffset   = 0;
#endif
                MeshHashToSkinMatrixIndex.Clear();
                int skinMatrixOffset = 0;

                foreach (var meshData in UniqueSharedMeshData)
                {
                    if (meshData.RenderMeshHash == 0)
                    {
                        continue;
                    }

                    int instanceCount = MeshHashToInstanceCount[meshData.RenderMeshHash];

#if ENABLE_COMPUTE_DEFORMATIONS
                    MeshHashToDeformedMeshIndex.Add(meshData.RenderMeshHash, deformedMeshOffset);
                    deformedMeshOffset += (uint)instanceCount * (uint)meshData.VertexCount;

                    if (meshData.HasBlendShapes)
                    {
                        MeshHashToBlendWeightIndex.Add(meshData.RenderMeshHash, blendShapeOffset);
                        blendShapeOffset += instanceCount * meshData.BlendShapeCount;
                    }
#endif

                    if (meshData.HasSkinning)
                    {
                        MeshHashToSkinMatrixIndex.Add(meshData.RenderMeshHash, skinMatrixOffset);
                        skinMatrixOffset += instanceCount * meshData.BoneCount;
                    }
                }

                // Write deformed mesh index to material property
#if ENABLE_COMPUTE_DEFORMATIONS
                var deformedMeshIndexType = GetComponentTypeHandle <DeformedMeshIndex>();
                var blendWeightIndexType  = GetComponentTypeHandle <BlendWeightBufferIndex>();
#endif
                var skinMatrixIndexType = GetComponentTypeHandle <SkinMatrixBufferIndex>();
                var sharedMeshDataType  = GetSharedComponentTypeHandle <SharedMeshData>();

                m_BufferIndexQuery.CompleteDependency();


                using (var chunks = m_BufferIndexQuery.CreateArchetypeChunkArray(Allocator.TempJob))
                {
                    var skinMatrixInstancesMap = new NativeHashMap <int, int>(chunks.Length, Allocator.Temp);
#if ENABLE_COMPUTE_DEFORMATIONS
                    var deformedMeshInstancesMap     = new NativeHashMap <int, int>(chunks.Length, Allocator.Temp);
                    var blendShapeWeightInstancesMap = new NativeHashMap <int, int>(chunks.Length, Allocator.Temp);
#endif
                    foreach (var chunk in chunks)
                    {
                        var sharedMeshData = chunk.GetSharedComponentData(sharedMeshDataType, EntityManager);
#if ENABLE_COMPUTE_DEFORMATIONS
                        deformedMeshInstancesMap.TryGetValue(sharedMeshData.RenderMeshHash, out int count);

                        var deformedMeshIndices = chunk.GetNativeArray(deformedMeshIndexType);
                        var deformedMeshIndex   = MeshHashToDeformedMeshIndex[sharedMeshData.RenderMeshHash];
                        for (int i = 0; i < chunk.Count; i++)
                        {
                            var index = deformedMeshIndex + count + (i * sharedMeshData.VertexCount);
                            deformedMeshIndices[i] = new DeformedMeshIndex {
                                Value = (uint)index
                            };
                        }
                        if (count == 0)
                        {
                            deformedMeshInstancesMap.Add(sharedMeshData.RenderMeshHash, chunk.Count * sharedMeshData.VertexCount);
                        }
                        else
                        {
                            deformedMeshInstancesMap[sharedMeshData.RenderMeshHash] += chunk.Count * sharedMeshData.VertexCount;
                        }

                        if (sharedMeshData.HasBlendShapes)
                        {
                            blendShapeWeightInstancesMap.TryGetValue(sharedMeshData.RenderMeshHash, out int instanceCount);
                            var blendWeightIndices = chunk.GetNativeArray(blendWeightIndexType);
                            int blendShapeIndex    = MeshHashToBlendWeightIndex[sharedMeshData.RenderMeshHash];

                            for (int i = 0; i < chunk.Count; i++)
                            {
                                var index = blendShapeIndex + instanceCount + (i * sharedMeshData.BlendShapeCount);
                                blendWeightIndices[i] = new BlendWeightBufferIndex {
                                    Value = index
                                };
                            }

                            if (instanceCount == 0)
                            {
                                blendShapeWeightInstancesMap.Add(sharedMeshData.RenderMeshHash, chunk.Count * sharedMeshData.BlendShapeCount);
                            }
                            else
                            {
                                blendShapeWeightInstancesMap[sharedMeshData.RenderMeshHash] += chunk.Count * sharedMeshData.BlendShapeCount;
                            }
                        }
#endif

                        if (sharedMeshData.HasSkinning)
                        {
                            skinMatrixInstancesMap.TryGetValue(sharedMeshData.RenderMeshHash, out int instanceCount);
                            var skinMatrixIndices = chunk.GetNativeArray(skinMatrixIndexType);
                            int skinMatrixIndex   = MeshHashToSkinMatrixIndex[sharedMeshData.RenderMeshHash];

                            for (int i = 0; i < chunk.Count; i++)
                            {
                                var index = skinMatrixIndex + instanceCount + (i * sharedMeshData.BoneCount);
                                skinMatrixIndices[i] = new SkinMatrixBufferIndex {
                                    Value = index
                                };
                            }

                            if (instanceCount == 0)
                            {
                                skinMatrixInstancesMap.Add(sharedMeshData.RenderMeshHash, chunk.Count * sharedMeshData.BoneCount);
                            }
                            else
                            {
                                skinMatrixInstancesMap[sharedMeshData.RenderMeshHash] += chunk.Count * sharedMeshData.BoneCount;
                            }
                        }
                    }

                    skinMatrixInstancesMap.Dispose();
#if ENABLE_COMPUTE_DEFORMATIONS
                    deformedMeshInstancesMap.Dispose();
                    blendShapeWeightInstancesMap.Dispose();
#endif

                    m_RebuildDeformedMeshBuffers = false;
                }

                k_MaterialPropertyUpdate.End();
            }
        }
Пример #3
0
    protected override void OnUpdate()
    {
        // For more simple code creating it the update allow us to use it in data application
        NativeArray <Vector3> _velocitiesOutput = new NativeArray <Vector3>(RoundedUpObjectCount, Allocator.TempJob);

        m_Query.CompleteDependency();

        var refjob = new ReferencingFOData_Job();

        refjob.velWriter   = velocities;
        refjob.transWriter = translations;
        refjob.massWriter  = masses;

        refjob.MassTypeHandle        = GetComponentTypeHandle <PhysicsMass>(true);
        refjob.TranslationTypeHandle = GetComponentTypeHandle <Translation>(true);
        refjob.VelocityTypeHandle    = GetComponentTypeHandle <PhysicsVelocity>(true);

        refjob.Run(m_Query);

        var setCD_job = new SetupComputeData_Job();

        setCD_job._masses       = _masses;
        setCD_job._translations = _translations;
        setCD_job._velocities   = _velocities;

        setCD_job.velWriter   = velocities;
        setCD_job.transWriter = translations;
        setCD_job.massWriter  = masses;

        setCD_job.Execute();

        var setK_job = new SetupKernel_Job();

        setK_job.objectCount       = ObjectCount;
        setK_job.shader            = GM._ref.CS_Gravity;
        setK_job._masses           = _masses;
        setK_job._translations     = _translations;
        setK_job._velocities       = _velocities;
        setK_job._velocitiesOutput = velocitiesOutput;

        setK_job.Execute();

        //Debug.Log("Mass : " + masses[0]);
        //Debug.Log("Translations : " + translations[0]);
        //Debug.Log("Velocitie : " + velocities[0]);
        //Debug.Log("Mass : " + masses[1]);
        //Debug.Log("Translations : " + translations[1]);
        //Debug.Log("Velocitie : " + velocities[1]);
        //Debug.Log("Mass : " + masses[2]);
        //Debug.Log("Translations : " + translations[2]);
        //Debug.Log("Velocitie : " + velocities[2]);

        var launchK_job = new LaunchKernel_Job();

        launchK_job.RoundedUpObjectCount = RoundedUpObjectCount;
        launchK_job.shader            = GM._ref.CS_Gravity;
        launchK_job.velocitiesOutput  = velocitiesOutput;
        launchK_job._velocitiesOutput = _velocitiesOutput;


        //Check load Compute Data is ok
        //launchK_job.transOutput = _translations;

        launchK_job.Execute();

        Entities
        .WithAll <PhysicsMass, Translation>()
        .ForEach((int entityInQueryIndex, ref PhysicsVelocity vel) =>
        {
            vel.Linear = new float3(_velocitiesOutput[entityInQueryIndex].x, _velocitiesOutput[entityInQueryIndex].y, _velocitiesOutput[entityInQueryIndex].z);
        }).Run();
        _velocitiesOutput.Dispose();
    }