浏览代码

Fixing issues in URP with orthographic view

/keypoint_self_occlusion
Jon Hogins 3 年前
当前提交
9a8f7ac3
共有 7 个文件被更改,包括 65 次插入144 次删除
  1. 22
      com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs
  2. 58
      com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheckHDRP.shader
  3. 18
      com.unity.perception/Tests/Runtime/GroundTruthTests/KeypointGroundTruthTests.cs
  4. 3
      com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheck.shader.meta
  5. 17
      com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthTest.compute
  6. 8
      com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthTest.compute.meta
  7. 83
      com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheck.shader

22
com.unity.perception/Runtime/GroundTruth/Labelers/KeypointLabeler.cs


var depthCheckShader = Shader.Find("Perception/KeypointDepthCheck");
var shaderVariantCollection = new ShaderVariantCollection();
m_MaterialDepthCheck = new Material(depthCheckShader);
string keyword;
if (SRPSupport.GetCurrentPipelineRenderingType() == RenderingPipelineType.HDRP)
keyword = "HDRP_ENABLED";
else
keyword = "HDRP_DISABLED";
m_MaterialDepthCheck.EnableKeyword(keyword);
new ShaderVariantCollection.ShaderVariant(depthCheckShader, PassType.ScriptableRenderPipeline, "HDRP_ENABLED"));
new ShaderVariantCollection.ShaderVariant(depthCheckShader, PassType.ScriptableRenderPipeline, keyword));
m_MaterialDepthCheck = new Material(depthCheckShader);
//TODO: URP support
m_MaterialDepthCheck.EnableKeyword("HDRP_ENABLED");
#if URP_PRESENT
var cameraData = UnityEngine.Rendering.Universal.CameraExtensions.GetUniversalAdditionalCameraData(perceptionCamera.attachedCamera);
cameraData.requiresDepthOption = UnityEngine.Rendering.Universal.CameraOverrideOption.On;
cameraData.requiresDepthTexture = true;
#endif
perceptionCamera.InstanceSegmentationImageReadback += OnInstanceSegmentationImageReadback;
perceptionCamera.RenderedObjectInfosCalculated += OnRenderedObjectInfoReadback;

m_MaterialDepthCheck.SetTexture("_Positions", keypointPositionsTexture);
m_MaterialDepthCheck.SetTexture("_KeypointCheckDepth", keypointCheckDepthTexture);
m_MaterialDepthCheck.SetTexture("_DepthTexture", depthTexture);
m_MaterialDepthCheck.SetTexture("_CameraDepthTexture", depthTexture);
commandBuffer.Blit(null, m_ResultsBuffer, m_MaterialDepthCheck);

58
com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheckHDRP.shader


SubShader
{
Tags{ "RenderPipeline" = "HDRenderPipeline" }
Pass
{
Tags { "LightMode" = "SRP" }

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

return float4(result, result, result, 1);
}
#else
/// Dummy Implementation for non HDRP_ENABLED variants
#include "UnityCG.cginc"
#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"
struct appdata
bool IsPerspectiveProjection()
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
return unity_OrthoParams.w == 0;
}
float ViewSpaceDepth(float depth)
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f Vert(appdata v)
{
v2f o;
o.uv = float2(0, 0);
o.vertex = float4(0, 0, 0, 0);
return o;
if (IsPerspectiveProjection())
return LinearEyeDepth(depth, _ZBufferParams);
else
return _ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * (1 - depth);
float4 FullScreenPass(v2f i) : SV_Target
float4 FullScreenPass(Varyings varyings) : SV_Target
return float4(0, 0, 0, 1);
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

18
com.unity.perception/Tests/Runtime/GroundTruthTests/KeypointGroundTruthTests.cs


AddTestObjectForCleanup(cam);
AddTestObjectForCleanup(cube);
//for (int i = 0; i < 10000; i++)
yield return null;
//force all async readbacks to complete

camComponent.orthographic = true;
camComponent.orthographicSize = .5f;
var cube = TestHelper.CreateLabeledCube(scale: args.scale, z: 8);
//For some reason the back of this cube is being resolved to 7.5 away from the camera, but on the CPU side it is being recorded as 18.34375
var cube = TestHelper.CreateLabeledCube(scale: args.scale, z: 0);
SetupCubeJoints(cube, template);
cube.SetActive(true);

false);
yield return (
Vector3.zero,
0.01f,
0.005f,
0.1f,
0.05f,
new Vector3(0, 0, 950),
0.01f,
0.005f,
new Vector3(0, 0, 88),
0.1f,
0.05f,
new Vector3(0, 0, 950),
new Vector3(0, 0, 88),
1f,
2f,
1f,

}, texture, defaultSelfOcclusionDistance: defaultSelfOcclusionDistance);
var camComponent = cam.GetComponent<Camera>();
camComponent.fieldOfView = args.cameraFieldOfView;
camComponent.farClipPlane = 100f;
if (projectionKind == ProjectionKind.Orthographic)
{

3
com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheck.shader.meta


fileFormatVersion: 2
guid: 5fd132f5a09c4301b4c9004b241e2659
timeCreated: 1618585131

17
com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthTest.compute


// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
// Create a RenderTexture with enableRandomWrite flag and set it
// with cs.SetTexture
StructuredBuffer<float3> CheckPositions;
Texture3D<float4> DepthBuffer;
RWStructuredBuffer<uint> CheckResults;
[numthreads(16,1,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float3 checkPosition = CheckPositions[id.x];
uint result = DepthBuffer[float3(checkPosition.x, checkPosition.y, 0)] < checkPosition.z - .001 ? 0 : 1;
CheckResults[id.x] = result;
}

8
com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthTest.compute.meta


fileFormatVersion: 2
guid: c78354f07f15bac48977fc60cc5082c9
ComputeShaderImporter:
externalObjects: {}
currentAPIMask: 4
userData:
assetBundleName:
assetBundleVariant:

83
com.unity.perception/Runtime/GroundTruth/Resources/KeypointDepthCheck.shader


Shader "Perception/KeypointDepthCheckOld"
{
Properties
{
_Positions("Positions", 2D) = "defaultTexture" {}
_KeypointDepth("KeypointDepth", 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
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
Tags { "LightMode" = "SRP" }
Blend Off
ZWrite On
ZTest LEqual
Cull Back
CGPROGRAM
#pragma vertex depthCheckVertexStage
#pragma fragment depthCheckFragmentStage
#include "UnityCG.cginc"
UNITY_DECLARE_TEX2DARRAY(_DepthTexture);
//sampler2D _CameraDepthTexture;
Texture2D _Positions;
SamplerState my_point_clamp_sampler;
Texture2D _KeypointDepth;
struct in_vert
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct vertexToFragment
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
vertexToFragment depthCheckVertexStage (in_vert vertWorldSpace)
{
vertexToFragment vertScreenSpace;
vertScreenSpace.vertex = UnityObjectToClipPos(vertWorldSpace.vertex);
vertScreenSpace.uv = vertWorldSpace.uv;
return vertScreenSpace;
}
fixed4 depthCheckFragmentStage (vertexToFragment vertScreenSpace) : SV_Target
{
float4 checkPosition = _Positions.Sample(my_point_clamp_sampler, vertScreenSpace.uv);
float4 checkDepth = _KeypointDepth.Sample(my_point_clamp_sampler, vertScreenSpace.uv);
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_decoded < checkDepth.x - .001 ? 0 : 1;
return fixed4(result, result, result, result);
}
ENDCG
}
}
}
正在加载...
取消
保存