void ReportObjectStats(PlacementStatics placementStatics, NativeList <PlacedObject> placedObjectBoundingBoxes, GameObject cameraObject)
    {
        var objectStates = new JArray();

        for (int i = 0; i < placedObjectBoundingBoxes.Length; i++)
        {
            var placedObject = placedObjectBoundingBoxes[i];
            var labeling     = m_ParentForeground.transform.GetChild(placedObject.PrefabIndex).GetChild(0).gameObject.GetComponent <Labeling>();
            int labelId;
            if (placementStatics.IdLabelConfig.TryGetMatchingConfigurationEntry(labeling, out IdLabelEntry labelEntry))
            {
                labelId = labelEntry.id;
            }
            else
            {
                labelId = -1;
            }

            var jObject = new JObject();
            jObject["label_id"] = labelId;
            var rotationEulerAngles = (float3)placedObject.Rotation.eulerAngles;
            jObject["rotation"] = new JRaw($"[{rotationEulerAngles.x}, {rotationEulerAngles.y}, {rotationEulerAngles.z}]");
            objectStates.Add(jObject);
        }
        DatasetCapture.ReportMetric(m_ForegroundPlacementInfoDefinition, objectStates.ToString(Formatting.Indented));
    }
    void PlaceOccludingObjects(
        GameObject[] objectsToPlace, Camera camera, PlacementStatics statics, NativeList <PlacedObject> placedObjects)
    {
        var textures = statics.BackgroundImages;

        if (m_OccludingObjectCache == null)
        {
            m_OccludingObjectCache = new GameObjectOneWayCache(m_ParentOccluding.transform, objectsToPlace);
        }

        m_OccludingObjectCache.ResetAllObjects();
        var occludingObjectBounds = ComputeObjectBounds(objectsToPlace);

        var materialPropertyBlock  = new MaterialPropertyBlock();
        var placedOccludingObjects = new NativeArray <PlacedObject>(placedObjects.Length, Allocator.TempJob);
        var placementRegion        = ObjectPlacementUtilities.ComputePlacementRegion(camera, k_OccludingLayerDistance);

        using (s_ComputePlacements.Auto())
        {
            var job = new ComputeOccludingObjectPlacements()
            {
                OccludingObjectBounds   = occludingObjectBounds,
                ImageCoordinates        = placementRegion,
                Transformer             = new WorldToScreenTransformer(camera),
                PlacedForegroundObjects = placedObjects,
                RandomSeed             = m_Rand.NextUInt(),
                PlacedOccludingObjects = placedOccludingObjects,
                ScalingMin             = statics.ScalingMin,
                ScalingSize            = statics.ScalingSize
            };
            job.Schedule(placedObjects.Length, 10).Complete();
        }

        using (s_SetupObjects.Auto())
        {
            foreach (var placedOccludingObject in placedOccludingObjects)
            {
                if (placedOccludingObject.PrefabIndex < 0)
                {
                    continue;
                }

                var prefab        = objectsToPlace[placedOccludingObject.PrefabIndex];
                var objectToPlace = m_OccludingObjectCache.GetOrInstantiate(prefab);
                objectToPlace.layer = m_OccludingLayer;

                var meshRenderer = objectToPlace.GetComponentInChildren <MeshRenderer>();
                meshRenderer.GetPropertyBlock(materialPropertyBlock);
                ObjectPlacementUtilities.CreateRandomizedHue(materialPropertyBlock, statics.OccludingHueMaxOffset, ref m_Rand);
                materialPropertyBlock.SetTexture("_BaseMap", textures[m_Rand.NextInt(textures.Length)]);
                meshRenderer.SetPropertyBlock(materialPropertyBlock);

                objectToPlace.transform.localPosition = placedOccludingObject.Position;
                objectToPlace.transform.localRotation = placedOccludingObject.Rotation;
                objectToPlace.transform.localScale    = Vector3.one * placedOccludingObject.Scale;
            }
        }
        placedOccludingObjects.Dispose();
        occludingObjectBounds.Dispose();
    }
 void EnsureObjectGroupsExist(PlacementStatics statics, int objectGroupIndex)
 {
     while (m_ParentForeground.transform.childCount < statics.ForegroundPrefabs.Length * (objectGroupIndex + 1))
     {
         foreach (var foregroundPrefab in statics.ForegroundPrefabs)
         {
             var newParent = new GameObject();
             newParent.transform.parent = m_ParentForeground.transform;
             var gameObject = Object.Instantiate(foregroundPrefab, newParent.transform);
             var bounds     = ObjectPlacementUtilities.ComputeBounds(gameObject);
             gameObject.transform.localPosition -= bounds.center;
             gameObject.layer = m_ForegroundLayer;
             newParent.name   = gameObject.name;
             ObjectPlacementUtilities.SetMeshRenderersEnabledRecursive(gameObject, false);
         }
     }
 }
    void EnsureObjectGroupsExist(PlacementStatics statics, int objectGroupIndex)
    {
        while (m_ParentForeground.transform.childCount < statics.ForegroundPrefabs.Length * (objectGroupIndex + 1))
        {
            foreach (var foregroundPrefab in statics.ForegroundPrefabs)
            {
                var gameObject = Object.Instantiate(foregroundPrefab, m_ParentForeground.transform);
                gameObject.layer = m_ForegroundLayer;
                ObjectPlacementUtilities.SetMeshRenderersEnabledRecursive(gameObject, false);
                var labeling = gameObject.AddComponent <Labeling>();
                var name     = foregroundPrefab.name;
                if (s_NameRegex.IsMatch(name))
                {
                    name = name.Substring(0, name.Length - 3);
                }

                labeling.labels.Add(name);
            }
        }
    }
    NativeList <PlacedObject> PlaceObjects(Camera camera, PlacementStatics statics, ref CurriculumState curriculumState)
    {
        var placedObjectBoundingBoxes = new NativeList <PlacedObject>(500, Allocator.TempJob);
        var objectBounds = ComputeObjectBounds(statics.ForegroundPrefabs);

        var localCurriculumState = curriculumState;
        var curriculumStatePtr   = (CurriculumState *)UnsafeUtility.AddressOf(ref localCurriculumState);
        var localRandom          = m_Rand;
        var randomPtr            = (Random *)UnsafeUtility.AddressOf(ref localRandom);
        var placementRegion      = ObjectPlacementUtilities.ComputePlacementRegion(camera, k_ForegroundLayerDistance);

        using (s_ComputePlacements.Auto())
        {
            var computePlacementsJob = new ComputePlacementsJob()
            {
                CurriculumStatePtr     = curriculumStatePtr,
                Transformer            = new WorldToScreenTransformer(camera),
                ImageCoordinates       = placementRegion,
                ObjectBounds           = objectBounds,
                PlaceObjects           = placedObjectBoundingBoxes,
                RandomPtr              = randomPtr,
                NativePlacementStatics = new NativePlacementStatics
                {
                    ForegroundPrefabCount = statics.ForegroundPrefabs.Length,
                    MaxForegroundObjects  = statics.MaxForegroundObjectsPerFrame,
                    InPlaneRotations      = statics.InPlaneRotations,
                    OutOfPlaneRotations   = statics.OutOfPlaneRotations,
                    ScaleFactors          = statics.ScaleFactors
                }
            };
            computePlacementsJob.Run();
            curriculumState = *computePlacementsJob.CurriculumStatePtr;
            m_Rand          = *computePlacementsJob.RandomPtr;
        }

        using (s_SetupObjects.Auto())
        {
            int objectGroupIndex = 0;
            foreach (var placedObject in placedObjectBoundingBoxes)
            {
                EnsureObjectGroupsExist(statics, objectGroupIndex);
                var gameObject = m_ParentForeground.transform.GetChild(placedObject.PrefabIndex + objectGroupIndex * statics.ForegroundPrefabs.Length).gameObject;

                gameObject.transform.localRotation = placedObject.Rotation;

                gameObject.transform.localScale =
                    Vector3.one * placedObject.Scale;
                gameObject.transform.localPosition = placedObject.Position;

                ObjectPlacementUtilities.SetMeshRenderersEnabledRecursive(gameObject, true);

                if (placedObject.PrefabIndex == statics.ForegroundPrefabs.Length - 1)
                {
                    objectGroupIndex++;
                }
            }
        }

        objectBounds.Dispose();

        return(placedObjectBoundingBoxes);
    }
