/// <inheritdoc/>
        protected override void OnUpdate()
        {
            var entityQuery = Entities.WithAll <IdAssignmentParameters>().ToEntityQuery();
            IdAssignmentParameters idAssignmentParameters;

            if (entityQuery.CalculateEntityCount() == 1)
            {
                idAssignmentParameters = entityQuery.GetSingleton <IdAssignmentParameters>();
            }
            else
            {
                idAssignmentParameters = new IdAssignmentParameters {
                    idStart = 1, idStep = 1
                }
            };

            var entityCount = Entities.WithAll <Labeling, GroundTruthInfo>().ToEntityQuery().CalculateEntityCount();

            if (entityCount == 0)
            {
                m_CurrentObjectIndex = -1;
            }

            Entities.WithNone <GroundTruthInfo>().ForEach((Entity e, Labeling labeling) =>
            {
                var objectIndex = (uint)Interlocked.Increment(ref m_CurrentObjectIndex);
                var instanceId  = idAssignmentParameters.idStart + objectIndex * idAssignmentParameters.idStep;
                var gameObject  = labeling.gameObject;
                if (!m_MaterialPropertyBlocks.IsValueCreated)
                {
                    m_MaterialPropertyBlocks.Value = new MaterialPropertyBlock();
                }

                InitGameObjectRecursive(gameObject, m_MaterialPropertyBlocks.Value, labeling, instanceId);
                EntityManager.AddComponentData(e, new GroundTruthInfo
                {
                    instanceId = instanceId
                });
                labeling.SetInstanceId(instanceId);
            });
        }

        void InitGameObjectRecursive(GameObject gameObject, MaterialPropertyBlock mpb, Labeling labeling, uint instanceId)
        {
            var renderer = (Renderer)gameObject.GetComponent <MeshRenderer>();

            if (renderer == null)
            {
                renderer = gameObject.GetComponent <SkinnedMeshRenderer>();
            }

            if (renderer != null)
            {
                renderer.GetPropertyBlock(mpb);
                foreach (var pass in m_ActiveGenerators)
                {
                    pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
                }

                renderer.SetPropertyBlock(mpb);
            }

            for (var i = 0; i < gameObject.transform.childCount; i++)
            {
                var child = gameObject.transform.GetChild(i).gameObject;
                if (child.GetComponent <Labeling>() != null)
                {
                    continue;
                }

                InitGameObjectRecursive(child, mpb, labeling, instanceId);
            }
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        protected override void OnUpdate()
        {
            var entityQuery = Entities.WithAll <IdAssignmentParameters>().ToEntityQuery();
            IdAssignmentParameters idAssignmentParameters;

            if (entityQuery.CalculateEntityCount() == 1)
            {
                idAssignmentParameters = entityQuery.GetSingleton <IdAssignmentParameters>();
            }
            else
            {
                idAssignmentParameters = new IdAssignmentParameters {
                    idStart = 1, idStep = 1
                }
            };

            var entityCount = Entities.WithAll <Labeling, GroundTruthInfo>().ToEntityQuery().CalculateEntityCount();

            if (entityCount == 0)
            {
                m_CurrentObjectIndex = -1;
            }

            Entities.WithNone <GroundTruthInfo>().ForEach((Entity e, Labeling labeling) =>
            {
                var objectIndex = (uint)Interlocked.Increment(ref m_CurrentObjectIndex);
                var instanceId  = idAssignmentParameters.idStart + objectIndex * idAssignmentParameters.idStep;
                var gameObject  = labeling.gameObject;
                if (!m_MaterialPropertyBlocks.IsValueCreated)
                {
                    m_MaterialPropertyBlocks.Value = new MaterialPropertyBlock();
                }

                InitGameObjectRecursive(gameObject, m_MaterialPropertyBlocks.Value, labeling, instanceId);
                Vector3 p_v       = gameObject.transform.position;
                Quaternion r_v    = gameObject.transform.rotation;
                float [] position = new float[] { p_v.x, p_v.y, p_v.z, r_v.x, r_v.y, r_v.z, };
                position_dictionary.Add(instanceId, position);
                EntityManager.AddComponentData(e, new GroundTruthInfo
                {
                    instanceId = instanceId
                });
                labeling.SetInstanceId(instanceId);
            });
        }

        void InitGameObjectRecursive(GameObject gameObject, MaterialPropertyBlock mpb, Labeling labeling, uint instanceId)
        {
            var renderer = (Renderer)gameObject.GetComponent <MeshRenderer>();

            if (renderer == null)
            {
                renderer = gameObject.GetComponent <SkinnedMeshRenderer>();
            }

            if (renderer != null)
            {
                renderer.GetPropertyBlock(mpb);
                foreach (var pass in m_ActiveGenerators)
                {
                    pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
                }

                renderer.SetPropertyBlock(mpb);

                var materialCount = renderer.materials.Length;
                for (int i = 0; i < materialCount; i++)
                {
                    renderer.GetPropertyBlock(mpb, i);
                    //Only apply to individual materials if there is already a MaterialPropertyBlock on it
                    if (!mpb.isEmpty)
                    {
                        foreach (var pass in m_ActiveGenerators)
                        {
                            pass.SetupMaterialProperties(mpb, renderer, labeling, instanceId);
                        }

                        renderer.SetPropertyBlock(mpb, i);
                    }
                }
            }

            for (var i = 0; i < gameObject.transform.childCount; i++)
            {
                var child = gameObject.transform.GetChild(i).gameObject;
                if (child.GetComponent <Labeling>() != null)
                {
                    continue;
                }

                InitGameObjectRecursive(child, mpb, labeling, instanceId);
            }
        }