SampleUVMappingNormalInternal.hlsl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. real3 ADD_FUNC_SUFFIX(ADD_NORMAL_FUNC_SUFFIX(SampleUVMappingNormal))(TEXTURE2D_PARAM(textureName, samplerName), UVMapping uvMapping, real scale, real param)
  2. {
  3. if (uvMapping.mappingType == UV_MAPPING_TRIPLANAR)
  4. {
  5. real3 triplanarWeights = uvMapping.triplanarWeights;
  6. #ifdef SURFACE_GRADIENT
  7. // Height map gradient. Basically, it encodes height map slopes along S and T axes.
  8. real2 derivXplane;
  9. real2 derivYPlane;
  10. real2 derivZPlane;
  11. derivXplane = derivYPlane = derivZPlane = real2(0.0, 0.0);
  12. if (triplanarWeights.x > 0.0)
  13. derivXplane = triplanarWeights.x * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param), scale);
  14. if (triplanarWeights.y > 0.0)
  15. derivYPlane = triplanarWeights.y * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param), scale);
  16. if (triplanarWeights.z > 0.0)
  17. derivZPlane = triplanarWeights.z * UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param), scale);
  18. // Assume derivXplane, derivYPlane and derivZPlane sampled using (z,y), (z,x) and (x,y) respectively.
  19. // TODO: Check with morten convention! Do it follow ours ?
  20. real3 volumeGrad = real3(derivZPlane.x + derivYPlane.x, derivZPlane.y + derivXplane.y, derivXplane.x + derivYPlane.y);
  21. return SurfaceGradientFromVolumeGradient(uvMapping.normalWS, volumeGrad);
  22. #else
  23. real3 val = real3(0.0, 0.0, 0.0);
  24. if (triplanarWeights.x > 0.0)
  25. val += triplanarWeights.x * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvZY, param), scale);
  26. if (triplanarWeights.y > 0.0)
  27. val += triplanarWeights.y * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXZ, param), scale);
  28. if (triplanarWeights.z > 0.0)
  29. val += triplanarWeights.z * UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uvXY, param), scale);
  30. return normalize(val);
  31. #endif
  32. }
  33. #ifdef SURFACE_GRADIENT
  34. else if (uvMapping.mappingType == UV_MAPPING_PLANAR)
  35. {
  36. // Note: Planar is on uv coordinate (and not uvXZ)
  37. real2 derivYPlane = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale);
  38. // See comment above
  39. real3 volumeGrad = real3(derivYPlane.x, 0.0, derivYPlane.y);
  40. return SurfaceGradientFromVolumeGradient(uvMapping.normalWS, volumeGrad);
  41. }
  42. #endif
  43. else
  44. {
  45. #ifdef SURFACE_GRADIENT
  46. real2 deriv = UNPACK_DERIVATIVE_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale);
  47. return SurfaceGradientFromTBN(deriv, uvMapping.tangentWS, uvMapping.bitangentWS);
  48. #else
  49. return UNPACK_NORMAL_FUNC(SAMPLE_TEXTURE_FUNC(textureName, samplerName, uvMapping.uv, param), scale);
  50. #endif
  51. }
  52. }