Advertisement
bluntgames

Attenuation Texture

Jan 21st, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. public static Texture2D MakeAttenuationTexture(int w, float pow, float scale) {
  2.     var bd = new Color[w * w];
  3.     for (int i = 0; i < bd.Length; i++)
  4.     {
  5.         bd[i] = new Color(0,0,0,0);
  6.     }
  7.     /// 1.0 / (1.0 + 25.0*r*r)
  8.     for (int x = 1; x < w - 1; x++)
  9.     {
  10.         for (int y = 1; y < w - 1; y++)
  11.         {
  12.             float actualX = 2.0f * ((float)x - ((float)w) / 2.0f) / (float)w;
  13.             float actualY = 2.0f * ((float)y - ((float)w) / 2.0f) / (float)w;
  14.             Vector2 vec = new Vector2(actualX, actualY);
  15.             float distance = vec.magnitude;
  16.             float attenuation = 1.0f / (1.0f + 25.0f * Mathf.Pow(distance, pow) * scale);
  17.             Color c = new Color(attenuation, attenuation, attenuation, attenuation);
  18.             bd[x + y * w] = c;
  19.         }
  20.     }
  21.     Texture2D newtex = new Texture2D(w, w);
  22.     newtex.SetPixels(bd);
  23.     newtex.wrapMode = TextureWrapMode.Clamp;
  24.     newtex.Apply();
  25.     return newtex;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement