Skip to content

Commit 4137b59

Browse files
committed
ARDK UPM 3.11
1 parent 16f0520 commit 4137b59

File tree

104 files changed

+4610
-1952
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+4610
-1952
lines changed

Assets/Shaders/LightshipScanningStripes.shader

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Shader "Unlit/LightshipScanningStripes"
88
_PositionAndConfidenceTex("RaycastPositionAndConfidence", 2D) = "white" {}
99
_ScreenOrientation("ScreenOrientation", int) = 0
1010
_StripeColor ("StripeColor", Color) = (1,0,0,1)
11+
_ArCameraTex ("ArCameraImage", 2D) = "white" {}
1112
}
1213
SubShader
1314
{
@@ -24,8 +25,10 @@ Shader "Unlit/LightshipScanningStripes"
2425
fixed4 _StripeColor;
2526
sampler2D _MainTex;
2627
sampler2D _ColorTex;
28+
sampler2D _ArCameraTex;
2729
float4 _MainTex_TexelSize;
2830
float4 _ColorTex_TexelSize;
31+
float4 _ArCameraTex_TexelSize;
2932
int _ScreenOrientation;
3033

3134
struct v2f
@@ -66,17 +69,33 @@ Shader "Unlit/LightshipScanningStripes"
6669
return uv * scale + 0.5f;
6770
}
6871

72+
// Returns cropped camera buffer coordinates to match the raycast buffer.
73+
inline float2 CropCameraImage(float2 uv)
74+
{
75+
uv -= 0.5f;
76+
float outAspect = _ColorTex_TexelSize.z / _ColorTex_TexelSize.w;
77+
// Inverted aspect to account for raycast rotated to landscape left
78+
float inAspect = _ArCameraTex_TexelSize.w / _ArCameraTex_TexelSize.z;
79+
80+
// If the camera image is wider than the raycast, crop the width.
81+
float uScaled = inAspect > outAspect ? uv.x * outAspect / inAspect : uv.x;
82+
// If the camera image is taller than the raycast, crop the height.
83+
float vScaled = inAspect < outAspect ? uv.y * inAspect / outAspect : uv.y;
84+
return float2(uScaled, vScaled) + 0.5f;
85+
}
86+
6987
fixed4 frag (v2f i, UNITY_VPOS_TYPE screenPos : VPOS) : SV_Target
7088
{
7189
// Sample the raycast color from _ColorTex.
7290
fixed4 color = tex2D(_ColorTex, GetRaycastSampleUV(i.uv));
91+
fixed4 cameraColor = tex2D(_ArCameraTex, CropCameraImage(i.uv));
7392

7493
// Compute the color of the diagonal stripes.
7594
unsigned int stripeCoord = ((int) (screenPos.x + _ScreenParams.y - screenPos.y)) & 15;
7695
fixed4 stripeColor = (stripeCoord < 10) ? _StripeColor : 1;
7796

78-
// Blend raycast color over the stripe color.
79-
return color * color.a + stripeColor * (1 - color.a);
97+
// Blend raycast color, AR camera view and the stripe color.
98+
return cameraColor * color.a / 2 + color * color.a / 2 + stripeColor * (1 - color.a);
8099
}
81100
ENDCG
82101
}

