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); } } }
protected override JobHandle OnUpdate(JobHandle inputDeps) { if (m_CameraContainer == null) { m_CameraContainer = GameObject.Find("MainCamera"); if (m_CameraContainer == null) { return(inputDeps); } var tfCamera = m_CameraContainer.transform; m_ParentForeground.transform.SetPositionAndRotation( tfCamera.position + tfCamera.forward * k_ForegroundLayerDistance, Quaternion.identity); m_ParentOccluding.transform.SetPositionAndRotation( tfCamera.position + tfCamera.forward * k_OccludingLayerDistance, Quaternion.identity); } using (s_ClearGameObjects.Auto()) { for (var i = 0; i < m_ParentForeground.transform.childCount; i++) { var gameObject = m_ParentForeground.transform.GetChild(i).gameObject; ObjectPlacementUtilities.SetMeshRenderersEnabledRecursive(gameObject, false); } } if (m_ResourceDirectoriesQuery.CalculateEntityCount() != 1) { return(inputDeps); } var singletonEntity = m_CurriculumQuery.GetSingletonEntity(); var curriculumState = EntityManager.GetComponentData <CurriculumState>(singletonEntity); var statics = EntityManager.GetComponentObject <PlacementStatics>(singletonEntity); if (statics.ForegroundPrefabs == null || statics.ForegroundPrefabs.Length == 0) { return(inputDeps); } if (statics.BackgroundPrefabs == null || statics.BackgroundPrefabs.Length == 0) { return(inputDeps); } if (curriculumState.ScaleIndex >= statics.ScaleFactors.Length) { return(inputDeps); } var perceptionCamera = m_CameraContainer.GetComponent <PerceptionCamera>(); if (perceptionCamera != null) { perceptionCamera.SetPersistentSensorData("scale", statics.ScaleFactors[curriculumState.ScaleIndex]); } var camera = m_CameraContainer.GetComponent <Camera>(); NativeList <PlacedObject> placedObjectBoundingBoxes; using (s_PlaceObjects.Auto()) placedObjectBoundingBoxes = PlaceObjects(camera, statics, ref curriculumState); ReportObjectStats(placedObjectBoundingBoxes, m_CameraContainer); var occludingObjects = statics.BackgroundPrefabs; using (s_PlaceOccludingObjects.Auto()) PlaceOccludingObjects(occludingObjects, camera, statics, placedObjectBoundingBoxes); EntityManager.SetComponentData(singletonEntity, curriculumState); placedObjectBoundingBoxes.Dispose(); return(inputDeps); }
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); }
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); }