zoukankan      html  css  js  c++  java
  • 图像混合模式 正片叠底、滤色、叠加

     1 public class OverlayBlendModeTest : MonoBehaviour
     2 {
     3     // 两张需要混合的图片
     4     public Texture2D ima1;
     5     public Texture2D ima2;
     6 
     7     public float Opacity = 0.1f;
     8 
     9     private Texture2D tex;
    10 
    11     void Start()
    12     {
    13          tex = new Texture2D(ima1.width, ima1.height, TextureFormat.ARGB32, false);
    14     }
    15 
    16     void Update()
    17 
    18     {   //设置混合度  逐像素读取混合
    19         Texture2dHandle.SetOpacity = Opacity;
    20         for (int y = 0; y < ima1.height; y++)
    21         {
    22             for (int x = 0; x < ima1.width; x++)
    23             {
    24 
    25                 var dC = ima1.GetPixel(x, y);
    26                 var uC = ima2.GetPixel(x, y);
    27                 var oC = Texture2dHandle.OverlayBlendMode(uC, dC);
    28                 tex.SetPixel(x, y, oC);
    29 
    30             }
    31         }
    32 
    33         tex.Apply();
    34         gameObject.GetComponent<Renderer>().material.mainTexture = tex;
    35     }
    36 }
      1 public class Texture2dHandle
      2 {
      3 
      4  /// <summary>
      5     /// 混合度
      6     /// </summary>
      7     private static float Opacity = 0.3f;
      8 
      9     /// <summary>
     10     /// 设置混合度
     11     /// </summary>
     12     public static float SetOpacity
     13     {
     14         set
     15         {
     16             if (value < 0)
     17                 value = 0;
     18             else if (value > 1)
     19                 value = 1;
     20 
     21             Opacity = value;
     22         }
     23     }
     24 
     25     /// <summary>
     26     /// 正片叠底
     27     /// </summary>
     28     /// <returns></returns>
     29     public static Color Multiply(Color b, Color t)
     30     {
     31         //b.g *= t.g;
     32         //b.b *= t.b;
     33         //b.r *= t.r;
     34         //b.a = 1;     
     35    
     36         var c = new Color(b.r * t.r, b.g * t.g, b.b * t.b, 1);
     37         return Color.Lerp(b, c, Opacity);
     38     }
     39 
     40     /// <summary>
     41     /// 滤色
     42     /// </summary>
     43     /// <param name="b"></param>
     44     /// <param name="t"></param>
     45     /// <returns></returns>
     46     public static Color Screen(Color b, Color t)
     47     {
     48         b = Minus(b);
     49         t = Minus(t);
     50 
     51         var c = Minus(Multiply(Multiply(b, t), 2.0f));
     52 
     53         return Color.Lerp(b, c, Opacity);
     54     }
     55 
     56     /// <summary>
     57     /// 叠加
     58     /// </summary>
     59     /// <param name="b"></param>
     60     /// <param name="t"></param>
     61     /// <returns></returns>
     62     public static Color OverlayBlendMode(Color b, Color t)
     63     {
     64 
     65         Color c = new Color();
     66         c.r = OverlayBlendMode(b.r, t.r);
     67         c.g = OverlayBlendMode(b.g, t.g);
     68         c.b = OverlayBlendMode(b.b, t.b);
     69         c.a = 1;
     70 
     71         return Color.Lerp(b, c, Opacity);
     72     }
     73 
     74 
     75     private static float OverlayBlendMode(float basePixel, float blendPixel)
     76     {
     77         if (basePixel < 0.5)
     78         {
     79             return (float)(2.0 * basePixel * blendPixel);
     80         }
     81 
     82 
     83         return (float)(1.0 - 2.0 * (1.0 - basePixel) * (1.0 - blendPixel));
     84     }
     85 
     86     /// <summary>
     87     /// 倍数
     88     /// </summary>
     89     /// <param name="c"></param>
     90     /// <param name="index"></param>
     91     /// <returns></returns>
     92     private static Color Multiply(Color c, float index = 1)
     93     {
     94         c.r *= index;
     95         c.g *= index;
     96         c.b *= index;
     97         return c;
     98     }
     99 
    100     private static Color Minus(Color c, int index = 1)
    101     {
    102         c.r = index - c.r;
    103         c.b = index - c.b;
    104         c.g = index - c.g;
    105         return c;
    106     }
    107 }
  • 相关阅读:
    BOM,文档宽高及窗口事件小析
    表格、表单操作
    DOM相关属性,方法,兼容性问题处理小析
    js回调函数,字符串,数组小析
    js中return,this,arguments,currentStyle和getComputedStyle小析
    JS作用域,浏览器解析原理
    JS中注意事项
    PS中常用快捷键
    javaweb之框架标签(day1 框架标签的使用)
    网络编程课程复习
  • 原文地址:https://www.cnblogs.com/mdrs/p/6002827.html
Copyright © 2011-2022 走看看