Miziziziz

FractalBasic

Oct 4th, 2016
193
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class FractalBase : MonoBehaviour {
  5.  
  6.     Texture2D texture;
  7.  
  8.     public int resolution = 128;
  9.     public int maxIterations = 1000;
  10.  
  11.     public Gradient g;
  12.     public GradientColorKey[] gck;
  13.     public GradientAlphaKey[] gak;
  14.  
  15.     public float zoom = 1f;
  16.     public float xShift = 0;
  17.     public float yShift = 0;
  18.     public float moveSens = 10;
  19.  
  20.     void Awake ()
  21.     {
  22.         texture = GetComponent<Texture2D>();
  23.     }
  24.  
  25.     void Start()
  26.     {
  27.         texture = new Texture2D(resolution, resolution);
  28.         texture.name = "MandelTexture";
  29.         UpdateTexture();
  30.         GetComponent<MeshRenderer>().material.mainTexture = texture;
  31.     }
  32.  
  33.     void Update()
  34.     {
  35.         //navigation stuff
  36.         if (Input.GetButtonDown("Fire1"))
  37.         {
  38.             float s = moveSens * zoom;
  39.             Vector2 v2 = Input.mousePosition;
  40.             xShift += (v2.x - Screen.width / 2) / s;
  41.             yShift += (v2.y - Screen.height / 2) / s;
  42.             texture = new Texture2D(resolution, resolution);
  43.             UpdateTexture();
  44.             GetComponent<MeshRenderer>().material.mainTexture = texture;
  45.         }
  46.         float z = Input.GetAxis("ZoomAxis");
  47.         if (z != 0)
  48.         {
  49.             zoom += z * 10;
  50.             texture = new Texture2D(resolution, resolution);
  51.             UpdateTexture();
  52.             GetComponent<MeshRenderer>().material.mainTexture = texture;
  53.         }
  54.     }
  55.  
  56.     void UpdateTexture()
  57.     {
  58.         for (int x = 0; x < resolution; x++)
  59.         {
  60.             for (int z = 0; z < resolution; z++)
  61.             {
  62.                 //called for every pixel on the quad
  63.                 CalcMandelbrot(x, z);
  64.             }
  65.         }
  66.         texture.Apply();
  67.     }
  68.  
  69.     //where the magic happens
  70.     void CalcMandelbrot(int row, int col)
  71.     {
  72.         float re = (((row - resolution / 2.0f) / zoom) + xShift) * 4.0f / resolution;
  73.         float im = (((col - resolution / 2.0f) / zoom) + yShift) * 4.0f / resolution;
  74.         float x = 0, y = 0;
  75.  
  76.         int iteration = 0;
  77.         while (x*x + y*y <= 4 && iteration < maxIterations)
  78.         {
  79.             float x1 = x * x - y * y + re;
  80.             y = 2 * x * y + im;
  81.             x = x1;
  82.             iteration++;
  83.         }
  84.         if (iteration < maxIterations)
  85.         {
  86.             texture.SetPixel(row, col, g.Evaluate((float)iteration / maxIterations));
  87.         }  
  88.         else
  89.         {
  90.             texture.SetPixel(row, col, g.Evaluate(1));
  91.         }
  92.     }
  93.  
  94.     float Norm(Vector2 v2)
  95.     {
  96.         return v2.x * v2.x + v2.y * v2.y;
  97.     }
  98. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×