Beispiel #6
0
    void Start()
    {
        m_ResourceDirectoriesEntity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity(typeof(ResourceDirectories));
        World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentData(m_ResourceDirectoriesEntity, new ResourceDirectories
        {
            ForegroundResourcePath = ForegroundObjectResourcesDirectory,
            BackgroundResourcePath = BackgroundObjectResourcesDirectory
        });
        var foregroundObjects = Resources.LoadAll <GameObject>(ForegroundObjectResourcesDirectory);
        var backgroundObjects = Resources.LoadAll <GameObject>(BackgroundObjectResourcesDirectory);
        var backgroundImages  = Resources.LoadAll <Texture2D>(BackgroundImageResourcesDirectory);

        if (foregroundObjects.Length == 0)
        {
            Debug.LogError($"No Prefabs of FBX files found in foreground object directory \"{ForegroundObjectResourcesDirectory}\".");
            return;
        }
        if (backgroundObjects.Length == 0)
        {
            Debug.LogError($"No Prefabs of FBX files found in background object directory \"{BackgroundObjectResourcesDirectory}\".");
            return;
        }
        //TODO: Fill in CurriculumState from app params
        if (TryGetAppParamPathFromCommandLine(out string appParamPath))
        {
            var AppParamsJson = File.ReadAllText(appParamPath);
            AppParameters = JsonUtility.FromJson <AppParams>(AppParamsJson);
        }
        else if (!String.IsNullOrEmpty(Configuration.Instance.SimulationConfig.app_param_uri))
        {
            AppParameters = Configuration.Instance.GetAppParams <AppParams>();
        }

        Debug.Log($"{nameof(ProjectInitialization)}: Starting up. MaxFrames: {AppParameters.MaxFrames}, " +
                  $"scale factors {{{string.Join(", ", AppParameters.ScaleFactors)}}}");

        m_PlacementStatics = new PlacementStatics(
            AppParameters.MaxFrames,
            AppParameters.MaxForegroundObjectsPerFrame,
            AppParameters.ScalingMin,
            AppParameters.ScalingSize,
            AppParameters.OccludingHueMaxOffset,
            foregroundObjects,
            backgroundObjects,
            backgroundImages,
            ObjectPlacementUtilities.GenerateInPlaneRotationCurriculum(Allocator.Persistent),
            ObjectPlacementUtilities.GenerateOutOfPlaneRotationCurriculum(Allocator.Persistent),
            new NativeArray <float>(AppParameters.ScaleFactors, Allocator.Persistent));
        var appParamsMetricDefinition = SimulationManager.RegisterMetricDefinition(
            "app-params", description: "The values from the app-params used in the simulation. Only triggered once per simulation.", id: k_AppParamsMetricGuid);

        SimulationManager.ReportMetric(appParamsMetricDefinition, new[] { AppParameters });
        m_CurriculumStateEntity = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntity();
        World.DefaultGameObjectInjectionWorld.EntityManager.AddComponentData(
            m_CurriculumStateEntity, new CurriculumState());
        World.DefaultGameObjectInjectionWorld.EntityManager.AddComponentObject(
            m_CurriculumStateEntity, m_PlacementStatics);

        ValidateForegroundLabeling(foregroundObjects, PerceptionCamera);

#if !UNITY_EDITOR
        if (Debug.isDebugBuild && EnableProfileLog)
        {
            Debug.Log($"Enabling profile capture");
            m_ProfileLogPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "profileLog.raw");
            if (System.IO.File.Exists(m_ProfileLogPath))
            {
                System.IO.File.Delete(m_ProfileLogPath);
            }

            UnityEngine.Profiling.Profiler.logFile         = m_ProfileLogPath;
            UnityEngine.Profiling.Profiler.enabled         = true;
            UnityEngine.Profiling.Profiler.enableBinaryLog = true;
        }
