浏览代码

HDRP: Improve camera relative rendering precision on object

Improve camera relative rendering precision on object
- Apply camera translation on the model matrix
- Add a function GetObjectPositionWS
- User are required to always used TransformObjectToWorld or GetObjectToWorldMatrix,  they should never access to UNITY_MATRIX_M
- When a user want absolute world position it must use  GetAbsolutePositionWS(positionWS).  positionWS is either camera relative space or absolute position depends on settings, only a call to GetAbsolutePositionWS() guarantet that we are in absolute position
/main
sebastienlagarde 6 年前
当前提交
ee31a3ad
共有 4 个文件被更改,包括 31 次插入8 次删除
  1. 9
      com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitDataMeshModification.hlsl
  2. 2
      com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassLightTransport.hlsl
  3. 6
      com.unity.render-pipelines.high-definition/HDRP/ShaderPass/VertMesh.hlsl
  4. 22
      com.unity.render-pipelines.high-definition/HDRP/ShaderVariablesFunctions.hlsl

9
com.unity.render-pipelines.high-definition/HDRP/Material/Lit/LitDataMeshModification.hlsl


// Note: positionWS can be either in camera relative space or not
float3 GetVertexDisplacement(float3 positionWS, float3 normalWS, float2 texCoord0, float2 texCoord1, float2 texCoord2, float2 texCoord3, float4 vertexColor)
{
// This call will work for both LayeredLit and Lit shader

return ComputePerVertexDisplacement(layerTexCoord, vertexColor, lod) * normalWS;
}
// Note: positionWS can be either in camera relative space or not
void ApplyVertexModification(AttributesMesh input, float3 normalWS, inout float3 positionWS, float4 time)
{
#if defined(_VERTEX_DISPLACEMENT)

#endif
#ifdef _VERTEX_WIND
float3 rootWP = mul(GetObjectToWorldMatrix(), float4(0, 0, 0, 1)).xyz;
ApplyWindDisplacement(positionWS, normalWS, rootWP, _Stiffness, _Drag, _ShiverDrag, _ShiverDirectionality, _InitialBend, input.color.a, time);
// current wind implementation is in absolute world space
float3 rootWP = GetObjectPositionWS();
float3 absolutePositionWS = GetAbsolutePositionWS(positionWS);
ApplyWindDisplacement(absolutePositionWS, normalWS, rootWP, _Stiffness, _Drag, _ShiverDrag, _ShiverDirectionality, _InitialBend, input.color.a, time);
positionWS = GetCameraRelativePositionWS(absolutePositionWS);
#endif
}

2
com.unity.render-pipelines.high-definition/HDRP/ShaderPass/ShaderPassLightTransport.hlsl


output.vmesh.positionCS = float4(uv * 2.0 - 1.0, inputMesh.positionOS.z > 0 ? 1.0e-4 : 0.0, 1.0);
#ifdef VARYINGS_NEED_POSITION_WS
float3 positionWS = GetCameraRelativePositionWS(TransformObjectToWorld(inputMesh.positionOS));
float3 positionWS = TransformObjectToWorld(inputMesh.positionOS);
output.vmesh.positionWS = positionWS;
#endif

6
com.unity.render-pipelines.high-definition/HDRP/ShaderPass/VertMesh.hlsl


UNITY_SETUP_INSTANCE_ID(input);
UNITY_TRANSFER_INSTANCE_ID(input, output);
// This return the camera relative position (if enable)
float3 positionWS = TransformObjectToWorld(input.positionOS);
#ifdef ATTRIBUTES_NEED_NORMAL
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);

float4 tangentWS = float4(TransformObjectToWorldDir(input.tangentOS.xyz), input.tangentOS.w);
#endif
// TODO: deal with camera center rendering and instancing (This is the reason why we always perform two steps transform to clip space + instancing matrix)
// Do vertex modification in camera relative space (if enable)
positionWS = GetCameraRelativePositionWS(positionWS);
#ifdef TESSELLATION_ON
output.positionWS = positionWS;

22
com.unity.render-pipelines.high-definition/HDRP/ShaderVariablesFunctions.hlsl


return UNITY_MATRIX_V;
}
// Return world position of current object
float3 GetObjectPositionWS()
{
float4x4 modelMatrix = UNITY_MATRIX_M;
return modelMatrix._m03_m13_m23; // Translation object to world
}
// Return the PreTranslated ObjectToWorld Matrix (i.e matrix with _WorldSpaceCameraPos apply to it if we use camera relative rendering)
return UNITY_MATRIX_M;
float4x4 modelMatrix = UNITY_MATRIX_M;
// To handle camera relative rendering we substract the camera position in the model matrix
// User must not use UNITY_MATRIX_M directly, unless they understand what they do
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
modelMatrix._m03_m13_m23 -= _WorldSpaceCameraPos;
#endif
return modelMatrix;
#if (SHADEROPTIONS_CAMERA_RELATIVE_RENDERING != 0)
// To handle camera relative rendering we need to apply translation before converting to object space
float4x4 modelMatrix = { {1.0, 0.0, 0.0, _WorldSpaceCameraPos.x}, { 0.0, 1.0, 0.0, _WorldSpaceCameraPos.y }, { 0.0, 0.0, 1.0, _WorldSpaceCameraPos.z }, { 0.0, 0.0, 0.0, 1.0 } };
return mul(UNITY_MATRIX_I_M, modelMatrix);
#else
#endif
}
// Transform to homogenous clip space

正在加载...
取消
保存