unity shader 单独给物体设置指定的反射贴图

//jave.lin 2019.06.17
//simple illumination
Shader "Custom/MyReflection"
{
	Properties{
		[NoScaleOffset] _MainTex("Main Tex", 2D) = "white" {}
		[NoScaleOffset] _ReflectCube("Reflect Cube", Cube) = "white" {}
		_ReflectIntensity("Reflection Intensity", Range(0,1)) = 0.5
		_ReflectBrightness("Reflection Brightness", Range(1,2)) = 1
		_SpecularIntensity("Specular Intensity", Range(0,1)) = 1
		_AmbientIntensity("Ambient Intensity", Range(0,1)) = 1
		_SelfShadowColor("Self Shadow Color", Color) = (0.5,0.5,0.5,0.5)
		[Toggle] _HalfLambert("Half Lambert", Float) = 1
	}
		SubShader{
			Pass{
				Tags {"Queue" = "Opaque" "LightMode" = "ForwardBase"} // shadow setup1:"LightMode"="ForwardBase"
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma multi_compile_fwdbase 	// shadow setup2:fwdbase
				#include "UnityCG.cginc"
				#include "Lighting.cginc" 		// shadow setup3:#include "Lighting.cginc"
				#include "AutoLight.cginc" 		// shadow setup4:#include "AutoLight.cginc"
				struct a2v {
					float4 vertex : POSITION;
					float2 uv : TEXCOORD0;
					float3 normal : NORMAL;
				};
				struct v2f {
					float4 pos : SV_POSITION;
					float2 uv : TEXCOORD0;
					fixed4 tc : TEXCOORD1;
					LIGHTING_COORDS(2,3)		// shadow setup5:LIGHTING_COORDS(n,n+1)
					float3 worldNormal : TEXCOORD4;
					float3 worldPos : TEXCOORD5;
					float3 eyeDir : TEXCOORD6;
					float3 lightDir : TEXCOORD7;
				};
				sampler2D _MainTex;
				samplerCUBE _ReflectCube;
				float _ReflectIntensity;
				float _ReflectBrightness;
				float _SpecularIntensity;
				float _AmbientIntensity;
				fixed4 _SelfShadowColor;
				bool _HalfLambert;
				v2f vert(a2v v) {
					v2f o = (v2f)0;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.uv = v.uv;
					o.worldNormal = UnityObjectToWorldNormal(v.normal);
					o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
					o.eyeDir = normalize(_WorldSpaceCameraPos.xyz - o.worldPos).xyz;
					o.lightDir = normalize(_WorldSpaceLightPos0.xyz);
					TRANSFER_VERTEX_TO_FRAGMENT(o);	// shadow setup6:TRANSFER_VERTEX_TO_FRAGMENT(o)
					return o;
				}
				fixed4 frag(v2f i) :SV_TARGET{
					float3 normalDir = normalize(i.worldNormal);
					float3 reflectDir = reflect(-i.eyeDir,normalDir);
					fixed3 specularDir = reflect(-i.lightDir,normalDir);

					fixed4 c = tex2D(_MainTex, i.uv); // albedo
					fixed4 r = texCUBE(_ReflectCube, reflectDir); // enviroment cubemap

					if (_HalfLambert) {
						c.xyz *= (dot(i.lightDir,normalDir) + 1) * 0.5; // diffuse
					}
	else {
	   c.xyz *= dot(i.lightDir,normalDir); // diffuse
   }
   c.xyz += c.xyz * UNITY_LIGHTMODEL_AMBIENT.rgb; // ambient

   float specularDotEye = max(0, dot(specularDir, i.eyeDir));
   c.xyz += (_LightColor0.rgb) * specularDotEye * _SpecularIntensity; // specular
   fixed3 skyDir = reflect(float3(0,-1,0),normalDir);
   c.xyz += unity_AmbientSky.rgb * unity_AmbientSky.a * max(0, dot(skyDir, i.eyeDir)) * _AmbientIntensity; // ambient sky
   fixed3 equatorDir = normalize(float3(_WorldSpaceCameraPos.x, i.worldPos.y, _WorldSpaceCameraPos.z) - i.worldPos).xyz;
   equatorDir = reflect(-equatorDir,normalDir);
   c.xyz += unity_AmbientEquator.rgb * unity_AmbientEquator.a * max(0, dot(equatorDir, i.eyeDir)) * _AmbientIntensity; // ambient equator
   fixed3 groundDir = reflect(float3(0,1,0),normalDir);
   c.xyz += unity_AmbientGround.rgb * unity_AmbientGround.a * max(0, dot(groundDir, i.eyeDir)) * _AmbientIntensity; // ambient ground
   c.xyz = lerp(c.xyz,c.xyz * r.xyz * _ReflectBrightness,_ReflectIntensity); // reflection

   UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos); // shadow setup7:UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos)
   atten = (atten + 1) * 0.5;
   c.xyz *= atten; // lighting and shadow attenuation
   return c;
}

ENDCG
}
		}
			Fallback "Diffuse"
}

posted @ 2022-06-22 18:04  哒哒哒~~~  阅读(88)  评论(0编辑  收藏  举报