• Unity实现混合模式的ADD模式


    一般来说,2D的特效都会用到ADD模式来播放,但是Unity居然没有内置任何的混合模式,网上资料太少,没有写好的Shader,这里提供下我自己编写Shader:

     1 Shader "Unlit/NewUnlitShader"
     2 {
     3     Properties
     4     {
     5         _MainTex ("Texture", 2D) = "white" {}
     6     }
     7     SubShader
     8     {
     9         //Tags { "RenderType"="Opaque" }
    10         Tags{ "Queue" = "Transparent" } // 不使用这个会导致出现背景透明部分不显示的情况
    11 
    12         LOD 100
    13 
    14         GrabPass{} // 绘制当前的屏幕图像到 _GrabTexture 里
    15 
    16         Pass
    17         {
    18             CGPROGRAM
    19             #pragma vertex vert
    20             #pragma fragment frag
    21             // make fog work
    22             #pragma multi_compile_fog
    23             
    24             #include "UnityCG.cginc"
    25 
    26             struct appdata
    27             {
    28                 float4 vertex : POSITION;
    29                 float2 uv : TEXCOORD0;
    30             };
    31 
    32             struct v2f
    33             {
    34                 float2 uv : TEXCOORD0;
    35                 UNITY_FOG_COORDS(1)
    36                 float4 vertex : SV_POSITION;
    37                 half4 screenuv : TEXCOORD2; // 当前屏幕图像的UV坐标
    38             };
    39 
    40             sampler2D _MainTex;
    41             float4 _MainTex_ST;
    42 
    43             sampler2D _GrabTexture; // 当前屏幕图像
    44             
    45             v2f vert (appdata v)
    46             {
    47                 v2f o;
    48                 o.vertex = UnityObjectToClipPos(v.vertex);
    49                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    50                 UNITY_TRANSFER_FOG(o,o.vertex);
    51                 o.screenuv = ComputeGrabScreenPos(o.vertex); // 获取当前屏幕图像的UV坐标
    52                 return o;
    53             }
    54             
    55             fixed4 frag (v2f i) : SV_Target
    56             {
    57 
    58                 fixed4 colour = tex2D(_GrabTexture, float2(i.screenuv.x, i.screenuv.y)); // 获取当前屏幕图像的颜色
    59 
    60                 // sample the texture
    61                 fixed4 col = tex2D(_MainTex, i.uv);
    62 
    63                 fixed4 endd = col + colour; // ADD 混合模式
    64 
    65                 // apply fog
    66                 UNITY_APPLY_FOG(i.fogCoord, endd);
    67                 return endd;
    68             }
    69             ENDCG
    70         }
    71     }
    72 }
  • 相关阅读:
    不容易系列之一(错排)
    找新朋友(欧拉函数)
    二分查找
    快速排序(分治)
    归并排序(分治)
    畅通工程(并查集)
    A Knight's Journey (DFS)
    Network Saboteur (DFS)
    Oil Deposits(油田)(DFS)
    Dungeon Master (三维BFS)
  • 原文地址:https://www.cnblogs.com/hammerc/p/6973318.html
Copyright © 2020-2023  润新知