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"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#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)
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);
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);
fixed4 r = texCUBE(_ReflectCube, reflectDir);
if (_HalfLambert) {
c.xyz *= (dot(i.lightDir,normalDir) + 1) * 0.5;
}
else {
c.xyz *= dot(i.lightDir,normalDir);
}
c.xyz += c.xyz * UNITY_LIGHTMODEL_AMBIENT.rgb;
float specularDotEye = max(0, dot(specularDir, i.eyeDir));
c.xyz += (_LightColor0.rgb) * specularDotEye * _SpecularIntensity;
fixed3 skyDir = reflect(float3(0,-1,0),normalDir);
c.xyz += unity_AmbientSky.rgb * unity_AmbientSky.a * max(0, dot(skyDir, i.eyeDir)) * _AmbientIntensity;
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;
fixed3 groundDir = reflect(float3(0,1,0),normalDir);
c.xyz += unity_AmbientGround.rgb * unity_AmbientGround.a * max(0, dot(groundDir, i.eyeDir)) * _AmbientIntensity;
c.xyz = lerp(c.xyz,c.xyz * r.xyz * _ReflectBrightness,_ReflectIntensity);
UNITY_LIGHT_ATTENUATION(atten,i,i.worldPos);
atten = (atten + 1) * 0.5;
c.xyz *= atten;
return c;
}
ENDCG
}
}
Fallback "Diffuse"
}