برمجة ال shader باستخدام ال CG الدرس السابع

Discard

بامكانك عمل discard للبكسل داخل برنامج ال fragment وهذا يؤدي الى عدم رسم البكسل .

fixed4 frag(VertexOutput vo):COLOR
{
	fixed4 col;
	if(vo.norm.z>=0)
		col = tex2D(tex,vo.tex);
	else
		col = tex2D(tex2,vo.tex);
				
	if(col.w<=0.5)discard;
	return col;
}

Blending

يدمج بين البكسل في ال destination وال source
ال destination هو البكسل الموجود في ال frame buffer
وال source هو البكسل الذي تم ارجاعه من برنامج ال fragment

اما صيغه

Blend Off: Turn off blending (this is the default)

Blend SrcFactor DstFactor: Configure and enable blending. The generated color is multiplied by the SrcFactor. The color already on screen is multiplied by DstFactor and the two are added together.

Blend SrcFactor DstFactor, SrcFactorA DstFactorA: Same as above, but use different factors for blending the alpha channel.

BlendOp Op: Instead of adding blended colors together, carry out a different operation on them.

BlendOp OpColor, OpAlpha: Same as above, but use different blend operation for color (RGB) and alpha (A) channels.

Blend operations

Add Add source and destination together.
Sub Subtract destination from source.
RevSub Subtract source from destination.
Min Use the smaller of source and destination.
Max Use the larger of source and destination.

 

 

 

 

Blend factors

All following properties are valid for both SrcFactor & DstFactor in the Blend command. Source refers to the calculated color, Destination is the color already on the screen

One The value of one – use this to let either the source or the destination color come through fully.
Zero The value zero – use this to remove either the source or the destination values.
SrcColor The value of this stage is multiplied by the source color value.
SrcAlpha The value of this stage is multiplied by the source alpha value.
DstColor The value of this stage is multiplied by frame buffer source color value.
DstAlpha The value of this stage is multiplied by frame buffer source alpha value.
OneMinusSrcColor The value of this stage is multiplied by (1 – source color).
OneMinusSrcAlpha The value of this stage is multiplied by (1 – source alpha).
OneMinusDstColor The value of this stage is multiplied by (1 – destination color).
OneMinusDstAlpha The value of this stage is multiplied by (1 – destination alpha).

 

 

 

 

 

 

 

 

 

اشهر العمليات

Blend SrcAlpha OneMinusSrcAlpha // Traditional transparency
Blend One OneMinusSrcAlpha // Premultiplied transparency
Blend One One // Additive
Blend OneMinusDstColor One // Soft Additive
Blend DstColor Zero // Multiplicative
Blend DstColor SrcColor // 2x Multiplicative

مثال

subshader
{
	Tags { "Queue" = "ALPHATEST" } 
	pass
	{
		BlendOp ADD
		Blend SrcAlpha OneMinusSrcAlpha
                // = (SrcAlpha*Source Color) + (OneMinusSrcAlpha*Destination Color)
		ZWRITE OFF
		CGPROGRAM
                ENDCG
        }
}