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;ENDCGSubShader {Tags { "RenderType"="Opaque" }LOD 200Lighting onPass{CGPROGRAM#pragma vertex vert#pragma fragment fragstruct 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"}