Advertisement
RaenirSalazar

Untitled

Dec 12th, 2020
1,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.34 KB | None | 0 0
  1.     public enum BLEND_MODES
  2.     {
  3.         NORMAL,
  4.         MULTIPLY,
  5.         ADDITIVE,
  6.         DIFFERENCE,
  7.         OVERLAY,
  8.         SCREEN
  9.     }
  10.  
  11.     static float[,] Blend_Normal(float[,] mapA, float[,] mapB, int width, int height, WorldMinMax blendMinMax)
  12.     {
  13.         for (int i = 0; i < width; i++)
  14.         {
  15.             for (int j = 0; j < height; j++)
  16.             {
  17.                 float v = mapB[i, j];
  18.                 blendMinMax.AddValue(v);
  19.             }
  20.         }
  21.         return mapB;
  22.     }
  23.  
  24.     static float[,] Blend_Multiply(float[,] mapA, float[,] mapB, int width, int height, WorldMinMax blendMinMax)
  25.     {
  26.         float[,] blendedMap = new float[width, height];
  27.         for (int i = 0; i < width; i++)
  28.         {
  29.             for (int j = 0; j < height; j++)
  30.             {
  31.                 float v = mapA[i, j] * mapB[i, j];
  32.                 blendedMap[i, j] = v;
  33.                 blendMinMax.AddValue(v);
  34.             }
  35.         }
  36.  
  37.         return blendedMap;
  38.     }
  39.  
  40.     // Given layer a and a layer b where b is the top layer and a is the underlying layer.
  41.     public static float[,] Blend(BLEND_MODES blend_mode, float[,] mapA, float[,] mapB, int width, int height,
  42.         WorldMinMax blendMinMax)
  43.     {
  44.        
  45.  
  46.         switch (blend_mode)
  47.         {
  48.             case BLEND_MODES.NORMAL:
  49.                 return Blend_Normal(mapA, mapB, width, height, blendMinMax);
  50.             case BLEND_MODES.MULTIPLY:
  51.                 return Blend_Multiply(mapA, mapB, width, height, blendMinMax);
  52.             //case BLEND_MODES.DIVIDE:
  53.             //    return Blend_Divide(mapA, mapB, width, height, blendMinMax);
  54.             case BLEND_MODES.ADDITIVE:
  55.                 return Blend_Add(mapA, mapB, width, height, blendMinMax);
  56.             case BLEND_MODES.DIFFERENCE:
  57.                 return Blend_Diff(mapA, mapB, width, height, blendMinMax);
  58.             case BLEND_MODES.OVERLAY:
  59.                 return Blend_Overlay(mapA, mapB, width, height, blendMinMax);
  60.             case BLEND_MODES.SCREEN:
  61.                 return Blend_Screen(mapA, mapB, width, height, blendMinMax);
  62.         }
  63.  
  64.         return new float[width, height];
  65.  
  66.     }
  67.  
  68.     static float[,] Blend_Add(float[,] mapA, float[,] mapB, int width, int height, WorldMinMax blendMinMax)
  69.     {
  70.         float[,] blendedMap = new float[width, height];
  71.         for (int i = 0; i < width; i++)
  72.         {
  73.             for (int j = 0; j < height; j++)
  74.             {
  75.                 float v = mapA[i, j] + mapB[i, j];
  76.                 blendedMap[i, j] = v;
  77.                 blendMinMax.AddValue(v);
  78.             }
  79.         }
  80.  
  81.         return blendedMap;
  82.     }
  83.  
  84.     static float[,] Blend_Diff(float[,] mapA, float[,] mapB, int width, int height, WorldMinMax blendMinMax)
  85.     {
  86.         float[,] blendedMap = new float[width, height];
  87.         for (int i = 0; i < width; i++)
  88.         {
  89.             for (int j = 0; j < height; j++)
  90.             {
  91.                 float v = mapA[i, j] - mapB[i, j];
  92.                 blendedMap[i, j] = v;
  93.                 blendMinMax.AddValue(v);
  94.             }
  95.         }
  96.  
  97.         return blendedMap;
  98.     }
  99.  
  100.     static float[,] Blend_Screen(float[,] mapA, float[,] mapB, int width, int height, WorldMinMax blendMinMax)
  101.     {
  102.         float[,] blendedMap = new float[width, height];
  103.         for (int i = 0; i < width; i++)
  104.         {
  105.             for (int j = 0; j < height; j++)
  106.             {
  107.                 float v = (1 - mapA[i, j]) * (1 - mapB[i, j]);
  108.                 blendedMap[i, j] = v;
  109.                 blendMinMax.AddValue(v);
  110.             }
  111.         }
  112.  
  113.         return blendedMap;
  114.     }
  115.  
  116.     static float[,] Blend_Overlay(float[,] mapA, float[,] mapB, int width, int height, WorldMinMax blendMinMax)
  117.     {
  118.         float[,] blendedMap = new float[width, height];
  119.         for (int i = 0; i < width; i++)
  120.         {
  121.             for (int j = 0; j < height; j++)
  122.             {
  123.                 float v_a = mapA[i, j];
  124.                 float v_b = mapB[i, j];
  125.                 float v = 0;
  126.                 if (v_a < 0.5f)
  127.                 {
  128.                     v = 2 * v_a * v_b;
  129.                 }
  130.                 else
  131.                 {
  132.                     v = 1 - (2 * (1 - v_a) * (1 - v_b));
  133.                 }
  134.                 blendedMap[i, j] = v;
  135.                 blendMinMax.AddValue(v);
  136.             }
  137.         }
  138.  
  139.         return blendedMap;
  140.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement