Generating a procedural galaxy-like structure

I currently am learning C# and Unity and was wondering how I could generate a procedural galaxy. I already wrote a code for a randomized Asteroid structure in a certain field, however, I can’t figure out how to focus on an area - so to speak the core, so I have more asteroids/objects at the centre than at the edge - and how I could simulate the spirals of a galaxy…

Here is my current generator-script.
If anybody has suggestions or ideas, I would be happy to hear them.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameController : MonoBehaviour {
  
    public int maxAsteroids;
  
    public float tumble;

    public GameObject spherePrefab;
    public GameObject asteroid1;
    public GameObject asteroid2;
    public GameObject asteroid3;

    public float asteroidXMin, asteroidXMax, asteroidYMin, asteroidYMax, asteroidZMin, asteroidZMax;

    public int xMin, xMax, yMin, yMax, zMin, zMax;

    Vector3 pos;
    GameObject[] asteroids;
    float x;
    float y;
    float z;

    void Start () {

        GameObject[] asteroids = new GameObject[] { asteroid1, asteroid2, asteroid3 };

        AsteroidGeneration(asteroids[Random.Range(1, asteroids.Length)]);
    }


    void AsteroidGeneration(GameObject theObject)
    {
        StartCoroutine(Asteroid(theObject));
      
      
    }
    IEnumerator Asteroid(GameObject theObject)
    {

      

            for (int i = 0; i <= maxAsteroids; i+=1)
        {

            Debug.Log(i);
            x = Random.Range(xMin, xMax);
            z = Random.Range(zMin, zMax);
            y = Random.Range(yMin, yMax);
            pos = new Vector3(x, y, z);

            if (Physics.CheckSphere(pos, asteroidXMin))
            {
                i-=1;
                Debug.Log("Game Object already there " + i);
            }
            else
            {
                Quaternion rot = new Quaternion(0, 0, 0, 0);

                transform.position = pos;
                Instantiate(theObject, transform.position, rot);
            }
          
            yield return new WaitForSeconds(0.01f);
        }
    }
    void Update () {
      
    }
}

My game is on a grid so its easier.
I made several gray scale mask images, 32x32, this is a Spiral Bar Galaxy type B

The brightness makes it more likely that a star appears on that space.
A star is the larger yellow boxes, the smaller boxes are just distant stars for decoration.

I was just working on these over the last few weeks, good timing.

1 Like

Faking the template with a texture isn’t a bad idea.

For a full procedural set up I would follow these steps:

  • Create a central ball of stars based on the distance from the centre
  • Create a set of ‘arms’
  • Slowly rotate the arms, as you rotate, generate new stars, moving further out from the centre as you go

Very interesting, that could definitely help.
I take it this is pre-made, and not randomly generated?

I had an idea:
Since the only problem I am having is generating the map of the galaxy itself, I only need to find a way to generate that.
I have found a way to spawn objects relative to a probability, which would be this snippet of code:

bool instantiated = false;
            int x = Random.Range(1, 100);
            if (x <= 10 && instantiated == false)
            {
                x = (int)Random.Range(boundaryOne.xMin, boundaryOne.xMax);
                z = (int)Random.Range(boundaryOne.zMin, boundaryOne.zMax);
                y = (int)Random.Range(boundaryOne.yMin, boundaryOne.yMax);
                pos = new Vector3(x, y, z);
                Quaternion rot = new Quaternion(0, 0, 0, 0);
                transform.position = pos;
                Instantiate(theObject, transform.position, rot);
                instantiated = true;
                yield return new WaitForSeconds(waitTime);
            }
            if (x <= 30 && instantiated == false)
            {
                x = (int)Random.Range(boundaryTwo.xMin, boundaryTwo.xMax);
                z = (int)Random.Range(boundaryTwo.zMin, boundaryTwo.zMax);
                y = (int)Random.Range(boundaryTwo.yMin, boundaryTwo.yMax);
                pos = new Vector3(x, y, z);
                Quaternion rot = new Quaternion(0, 0, 0, 0);
                transform.position = pos;
                Instantiate(theObject, transform.position, rot);
                instantiated = true;
                yield return new WaitForSeconds(waitTime);
            }
            if (x <= 60 && instantiated == false)
            {
                x = (int)Random.Range(boundaryThree.xMin, boundaryThree.xMax);
                z = (int)Random.Range(boundaryThree.zMin, boundaryThree.zMax);
                y = (int)Random.Range(boundaryThree.yMin, boundaryThree.yMax);
                pos = new Vector3(x, y, z);
                Quaternion rot = new Quaternion(0, 0, 0, 0);
                transform.position = pos;
                Instantiate(theObject, transform.position, rot);
                instantiated = true;
                yield return new WaitForSeconds(waitTime);
            }

In this code however, the boundaries are simply shaped like a box.

Does anybody know of a way to shape the boundaries it like a sphere or even like something else, like a custom object?

Because right now, it looks kind of weird, very box-shaped…

Have you taken a look at Random.insideUnitSphere ?

It might be what you are looking for.

1 Like

Thats already way better, thanks, but is there something like a Random.insideGameObject or something? Because for a galaxy, i would need the sphere at least to be flattened out a bit…

You could check out this video:
https://www.youtube.com/watch?v=GJWuVwZO98s

It depends what you need to do once you get to a planet.

If you’re trying to model a spiral, think about the structure of it first, it’s like a coil wrapped round a cone, or a cylinder whose radius increases from 0 as you go up in height. You could then drop off groups of stuff at random intervals, if you need procedural and random but keep some constant then it might please you to find out that you can give random a seed value: https://docs.unity3d.com/530/Documentation/ScriptReference/Random-seed.html then you can get a bit more predictable results

Yes the gray scale image was drawn by me.
I have 11 of them so far with different galaxy types.

A spiral bar galaxy comes in 3 basic shapes.

( this is a reference image, not drawn by me )

The randomness doesnt come from the shape of the spiral, they are all very similar within a type.
The randomness is in the star distribution within the volume, for what I am doing.

My first attempts I just showed stars, but even though I had the template it wasnt very spiral looking. So then I drew the spiral as a background gas, then you could really see the spiral shape.