static CurriculumState NextCurriculumState(CurriculumState curriculumState, NativePlacementStatics statics)
    {
        curriculumState.PrefabIndex++;
        if (curriculumState.PrefabIndex < statics.ForegroundPrefabCount)
        {
            return(curriculumState);
        }

        curriculumState.PrefabIndex = 0;
        curriculumState.OutOfPlaneRotationIndex++;
        if (curriculumState.OutOfPlaneRotationIndex < statics.OutOfPlaneRotations.Length)
        {
            return(curriculumState);
        }

        curriculumState.OutOfPlaneRotationIndex = 0;
        curriculumState.InPlaneRotationIndex++;
        if (curriculumState.InPlaneRotationIndex < statics.InPlaneRotations.Length)
        {
            return(curriculumState);
        }

        curriculumState.InPlaneRotationIndex = 0;
        curriculumState.ScaleIndex++;

        return(curriculumState);
    }
Example #2
0
    public static Quaternion ComposeForegroundRotation(CurriculumState state, NativeArray <Quaternion> outOfPlaneRotations, NativeArray <Quaternion> inPlaneRotations)
    {
        var outRotate = outOfPlaneRotations[state.OutOfPlaneRotationIndex];
        var inRotate  = inPlaneRotations[state.InPlaneRotationIndex];

        return(inRotate * outRotate);
    }
    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);
    }