首页 > 其他 > 详细

Cg入门18:Fragment shader - 边缘泛光和透明

时间:2016-03-23 22:21:30      阅读:295      评论:0      收藏:0      [点我收藏+]
泛光:
技术分享
技术分享
处理后效果:
技术分享
技术分享

透明:
1.tags{"queue" = "transparent }     //调整渲染顺序
2.blend srcAlpha oneMinussrcalpha   //混合透明
3. zwrite off                        //写入的z轴深度关掉

关掉z轴深度与不关掉的区别:
技术分享技术分享
技术分享技术分享
代码:
Shader "Sbin/trans" {
	Properties {
		_Scale("Scale",Range(1,8)) = 2
	}
	
	SubShader {
		tags{"queue" = "transparent" }
		
		pass{
			blend srcAlpha oneMinussrcalpha
			zwrite off	//写入的z轴深度关掉
		
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct v2f{
				float4 pos : POSITION;
				float4 col : COLOR;
				float3 normal:TEXCOORD0;
				float4 vertex:TEXCOORD1;
			};

			v2f vert(appdata_base v){
				v2f o;
				
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				o.col = fixed4(0,1,1,1);
				o.vertex = v.vertex;
				o.normal = v.normal;
				
				return o;
			}
			
			float _Scale;
			
			fixed4 frag(v2f v):COLOR
			{
				float3 N = mul((float3x3)_World2Object,v.normal);
				N = normalize(N);
				
				float4 wpos = mul(_Object2World,v.vertex);
				float3 V = _WorldSpaceCameraPos.xyz - wpos.xyz;
				V = normalize(V);
				
				float bright = 1 -saturate(dot(N,V));
				bright = pow(bright,_Scale);
				
				return fixed4(1,1,1,1)*bright;
			}

			ENDCG
			
		}
	} 
	
	FallBack "Diffuse"
}


技术分享

优化部分:
怎么关掉pass方法,
方法1:注释掉
方法2:blend zero one  (解释:zero 表示0乘以当前pass颜色输出,0就是不输出;one就是全部输出前面的pass通道计算的颜色值)

dstAlpha : 当前渲染的alpha值乘以当前alpha值
srcAlpha :采用原始alpha乘以当前alpha值

blendop revsub:用之前pass渲染的颜色值减去当前的pass颜色值
技术分享
代码:
Shader "Sbin/trans" {
	Properties {
		_MainColor("MainColor",Color) = (1,1,1,1)
		_Scale("Scale",Range(1,10)) = 2
		_Outer("Outer",Range(0,1)) = 0.2
		
	}
	
	SubShader {
		tags{"queue" = "transparent" }
		
		pass{
			blend srcAlpha oneMinussrcalpha
			zwrite off	//写入的z轴深度关掉
		
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct v2f{
				float4 pos : POSITION;
				float4 col : COLOR;
				float3 normal:TEXCOORD0;
				float4 vertex:TEXCOORD1;
			};
			
			float _Scale;
			fixed4 _MainColor;
			float _Outer;

			v2f vert(appdata_base v){
				v2f o;
				
				v.vertex.xyz += v.normal*_Outer;
				
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				o.col = fixed4(0,1,1,1);
				o.vertex = v.vertex;
				o.normal = v.normal;
				
				return o;
			}
			
			
			
			fixed4 frag(v2f v):COLOR
			{
				float3 N = mul((float3x3)_World2Object,v.normal);
				N = normalize(N);
				
				float4 wpos = mul(_Object2World,v.vertex);
				float3 V = _WorldSpaceCameraPos.xyz - wpos.xyz;
				V = normalize(V);
				
				float bright = saturate(dot(N,V));
				bright = pow(bright,_Scale);
				
				_MainColor.a *= bright;
				
				return _MainColor;
			}

			ENDCG
		}
		
		//==============================================================================
		
		pass{
			blendop revsub
			blend dstAlpha one  //之前的alpha值
			//blend zero one
			zwrite off	//写入的z轴深度关掉
		
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct v2f{
				float4 pos : POSITION;
			};

			v2f vert(appdata_base v){
				v2f o;
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				return o;
			}
			
			fixed4 frag(v2f v):COLOR
			{
				return fixed4(1,1,1,1);
			}

			ENDCG
		}
		
		//==============================================================================
		
		pass{
			blend srcAlpha oneMinussrcalpha
			//blend zero one
			zwrite off	//写入的z轴深度关掉
		
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			struct v2f{
				float4 pos : POSITION;
				float4 col : COLOR;
				float3 normal:TEXCOORD0;
				float4 vertex:TEXCOORD1;
			};

			v2f vert(appdata_base v){
				v2f o;
				
				o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
				o.col = fixed4(0,1,1,1);
				o.vertex = v.vertex;
				o.normal = v.normal;
				
				return o;
			}
			
			float _Scale;
			
			fixed4 frag(v2f v):COLOR
			{
				float3 N = mul((float3x3)_World2Object,v.normal);
				N = normalize(N);
				
				float4 wpos = mul(_Object2World,v.vertex);
				float3 V = _WorldSpaceCameraPos.xyz - wpos.xyz;
				V = normalize(V);
				
				float bright = 1 - saturate(dot(N,V));
				bright = pow(bright,_Scale);
				
				return fixed4(1,1,1,1)*bright;
			}

			ENDCG
		}
	} 
	
	FallBack "Diffuse"
}


技术分享
技术分享

Cg入门18:Fragment shader - 边缘泛光和透明

原文:http://blog.csdn.net/aa4790139/article/details/50964474

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!