Assets/Shaders/OcclusionMesh.shader

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
Shader "Lightship/OcclusionMesh"
2+
{
3+
Properties
4+
{
5+
// Retrievable properties
6+
_Depth ("DepthTexture", 2D) = "black" {}
7+
_ColorMask ("Color Mask", Float) = 0
8+
}
9+
SubShader
10+
{
11+
Tags { "RenderType"="Opaque" }
12+
LOD 100
13+
14+
Pass
15+
{
16+
Offset 1, 1
17+
ColorMask [_ColorMask]
18+
19+
CGPROGRAM
20+
#pragma vertex vert
21+
#pragma fragment frag
22+
#pragma target 3.5
23+
24+
#pragma multi_compile_local __ FEATURE_SUPPRESSION
25+
#pragma multi_compile_local __ FEATURE_STABILIZATION
26+
27+
#include "UnityCG.cginc"
28+
29+
struct appdata
30+
{
31+
float4 vertex : POSITION;
32+
uint vid : SV_VertexID;
33+
34+
UNITY_VERTEX_INPUT_INSTANCE_ID
35+
};
36+
37+
struct v2f
38+
{
39+
float4 pos : SV_POSITION;
40+
half4 color : COLOR;
41+
42+
UNITY_VERTEX_INPUT_INSTANCE_ID
43+
UNITY_VERTEX_OUTPUT_STEREO
44+
};
45+
46+
sampler2D _Depth;
47+
48+
#ifdef FEATURE_STABILIZATION
49+
sampler2D _FusedDepth;
50+
float4x4 _FusedDepthTransform;
51+
#endif
52+
53+
#ifdef FEATURE_SUPPRESSION
54+
sampler2D _Suppression;
55+
float4x4 _SuppressionTransform;
56+
#endif
57+
58+
// Matrices
59+
float4 _Intrinsics;
60+
float4x4 _Extrinsics;
61+
62+
float _UnityCameraForwardScale;
63+
float _StabilizationThreshold;
64+
int _ImageWidth;
65+
int _ImageHeight;
66+
67+
inline float ScaleEyeDepth(float d)
68+
{
69+
d = _UnityCameraForwardScale > 0.0 ? _UnityCameraForwardScale * d : d;
70+
return d < _ProjectionParams.y ? 0.0f : d;
71+
}
72+
73+
inline float4 WorldToClipPos(float3 posWorld)
74+
{
75+
float4 clipPos;
76+
#if defined(STEREO_CUBEMAP_RENDER_ON)
77+
float3 offset = ODSOffset(posWorld, unity_HalfStereoSeparation.x);
78+
clipPos = mul(UNITY_MATRIX_VP, float4(posWorld + offset, 1.0));
79+
#else
80+
clipPos = mul(UNITY_MATRIX_VP, float4(posWorld, 1.0));
81+
#endif
82+
return clipPos;
83+
}
84+
85+
v2f vert (appdata v)
86+
{
87+
v2f o;
88+
89+
UNITY_SETUP_INSTANCE_ID(v);
90+
UNITY_INITIALIZE_OUTPUT(v2f, o);
91+
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
92+
93+
// Get the pixel coordinates for the vertex
94+
uint2 pixel = uint2(v.vid % _ImageWidth, v.vid / _ImageHeight);
95+
96+
// Calculate UV coordinates for the vertex
97+
float4 uv = float4(pixel.x / (float)_ImageWidth, pixel.y / (float)_ImageHeight, 0.0f, 0.0f);
98+
uv.y = 1.0f - uv.y; // Flip y-axis (native image is mirrored)
99+
100+
// Sample depth
101+
float eye_depth = tex2Dlod(_Depth, uv).r;
102+
103+
#ifdef FEATURE_STABILIZATION
104+
// Calculate UV coordinates for the fused depth
105+
// This is not a native image, so we have to un-invert the y-axis
106+
float4 fusedDepthUV = mul(_FusedDepthTransform, float4(uv.x, 1.0f - uv.y, 1.0f, 1.0f));
107+
108+
// Sample fused depth
109+
const float eps = 0.1f;
110+
float fused_depth_linear_eye =
111+
tex2Dlod(_FusedDepth, float4(fusedDepthUV.x / fusedDepthUV.z, fusedDepthUV.y / fusedDepthUV.z, 0, 0)) + eps;
112+
113+
// Determine the depth value
114+
bool useFrameDepth = (abs(fused_depth_linear_eye - eye_depth) / fused_depth_linear_eye) >= _StabilizationThreshold;
115+
eye_depth = useFrameDepth ? eye_depth : fused_depth_linear_eye;
116+
#endif
117+
118+
// Convert from image space to view space
119+
float4 view_position;
120+
view_position.x = (pixel.x - _Intrinsics.z) * eye_depth / _Intrinsics.x;
121+
view_position.y = (pixel.y - _Intrinsics.w) * eye_depth / _Intrinsics.y;
122+
view_position.z = ScaleEyeDepth(eye_depth);
123+
view_position.w = 1.0f;
124+
125+
#ifdef FEATURE_SUPPRESSION
126+
// Sample the suppression mask
127+
float4 suppression_uv = mul(_SuppressionTransform, float4(uv.x, 1.0f - uv.y, 1.0f, 1.0f));
128+
float mask = tex2Dlod(_Suppression, float4(suppression_uv.x / suppression_uv.z, suppression_uv.y / suppression_uv.z, 0, 0)).r;
129+
130+
// Determine whether to offset the vertex
131+
float scalar = mask > 0.5f ? 1000.0f : 1.0f;
132+
view_position = view_position * scalar;
133+
view_position.w = 1.0f;
134+
#endif
135+
136+
// Transform to world space
137+
float4 world_position = mul(_Extrinsics, view_position);
138+
139+
// Transform to clip space
140+
o.pos = WorldToClipPos(world_position.xyz);
141+
142+
// Set colors to debug UV coordinates
143+
o.color.x = uv.x;
144+
o.color.z = uv.y;
145+
146+
// Set colors to debug eye depth
147+
const float max_view_disp = 1.0f;
148+
o.color.y = (1.0f / eye_depth) / max_view_disp;
149+
150+
// For debug visualization, we need opaque
151+
o.color.w = 1.0f;
152+
153+
return o;
154+
}
155+
156+
half4 frag(v2f i) : COLOR
157+
{
158+
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
159+
160+
// Use the input color from the vertex, in the event we're using debug visualization.
161+
return i.color;
162+
}
163+
164+
ENDCG
165+
}
166+
}
167+
}

