分類  >  Web前端 >

unity容易反射

tags:    時間:2013-12-10 10:30:33
unity簡單反射
一個用於unity的簡單的反射效果.筆記

Shader "Level4/Reflect/SimpleRefl" { 	Properties { 		_MainTex ("Base (RGB)", 2D) = "white" {} 		_CubeMap("CubeMap",Cube) = ""{} 		_SelfStrength("Self Strength",Range(0,1.5)) = 1 		_RoomReflAmount("RoomReflAmount",range(0,3.5)) = 1 		_TintColor("TintColor",color) = (0,0,0,0) 		_ChannelAmount("rgba,ChannelAmout",float) = 0.225 	} 	 	CGINCLUDE 		#include "UnityCG.cginc" 		sampler2D _MainTex; 		samplerCUBE _CubeMap; 		half _SelfStrength; 		half _RoomReflAmount; 		float4 _TintColor; 		half _ChannelAmount; 	ENDCG 	 	SubShader { 		Tags { "RenderType"="Opaque" } 		LOD 200 		Lighting on 		 		Pass{ 			CGPROGRAM 			#pragma vertex vert 			#pragma fragment frag 			struct v2f{ 				half4 pos:POSITION; 				half4 color:TEXCOORD; 				half2 uv:TEXCOORD1; 				half3 reflDir:TEXCOORD2; 			}; 			/**  				vertex : 根據eye向量和normal向量,計算反射向量(2N(N.L)-L). 				fragment : 根據反射向量取cube的顏色 * 反射量 * saturate(b通道顏色-通道量) 			*/ 			v2f vert(appdata_base v){ 				v2f o; 				o.pos = mul(UNITY_MATRIX_MVP,v.vertex); 				half3 worldPos = mul(_Object2World,v.vertex).xyz; 				half3 worldNormal = mul((half3x3)_Object2World,v.normal.xyz).xzy; 				o.uv.xy = v.texcoord.xy; 				half3 eyeVector = (_WorldSpaceCameraPos.xyz - worldPos); 				o.reflDir = reflect(eyeVector,worldNormal); 				return o; 			} 			half4 frag(v2f i):COLOR{ 				fixed4 tex = tex2D(_MainTex,i.uv.xy); 				fixed4 refl = texCUBE(_CubeMap,i.reflDir); 				half4 outColor = tex; 				 				outColor.rgb *= _TintColor + tex.a * _SelfStrength; 				outColor += refl * _RoomReflAmount * saturate(tex.b - _ChannelAmount); 				return outColor; 			} 			 			ENDCG 		} 	}  	FallBack "Diffuse" } 

推薦閱讀文章

Bookmark the permalink ,來源:互聯網