1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #pragma once
- #pragma import ViewSrg
- #include <Atom/Features/ParallaxMapping.azsli>
- // This file provides utilities for common handling of inputs for the parallax feature of materials.
- // These macros can be used to declare common shader inputs for this feature.
- // 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.
- // 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.
- #define COMMON_SRG_INPUTS_PARALLAX(prefix) \
- Texture2D prefix##m_heightmap; \
- float prefix##m_heightmapScale; \
- float prefix##m_heightmapOffset;
- #define COMMON_OPTIONS_PARALLAX(prefix) \
- option bool prefix##o_useHeightmap;
- void GetParallaxInput(const MaterialParameters params, Texture2D heightmap, sampler mapSampler, float3 normal, float3 tangent, float3 bitangent, float heightmapScale, float heightmapOffset,
- float4x4 objectWorldMatrix, real3x3 uvMatrix, real3x3 uvMatrixInverse,
- inout float2 uv, inout float3 worldPosition, inout float depthNDC, inout float depthCS, out bool isClipped)
- {
- isClipped = false;
- if(o_parallax_feature_enabled)
- {
- float3 dirToCamera;
- if(ViewSrg::m_projectionMatrix[3].w == 1)
- {
- // orthographic projection (directional light)
- // No view position, use light direction
- dirToCamera = float3(ViewSrg::m_viewMatrix[2].xyz);
- }
- else
- {
- dirToCamera = float3(ViewSrg::m_worldPosition.xyz - worldPosition);
- }
-
- ParallaxOffset tangentOffset = GetParallaxOffset( params,
- heightmap,
- mapSampler,
- heightmapScale,
- -heightmapOffset,
- uv,
- dirToCamera,
- tangent,
- bitangent,
- normal,
- uvMatrix);
- uv += float2(tangentOffset.m_offsetTS.xy);
- isClipped = tangentOffset.m_isClipped;
- if(o_parallax_enablePixelDepthOffset)
- {
- PixelDepthOffset pdo = CalcPixelDepthOffset(heightmapScale,
- float3(tangentOffset.m_offsetTS),
- worldPosition,
- tangent,
- bitangent,
- normal,
- uvMatrixInverse,
- objectWorldMatrix,
- float4x4(ViewSrg::m_viewProjectionMatrix));
- depthCS = pdo.m_depthCS;
- depthNDC = pdo.m_depthNDC;
- worldPosition = pdo.m_worldPosition;
- }
- }
- }
- void GetParallaxInput(const MaterialParameters params, Texture2D heightmap, sampler mapSampler, float3 normal, float3 tangent, float3 bitangent, float heightmapScale, float heightmapOffset,
- float4x4 objectWorldMatrix, float3x3 uvMatrix, float3x3 uvMatrixInverse,
- inout float2 uv, inout float3 worldPosition, inout float depthNDC, inout float depthCS)
- {
- bool isClipped = false;
- GetParallaxInput(params, heightmap, mapSampler, normal, tangent, bitangent, heightmapScale, heightmapOffset, objectWorldMatrix, uvMatrix, uvMatrixInverse, uv, worldPosition, depthNDC, depthCS, isClipped);
- }
- void GetParallaxInput(const MaterialParameters params, Texture2D heightmap, sampler mapSampler, float3 normal, float3 tangent, float3 bitangent, float heightmapScale, float heightmapOffset,
- float4x4 objectWorldMatrix, float3x3 uvMatrix, float3x3 uvMatrixInverse,
- inout float2 uv, inout float3 worldPosition, inout float depthNDC)
- {
- float depthCS;
- GetParallaxInput(params, heightmap, mapSampler, normal, tangent, bitangent, heightmapScale, heightmapOffset, objectWorldMatrix, uvMatrix, uvMatrixInverse, uv, worldPosition, depthNDC, depthCS);
- }
|