Assets/Shaders/OcclusionMesh.shader.meta

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Shaders/OcclusionExtension.shader renamed to Assets/Shaders/ZBufferOcclusion.shader

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
Shader "Lightship/OcclusionExtension"
1+
Shader "Lightship/ZBufferOcclusion"
22
{
33
Properties
44
{
5+
// Retrievable properties
56
_Depth ("DepthTexture", 2D) = "black" {}
6-
_FusedDepth("FusedDepthTexture", 2D) = "black" {}
7-
_Suppression ("SuppressionTexture", 2D) = "black" {}
8-
_StabilizationThreshold ("Stabilization Threshold", Range(0.0, 1.0)) = 0.5
97
}
108
SubShader
119
{

Editor/BuildUtilities/LightshipBuildProcessor.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Niantic.Lightship.AR.Utilities.Logging;
99
using Niantic.Lightship.AR.Loader;
1010
using Niantic.Lightship.AR.Occlusion;
11+
using Niantic.Lightship.AR.Occlusion.Features;
1112
using Niantic.Lightship.AR.Subsystems.Playback;
1213
using Unity.EditorCoroutines.Editor;
1314
using UnityEditor;
@@ -192,8 +193,9 @@ private void PreprocessBuild(BuildReport report)
192193
BuildHelper.AddBackgroundShaderToProject(backgroundShaderName);
193194
}
194195

195-
BuildHelper.AddBackgroundShaderToProject(LightshipOcclusionExtension.DefaultShaderName);
196-
BuildHelper.AddBackgroundShaderToProject(LightshipFusedDepthCamera.DefaultShaderName);
196+
BuildHelper.AddBackgroundShaderToProject(ZBufferOcclusion.RequiredShaderName);
197+
BuildHelper.AddBackgroundShaderToProject(OcclusionMesh.RequiredShaderName);
198+
BuildHelper.AddBackgroundShaderToProject(Stabilization.RequiredShaderName);
197199

198200
// TODO: Things that ARKit and ARCore BuildProcessor implementations do
199201
// - Check camera usage description

0 commit comments

Comments
 (0)