浏览代码
Fixing various bugs in keypoint self occlusion, including (hopefully) properly supporting player builds
/keypoint_self_occlusion
Fixing various bugs in keypoint self occlusion, including (hopefully) properly supporting player builds
/keypoint_self_occlusion
Jon Hogins
4 年前
当前提交
2eff8c69
共有 8 个文件被更改,包括 290 次插入 和 152 次删除
-
41com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs
-
15com.unity.perception/Runtime/GroundTruth/RenderTextureReader.cs
-
54com.unity.perception/Tests/Runtime/GroundTruthTests/KeypointGroundTruthTests.cs
-
38com.unity.perception/Editor/GroundTruth/ShaderPreprocessor.cs
-
3com.unity.perception/Editor/GroundTruth/ShaderPreprocessor.cs.meta
-
166com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheck.shader
-
125com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheckHDRP.shader
-
0/com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheck.shader.meta
|
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using UnityEditor.Build; |
|||
using UnityEditor.Rendering; |
|||
using UnityEngine; |
|||
using UnityEngine.Rendering; |
|||
|
|||
namespace UnityEditor.Perception.GroundTruth |
|||
{ |
|||
public class ShaderPreprocessor : IPreprocessShaders |
|||
{ |
|||
private string[] shadersToPreprocess = new[] |
|||
{ |
|||
"Perception/KeypointDepthCheck" |
|||
}; |
|||
public int callbackOrder => 0; |
|||
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data) |
|||
{ |
|||
if (!shadersToPreprocess.Contains(shader.name)) |
|||
return; |
|||
|
|||
#if HDRP_PRESENT || URP_PRESENT
|
|||
#if HDRP_PRESENT
|
|||
ShaderKeyword keyword = new ShaderKeyword(shader, "HDRP_ENABLED"); |
|||
#else
|
|||
ShaderKeyword keyword = new ShaderKeyword(shader, "HDRP_DISABLED"); |
|||
#endif
|
|||
for (int i = data.Count - 1; i >= 0; --i) |
|||
{ |
|||
if (!data[i].shaderKeywordSet.IsEnabled(keyword)) |
|||
continue; |
|||
|
|||
data.RemoveAt(i); |
|||
} |
|||
#endif
|
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2156803809034108bf01e9ab96302a9f |
|||
timeCreated: 1619623320 |
|
|||
Shader "Perception/KeypointDepthCheck" |
|||
{ |
|||
Properties |
|||
{ |
|||
_Positions("Positions", 2D) = "defaultTexture" {} |
|||
_KeypointCheckDepth("KeypointCheckDepth", 2D) = "defaultTexture" {} |
|||
_DepthTexture("Depth", 2DArray) = "defaultTexture" {} |
|||
} |
|||
|
|||
HLSLINCLUDE |
|||
|
|||
#pragma target 4.5 |
|||
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch |
|||
|
|||
//enable GPU instancing support |
|||
#pragma multi_compile HDRP_DISABLED HDRP_ENABLED |
|||
#pragma multi_compile_instancing |
|||
|
|||
ENDHLSL |
|||
|
|||
SubShader |
|||
{ |
|||
Pass |
|||
{ |
|||
Tags { "LightMode" = "SRP" } |
|||
|
|||
Name "KeypointDepthCheck" |
|||
ZWrite Off |
|||
ZTest Always |
|||
Blend SrcAlpha OneMinusSrcAlpha |
|||
Cull Off |
|||
|
|||
HLSLPROGRAM |
|||
#pragma only_renderers d3d11 vulkan metal |
|||
#pragma target 4.5 |
|||
#pragma vertex Vert |
|||
#pragma fragment Frag |
|||
|
|||
#if HDRP_ENABLED |
|||
|
|||
|
|||
#pragma enable_d3d11_debug_symbols |
|||
|
|||
Texture2D _Positions; |
|||
SamplerState my_point_clamp_sampler; |
|||
Texture2D _KeypointCheckDepth; |
|||
|
|||
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl" |
|||
|
|||
float4 Frag(Varyings varyings) : SV_Target |
|||
{ |
|||
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings); |
|||
|
|||
float4 checkPosition = _Positions.Load(float3(varyings.positionCS.xy, 0)); |
|||
float checkDepth = _KeypointCheckDepth.Load(float3(varyings.positionCS.xy, 0)).r; |
|||
|
|||
float depth = LoadCameraDepth(float2(checkPosition.x, _ScreenSize.y - checkPosition.y)); |
|||
PositionInputs positionInputs = GetPositionInput(checkPosition, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V); |
|||
depth = positionInputs.linearDepth; |
|||
|
|||
uint result = depth >= checkDepth ? 1 : 0; |
|||
return float4(result, result, result, 1); |
|||
} |
|||
#else |
|||
#include "UnityCG.cginc" |
|||
|
|||
Texture2D _Positions; |
|||
SamplerState my_point_clamp_sampler; |
|||
Texture2D _KeypointCheckDepth; |
|||
|
|||
//copied from UnityInput.hlsl |
|||
float4x4 _InvViewProjMatrix; |
|||
float4x4 _InvProjMatrix; |
|||
|
|||
//#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" |
|||
|
|||
struct appdata |
|||
{ |
|||
float4 vertex : POSITION; |
|||
float2 uv : TEXCOORD0; |
|||
}; |
|||
|
|||
struct v2f |
|||
{ |
|||
float2 uv : TEXCOORD0; |
|||
float4 vertex : SV_POSITION; |
|||
}; |
|||
|
|||
v2f Vert (appdata v) |
|||
{ |
|||
v2f o; |
|||
o.vertex = UnityObjectToClipPos(v.vertex); |
|||
o.uv = v.uv; |
|||
return o; |
|||
} |
|||
|
|||
bool IsPerspectiveProjection() |
|||
{ |
|||
return unity_OrthoParams.w == 0; |
|||
} |
|||
float ViewSpaceDepth(float depth) |
|||
{ |
|||
if (IsPerspectiveProjection()) |
|||
return LinearEyeDepth(depth); |
|||
else |
|||
return _ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * (1 - depth); |
|||
} |
|||
|
|||
float EncodeAndDecodeDepth(float vsDepth) |
|||
{ |
|||
if (IsPerspectiveProjection()) |
|||
{ |
|||
//derived from vsDepth = 1.0 / (_ZBufferParams.z * dtDepth + _ZBufferParams.w); |
|||
float dtDepth = (1.0 / vsDepth - _ZBufferParams.w) / _ZBufferParams.z; |
|||
dtDepth = dtDepth; |
|||
return LinearEyeDepth(dtDepth); |
|||
} |
|||
else //in orthographic projections depth is linear so there is no loss of precision. |
|||
return vsDepth; |
|||
} |
|||
|
|||
Texture2D _CameraDepthTexture; |
|||
|
|||
float LoadSceneDepth(uint2 uv) |
|||
{ |
|||
return _CameraDepthTexture.Load(float3(uv, 0)).r; |
|||
} |
|||
|
|||
float4 ComputeClipSpacePosition(float2 positionNDC, float deviceDepth) |
|||
{ |
|||
float4 positionCS = float4(positionNDC * 2.0 - 1.0, deviceDepth, 1.0); |
|||
|
|||
#if UNITY_UV_STARTS_AT_TOP |
|||
// Our world space, view space, screen space and NDC space are Y-up. |
|||
// Our clip space is flipped upside-down due to poor legacy Unity design. |
|||
// The flip is baked into the projection matrix, so we only have to flip |
|||
// manually when going from CS to NDC and back. |
|||
positionCS.y = -positionCS.y; |
|||
#endif |
|||
|
|||
return positionCS; |
|||
} |
|||
|
|||
float3 ComputeWorldSpacePosition(float2 positionNDC, float deviceDepth, float4x4 invViewProjMatrix) |
|||
{ |
|||
float4 positionCS = ComputeClipSpacePosition(positionNDC, deviceDepth); |
|||
float4 hpositionWS = mul(invViewProjMatrix, positionCS); |
|||
return hpositionWS.xyz / hpositionWS.w; |
|||
} |
|||
fixed4 Frag (v2f i) : SV_Target |
|||
{ |
|||
float4 checkPosition = _Positions.Load(float3(i.vertex.xy, 0)); |
|||
float depthVSToCheck = _KeypointCheckDepth.Load(float3(i.vertex.xy, 0)).r; |
|||
|
|||
float depth = LoadSceneDepth(float2(checkPosition.x, _ScreenParams.y - checkPosition.y)); |
|||
float depthVSActual = ViewSpaceDepth(depth); |
|||
|
|||
uint result = depthVSActual >= depthVSToCheck ? 1 : 0; |
|||
return float4(result, result, result, 1); |
|||
} |
|||
#endif |
|||
ENDHLSL |
|||
} |
|||
} |
|||
Fallback Off |
|||
} |
|
|||
Shader "Perception/KeypointDepthCheck" |
|||
{ |
|||
Properties |
|||
{ |
|||
_Positions("Positions", 2D) = "defaultTexture" {} |
|||
_KeypointCheckDepth("KeypointCheckDepth", 2D) = "defaultTexture" {} |
|||
_DepthTexture("Depth", 2DArray) = "defaultTexture" {} |
|||
} |
|||
|
|||
HLSLINCLUDE |
|||
|
|||
#pragma target 4.5 |
|||
#pragma only_renderers d3d11 ps4 xboxone vulkan metal switch |
|||
|
|||
//enable GPU instancing support |
|||
#pragma multi_compile_instancing |
|||
|
|||
ENDHLSL |
|||
|
|||
SubShader |
|||
{ |
|||
Pass |
|||
{ |
|||
Tags { "LightMode" = "SRP" } |
|||
|
|||
Name "KeypointDepthCheck" |
|||
ZWrite Off |
|||
ZTest Always |
|||
Blend SrcAlpha OneMinusSrcAlpha |
|||
Cull Off |
|||
|
|||
HLSLPROGRAM |
|||
|
|||
#pragma multi_compile HDRP_DISABLED HDRP_ENABLED |
|||
#pragma enable_d3d11_debug_symbols |
|||
#pragma only_renderers d3d11 vulkan metal |
|||
#pragma target 4.5 |
|||
#pragma vertex Vert |
|||
#pragma fragment FullScreenPass |
|||
|
|||
|
|||
//UNITY_DECLARE_TEX2DARRAY(_DepthTexture); |
|||
//sampler2D _CameraDepthTexture; |
|||
|
|||
Texture2D _Positions; |
|||
SamplerState my_point_clamp_sampler; |
|||
Texture2D _KeypointCheckDepth; |
|||
|
|||
#if HDRP_ENABLED |
|||
|
|||
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassCommon.hlsl" |
|||
|
|||
float4 FullScreenPass(Varyings varyings) : SV_Target |
|||
{ |
|||
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings); |
|||
|
|||
float4 checkPosition = _Positions.Load(float3(varyings.positionCS.xy, 0)); |
|||
float4 checkDepth = _KeypointCheckDepth.Load(float3(varyings.positionCS.xy, 0)); |
|||
|
|||
float depth = LoadCameraDepth(float2(checkPosition.x, _ScreenSize.y - checkPosition.y)); |
|||
PositionInputs positionInputs = GetPositionInput(checkPosition, _ScreenSize.zw, depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V); |
|||
depth = positionInputs.linearDepth; |
|||
|
|||
//encode and decode checkDepth to account for loss of precision with depth values close to far plane |
|||
float4 viewPos = mul(UNITY_MATRIX_I_VP, float4(positionInputs.positionWS.x, positionInputs.positionWS.y, checkDepth.x, 1.0)); |
|||
float4 positionCheckWS = mul(UNITY_MATRIX_VP, viewPos); |
|||
|
|||
//depth = LinearEyeDepth(depth, _ZBufferParams); |
|||
// float depth = UNITY_SAMPLE_TEX2DARRAY(_DepthTexture, float3(checkPosition.xy, 0)).r; //SAMPLE_DEPTH_TEXTURE(_DepthTexture, checkPosition.xy); |
|||
//float depth_decoded = LinearEyeDepth(depth); |
|||
// float depth_decoded = Linear01Depth(depth); |
|||
uint result = depth >= positionCheckWS.z ? 1 : 0; |
|||
return float4(result, result, result, 1); |
|||
} |
|||
#else |
|||
|
|||
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" |
|||
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Filtering.hlsl" |
|||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" |
|||
#include "Packages/com.unity.render-pipelines.universal/Shaders/PostProcessing/Common.hlsl" |
|||
|
|||
bool IsPerspectiveProjection() |
|||
{ |
|||
return unity_OrthoParams.w == 0; |
|||
} |
|||
float ViewSpaceDepth(float depth) |
|||
{ |
|||
if (IsPerspectiveProjection()) |
|||
return LinearEyeDepth(depth, _ZBufferParams); |
|||
else |
|||
return _ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * (1 - depth); |
|||
} |
|||
|
|||
float4 FullScreenPass(Varyings varyings) : SV_Target |
|||
{ |
|||
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(varyings); |
|||
|
|||
float4 checkPosition = _Positions.Load(float3(varyings.positionCS.xy, 0)); |
|||
float checkDepth = _KeypointCheckDepth.Load(float3(varyings.positionCS.xy, 0)).r; |
|||
|
|||
float depth = LoadSceneDepth(float2(checkPosition.x, _ScreenParams.y - checkPosition.y)); |
|||
|
|||
depth = ViewSpaceDepth(depth); |
|||
|
|||
//encode and decode checkDepth to account for loss of precision with depth values close to far plane |
|||
PositionInputs positionInputs = GetPositionInput(checkPosition, _ScreenParams.zw - float2(1, 1), depth, UNITY_MATRIX_I_VP, UNITY_MATRIX_V); |
|||
float4 viewPos = mul(UNITY_MATRIX_V, float4(positionInputs.positionWS.x, positionInputs.positionWS.y, checkDepth, 1.0)); |
|||
float4 positionCheckWS = mul(UNITY_MATRIX_I_V, viewPos); |
|||
float depthCompare = positionCheckWS.z; |
|||
|
|||
//float depthCompare = checkDepth; |
|||
|
|||
//depth = LinearEyeDepth(depth, _ZBufferParams); |
|||
// float depth = UNITY_SAMPLE_TEX2DARRAY(_DepthTexture, float3(checkPosition.xy, 0)).r; //SAMPLE_DEPTH_TEXTURE(_DepthTexture, checkPosition.xy); |
|||
//float depth_decoded = LinearEyeDepth(depth); |
|||
// float depth_decoded = Linear01Depth(depth); |
|||
uint result = depth >= depthCompare ? 1 : 0; |
|||
return float4(result, result, result, 1); |
|||
} |
|||
#endif |
|||
ENDHLSL |
|||
} |
|||
} |
|||
Fallback Off |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue