Advertisement
webik150

ProceduralAsteroid - Horrible thing

Oct 31st, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. public ProceduralAsteroid Generate (float seed, float size)        //This horrible thing creates asteroids.
  2.     {
  3.         //Material mat = Instantiate<Material> (GetComponent<SpriteRenderer> ().material);    //Uncomment this if you're using custom shaders.
  4.         Color[] finalColors = referenceTexture.GetPixels ();           //First, we need to get the pixels.
  5.         int y = 0;                                                     //Now, we create Height and Width counters (Because GetPixels() returns one dimensional array. Don't ask me why.).
  6.         int xCounter = 0;
  7.         for (int i = 0; i < wearLevel; i++) {                          //For each wearLevel we assigned in the variables
  8.             xCounter = 0;                                              //Reset the counters
  9.             y = 0;
  10.             for (int x = 0; x < finalColors.Length; x++) {             //Iterate through all the pixels
  11.                 if (xCounter == referenceTexture.width) {              //Reset width counter if equal to texture width
  12.                     xCounter = 0;
  13.                 }
  14.                 if (x > 0) {                                           //YOU DON'T LIKE BUGS! THIS KILLS BUGS!
  15.                     if (x % (referenceTexture.width - 1) == 0) {
  16.                         if (y == referenceTexture.width - 1) {
  17.                             break;
  18.                         }
  19.                        
  20.                     }
  21.                     y += 1;                                             //Please note: the y+=1 is in weird place, because it generates the nice random texture. It would normally be two lines up, but that makes the texture smooth.
  22.                 }
  23.                
  24.                 if (finalColors [x] == Color.black) {                                   //Each black pixel,
  25.                     if ((finalColors [x + 1].a == 0) || (finalColors [x - 1].a == 0)) { //that has at least one neighbor missing
  26.                         if (Random.value > wearChance) {                                //has a chance
  27.                             finalColors [x] = new Color (0, 0, 0, 0);                   //of getting removed.
  28.                         }                                                               //Wow, that seems so racist....
  29.                     }
  30.                     if ((finalColors [x - referenceTexture.width].a == 0) || (finalColors [x + referenceTexture.width].a == 0)) {  //The same as above, but checks for up and down.
  31.                         if (Random.value > wearChance) {
  32.                             finalColors [x] = new Color (0, 0, 0, 0);
  33.                         }
  34.                     }
  35.                 }
  36.                 xCounter++;
  37.                
  38.             }
  39.         }
  40.        
  41.         xCounter = 0;                                   //Now, we basically do the same thing, but we assign colours instead of removing pixels.
  42.         y = 0;
  43.         for (int x = 0; x < finalColors.Length; x++) {
  44.             if (xCounter == referenceTexture.width) {
  45.                 xCounter = 0;
  46.             }
  47.             if (x > 0) {
  48.                 if (x % (referenceTexture.width - 1) == 0) {
  49.                     if (y == referenceTexture.width - 1) {
  50.                         break;
  51.                     }
  52.                    
  53.                 }
  54.                 y += 1;
  55.             }
  56.            
  57.             if (finalColors [x] == Color.black) {                                   //Each black pixel
  58.                 finalColors [x] = GetColorByCoordinate (xCounter, y, size, seed);   //Gets the proper colour.
  59.                                                                                     //Wow... again...
  60.                
  61.             }
  62.             xCounter++;
  63.            
  64.         }
  65.         Texture2D finalTexture2D = Instantiate<Texture2D> (referenceTexture);   //Now, we just make a copy of the referenceTexture,
  66.         finalTexture2D.SetPixels (finalColors);                                 //and apply the new colors to it.
  67.         finalTexture2D.filterMode = FilterMode.Point;                           //This kinda makes the texture pixel-perfect.
  68.         finalTexture2D.Apply ();                                                //NEVER FORGET TO APPLY CHANGES!!!!!!!!!
  69.         GetComponent<SpriteRenderer> ().sprite = Sprite.Create (finalTexture2D, new Rect (0, 0, finalTexture2D.width, finalTexture2D.height), new Vector2 (0.5f, 0.5f), GetComponent<SpriteRenderer>().sprite.pixelsPerUnit);  //Now just create a new sprite, with our texture.
  70.         //GetComponent<SpriteRenderer> ().material = mat;     //Uncomment this if you're using custom shaders.
  71.         //GetComponent<SpriteRenderer> ().material.SetTexture ("_MainTex", finalTexture2D);     //Uncomment this if you're using custom shaders.
  72.         return this;   //Enjoy!
  73.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement