Code Monkey home page Code Monkey logo

Comments (4)

mkamalza avatar mkamalza commented on June 14, 2024

Hi,

The background placement Randomizer included with the Perception package uses Poisson disk sampling to select random positions within a given area. In order to make this process more organized, you can modify the corresponding part of the code to have it sample positions in a different manner. To do this, you would first need to create a new background randomizer by copying the code that's already there into a new C# class in your own project. The file is named BackgroundObjectPlacementRandomizer.cs. Just make sure to change the new class's name and title in the Add Randomizer menu (defined at the top) so that they are different from the stock class.

The part of the code responsible for position sampling and object placement is this:

protected override void OnIterationStart()
        {
            for (var i = 0; i < layerCount; i++)
            {
                var seed = SamplerState.NextRandomState();
                var placementSamples = PoissonDiskSampling.GenerateSamples(
                    placementArea.x, placementArea.y, separationDistance, seed);
                var offset = new Vector3(placementArea.x, placementArea.y, 0f) * -0.5f;
                foreach (var sample in placementSamples)
                {
                    var instance = m_GameObjectOneWayCache.GetOrInstantiate(prefabs.Sample());
                    instance.transform.position = new Vector3(sample.x, sample.y, separationDistance * i + depth) + offset;
                }
                placementSamples.Dispose();
            }
        }

Specifically, the lines that sample positions are:

var placementSamples = PoissonDiskSampling.GenerateSamples(
                    placementArea.x, placementArea.y, separationDistance, seed);

One thing you could do is to change those lines to have them output a specific order of positions within placementArea.

Alternately, you could also create complete backgrounds, save them as prefabs, and feed those into the background placement randomizer. With this approach, the background placement randomizer would just have to pick one of your pre-made backgrounds and instantiate it at the same position on each frame. Therefore, the position sampling code would be removed and the position would always be constant.

Let me know if that helps.

from com.unity.perception.

arjunrao796123 avatar arjunrao796123 commented on June 14, 2024

Thank you for the quick response. I'll try this and get back to you.

from com.unity.perception.

arjunrao796123 avatar arjunrao796123 commented on June 14, 2024

Hi,

The background placement Randomizer included with the Perception package uses Poisson disk sampling to select random positions within a given area. In order to make this process more organized, you can modify the corresponding part of the code to have it sample positions in a different manner. To do this, you would first need to create a new background randomizer by copying the code that's already there into a new C# class in your own project. The file is named BackgroundObjectPlacementRandomizer.cs. Just make sure to change the new class's name and title in the Add Randomizer menu (defined at the top) so that they are different from the stock class.

The part of the code responsible for position sampling and object placement is this:

protected override void OnIterationStart()
        {
            for (var i = 0; i < layerCount; i++)
            {
                var seed = SamplerState.NextRandomState();
                var placementSamples = PoissonDiskSampling.GenerateSamples(
                    placementArea.x, placementArea.y, separationDistance, seed);
                var offset = new Vector3(placementArea.x, placementArea.y, 0f) * -0.5f;
                foreach (var sample in placementSamples)
                {
                    var instance = m_GameObjectOneWayCache.GetOrInstantiate(prefabs.Sample());
                    instance.transform.position = new Vector3(sample.x, sample.y, separationDistance * i + depth) + offset;
                }
                placementSamples.Dispose();
            }
        }

Specifically, the lines that sample positions are:

var placementSamples = PoissonDiskSampling.GenerateSamples(
                    placementArea.x, placementArea.y, separationDistance, seed);

One thing you could do is to change those lines to have them output a specific order of positions within placementArea.

Alternately, you could also create complete backgrounds, save them as prefabs, and feed those into the background placement randomizer. With this approach, the background placement randomizer would just have to pick one of your pre-made backgrounds and instantiate it at the same position on each frame. Therefore, the position sampling code would be removed and the position would always be constant.

Let me know if that helps.

This was really helpful. I am new to Unity and I was able to understand what is happening in a better manner.

from com.unity.perception.

mkamalza avatar mkamalza commented on June 14, 2024

Glad to know it helped. I will go ahead and close this issue for now. Feel free to open a new one in case there is anything else to discuss.

from com.unity.perception.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.