unity単純反射

2070 ワード

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