Search Unity

Showcase AI Burst Mandelbrot

Discussion in 'C# Job System' started by Trindenberg, Dec 10, 2022.

  1. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    I used ChatGPT to create 99% of this code, simply for a custom inspector that shows a Mandelbrot in a Texture2D with offsets and texture pixel width (useless but just testing!). Initially this was in managed code, so I said to put the algorithm into a parallel job which gave me a substantial speed up. Some minor edits as I couldn't figure what to prompt to make it draw the texture at the right height, but amazing what is possible in half an hour! Also the capacity to use jobs/Burst in the Editor space.

    upload_2022-12-10_11-18-8.png

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using Unity.Jobs;
    4.  
    5. using Unity.Jobs;
    6. using Unity.Collections;
    7.  
    8. public struct MandelbrotJob : IJobParallelFor
    9. {
    10.     // The data for the texture
    11.     public NativeArray<Color32> data;
    12.  
    13.     // The width and height of the texture
    14.     public int textureWidth;
    15.     public int textureHeight;
    16.  
    17.     // The x and y offsets for the Mandelbrot set
    18.     public float offsetX;
    19.     public float offsetY;
    20.  
    21.     public void Execute(int i)
    22.     {
    23.         // Calculate the coordinates of the current pixel
    24.         int pixelX = i % textureWidth;
    25.         int pixelY = i / textureWidth;
    26.  
    27.         // Calculate the complex number at the current pixel
    28.         float cRe = offsetX + (pixelX - textureWidth / 2.0f) * 4.0f / textureWidth;
    29.         float cIm = offsetY + (pixelY - textureHeight / 2.0f) * 4.0f / textureWidth;
    30.  
    31.         // Calculate the value of the texture at the current pixel
    32.         float x = 0, y = 0;
    33.         int iteration = 0;
    34.         while (x * x + y * y <= 4 && iteration < 8192)
    35.         {
    36.             float xNew = x * x - y * y + cRe;
    37.             y = 2 * x * y + cIm;
    38.             x = xNew;
    39.             iteration++;
    40.         }
    41.  
    42.         // Set the color of the pixel
    43.         data[i] = Color.Lerp(Color.black, Color.white, (float)iteration / 8192);
    44.     }
    45. }
    46. [CustomEditor(typeof(BurstMandelbrot))]
    47. public class BurstMandelbrotEditor : Editor
    48. {
    49.     public float x;
    50.     public float y;
    51.     public int pixelWidth = 5;
    52.     private Texture2D texture;
    53.  
    54.     public override void OnInspectorGUI()
    55.     {
    56.         EditorGUILayout.BeginVertical();
    57.         x = EditorGUILayout.FloatField("OffsetX:", x);
    58.         y = EditorGUILayout.FloatField("OffsetY:", y);
    59.         pixelWidth = EditorGUILayout.IntField("Width:", pixelWidth);
    60.  
    61.         texture = new Texture2D(pixelWidth, pixelWidth);
    62.  
    63.         // Create a NativeArray to store the texture data in
    64.         NativeArray<Color32> data = new NativeArray<Color32>(texture.width * texture.height, Allocator.TempJob);
    65.  
    66.         // Create a job to calculate the texture data
    67.         MandelbrotJob job = new MandelbrotJob
    68.         {
    69.             data = data,
    70.             textureWidth = texture.width,
    71.             textureHeight = texture.height,
    72.             offsetX = x,
    73.             offsetY = y
    74.         };
    75.  
    76.         // Schedule the job and wait for it to complete
    77.         JobHandle handle = job.Schedule(texture.width * texture.height, 64);
    78.         handle.Complete();
    79.  
    80.        
    81.         // Load the texture data into the texture
    82.         //texture.LoadRawTextureData(data);   Didn't work for some reason
    83.         texture.SetPixelData(data, 0);
    84.         // Dispose of the NativeArray
    85.         data.Dispose();
    86.  
    87.         // Apply the changes to the texture
    88.         texture.Apply();
    89.  
    90.         // Calculate the width of the inspector
    91.         float inspectorWidth = EditorGUIUtility.currentViewWidth;
    92.  
    93.         // Calculate the width and height of the texture
    94.         float textureWidth = inspectorWidth;
    95.         float textureHeight = textureWidth * texture.height / texture.width;
    96.  
    97.         // Create a rectangle for the texture
    98.         Rect rect = new Rect(0, 70, textureWidth, textureHeight);
    99.  
    100.         EditorGUILayout.Space(textureHeight);
    101.         // Draw the texture in the inspector
    102.         EditorGUI.DrawPreviewTexture(rect, texture);
    103.  
    104.         // End the vertical layout
    105.         EditorGUILayout.EndVertical();
    106.     }
    107. }
     
    CosmicStud, jGate99 and mgear like this.
  2. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    who cares about 100 liner AI generated code of a very well known algorithm
     
    Trindenberg likes this.
  3. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    Ok give me a better idea!
     
    rmb303 and jGate99 like this.
  4. CosmicStud

    CosmicStud

    Joined:
    Jun 13, 2017
    Posts:
    55
    Fractals are amazing, can't wait to see more! Keep it up!
     
  5. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Create a 2D-platformer on an infinite mandelbrot-world.
     
  6. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    That has come to mind, would be interesting for sure!