1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
Shader "ITS/test/testFlashLight" { Properties { _NormalTex ("法线贴图", 2D) = "white" {}
_Specular ("高光颜色", Color) = (1, 1, 1, 1) _Gloss ("高光系数", Range(8, 256)) = 20
_RimColor ("边缘颜色", Color) = (1, 0, 0, 1) _RimPower ("边缘颜色强度", Range(0.1, 1)) = 1
_MaskTex ("光遮罩图", 2D) = "white" {} _MoveDir ("边缘光移动方向", Range(-1, 1)) = 1 _MainTex ("Base 2d", 2D) = "white" {} } SubShader { Tags { "LightMode" = "ForwardBase" }
Pass { CGPROGRAM #pragma vertex vert2 #pragma fragment frag2 #include "UnityCG.cginc" #include "Lighting.cginc"
struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; float4 tangent : TANGENT; };
struct v2f { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0; float3 lightDir : TEXCOORD1; float3 viewDir : TEXCOORD2; };
sampler2D _NormalTex;
fixed4 _Specular; float _Gloss;
fixed4 _RimColor; half _RimPower;
sampler2D _MaskTex; sampler2D _MainTex; fixed4 _MainTex_ST; fixed _MoveDir;
v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz; o.viewDir = mul(rotation, ObjSpaceViewDir(v.vertex)).xyz; return o; } fixed4 frag (v2f i) : SV_Target { fixed3 tangentLightDir = normalize(i.lightDir); fixed3 tangentViewDir = normalize(i.viewDir); fixed4 packedNormal = tex2D(_NormalTex, i.uv); fixed3 tangentNormal = UnpackNormal(packedNormal);
fixed4 tex = tex2D(_MainTex, i.uv); fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * tex.rgb;
fixed3 diffuse = _LightColor0.rgb * tex.rgb * saturate(dot(tangentNormal, tangentLightDir)); fixed3 halfDir = normalize(tangentLightDir + tangentViewDir); fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(tangentNormal, halfDir)), _Gloss); fixed dotProduct = 1 - saturate(dot(tangentNormal, tangentViewDir)); fixed3 rim = _RimColor.rgb * pow(dotProduct, 1 / _RimPower);
fixed4 maskCol = tex2D(_MaskTex, i.uv + float2(0, _Time.y * _MoveDir)); return fixed4(ambient + diffuse + specular + rim * maskCol.rgb, 1); }
struct appdata2 { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; float4 tangent : TANGENT; };
struct v2f2 { float4 vertex : SV_POSITION; float2 uv : TEXCOORD0;
float4 TtoW0 :TEXCOORD1; float4 TtoW1 :TEXCOORD2; float4 TtoW2 :TEXCOORD3;
float3 lightDir : TEXCOORD4; float3 viewDir : TEXCOORD5; };
v2f2 vert2 (appdata2 v) { v2f2 o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float3 worldPos = mul(unity_ObjectToWorld,v.vertex).xyz; o.lightDir = UnityWorldSpaceLightDir(worldPos); o.viewDir = UnityWorldSpaceViewDir(worldPos);
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal); fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz); fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x); o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y); o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z); return o; }
fixed4 frag2 (v2f2 i) : SV_Target { float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w); fixed3 worldLightDir = normalize(i.lightDir); fixed3 worldViewDir = normalize(i.viewDir);
fixed4 packedNormal = tex2D(_NormalTex, i.uv); fixed4 tex = tex2D(_MainTex, i.uv);
fixed3 tangentNormal = UnpackNormal(packedNormal);
fixed3 worldNormal = normalize(half3(dot(i.TtoW0.xyz, tangentNormal), dot(i.TtoW1.xyz, tangentNormal), dot(i.TtoW2.xyz, tangentNormal)));
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * tex.rgb;
fixed3 diffuse = _LightColor0.rgb * tex.rgb * saturate(dot(worldNormal, worldLightDir));
fixed3 halfDir = normalize(worldLightDir + worldViewDir); fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(saturate(dot(worldNormal, halfDir)), _Gloss); fixed dotProduct = 1 - saturate(dot(worldNormal, worldViewDir)); fixed3 rim = _RimColor.rgb * pow(dotProduct, 1 / _RimPower);
fixed4 maskCol = tex2D(_MaskTex, i.uv + float2(0, _Time.y * _MoveDir)); return fixed4(ambient + diffuse + specular + rim * maskCol.rgb, 1); } ENDCG } } }
|