#endif
        Manager.Instance.ShutdownNotification += CleanupState;

        //PerceptionCamera.renderedObjectInfosCalculated += OnRenderedObjectInfosCalculated;
    }
    NativeList <PlacedObject> PlaceObjects(Camera camera, PlacementStatics statics, NativeArray <Bounds> occludingObjectBounds, ref CurriculumState curriculumState)
    {
        var placedObjectBoundingBoxes = new NativeList <PlacedObject>(500, Allocator.TempJob);
        var objectBounds = ComputeObjectBounds(statics.ForegroundPrefabs);

        var localCurriculumState = curriculumState;
        var curriculumStatePtr   = (CurriculumState *)UnsafeUtility.AddressOf(ref localCurriculumState);
        var localRandom          = m_Rand;
        var randomPtr            = (Random *)UnsafeUtility.AddressOf(ref localRandom);
        var placementRegion      = ObjectPlacementUtilities.ComputePlacementRegion(camera, k_ForegroundLayerDistance);

        using (s_ComputePlacements.Auto())
        {
            var computePlacementsJob = new ComputePlacementsJob()
            {
                CurriculumStatePtr     = curriculumStatePtr,
                Transformer            = new WorldToScreenTransformer(camera),
                ImageCoordinates       = placementRegion,
                ObjectBounds           = objectBounds,
                OccludingObjectBounds  = occludingObjectBounds,
                PlaceObjects           = placedObjectBoundingBoxes,
                RandomPtr              = randomPtr,
                NativePlacementStatics = new NativePlacementStatics
                {
                    ForegroundPrefabCount = statics.ForegroundPrefabs.Length,
                    MaxForegroundObjects  = statics.MaxForegroundObjectsPerFrame,
                    InPlaneRotations      = statics.InPlaneRotations,
                    OutOfPlaneRotations   = statics.OutOfPlaneRotations,
                    ScaleFactors          = statics.ScaleFactors,
                    BackgroundObjectInForegroundChance = statics.BackgroundObjectInForegroundChance,
                }
            };
            computePlacementsJob.Run();
            curriculumState = *computePlacementsJob.CurriculumStatePtr;
            m_Rand          = *computePlacementsJob.RandomPtr;
        }

        using (s_SetupObjects.Auto())
        {
            var materialPropertyBlock = new MaterialPropertyBlock();
            int objectGroupIndex      = 0;
            foreach (var placedObject in placedObjectBoundingBoxes)
            {
                GameObject gameObject;
                if (placedObject.IsOccluding)
                {
                    gameObject = m_BackgroundInForegroundObjectCache.GetOrInstantiate(statics.BackgroundPrefabs[placedObject.PrefabIndex]);

                    var meshRenderer = gameObject.GetComponentInChildren <MeshRenderer>();
                    meshRenderer.GetPropertyBlock(materialPropertyBlock);
                    ObjectPlacementUtilities.CreateRandomizedHue(materialPropertyBlock, statics.OccludingHueMaxOffset, ref m_Rand);
                    materialPropertyBlock.SetTexture("_BaseMap", statics.BackgroundImages[m_Rand.NextInt(statics.BackgroundImages.Length)]);
                    meshRenderer.SetPropertyBlock(materialPropertyBlock);
                }
                else
                {
                    EnsureObjectGroupsExist(statics, objectGroupIndex);
                    gameObject = m_ParentForeground.transform.GetChild(placedObject.PrefabIndex + objectGroupIndex * statics.ForegroundPrefabs.Length).gameObject;
                }

                gameObject.transform.localRotation = placedObject.Rotation;
                gameObject.transform.localScale    = Vector3.one * placedObject.Scale;
                gameObject.transform.localPosition = placedObject.Position;

                ObjectPlacementUtilities.SetMeshRenderersEnabledRecursive(gameObject, true);

                if (placedObject.PrefabIndex == statics.ForegroundPrefabs.Length - 1)
                {
                    objectGroupIndex++;
                }
            }
        }

        objectBounds.Dispose();

        return(placedObjectBoundingBoxes);
    }