MTECommonPBR.hlsl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef MTECommonPBR_INCLUDED
  2. #define MTECommonPBR_INCLUDED
  3. void Max4WeightLayer(
  4. float4 splat_control_0,
  5. float4 splat_control_1,
  6. float4 splat_control_2,
  7. out float4 weights, out half4 indexes)
  8. {
  9. float control[12];
  10. control[ 0] = splat_control_0.r;
  11. control[ 1] = splat_control_0.g;
  12. control[ 2] = splat_control_0.b;
  13. control[ 3] = splat_control_0.a;
  14. control[ 4] = splat_control_1.r;
  15. control[ 5] = splat_control_1.g;
  16. control[ 6] = splat_control_1.b;
  17. control[ 7] = splat_control_1.a;
  18. control[ 8] = splat_control_2.r;
  19. control[ 9] = splat_control_2.g;
  20. control[10] = splat_control_2.b;
  21. control[11] = splat_control_2.a;
  22. weights = 0;
  23. indexes = 0;
  24. int i = 0;
  25. for (i = 0; i < 12; ++i)
  26. {
  27. float w = control[i];
  28. if (w >= weights[0])
  29. {
  30. weights[3] = weights[2];
  31. indexes[3] = indexes[2];
  32. weights[2] = weights[1];
  33. indexes[2] = indexes[1];
  34. weights[1] = weights[0];
  35. indexes[1] = indexes[0];
  36. weights[0] = w;
  37. indexes[0] = i;
  38. }
  39. else if (w >= weights[1])
  40. {
  41. weights[3] = weights[2];
  42. indexes[3] = indexes[2];
  43. weights[2] = weights[1];
  44. indexes[2] = indexes[1];
  45. weights[1] = w;
  46. indexes[1] = i;
  47. }
  48. else if (w >= weights[2])
  49. {
  50. weights[3] = weights[2];
  51. indexes[3] = indexes[2];
  52. weights[2] = w;
  53. indexes[2] = i;
  54. }
  55. else if (w >= weights[3])
  56. {
  57. weights[3] = w;
  58. indexes[3] = i;
  59. }
  60. //less weighted layers are ignored: not considered in weighted-blending
  61. }
  62. }
  63. // Reoriented Normal Mapping
  64. // http://blog.selfshadow.com/publications/blending-in-detail/
  65. // Altered to take normals (-1 to 1 ranges) rather than unsigned normal maps (0 to 1 ranges)
  66. half3 blend_rnm(half3 n1, half3 n2)
  67. {
  68. n1.z += 1;
  69. n2.xy = -n2.xy;
  70. return n1 * dot(n1, n2) / n1.z - n2;
  71. }
  72. // Restore tangent space normal.xyz from normal in [0, 1].
  73. float3 RestoreNormal(float2 normalXY, float intensity)
  74. {
  75. //first scale to [-1, 1]: now normalXY.xy is in tangent space
  76. normalXY = normalXY * 2 - 1;
  77. //reconstruct normal.z from normal.xy
  78. float reconstructZ = sqrt(1.0 - saturate(dot(normalXY, normalXY)));
  79. float3 normal = float3(normalXY, reconstructZ);
  80. normal = normalize(normal);
  81. #ifdef ENABLE_NORMAL_INTENSITY
  82. normal = lerp(normal, float3(0,0,1), - intensity + 1.0);//normal scaled by intensity
  83. #endif
  84. return normal;
  85. }
  86. // Scale tangent space normal from [-1, 1] to [0, 1], to be represented as normal map texel color
  87. half3 ScaleNormalTo01(half3 normalInTangentSpace)
  88. {
  89. return normalInTangentSpace = normalInTangentSpace * 0.5 + 0.5;
  90. }
  91. #endif // MTECommonPBR_INCLUDED