FantacyLivings_uiShow.shader 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
  2. ///
  3. /// ----- Allen 2018 -----
  4. /// 尝试使用rim texture和 specular texture
  5. /// support lambert diffuse && blinn specular
  6. /// 支持Unity线性的雾
  7. Shader "Am/FantacyQuanlity/livings_ui"
  8. {
  9. Properties {
  10. //diffuse 贴图
  11. _MainTex("Base(RGB)", 2D) = "white"{}
  12. //边缘发光
  13. _RimLookup("Rim LUT(LookUp Table)", 2D) = "white" {}
  14. _SpecularLookup("Specular LUT(LookUp Table", 2D) = "white" {}
  15. _LightProbeDiffuseStrength("LightProbe Diff Strength", Vector) = (0.5, 0.5, 0.5)
  16. _RealtimeDiffuseStrength("Realtime Diff Strength", Vector) = (0.5, 0.5, 0.5)
  17. _RealtimeLightColor("RealTime Light Color", Color) = (0.5, 0.5, 0.5)
  18. _RealtimeLightDirection("RealTime Light Direction", Vector) = (0, -0.5, 0)
  19. //最终决定贴图的alpha
  20. _Alpha("alpha", Range(0, 1)) = 1
  21. _DiffuseStr("Diffuse Strength", Range(0, 2)) = 0.5
  22. _LightProbeRimStrength("LightProbe Rim Strength", float) = 0.5
  23. _LightProbeSpecularStrength("LightProbe Spec Strength", Range(0, 1)) = 0.5
  24. _RealtimeRimStrength("RealTime Rim Strength", float) = 0.5
  25. _RealtimeSpecularStrength("RealTime Specular Strength", Range(0, 1)) = 0.5
  26. _RimClamp("Rim Clamp", float) = 0.5
  27. _RimColor("Rim Color", Color) = (1.0, 1.0, 1.0, 1.0)
  28. _RimStr("Rim Strength", float) = 0.5
  29. _RimSpecularMaskStrength("Rim Specular Mask Strength", float) = 0.5
  30. _SpecularStr("Specular Strength", Range(0, 1)) = 0.5
  31. _FlowColor("Flow Color", Color) = (1,1,1,1)
  32. _FlowRange("Flow Range", Float) = 0.01
  33. _BrightScale("Light scale", Float) = 0.1
  34. }
  35. SubShader {
  36. Tags { "Queue"="Geometry" "RenderType"="Opaque" "LightMode"="ForwardBase" }
  37. LOD 100
  38. Pass {
  39. CGPROGRAM
  40. #pragma shader_feature _SCAN_ON
  41. #pragma vertex vert
  42. #pragma fragment frag
  43. #pragma target 2.0
  44. #include "UnityCG.cginc"
  45. struct appdata_t
  46. {
  47. float4 vertex : POSITION;
  48. float2 texcoord : TEXCOORD0;
  49. float3 normal : NORMAL;
  50. };
  51. struct v2f
  52. {
  53. float4 vertex : SV_POSITION;
  54. float4 texcoord : TEXCOORD0; //第0,1是UV0信息, 第2是雾的距离,第3是fresnel值
  55. float2 angle : TEXCOORD1; //0 : lightprobe specular(UV) 1 : realtime light specular(UV)
  56. float3 probecolor : COLOR0;
  57. float3 color : COLOR1;
  58. float nr : TEXCOORD2;
  59. };
  60. sampler2D _MainTex;
  61. sampler2D _RimLookup;
  62. sampler2D _SpecularLookup;
  63. float4 _MainTex_ST;
  64. float3 _RealtimeLightColor;
  65. float3 _RealtimeLightDirection;
  66. float3 _RealtimeDiffuseStrength;
  67. float3 _LightProbeDiffuseStrength;
  68. float _Alpha;
  69. float _DiffuseStr;
  70. float _LightProbeRimStrength;
  71. float _LightProbeSpecularStrength;
  72. float _RealtimeRimStrength;
  73. float _RealtimeSpecularStrength;
  74. float _RimClamp;
  75. float3 _RimColor;
  76. float _RimStr;
  77. float _RimSpecularMaskStrength;
  78. float _SpecularStr;
  79. float4 _FlowColor;
  80. float _FlowRange;
  81. float _BrightScale;
  82. v2f vert (appdata_t v)
  83. {
  84. v2f o;
  85. UNITY_INITIALIZE_OUTPUT(v2f,o);
  86. o.vertex = UnityObjectToClipPos(v.vertex);
  87. // Transforms 2D UV by scale/bias property
  88. o.texcoord.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
  89. //获得世界坐标中的normal
  90. float3 worldNormal = normalize(mul((float3x3)unity_ObjectToWorld, v.normal));
  91. float3 worldlook = normalize(_WorldSpaceCameraPos - mul(unity_ObjectToWorld, v.vertex).xyz);
  92. //for fresnel
  93. o.texcoord.w = dot(worldlook, worldNormal);
  94. //use lambert's law diffuse
  95. float3 rt_dir_lit = -_RealtimeLightDirection;
  96. float ndot = max(0, dot(worldNormal, rt_dir_lit));
  97. float3 color = _RealtimeLightColor * ndot * _RealtimeDiffuseStrength;
  98. o.color = color + fixed3(1, 1, 1) * _BrightScale * max(0, dot(v.normal, rt_dir_lit));
  99. return o;
  100. }
  101. fixed4 frag (v2f i) : SV_Target
  102. {
  103. fixed4 col = tex2D(_MainTex, i.texcoord.xy);
  104. col.rgb = col.rgb * _DiffuseStr;
  105. fixed fresnel = i.texcoord.w;
  106. fixed3 rtspec = _RealtimeLightColor * tex2D(_SpecularLookup, fixed2(i.angle.y, i.angle.y)) * _RealtimeSpecularStrength;
  107. fixed3 reflectionContribution = rtspec * col.a * _SpecularStr;
  108. col.rgb = col.rgb * i.color.rgb;
  109. col.rgb = col.rgb + reflectionContribution;
  110. fixed4 rimcolor = tex2D(_RimLookup, float2(fresnel, fresnel));
  111. col.rgb = col.rgb + _RimColor.rgb * rimcolor.rgb * _RimStr;
  112. col.a = _Alpha;
  113. return col;
  114. }
  115. ENDCG
  116. }
  117. }
  118. }