Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ParallaxMapping.hlsl 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #ifndef UNIVERSAL_PARALLAX_MAPPING_INCLUDED
  2. #define UNIVERSAL_PARALLAX_MAPPING_INCLUDED
  3. // Return view direction in tangent space, make sure tangentWS.w is already multiplied by GetOddNegativeScale()
  4. half3 GetViewDirectionTangentSpace(half4 tangentWS, half3 normalWS, half3 viewDirWS)
  5. {
  6. // must use interpolated tangent, bitangent and normal before they are normalized in the pixel shader.
  7. half3 unnormalizedNormalWS = normalWS;
  8. const half renormFactor = 1.0 / length(unnormalizedNormalWS);
  9. // use bitangent on the fly like in hdrp
  10. // IMPORTANT! If we ever support Flip on double sided materials ensure bitangent and tangent are NOT flipped.
  11. half crossSign = (tangentWS.w > 0.0 ? 1.0 : -1.0); // we do not need to multiple GetOddNegativeScale() here, as it is done in vertex shader
  12. half3 bitang = crossSign * cross(normalWS.xyz, tangentWS.xyz);
  13. half3 WorldSpaceNormal = renormFactor * normalWS.xyz; // we want a unit length Normal Vector node in shader graph
  14. // to preserve mikktspace compliance we use same scale renormFactor as was used on the normal.
  15. // This is explained in section 2.2 in "surface gradient based bump mapping framework"
  16. half3 WorldSpaceTangent = renormFactor * tangentWS.xyz;
  17. half3 WorldSpaceBiTangent = renormFactor * bitang;
  18. half3x3 tangentSpaceTransform = half3x3(WorldSpaceTangent, WorldSpaceBiTangent, WorldSpaceNormal);
  19. half3 viewDirTS = mul(tangentSpaceTransform, viewDirWS);
  20. return viewDirTS;
  21. }
  22. #ifndef BUILTIN_TARGET_API
  23. half2 ParallaxOffset1Step(half height, half amplitude, half3 viewDirTS)
  24. {
  25. height = height * amplitude - amplitude / 2.0;
  26. half3 v = normalize(viewDirTS);
  27. v.z += 0.42;
  28. return height * (v.xy / v.z);
  29. }
  30. #endif
  31. float2 ParallaxMapping(TEXTURE2D_PARAM(heightMap, sampler_heightMap), half3 viewDirTS, half scale, float2 uv)
  32. {
  33. half h = SAMPLE_TEXTURE2D(heightMap, sampler_heightMap, uv).g;
  34. float2 offset = ParallaxOffset1Step(h, scale, viewDirTS);
  35. return offset;
  36. }
  37. float2 ParallaxMappingChannel(TEXTURE2D_PARAM(heightMap, sampler_heightMap), half3 viewDirTS, half scale, float2 uv, int channel)
  38. {
  39. half h = SAMPLE_TEXTURE2D(heightMap, sampler_heightMap, uv)[channel];
  40. float2 offset = ParallaxOffset1Step(h, scale, viewDirTS);
  41. return offset;
  42. }
  43. #endif // UNIVERSAL_PARALLAX_MAPPING_INCLUDED