Spawn objects within a certain path/countour/object

Hi everyone,

I have a script that creates a grid of squares after being given a width and height and how many rows and columns I want for it. Works great to make rectangular grids, but I want to be able to fill an area with an arbitrary shape determined, ideally, by a surrounding object.

My code works well if I instantiate the objects inside a rectangular tube, but if the tube was , for example, circular, I have no idea how this would work.

This is the code I have at the moment:

    public float areaWidth;
    public float areaHeight;
    public int numberOfBlocksX;
    public int numberOfBlocksY;
    public float factor;

    private Vector3 blockScale;
    private GameObject tempBlock;
 
    void Start () {
        int i=0;
        int j=0;
        
        //Time to create all the blocks we need.
        while (i<(numberOfBlocksX*numberOfBlocksY)) {
            GameObject block = Instantiate (blockType) as GameObject;
            block.name = "block"+i;
            block.transform.localScale = blockScale*factor; 
            i++;
        }
    
        //Now let's position those blocks on a grid, row by row
        for (i=0; i<numberOfBlocksY; i++) {
            for (j=0; j<numberOfBlocksX; j++){
                tempBlock=GameObject.Find("block"+((j*numberOfBlocksY)+i));//This is how to traverse my self made "matrix".
                Vector3 tempPos;
                tempPos.x=(blockScale.x/2)+(blockScale.x*j)-(areaWidth/2);
                tempPos.y=(blockScale.y/2)+(blockScale.y*i)-(areaHeight/2);
                tempPos.z=0.0f;
                tempBlock.transform.position = tempPos;
            }
        }
    
    }

I believe I would have to do quite the rewrite to this to make it approximate other shapes, as this structure is very much a rectangular matrix however way you look at it, so any help appreciated.

I also realize I am kind of making my own matrix structure, perhaps there’s an easier way to make this, but I just couldn’t figure out how to make a matrix in C# in Unity in the way I wanted, because my idea was to have a fixed NxN matrix which I would use as a template on how to instantiate the rectangles (using 0s where none and 1s where an object goes), I just had no idea how to approach that or if it was a better solution. I would still not know how to detect the surrounding shape to approximate to.

Thanks in advance for any info!

I have developed something similar in Interactive Terrain asset to generate automatically colliders based on any shape. It is using disc-poisson algorithm.

https://www.assetstore.unity3d.com/en/#!/content/49448

Source code is provided in this asset.


Game Rules
Unified Visual Scripting Asset with rules engine

That’s cool, thanks for sharing.
I’d like to see some free source though, or rather, talk about it here, so I can learn instead. Not looking for a premade solution.
As much as you are probably aware and used to the algorithm you mention, I never heard of it.
Looked it up and found an article here:
http://gregschlom.com/devlog/2014/06/29/Poisson-disc-sampling-Unity.html
It includes an implementation of said algorithm.

Seems to me this would not be suited to what I want to do though, and more suited to generating random but orderly-looking patterns. I am looking to create precise shapes (hence my example of a matrix filled with 1s and 0s to create a shape) in a very non-random grid shaped after a surrounding object’s mesh.

Anyone has any idea how to do this?

My idea would be, make a collider. Let’s say, a sphere collider.
For each row, cast one raycast from behind and one from the front. After that, fill all lines between this two points with the blocks. I hope i understood your problem correctly.

Hmm, yeah, ray casting, is what I was thinking next, but I haven’t learned how to use them properly yet, so I have no idea how I would apply it to this problem.
Will be good to keep it in mind though, thanks for your input!