79675494

Date: 2025-06-22 22:48:50
Score: 1
Natty:
Report link

I found a solution, I think. It doesn't exactly give me what I want, but it's a great start and I no longer feel stuck.

I made an empty game object, and attached a script to it that contains LineRenderer and Mesh.

Here is the code below I have now

using UnityEngine;

/* Ensures that the attached GameObject has a MeshFilter and MeshRenderer component.
 * Unity automatically generates one if not.*/
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class ProceduralPlanet : MonoBehaviour
{
    /* Number of points around the circle.
     * Increasing the number makes it smoother and increases resolution */
    public int segments = 1000;

    /* The radius of the object */
    public float baseRadius = 50f;

    /* Controls the scale of the Perlin noise applied to the radius */
    public float noiseScale = 4f;

    /* Controls the amplitude of the Perlin noise applied to the radius */
    public float noiseAmplitude = 0.05f;

    private void Start()
    {
        /* Takes the attached MeshFilter, creates a mesh object, and names it */
        MeshFilter meshFilter = GetComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        mesh.name = "Procedural Planet";

        /* Create arrays for vertices and triangles.
         * Vertices will hold the positions of the points in 3D space,
         * Triangles will define how these points are connected to form the mesh */
        Vector3[] vertices = new Vector3[segments + 2]; // center + edge points
        int[] triangles = new int[segments * 3]; // 3 per triangle (center + 2 edge points)

        /* Center of the planet */
        vertices[0] = Vector3.zero; // center point of planet

        
        for (int i = 0; i <= segments; i++)
        {
            /* Goes around to create a circle*/
            float angle = (float)i / segments * Mathf.PI * 2f;

            // Calculate unit vector for the current angle
            float xUnit = Mathf.Cos(angle);
            float yUnit = Mathf.Sin(angle);

            // Apply Perlin noise
            float noise = Mathf.PerlinNoise(xUnit * noiseScale + 100f, yUnit * noiseScale + 100f);

            // Calculate the radius with noise applied
            float radius = baseRadius + (noise - 0.5f) * 2f * noiseAmplitude;

            // Set the vertex position
            vertices[i + 1] = new Vector3(xUnit, yUnit, 0) * radius;

            // Create triangles (except on last iteration)
            if (i < segments)
            {
                // Each triangle consists of the center and two consecutive edge points
                int start = i * 3;
                triangles[start] = 0; // center
                triangles[start + 1] = i + 1;
                triangles[start + 2] = i + 2;
            }
        }

        // Handle the last triangle to close the circle
        mesh.vertices = vertices;

        // The last triangle connects the last edge point back to the first edge point
        mesh.triangles = triangles;

        // Calculate normals and bounds for lighting and rendering
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();

        // Assign the mesh to the MeshFilter
        meshFilter.mesh = mesh;
    }
}

I'll make this a community wiki, so anyone is free to edit and help other people. Have no idea why the syntax isn't highlighting though.

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Explorer