/// <inheritdoc/>
        public override ISensor[] CreateSensors()
        {
            DetectableGameObject.ClearCache();
            ValidateGameObjectSettings();

            if (m_GameObjectSettingsMeta.DetectableTags.Count > 0)
            {
                // Unlike GridSensorComponentBase, this component creates its own GridBuffer.
                // We use a ColorGridBuffer in order to support PNG compression.
                // GridShape is set by the 2D and 3D subcomponents prior to creating the buffer.
                GridBuffer = new ColorGridBuffer(GridShape);

                var sensors = base.CreateSensors();

                if (Application.isPlaying)
                {
                    // TODO It should be clearer which sensor features are enabled
                    // in editor vs play mode. Detector/Encoder code is supposed to
                    // be runtime only.
                    m_GridSensor.EnableAutoDetection(CreateDetector(), CreateEncoder());
                }

                return(sensors);
            }

            throw new UnityAgentsException("No detectable tags found! " +
                                           "Add a detectable gameobject to the inspector settings.");
        }
Esempio n. 2
0
 public override void Reset()
 {
     if (m_ClearCacheOnReset)
     {
         DetectableGameObject.ClearCache();
     }
 }
Esempio n. 3
0
        protected void ParseColliders(
            int numFound,
            DetectionConstraint constraint,
            Matrix4x4 worldToLocalMatrix)
        {
            Vector3 sensorPos = m_Transform.position;

            for (int i = 0; i < numFound; i++)
            {
                if (m_Settings.IsDetectableTag(
                        m_ColliderBuffer[i].tag,
                        out PointDetectionType type))
                {
                    var detectable = DetectableGameObject.GetCached(m_ColliderBuffer[i]);
                    // Need to filter out compound collider duplicate results,
                    // as each collider is a key in DetectableGameObject's cache.
                    // We also ignore the sensor owner if there is a
                    // DetectableGameObject parent to the sensor.
                    if (detectable != m_Owner && !Result.Contains(detectable))
                    {
                        m_NormPoints.Clear();
                        m_WorldPoints.Clear();

                        switch (type)
                        {
                        case PointDetectionType.Position:
                            m_WorldPoints.Add(detectable.GetWorldPosition());
                            break;

                        case PointDetectionType.ClosestPoint:
                            m_WorldPoints.Add(detectable.GetClosestWorldPoint(sensorPos));
                            break;

                        case PointDetectionType.Shape:
                            float normDistance = constraint.GetNormalizedDistance(
                                worldToLocalMatrix.MultiplyPoint3x4(
                                    detectable.GetClosestWorldPoint(sensorPos)));
                            m_WorldPoints.AddRange(
                                detectable.GetShapeWorldPoints(normDistance));
                            break;
                        }

                        for (int j = 0, c = m_WorldPoints.Count; j < c; j++)
                        {
                            if (constraint.ContainsPoint(
                                    worldToLocalMatrix.MultiplyPoint3x4(m_WorldPoints[j]),
                                    out Vector3 normPoint))
                            {
                                m_NormPoints.Add(normPoint);
                            }
                        }

                        if (m_NormPoints.Count > 0)
                        {
                            Result.Add(detectable, m_NormPoints);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Invoked on sensor reset at the end of each episode.
 /// </summary>
 public void OnSensorReset()
 {
     if (m_ClearCacheOnReset)
     {
         DetectableGameObject.ClearCache();
     }
 }
Esempio n. 5
0
        protected void ParseColliders(int n, Constraint constraint, Matrix4x4 worldToLocalMatrix)
        {
            Vector3 sensorPos = m_Transform.position;

            for (int i = 0; i < n; i++)
            {
                if (m_Settings.IsDetectableTag(m_Buffer[i].tag, out ColliderDetectionType type))
                {
                    var detectable = DetectableGameObject.GetCached(m_Buffer[i]);
                    // Need to filter out compound collider duplicate results,
                    // as each collider is a key in DetectableGameObject's cache.
                    // NOTE
                    // We also skip the sensor owner if there's a DetectableGameObject
                    // parent to the sensor.
                    // Although the owner could be excluded by setting the minimum detection
                    // distance large enough for 3D, it's better to avoid checking the
                    // owner's points at every step in the first place.
                    if (detectable != m_Owner && !Result.Contains(detectable))
                    {
                        m_TmpNormPoints.Clear();
                        m_TmpWorldPoints.Clear();

                        switch (type)
                        {
                        case ColliderDetectionType.Position:
                            m_TmpWorldPoints.Add(detectable.GetPosition());
                            break;

                        case ColliderDetectionType.ClosestPoint:
                            m_TmpWorldPoints.Add(detectable.GetClosestPoint(sensorPos));
                            break;

                        case ColliderDetectionType.Shape:
                            m_TmpWorldPoints.AddRange(detectable.GetShapePoints(
                                                          constraint.NormalizeDistance(
                                                              worldToLocalMatrix.MultiplyPoint3x4(
                                                                  detectable.GetPosition()))));
                            break;
                        }

                        for (int j = 0, c = m_TmpWorldPoints.Count; j < c; j++)
                        {
                            if (constraint.ContainsPoint(
                                    worldToLocalMatrix.MultiplyPoint3x4(m_TmpWorldPoints[j]),
                                    out Vector3 normalized))
                            {
                                m_TmpNormPoints.Add(normalized);
                            }
                        }

                        if (m_TmpNormPoints.Count > 0)
                        {
                            Result.Add(detectable, m_TmpNormPoints);
                        }
                    }
                }
            }
        }
Esempio n. 6
0
            = new Dictionary <Collider, DetectableGameObject>(); // capacity?

        private static void AddToCache(Collider cld, DetectableGameObject obj)
        {
            if (!s_SharedCache.ContainsKey(cld))
            {
                s_SharedCache.Add(cld, obj);
            }
            else
            {
                Debug.LogWarning("Collider already added to cache " + cld);
            }
        }
 private void TryAddDetectableObject()
 {
     if (m_TmpObjectToAdd != null)
     {
         if (m_GameObjectSettingsMeta.TryAddDetectableObject(
                 m_GameObjectSettingsList, m_TmpObjectToAdd, DetectorSpaceType))
         {
             ValidateGameObjectSettings();
         }
         m_TmpObjectToAdd = null;
     }
 }
        /// <summary>
        /// Adds a <see cref="DetectableGameObject"/> to the settings.
        /// </summary>
        /// <param name="settingsList">List of <see cref="GameObjectSettings"/></param>
        /// <param name="obj"><see cref="DetectableGameObject"/> to add</param>
        /// <param name="detectorSpaceType"><see cref="DetectorSpaceType"/>,
        /// box (2D) or sphere (3D)</param>
        /// <returns>If <see cref="GameObjectSettings"/> were created for
        /// <see cref="DetectableGameObject"/></returns>
        public bool TryAddDetectableObject(
            List <GameObjectSettings> settingsList,
            DetectableGameObject obj,
            DetectorSpaceType detectorSpaceType)
        {
            if (!m_Tags.Contains(obj.Tag))
            {
                settingsList.Add(new GameObjectSettings(obj, detectorSpaceType));
                return(true);
            }

            Debug.LogWarning($"Tag '{obj.Tag}' already added, tags must be distinct.");
            return(false);
        }
 public GameObjectSettings(DetectableGameObject obj)
 {
     Detectable = obj;
 }
Esempio n. 10
0
 /// <summary>
 /// Creates the <see cref="GameObjectSettings"/> instance.
 /// </summary>
 /// <param name="detectable"><see cref="DetectableGameObject"/>
 /// the <see cref="GameObjectSettings"/> apply to</param>
 public GameObjectSettings(DetectableGameObject detectable, DetectorSpaceType detectorSpaceType)
 {
     Detectable          = detectable;
     m_DetectorSpaceType = detectorSpaceType;
 }