ParallaxInput.azsli 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #pragma import ViewSrg
  10. #include <Atom/Features/ParallaxMapping.azsli>
  11. // This file provides utilities for common handling of inputs for the parallax feature of materials.
  12. // These macros can be used to declare common shader inputs for this feature.
  13. // Use the COMMON_SRG_INPUTS_* macro in your material SRG definition, and use the COMMON_OPTIONS_* macro at the global scope in your shader. Then you can pass these variables to the Get*Input() function below.
  14. // You can optionally provide a prefix for the set of inputs which corresponds to a prefix string supplied by the .materialtype file. This is common for multi-layered material types.
  15. #define COMMON_SRG_INPUTS_PARALLAX(prefix) \
  16. Texture2D prefix##m_heightmap; \
  17. float prefix##m_heightmapScale; \
  18. float prefix##m_heightmapOffset;
  19. #define COMMON_OPTIONS_PARALLAX(prefix) \
  20. option bool prefix##o_useHeightmap;
  21. void GetParallaxInput(const MaterialParameters params, Texture2D heightmap, sampler mapSampler, float3 normal, float3 tangent, float3 bitangent, float heightmapScale, float heightmapOffset,
  22. float4x4 objectWorldMatrix, real3x3 uvMatrix, real3x3 uvMatrixInverse,
  23. inout float2 uv, inout float3 worldPosition, inout float depthNDC, inout float depthCS, out bool isClipped)
  24. {
  25. isClipped = false;
  26. if(o_parallax_feature_enabled)
  27. {
  28. float3 dirToCamera;
  29. if(ViewSrg::m_projectionMatrix[3].w == 1)
  30. {
  31. // orthographic projection (directional light)
  32. // No view position, use light direction
  33. dirToCamera = float3(ViewSrg::m_viewMatrix[2].xyz);
  34. }
  35. else
  36. {
  37. dirToCamera = float3(ViewSrg::m_worldPosition.xyz - worldPosition);
  38. }
  39. ParallaxOffset tangentOffset = GetParallaxOffset( params,
  40. heightmap,
  41. mapSampler,
  42. heightmapScale,
  43. -heightmapOffset,
  44. uv,
  45. dirToCamera,
  46. tangent,
  47. bitangent,
  48. normal,
  49. uvMatrix);
  50. uv += float2(tangentOffset.m_offsetTS.xy);
  51. isClipped = tangentOffset.m_isClipped;
  52. if(o_parallax_enablePixelDepthOffset)
  53. {
  54. PixelDepthOffset pdo = CalcPixelDepthOffset(heightmapScale,
  55. float3(tangentOffset.m_offsetTS),
  56. worldPosition,
  57. tangent,
  58. bitangent,
  59. normal,
  60. uvMatrixInverse,
  61. objectWorldMatrix,
  62. float4x4(ViewSrg::m_viewProjectionMatrix));
  63. depthCS = pdo.m_depthCS;
  64. depthNDC = pdo.m_depthNDC;
  65. worldPosition = pdo.m_worldPosition;
  66. }
  67. }
  68. }
  69. void GetParallaxInput(const MaterialParameters params, Texture2D heightmap, sampler mapSampler, float3 normal, float3 tangent, float3 bitangent, float heightmapScale, float heightmapOffset,
  70. float4x4 objectWorldMatrix, float3x3 uvMatrix, float3x3 uvMatrixInverse,
  71. inout float2 uv, inout float3 worldPosition, inout float depthNDC, inout float depthCS)
  72. {
  73. bool isClipped = false;
  74. GetParallaxInput(params, heightmap, mapSampler, normal, tangent, bitangent, heightmapScale, heightmapOffset, objectWorldMatrix, uvMatrix, uvMatrixInverse, uv, worldPosition, depthNDC, depthCS, isClipped);
  75. }
  76. void GetParallaxInput(const MaterialParameters params, Texture2D heightmap, sampler mapSampler, float3 normal, float3 tangent, float3 bitangent, float heightmapScale, float heightmapOffset,
  77. float4x4 objectWorldMatrix, float3x3 uvMatrix, float3x3 uvMatrixInverse,
  78. inout float2 uv, inout float3 worldPosition, inout float depthNDC)
  79. {
  80. float depthCS;
  81. GetParallaxInput(params, heightmap, mapSampler, normal, tangent, bitangent, heightmapScale, heightmapOffset, objectWorldMatrix, uvMatrix, uvMatrixInverse, uv, worldPosition, depthNDC, depthCS);
  82. }