当前位置:   article > 正文

【UnityShader】shader基础关键字和算法_unity shader关键字

unity shader关键字

1.时间_Time

_Time是float4类型,
_Time.x表示当前时间 / 20,
_Time.y表示当前时间,
_Time.z表示当前时间 * 2,
_Time.w表示当前时间 * 3;

2.UV旋转计算过程

在二维坐标系做运算,被旋转的点P(x,y),与x轴夹角为A。求绕原点旋转B度后的P1(x1,y1)。
P和P1与原点距离恒定,都是斜边的长度r。
x = r * cos(A); y = r * sin(A);
P1与x轴夹角为A-B。
x1 = r * cos(A - B); y1 = r * sin(A - B);
由三角函数公式可知: cos(A - B) = cos(A) * cos(B) + sin(A) * sin(B), sin(A - B) = sin(A) * cos(B) - cos(A) * sin(B),
则: x1 = r * cos(A) * cos(B) + r * sin(A) * sin(B) = x * cos(B) + y * sin(B);
y1 = r * sin(A) * cos(B) - r * cos(A) * sin(B) = y * cos(B) - x * sin(B);

3.透明测试

 fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                if (col.a < 0.5)
                {
                    return fixed4(0, 0, 0, 0);
                }
                return col;
            }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.模板测试

https://docs.unity3d.com/cn/current/Manual/SL-Stencil.html
Ref 要比较的参考值

Comp 用于比较参考值与当前缓冲区内容的函数。(大于、等于、小于……)
Comp比较函数

Greater仅渲染参考值大于缓冲区值的像素。
GEqual仅渲染参考值大于或等于缓冲区值的像素。
Less仅渲染参考值小于缓冲区值的像素。
LEqual仅渲染参考值小于或等于缓冲区值的像素。
Equal仅渲染参考值等于缓冲区值的像素。
NotEqual仅渲染参考值不同于缓冲区值的像素。
Always使模板测试始终通过。
Never使模板测试始终失败。

Pass 通过测试后,如何处理缓冲区内容

Shader "Red" {
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry"}
        Pass {
        //模板测试代码开始
            Stencil {
                Ref 2
                Comp always
                Pass replace
            }
        //模板测试代码结束
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            struct appdata {
                float4 vertex : POSITION;
            };
            struct v2f {
                float4 pos : SV_POSITION;
            };
            v2f vert(appdata v) {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                return o;
            }
            half4 frag(v2f i) : SV_Target {
                return half4(1,0,0,1);
            }
            ENDCG
        }
    } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

5.深度测试

https://docs.unity3d.com/cn/current/Manual/SL-CullAndDepth.html

Cull 剔除

Cull Back | Front | Off

控制剔除多边形的哪些面:Back剔除背面,Front剔除正面,Off禁用剔除(创建双面材质)

ZWrite 写入像素

ZWrite On | Off

是否写入像素到深度缓冲区,写入才可进行深度测试判断。

ZTest 深度测试

ZTest Less | Greater | LEqual | GEqual | Equal | NotEqual | Always

如何进行深度测试,默认为LEqual(当前深度值小于等于深度缓冲区值时,通过测试)。

6.灰度公式 float3(0.299, 0.587,0.114)

使颜色置灰。

color.rgb = dot(color.rgb, float3(0.299, 0.587,0.114));
  • 1

7.模糊效果

根据偏移量Offset,取得当前像素点的Color,及其上下左右像素点的Color。
将五个Color相加,取平均值。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/108283
推荐阅读
相关标签
  

闽ICP备14008679号