源战役客户端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

142 行
4.5 KiB

  1. float4 _GlobalLightDir, _GlobalLightDir1;
  2. float4 _GlobalDirColor, _GlobalPointColor0, _GlobalPointColor1, _GlobalPointColor2, _GlobalPointColor3;
  3. float4 _GlobalPointX, _GlobalPointY, _GlobalPointZ, _GlobalPointAtten;
  4. //�ܽ�
  5. bool CheckDissolve(fixed modelVertexX, sampler2D _DissolveMap, float2 uv, fixed _Max, fixed _Min, fixed _DissolveThreshold, fixed _ColorFactorA, fixed4 _DissolveColorA) {
  6. fixed4 dissolveValue = tex2D(_DissolveMap, uv);
  7. fixed range = _Max - _Min;
  8. fixed mapping = (modelVertexX - _Min) / range;
  9. fixed finalThreshold = lerp(_DissolveThreshold, _DissolveThreshold / 10, saturate(mapping));
  10. if (dissolveValue.r < finalThreshold)
  11. {
  12. discard;
  13. return false;
  14. }
  15. fixed lerpValue = finalThreshold / dissolveValue.r;
  16. return lerpValue > _ColorFactorA;
  17. }
  18. half GGX_Mobile(half Roughness, half NoH)
  19. {
  20. float OneMinusNoHSqr = 1.0 - NoH * NoH;
  21. half a = Roughness * Roughness;
  22. float n = NoH * a;
  23. float p = a / (OneMinusNoHSqr + n * n);
  24. float d = min(32, p * p);
  25. return d;
  26. }
  27. half3 EnvBRDFApprox(half3 SpecularColor, half Roughness, half NoV)
  28. {
  29. const half4 c0 = { -1, -0.0275, -0.572, 0.022 };
  30. const half4 c1 = { 1, 0.0425, 1.04, -0.04 };
  31. half4 r = Roughness * c0 + c1;
  32. half a004 = min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
  33. half2 AB = half2(-1.04, 1.04) * a004 + r.zw;
  34. AB.y *= saturate(50.0 * SpecularColor.g);
  35. return SpecularColor * AB.x + AB.y;
  36. }
  37. float BRDFSpec(float noh, float loh, float r4, float r)
  38. {
  39. float a = (noh * noh) * (r4 - 1) + 1;
  40. a *= loh;
  41. float a2 = a * a;
  42. return min(32, r4 / (4 * UNITY_PI * a2 * (r + 0.5)));
  43. }
  44. float3 BRDFPointLights(float3 pos, float3 V, float3 N, float3 DiffuseColor, float3 SpecularColor, float Roughness)
  45. {
  46. float4 toLightX = _GlobalPointX - pos.x;
  47. float4 toLightY = _GlobalPointY - pos.y;
  48. float4 toLightZ = _GlobalPointZ - pos.z;
  49. float4 lengthSq = 0;
  50. lengthSq = toLightX * toLightX + toLightY * toLightY + toLightZ * toLightZ;
  51. lengthSq = max(lengthSq, 0.0001);
  52. float4 corr = rsqrt(lengthSq);
  53. toLightX *= corr;
  54. toLightY *= corr;
  55. toLightZ *= corr;
  56. float4 ndotl = 0;
  57. ndotl = toLightX * N.x + toLightY * N.y + toLightZ * N.z;
  58. ndotl = max(float4(0, 0, 0, 0), ndotl);
  59. float4 atten = clamp(1.0 - lengthSq * _GlobalPointAtten, 0, 1) / (1 + lengthSq);
  60. float4 diff = ndotl * atten;
  61. float3 col0 = _GlobalPointColor0.rgb * diff.x;
  62. float3 col1 = _GlobalPointColor1.rgb * diff.y;
  63. float3 col2 = _GlobalPointColor2.rgb * diff.z;
  64. float3 col3 = _GlobalPointColor3.rgb * diff.w;
  65. float3 col = (col0 + col1 + col2 + col3) * DiffuseColor / UNITY_PI;
  66. float4 HX = toLightX + V.x;
  67. float4 HY = toLightY + V.y;
  68. float4 HZ = toLightZ + V.z;
  69. lengthSq = 0;
  70. lengthSq = HX * HX + HY * HY + HZ * HZ;
  71. lengthSq = max(lengthSq, 0.000001);
  72. corr = rsqrt(lengthSq);
  73. HX *= corr;
  74. HY *= corr;
  75. HZ *= corr;
  76. float4 noh = 0, loh = 0;
  77. noh = HX * N.x + HY * N.y + HZ * N.z;
  78. noh = max(float4(0.01, 0.01, 0.01, 0.01), noh);
  79. loh = HX * toLightX + HY * toLightY + HZ * toLightZ;
  80. loh = max(float4(0.01, 0.01, 0.01, 0.01), loh);
  81. float r4 = Roughness * Roughness;
  82. r4 *= r4;
  83. col += BRDFSpec(noh.x, loh.x, r4, Roughness) * SpecularColor * col0;
  84. col += BRDFSpec(noh.y, loh.y, r4, Roughness) * SpecularColor * col1;
  85. col += BRDFSpec(noh.z, loh.z, r4, Roughness) * SpecularColor * col2;
  86. col += BRDFSpec(noh.w, loh.w, r4, Roughness) * SpecularColor * col3;
  87. return col;
  88. }
  89. half EnvBRDFApproxNonmetal(half Roughness, half NoV)
  90. {
  91. const half2 c0 = { -1, -0.0275 };
  92. const half2 c1 = { 1, 0.0425 };
  93. half2 r = Roughness * c0 + c1;
  94. return min(r.x * r.x, exp2(-9.28 * NoV)) * r.x + r.y;
  95. }
  96. float3 mod2D289(float3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
  97. float2 mod2D289(float2 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
  98. float3 permute(float3 x) { return mod2D289(((x * 34.0) + 1.0) * x); }
  99. float Snoise(float2 v)
  100. {
  101. const float4 C = float4(0.211324865405187, 0.366025403784439, -0.577350269189626, 0.024390243902439);
  102. float2 i = floor(v + dot(v, C.yy));
  103. float2 x0 = v - i + dot(i, C.xx);
  104. float2 i1;
  105. i1 = (x0.x > x0.y) ? float2(1.0, 0.0) : float2(0.0, 1.0);
  106. float4 x12 = x0.xyxy + C.xxzz;
  107. x12.xy -= i1;
  108. i = mod2D289(i);
  109. float3 p = permute(permute(i.y + float3(0.0, i1.y, 1.0)) + i.x + float3(0.0, i1.x, 1.0));
  110. float3 m = max(0.5 - float3(dot(x0, x0), dot(x12.xy, x12.xy), dot(x12.zw, x12.zw)), 0.0);
  111. m = m * m;
  112. m = m * m;
  113. float3 x = 2.0 * frac(p * C.www) - 1.0;
  114. float3 h = abs(x) - 0.5;
  115. float3 ox = floor(x + 0.5);
  116. float3 a0 = x - ox;
  117. m *= 1.79284291400159 - 0.85373472095314 * (a0 * a0 + h * h);
  118. float3 g;
  119. g.x = a0.x * x0.x + h.x * x0.y;
  120. g.yz = a0.yz * x12.xz + h.yz * x12.yw;
  121. return 130.0 * dot(m, g);
  122. }