unity shader渲染状态的设置

10 篇文章 3 订阅

在pass中,我们可以设置一些渲染状态:

culling 与深度测试阶段:

    culling与深度测试是为了减少没必要的overdraw的,大大减少像素着色器的计算量。

  • Cull 控制哪一面将会被剔除:
  1. Back ,不渲染多边形背面(默认) 
  2. Front ,不渲染正面
  3. Off ,都不剔除,两面都渲染
  • ZWrite 控制是否需要写入深度缓存,默认开启,一般渲染半透明物体的时候关闭
  • ZTest 控制深度测试怎么测试,默认为LEqual 

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

  • Offset 两个参数,一个缩放因子,一个偏移

        Offset Factor, Units

 

Blending阶段:

         当图形被渲染时,在所有着色器被执行和所有纹理被应用之后,像素被写到屏幕上。它们如何与已经存在的内容相混合是由Blend命令控制的。

Blend Syntax
Syntax描述
Blend Off关闭混合(默认)
Blend SrcFactor DstFactor配置并且开启混合,SrcColor*SrcFacto + DstCoclor * DstFactor
Blend SrcFactor DstFactor, SrcFactorA DstFactorA与上面相同,不过alpha使用的factor为 SrcFactorA  DstFactorA
BlendOp Op默认是相加操作,我们可以指定不一样的混合操作
BlendOp OpColor, OpAlpha与上面相同,不过alpha使用的操作可以指定不一样的
Blend operations
Add对混合后 SrcColor 与 DstCoclor 使用相加操作
Sub对混合后 SrcColor 与 DstCoclor 使用相减操作
RevSub对混合后 DstCoclor 与 SrcColor 使用相减操作
Min逐分量计算DstCoclor SrcColor中较小的
Max逐分量计算DstCoclor SrcColor中较大的
Blend factors
OneThe value of one - use this to let either the source or the destination color come through fully.
ZeroThe value zero - use this to remove either the source or the destination values.
SrcColorThe value of this stage is multiplied by the source color value.
SrcAlphaThe value of this stage is multiplied by the source alpha value.
DstColorThe value of this stage is multiplied by frame buffer source color value.
DstAlphaThe value of this stage is multiplied by frame buffer source alpha value.
OneMinusSrcColorThe value of this stage is multiplied by (1 - source color).
OneMinusSrcAlphaThe value of this stage is multiplied by (1 - source alpha).
OneMinusDstColorThe value of this stage is multiplied by (1 - destination color).
OneMinusDstAlphaThe value of this stage is multiplied by (1 - destination alpha).

示例:

Shader "Shaders/Blend" {
	Properties {
		_Color ("Color Tint", Color) = (1, 1, 1, 1)
		_MainTex ("Main Tex", 2D) = "white" {}
		_AlphaScale ("Alpha Scale", Range(0, 1)) = 1
	}
	SubShader {
		Tags {"Queue"="Transparent" "IgnoreProjector"="True" 
        "RenderType"="Transparent"}
		
		Pass {
			Tags { "LightMode"="ForwardBase" }
			
			ZWrite Off
			
//			// Normal
//			Blend SrcAlpha OneMinusSrcAlpha
//			
//			// Soft Additive
//			Blend OneMinusDstColor One
//			
//			// Multiply
			Blend DstColor Zero
//			
//			// 2x Multiply
//			Blend DstColor SrcColor
//			
//			// Darken
//			BlendOp Min
//			Blend One One	// When using Min operation, these factors are ignored
//			
//			//  Lighten
//			BlendOp Max
//			Blend One One // When using Max operation, these factors are ignored
//			
//			// Screen
//			Blend OneMinusDstColor One
			// Or
//			Blend One OneMinusSrcColor
//			
//			// Linear Dodge
			Blend One One			
		}
	} 
}

Stencil测试:

      模板缓冲,用一个8位的整数标识每一个像素的模板值,模板的值可以被修改,之后的绘制调用可以针对该值进行测试,决定是丢弃该像素。

       

Syntax
Syntax使用描述
RefRef referenceValue要与之比较的值 和/或 要写入缓冲区的值(如果Pass、Fail或ZFail被设置为replace)。0 - 255整数。
ReadMaskReadMask readMask一个8位的掩码,作为0-255的整数,在比较参考值和缓冲区内容(referenceValue和readMask)比较函数(stencilBufferValue和readMask)时使用。默认值:255。
WriteMaskWriteMask writeMask一个8位掩码,作为0-255整数,在写入缓冲区时使用。请注意,与其他写掩码一样,它指定哪些模板缓冲区位将受到写的影响(例如,WriteMask 0表示没有位受到影响,也不会写入0)。默认值:255。
CompComp comparisonFunction用于将引用值与缓冲区的当前内容进行比较的函数。Default:always。
PassPass stencilOperation如果模板测试(和深度测试)通过,该如何处理缓冲区的内容,Default:keep。
FailFail stencilOperation如果模具测试失败,该如何处理缓冲区的内容Default: keep.
ZFailZFail stencilOperation如果模板测试通过,但是深度测试失败,该如何处理缓冲区的内容Default: keep.

Comparison Function:

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

Stencil Operation

Keep保持缓冲区的当前内容。
Zero将 0 写入缓冲区。
Replace将参考值写入缓冲区。
IncrSat递增缓冲区中的当前值。如果该值已经是 255,则保持为 255。
DecrSat递减缓冲区中的当前值。如果该值已经是 0,则保持为 0。
Invert将所有位求反。
IncrWrap递增缓冲区中的当前值。如果该值已经是 255,则变为 0。
DecrWrap递减缓冲区中的当前值。如果该值已经是 0,则变为 255。

示例:

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
        }
    } 
}

 

Name:

Name "PassName" 指定pass的name

 

还有一些旧版本的光照,纹理 ,雾效可以直接访问:

https://docs.unity3d.com/Manual/SL-SubShader.html

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值