DefaultShader.shader 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. Shader "Scene/DefaultShader" {
  2. Properties
  3. {
  4. _Color("Main Color", Color) = (1,1,1,1)
  5. _MainTex("Main Texture", 2D) = "white" {}
  6. _Strength("Strength", Range(0, 2)) = 2
  7. _ClrStrength("ClrStrength", Range(0, 1.7)) = 1.7
  8. _ShadowStrength("ShadowStrength", Range(0, 1)) = 0
  9. }
  10. SubShader
  11. {
  12. Tags { "RenderType"="Opaque" }
  13. Pass
  14. {
  15. Tags { "LightMode" = "ForwardBase" }
  16. CGPROGRAM
  17. #pragma vertex vert
  18. #pragma fragment frag
  19. #pragma multi_compile_fog
  20. #pragma multi_compile_fwdbase
  21. #include "HLSLSupport.cginc"
  22. #include "UnityCG.cginc"
  23. #include "Lighting.cginc"
  24. #include "AutoLight.cginc"
  25. sampler2D _MainTex;
  26. float4 _MainTex_ST;
  27. fixed4 _Color;
  28. fixed _Strength;
  29. half _ClrStrength;
  30. half _ShadowStrength;
  31. struct V2f {
  32. fixed4 pos : SV_POSITION;
  33. fixed2 uv0 : TEXCOORD0;
  34. #ifndef LIGHTMAP_ON
  35. fixed3 normal : TEXCOORD1;
  36. #endif
  37. #ifdef LIGHTMAP_ON
  38. float2 lmap : TEXCOORD2;
  39. #endif
  40. #ifndef LIGHTMAP_ON
  41. fixed3 vlight : TEXCOORD2;
  42. #endif
  43. LIGHTING_COORDS(3,4)
  44. UNITY_FOG_COORDS(5)
  45. UNITY_VERTEX_OUTPUT_STEREO
  46. };
  47. V2f vert(appdata_full v) {
  48. V2f o;
  49. o.uv0 = TRANSFORM_TEX(v.texcoord, _MainTex);
  50. float3 worldNormal = UnityObjectToWorldNormal(v.normal);
  51. fixed3 normalDir = worldNormal;
  52. o.pos = UnityObjectToClipPos(v.vertex);
  53. #ifdef LIGHTMAP_ON
  54. o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
  55. #endif
  56. #ifndef LIGHTMAP_ON
  57. o.normal = worldNormal;
  58. #endif
  59. #ifndef LIGHTMAP_ON
  60. fixed3 lightDirection = _WorldSpaceLightPos0.xyz;
  61. o.vlight = lerp(max(0,dot(lightDirection, normalDir))*_LightColor0,UNITY_LIGHTMODEL_AMBIENT.rgb,0.2 );
  62. #endif
  63. TRANSFER_VERTEX_TO_FRAGMENT(o);
  64. UNITY_TRANSFER_FOG(o,o.pos);
  65. return o;
  66. }
  67. fixed4 frag(V2f i) : SV_Target{
  68. fixed4 mainColor = tex2D(_MainTex, i.uv0) * _Color * _ClrStrength;
  69. fixed atten = LIGHT_ATTENUATION(i);
  70. fixed4 col = 1;
  71. #ifdef LIGHTMAP_ON
  72. fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmap.xy));
  73. #ifdef SHADOWS_SCREENqq
  74. col.rgb = mainColor.rgb * min(lm, atten*2);
  75. #else
  76. col.rgb = mainColor.rgb * lm;
  77. #endif
  78. fixed4 mixCol = mainColor + col;
  79. // 为了让阴影表现明显一些, 使用HSV值的v值来判断阴影部分
  80. half v = max(max(col.r, col.g), col.b);
  81. fixed4 shadowCol = lerp(col, mixCol, v);
  82. col.rgb = lerp(mixCol, shadowCol, _ShadowStrength).rgb;
  83. #else
  84. col.rgb = mainColor.rgb * i.vlight * atten;
  85. col.rgb = mainColor.rgb + col.rgb;
  86. #endif
  87. col.rgb = col.rgb * _Strength;
  88. UNITY_APPLY_FOG(i.fogCoord, col);
  89. UNITY_OPAQUE_ALPHA(col.a);
  90. return col;
  91. }
  92. ENDCG
  93. } //normal pass
  94. }
  95. FallBack "Mobile/Diffuse"
  96. }