com.github.asus4.texture-source 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,7 +57,7 @@ Add the following setting to `Packages/manifest.json`
57
57
  }
58
58
  ],
59
59
  "dependencies": {
60
- "com.github.asus4.texture-source": "0.3.0",
60
+ "com.github.asus4.texture-source": "0.3.1",
61
61
  ...// other dependencies
62
62
  }
63
63
  }
@@ -0,0 +1,101 @@
1
+ // Only available with AR Foundation
2
+ #if MODULE_ARFOUNDATION_ENABLED
3
+ namespace TextureSource
4
+ {
5
+ using System.Collections;
6
+ using System.Collections.Generic;
7
+ using UnityEngine;
8
+ using UnityEngine.XR.ARFoundation;
9
+
10
+ /// <summary>
11
+ /// The same with ARFoundationTextureSource but depth is encoded into alpha channel
12
+ /// (Experimental feature, only available with the latest AR Foundation)
13
+ /// </summary>
14
+ [CreateAssetMenu(menuName = "ScriptableObject/Texture Source/ARFoundation Depth", fileName = "ARFoundationDepthTextureSource")]
15
+ public sealed class ARFoundationDepthTextureSource : ARFoundationTextureSource
16
+ {
17
+ public enum DepthMode
18
+ {
19
+ Depth01,
20
+ RawDistance,
21
+ }
22
+
23
+ [SerializeField]
24
+ private DepthMode depthMode = DepthMode.Depth01;
25
+
26
+ // [HideInInspector]
27
+ [SerializeField]
28
+ private Shader shaderForARCore;
29
+ // [HideInInspector]
30
+ [SerializeField]
31
+ private Shader shaderForARKit;
32
+
33
+ private AROcclusionManager occlusionManager;
34
+
35
+ protected override RenderTextureFormat PreferredRenderTextureFormat => RenderTextureFormat.ARGBHalf;
36
+
37
+ protected override Shader ARCameraBackgroundShader
38
+ {
39
+ get
40
+ {
41
+ return Application.platform switch
42
+ {
43
+ RuntimePlatform.Android => shaderForARCore,
44
+ RuntimePlatform.IPhonePlayer => shaderForARKit,
45
+ #if UNITY_ANDROID
46
+ _ => shaderForARCore,
47
+ #elif UNITY_IOS
48
+ _ => shaderForARKit,
49
+ #else
50
+ _ => throw new System.NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
51
+ #endif
52
+ };
53
+ }
54
+ }
55
+
56
+ public override void Start()
57
+ {
58
+ occlusionManager = FindAnyObjectByType<AROcclusionManager>();
59
+ if (occlusionManager == null)
60
+ {
61
+ throw new System.InvalidOperationException("Requires AROcclusionManager to use ARFoundationDepthTextureSource");
62
+ }
63
+ occlusionManager.frameReceived += OnOcclusionFrameReceived;
64
+
65
+ base.Start();
66
+
67
+ if (depthMode == DepthMode.RawDistance)
68
+ {
69
+ material.EnableKeyword("TEXTURE_SOURCE_RAW_DISTANCE");
70
+ }
71
+ }
72
+
73
+ public override void Stop()
74
+ {
75
+ if (occlusionManager != null)
76
+ {
77
+ occlusionManager.frameReceived -= OnOcclusionFrameReceived;
78
+ }
79
+
80
+ base.Stop();
81
+ }
82
+
83
+ private void OnOcclusionFrameReceived(AROcclusionFrameEventArgs args)
84
+ {
85
+ if (args.textures.Count == 0)
86
+ {
87
+ return;
88
+ }
89
+
90
+ SetMaterialKeywords(material, args.enabledMaterialKeywords, args.disabledMaterialKeywords);
91
+
92
+ int count = args.textures.Count;
93
+ for (int i = 0; i < count; i++)
94
+ {
95
+ var tex = args.textures[i];
96
+ material.SetTexture(args.propertyNameIds[i], tex);
97
+ }
98
+ }
99
+ }
100
+ }
101
+ #endif // MODULE_ARFOUNDATION_ENABLED
@@ -0,0 +1,13 @@
1
+ fileFormatVersion: 2
2
+ guid: e468045a73ccc46f1afb6f48292c11b1
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences:
7
+ - shaderForARKit: {fileID: 4800000, guid: c8b0b30194e1b4026abc942f037e6f86, type: 3}
8
+ - shaderForARCore: {fileID: 4800000, guid: a68a981b98d2c47b881e7e696a7b0f1f, type: 3}
9
+ executionOrder: 0
10
+ icon: {instanceID: 0}
11
+ userData:
12
+ assetBundleName:
13
+ assetBundleVariant:
@@ -3,6 +3,7 @@
3
3
  namespace TextureSource
4
4
  {
5
5
  using System;
6
+ using System.Collections.Generic;
6
7
  using UnityEngine;
7
8
  using UnityEngine.XR.ARFoundation;
8
9
 
@@ -10,34 +11,40 @@ namespace TextureSource
10
11
  /// Source from ARFoundation
11
12
  /// </summary>
12
13
  [CreateAssetMenu(menuName = "ScriptableObject/Texture Source/ARFoundation", fileName = "ARFoundationTextureSource")]
13
- public sealed class ARFoundationTextureSource : BaseTextureSource
14
+ public class ARFoundationTextureSource : BaseTextureSource
14
15
  {
15
16
  private static readonly int _DisplayTransformID = Shader.PropertyToID("_UnityDisplayTransform");
16
17
 
17
18
  private ARCameraManager cameraManager;
18
19
  private RenderTexture texture;
19
- private Material material;
20
20
  private int lastUpdatedFrame = -1;
21
21
 
22
- private static readonly Lazy<Shader> ARCameraBackgroundShader = new(() =>
22
+ protected Material material;
23
+
24
+ public override bool DidUpdateThisFrame => lastUpdatedFrame == Time.frameCount;
25
+ public override Texture Texture => texture;
26
+
27
+ protected virtual RenderTextureFormat PreferredRenderTextureFormat => RenderTextureFormat.ARGB32;
28
+
29
+ protected virtual Shader ARCameraBackgroundShader
23
30
  {
24
- string shaderName = Application.platform switch
31
+ get
25
32
  {
26
- RuntimePlatform.Android => "Unlit/ARCoreBackground",
27
- RuntimePlatform.IPhonePlayer => "Unlit/ARKitBackground",
33
+ string shaderName = Application.platform switch
34
+ {
35
+ RuntimePlatform.Android => "Unlit/ARCoreBackground",
36
+ RuntimePlatform.IPhonePlayer => "Unlit/ARKitBackground",
28
37
  #if UNITY_ANDROID
29
- _ => "Unlit/ARCoreBackground",
38
+ _ => "Unlit/ARCoreBackground",
30
39
  #elif UNITY_IOS
31
- _ => "Unlit/ARKitBackground",
40
+ _ => "Unlit/ARKitBackground",
32
41
  #else
33
- _ => throw new NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
42
+ _ => throw new System.NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
34
43
  #endif
35
- };
36
- return Shader.Find(shaderName);
37
- });
38
-
39
- public override bool DidUpdateThisFrame => lastUpdatedFrame == Time.frameCount;
40
- public override Texture Texture => texture;
44
+ };
45
+ return Shader.Find(shaderName);
46
+ }
47
+ }
41
48
 
42
49
  public override void Start()
43
50
  {
@@ -46,9 +53,7 @@ namespace TextureSource
46
53
  {
47
54
  throw new InvalidOperationException("ARCameraManager is not found");
48
55
  }
49
-
50
- var shader = ARCameraBackgroundShader.Value;
51
- material = new Material(shader);
56
+ material = new Material(ARCameraBackgroundShader);
52
57
 
53
58
  cameraManager.frameReceived += OnFrameReceived;
54
59
  }
@@ -89,8 +94,12 @@ namespace TextureSource
89
94
  };
90
95
  }
91
96
 
97
+
92
98
  private void OnFrameReceived(ARCameraFrameEventArgs args)
93
99
  {
100
+ // The shader doesn't work for some reason when set ARKIT_BACKGROUND_URP
101
+ // SetMaterialKeywords(material, args.enabledMaterialKeywords, args.disabledMaterialKeywords);
102
+
94
103
  // Find best texture size
95
104
  int bestWidth = 0;
96
105
  int bestHeight = 0;
@@ -112,11 +121,11 @@ namespace TextureSource
112
121
 
113
122
  // Create render texture
114
123
  Utils.GetTargetSizeScale(
115
- new Vector2Int(bestWidth, bestHeight), screenAspect,
116
- out Vector2Int dstSize, out Vector2 scale);
117
- EnsureRenderTexture(dstSize.x, dstSize.y);
118
-
119
- // SetMaterialKeywords(material, args.enabledMaterialKeywords, args.disabledMaterialKeywords);
124
+ new Vector2Int(bestWidth, bestHeight),
125
+ screenAspect,
126
+ out Vector2Int dstSize,
127
+ out Vector2 scale);
128
+ EnsureRenderTexture(ref texture, dstSize.x, dstSize.y, 24, PreferredRenderTextureFormat);
120
129
 
121
130
  if (args.displayMatrix.HasValue)
122
131
  {
@@ -128,17 +137,35 @@ namespace TextureSource
128
137
  lastUpdatedFrame = Time.frameCount;
129
138
  }
130
139
 
131
- private void EnsureRenderTexture(int width, int height)
140
+ protected static void SetMaterialKeywords(Material material, List<string> enabledKeywords, List<string> disabledKeywords)
141
+ {
142
+ if (enabledKeywords != null)
143
+ {
144
+ foreach (var keyword in enabledKeywords)
145
+ {
146
+ material.EnableKeyword(keyword);
147
+ }
148
+ }
149
+ if (disabledKeywords != null)
150
+ {
151
+ foreach (var keyword in disabledKeywords)
152
+ {
153
+ material.DisableKeyword(keyword);
154
+ }
155
+ }
156
+ }
157
+
158
+ public static void EnsureRenderTexture(ref RenderTexture texture,
159
+ int width, int height,
160
+ int depth, RenderTextureFormat format)
132
161
  {
133
162
  if (texture == null || texture.width != width || texture.height != height)
134
163
  {
135
164
  if (texture != null)
136
165
  {
137
166
  texture.Release();
138
- texture = null;
139
167
  }
140
- int depth = 32;
141
- texture = new RenderTexture(width, height, depth, RenderTextureFormat.ARGB32);
168
+ texture = new RenderTexture(width, height, depth, format);
142
169
  texture.Create();
143
170
  }
144
171
  }
@@ -27,7 +27,7 @@ namespace TextureSource
27
27
 
28
28
  public RenderTexture Texture => texture;
29
29
 
30
- public TextureTransformer(int width, int height, ComputeShader shader = null)
30
+ public TextureTransformer(int width, int height, RenderTextureFormat format, ComputeShader shader = null)
31
31
  {
32
32
  compute = shader != null
33
33
  ? shader
@@ -37,7 +37,7 @@ namespace TextureSource
37
37
  this.width = width;
38
38
  this.height = height;
39
39
 
40
- var desc = new RenderTextureDescriptor(width, height, RenderTextureFormat.ARGB32)
40
+ var desc = new RenderTextureDescriptor(width, height, format)
41
41
  {
42
42
  enableRandomWrite = true,
43
43
  useMipMap = false,
@@ -118,7 +118,11 @@ namespace TextureSource
118
118
  if (needInitialize)
119
119
  {
120
120
  transformer?.Dispose();
121
- transformer = new TextureTransformer(dstSize.x, dstSize.y);
121
+ // Copy the format if the source is a RenderTexture
122
+ RenderTextureFormat format = (texture is RenderTexture renderTex)
123
+ ? renderTex.format :
124
+ RenderTextureFormat.ARGB32;
125
+ transformer = new TextureTransformer(dstSize.x, dstSize.y, format);
122
126
  }
123
127
 
124
128
  return transformer.Transform(texture, Vector2.zero, 0, scale);
@@ -159,7 +159,7 @@ namespace TextureSource
159
159
  if (needInitialize)
160
160
  {
161
161
  transformer?.Dispose();
162
- transformer = new TextureTransformer(width, height);
162
+ transformer = new TextureTransformer(width, height, RenderTextureFormat.ARGB32);
163
163
  }
164
164
 
165
165
  Vector2 scale;
@@ -0,0 +1,138 @@
1
+ Shader "Unlit/TextureSource/ARCoreBackgroundDepth"
2
+ {
3
+ Properties
4
+ {
5
+ _MainTex("Texture", 2D) = "white" {}
6
+ _EnvironmentDepth("Texture", 2D) = "black" {}
7
+ }
8
+
9
+ SubShader
10
+ {
11
+ Name "ARCore Background (Before Opaques)"
12
+ Tags
13
+ {
14
+ "Queue" = "Background"
15
+ "RenderType" = "Background"
16
+ "ForceNoShadowCasting" = "True"
17
+ }
18
+
19
+ Pass
20
+ {
21
+ Name "AR Camera Background (ARCore)"
22
+ Cull Off
23
+ ZTest Always
24
+ ZWrite On
25
+ Lighting Off
26
+ LOD 100
27
+ Tags
28
+ {
29
+ "LightMode" = "Always"
30
+ }
31
+
32
+ GLSLPROGRAM
33
+
34
+ #pragma multi_compile_local __ ARCORE_ENVIRONMENT_DEPTH_ENABLED
35
+ #pragma multi_compile_local __ TEXTURE_SOURCE_RAW_DISTANCE
36
+
37
+ #pragma only_renderers gles3
38
+
39
+ #include "UnityCG.glslinc"
40
+
41
+ #ifdef SHADER_API_GLES3
42
+ #extension GL_OES_EGL_image_external_essl3 : require
43
+ #endif // SHADER_API_GLES3
44
+
45
+ // Device display transform is provided by the AR Foundation camera background renderer.
46
+ uniform mat4 _UnityDisplayTransform;
47
+
48
+ #ifdef VERTEX
49
+ varying vec2 textureCoord;
50
+
51
+ void main()
52
+ {
53
+ #ifdef SHADER_API_GLES3
54
+ // Transform the position from object space to clip space.
55
+ gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
56
+
57
+ // Remap the texture coordinates based on the device rotation.
58
+ textureCoord = (_UnityDisplayTransform * vec4(gl_MultiTexCoord0.x, 1.0f - gl_MultiTexCoord0.y, 1.0f, 0.0f)).xy;
59
+ #endif // SHADER_API_GLES3
60
+ }
61
+ #endif // VERTEX
62
+
63
+ #ifdef FRAGMENT
64
+ varying vec2 textureCoord;
65
+ uniform samplerExternalOES _MainTex;
66
+ uniform float _UnityCameraForwardScale;
67
+
68
+ #ifdef ARCORE_ENVIRONMENT_DEPTH_ENABLED
69
+ uniform sampler2D _EnvironmentDepth;
70
+ #endif // ARCORE_ENVIRONMENT_DEPTH_ENABLED
71
+
72
+ #if defined(SHADER_API_GLES3) && !defined(UNITY_COLORSPACE_GAMMA)
73
+ float GammaToLinearSpaceExact (float value)
74
+ {
75
+ if (value <= 0.04045F)
76
+ return value / 12.92F;
77
+ else if (value < 1.0F)
78
+ return pow((value + 0.055F)/1.055F, 2.4F);
79
+ else
80
+ return pow(value, 2.2F);
81
+ }
82
+
83
+ vec3 GammaToLinearSpace (vec3 sRGB)
84
+ {
85
+ // Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
86
+ return sRGB * (sRGB * (sRGB * 0.305306011F + 0.682171111F) + 0.012522878F);
87
+
88
+ // Precise version, useful for debugging, but the pow() function is too slow.
89
+ // return vec3(GammaToLinearSpaceExact(sRGB.r), GammaToLinearSpaceExact(sRGB.g), GammaToLinearSpaceExact(sRGB.b));
90
+ }
91
+
92
+ #endif // SHADER_API_GLES3 && !UNITY_COLORSPACE_GAMMA
93
+
94
+ float ConvertDistanceToDepth(float d)
95
+ {
96
+ #if TEXTURE_SOURCE_RAW_DISTANCE
97
+ return d;
98
+ #else
99
+ d = _UnityCameraForwardScale > 0.0 ? _UnityCameraForwardScale * d : d;
100
+
101
+ float zBufferParamsW = 1.0 / _ProjectionParams.y;
102
+ float zBufferParamsY = _ProjectionParams.z * zBufferParamsW;
103
+ float zBufferParamsX = 1.0 - zBufferParamsY;
104
+ float zBufferParamsZ = zBufferParamsX * _ProjectionParams.w;
105
+
106
+ // Clip any distances smaller than the near clip plane, and compute the depth value from the distance.
107
+ return (d < _ProjectionParams.y) ? 1.0f : ((1.0 / zBufferParamsZ) * ((1.0 / d) - zBufferParamsW));
108
+ #endif // TEXTURE_SOURCE_RAW_DISTANCE
109
+ }
110
+
111
+ void main()
112
+ {
113
+ #ifdef SHADER_API_GLES3
114
+ vec3 result = texture(_MainTex, textureCoord).xyz;
115
+ float depth = 1.0;
116
+
117
+ #ifdef ARCORE_ENVIRONMENT_DEPTH_ENABLED
118
+ float distance = texture(_EnvironmentDepth, textureCoord).x;
119
+ depth = ConvertDistanceToDepth(distance);
120
+ #endif // ARCORE_ENVIRONMENT_DEPTH_ENABLED
121
+
122
+ #ifndef UNITY_COLORSPACE_GAMMA
123
+ result = GammaToLinearSpace(result);
124
+ #endif // !UNITY_COLORSPACE_GAMMA
125
+
126
+ // gl_FragColor = vec4(result, 1.0);
127
+ gl_FragColor = vec4(result, depth);
128
+ gl_FragDepth = depth;
129
+ #endif // SHADER_API_GLES3
130
+ }
131
+
132
+ #endif // FRAGMENT
133
+ ENDGLSL
134
+ }
135
+ }
136
+
137
+ FallBack Off
138
+ }
@@ -0,0 +1,9 @@
1
+ fileFormatVersion: 2
2
+ guid: a68a981b98d2c47b881e7e696a7b0f1f
3
+ ShaderImporter:
4
+ externalObjects: {}
5
+ defaultTextures: []
6
+ nonModifiableTextures: []
7
+ userData:
8
+ assetBundleName:
9
+ assetBundleVariant:
@@ -0,0 +1,199 @@
1
+ Shader "Unlit/TextureSource/ARKitBackgroundDepth"
2
+ {
3
+ Properties
4
+ {
5
+ _textureY ("TextureY", 2D) = "white" {}
6
+ _textureCbCr ("TextureCbCr", 2D) = "black" {}
7
+ _HumanStencil ("HumanStencil", 2D) = "black" {}
8
+ _HumanDepth ("HumanDepth", 2D) = "black" {}
9
+ _EnvironmentDepth ("EnvironmentDepth", 2D) = "black" {}
10
+ }
11
+
12
+ SubShader
13
+ {
14
+ Tags
15
+ {
16
+ "Queue" = "Background"
17
+ "RenderType" = "Background"
18
+ "ForceNoShadowCasting" = "True"
19
+ }
20
+
21
+ Pass
22
+ {
23
+ Cull Off
24
+ ZTest Always
25
+ ZWrite On
26
+ Lighting Off
27
+ LOD 100
28
+ Tags
29
+ {
30
+ "LightMode" = "Always"
31
+ }
32
+
33
+
34
+ HLSLPROGRAM
35
+
36
+ #pragma vertex vert
37
+ #pragma fragment frag
38
+
39
+ #pragma multi_compile_local __ ARKIT_BACKGROUND_URP
40
+ #pragma multi_compile_local __ ARKIT_HUMAN_SEGMENTATION_ENABLED ARKIT_ENVIRONMENT_DEPTH_ENABLED
41
+ #pragma multi_compile_local __ TEXTURE_SOURCE_RAW_DISTANCE
42
+
43
+
44
+ #if ARKIT_BACKGROUND_URP
45
+
46
+ #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
47
+ #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
48
+
49
+ #define ARKIT_TEXTURE2D_HALF(texture) TEXTURE2D(texture)
50
+ #define ARKIT_SAMPLER_HALF(sampler) SAMPLER(sampler)
51
+ #define ARKIT_TEXTURE2D_FLOAT(texture) TEXTURE2D(texture)
52
+ #define ARKIT_SAMPLER_FLOAT(sampler) SAMPLER(sampler)
53
+ #define ARKIT_SAMPLE_TEXTURE2D(texture,sampler,texcoord) SAMPLE_TEXTURE2D(texture,sampler,texcoord)
54
+
55
+ #else // Legacy RP
56
+
57
+ #include "UnityCG.cginc"
58
+
59
+ #define real4 half4
60
+ #define real4x4 half4x4
61
+ #define TransformObjectToHClip UnityObjectToClipPos
62
+ #define FastSRGBToLinear GammaToLinearSpace
63
+
64
+ #define ARKIT_TEXTURE2D_HALF(texture) UNITY_DECLARE_TEX2D_HALF(texture)
65
+ #define ARKIT_SAMPLER_HALF(sampler)
66
+ #define ARKIT_TEXTURE2D_FLOAT(texture) UNITY_DECLARE_TEX2D_FLOAT(texture)
67
+ #define ARKIT_SAMPLER_FLOAT(sampler)
68
+ #define ARKIT_SAMPLE_TEXTURE2D(texture,sampler,texcoord) UNITY_SAMPLE_TEX2D(texture,texcoord)
69
+
70
+ #endif
71
+
72
+
73
+ struct appdata
74
+ {
75
+ float3 position : POSITION;
76
+ float2 texcoord : TEXCOORD0;
77
+ };
78
+
79
+ struct v2f
80
+ {
81
+ float4 position : SV_POSITION;
82
+ float2 texcoord : TEXCOORD0;
83
+ };
84
+
85
+ struct fragment_output
86
+ {
87
+ real4 color : SV_Target;
88
+ // float depth : SV_Depth;
89
+ };
90
+
91
+
92
+ CBUFFER_START(UnityARFoundationPerFrame)
93
+ // Device display transform is provided by the AR Foundation camera background renderer.
94
+ float4x4 _UnityDisplayTransform;
95
+ float _UnityCameraForwardScale;
96
+ CBUFFER_END
97
+
98
+
99
+ v2f vert (appdata v)
100
+ {
101
+ // Transform the position from object space to clip space.
102
+ float4 position = TransformObjectToHClip(v.position);
103
+
104
+ // Remap the texture coordinates based on the device rotation.
105
+ float2 texcoord = mul(float4(v.texcoord, 1.0f, 1.0f), _UnityDisplayTransform).xy;
106
+
107
+ v2f o;
108
+ o.position = position;
109
+ o.texcoord = texcoord;
110
+ return o;
111
+ }
112
+
113
+
114
+ CBUFFER_START(ARKitColorTransformations)
115
+ static const real4x4 s_YCbCrToSRGB = real4x4(
116
+ real4(1.0h, 0.0000h, 1.4020h, -0.7010h),
117
+ real4(1.0h, -0.3441h, -0.7141h, 0.5291h),
118
+ real4(1.0h, 1.7720h, 0.0000h, -0.8860h),
119
+ real4(0.0h, 0.0000h, 0.0000h, 1.0000h)
120
+ );
121
+ CBUFFER_END
122
+
123
+
124
+ inline float ConvertDistanceToDepth(float d)
125
+ {
126
+ #if TEXTURE_SOURCE_RAW_DISTANCE
127
+ return d;
128
+ #else
129
+ // Account for scale
130
+ d = _UnityCameraForwardScale > 0.0 ? _UnityCameraForwardScale * d : d;
131
+
132
+ // Clip any distances smaller than the near clip plane, and compute the depth value from the distance.
133
+ return (d < _ProjectionParams.y) ? 0.0f : ((1.0f / _ZBufferParams.z) * ((1.0f / d) - _ZBufferParams.w));
134
+ #endif // TEXTURE_SOURCE_RAW_DISTANCE
135
+ }
136
+
137
+
138
+ ARKIT_TEXTURE2D_HALF(_textureY);
139
+ ARKIT_SAMPLER_HALF(sampler_textureY);
140
+ ARKIT_TEXTURE2D_HALF(_textureCbCr);
141
+ ARKIT_SAMPLER_HALF(sampler_textureCbCr);
142
+ #if ARKIT_ENVIRONMENT_DEPTH_ENABLED
143
+ ARKIT_TEXTURE2D_FLOAT(_EnvironmentDepth);
144
+ ARKIT_SAMPLER_FLOAT(sampler_EnvironmentDepth);
145
+ #elif ARKIT_HUMAN_SEGMENTATION_ENABLED
146
+ ARKIT_TEXTURE2D_HALF(_HumanStencil);
147
+ ARKIT_SAMPLER_HALF(sampler_HumanStencil);
148
+ ARKIT_TEXTURE2D_FLOAT(_HumanDepth);
149
+ ARKIT_SAMPLER_FLOAT(sampler_HumanDepth);
150
+ #endif // ARKIT_HUMAN_SEGMENTATION_ENABLED
151
+
152
+
153
+ fragment_output frag (v2f i)
154
+ {
155
+ // Sample the video textures (in YCbCr).
156
+ real4 ycbcr = real4(ARKIT_SAMPLE_TEXTURE2D(_textureY, sampler_textureY, i.texcoord).r,
157
+ ARKIT_SAMPLE_TEXTURE2D(_textureCbCr, sampler_textureCbCr, i.texcoord).rg,
158
+ 1.0h);
159
+
160
+ // Convert from YCbCr to sRGB.
161
+ real4 videoColor = mul(s_YCbCrToSRGB, ycbcr);
162
+
163
+ #if !UNITY_COLORSPACE_GAMMA
164
+ // If rendering in linear color space, convert from sRGB to RGB.
165
+ videoColor.xyz = FastSRGBToLinear(videoColor.xyz);
166
+ #endif // !UNITY_COLORSPACE_GAMMA
167
+
168
+ // Assume the background depth is the back of the depth clipping volume.
169
+ float depthValue = 0.0f;
170
+
171
+ #if ARKIT_ENVIRONMENT_DEPTH_ENABLED
172
+ // Sample the environment depth (in meters).
173
+ float envDistance = ARKIT_SAMPLE_TEXTURE2D(_EnvironmentDepth, sampler_EnvironmentDepth, i.texcoord).r;
174
+
175
+ // Convert the distance to depth.
176
+ depthValue = ConvertDistanceToDepth(envDistance);
177
+ #elif ARKIT_HUMAN_SEGMENTATION_ENABLED
178
+ // Check the human stencil, and skip non-human pixels.
179
+ if (ARKIT_SAMPLE_TEXTURE2D(_HumanStencil, sampler_HumanStencil, i.texcoord).r > 0.5h)
180
+ {
181
+ // Sample the human depth (in meters).
182
+ float humanDistance = ARKIT_SAMPLE_TEXTURE2D(_HumanDepth, sampler_HumanDepth, i.texcoord).r;
183
+
184
+ // Convert the distance to depth.
185
+ depthValue = ConvertDistanceToDepth(humanDistance);
186
+ }
187
+ #endif // ARKIT_HUMAN_SEGMENTATION_ENABLED
188
+
189
+ fragment_output o;
190
+ o.color = real4(videoColor.x, videoColor.y, videoColor.z, depthValue);
191
+ // o.color = real4(depthValue, depthValue, depthValue, 0.5h);
192
+ // o.depth = depthValue;
193
+ return o;
194
+ }
195
+
196
+ ENDHLSL
197
+ }
198
+ }
199
+ }
@@ -0,0 +1,9 @@
1
+ fileFormatVersion: 2
2
+ guid: c8b0b30194e1b4026abc942f037e6f86
3
+ ShaderImporter:
4
+ externalObjects: {}
5
+ defaultTextures: []
6
+ nonModifiableTextures: []
7
+ userData:
8
+ assetBundleName:
9
+ assetBundleVariant:
package/Shaders.meta ADDED
@@ -0,0 +1,8 @@
1
+ fileFormatVersion: 2
2
+ guid: 72ce354a7d6da4f62bd4862f571441a8
3
+ folderAsset: yes
4
+ DefaultImporter:
5
+ externalObjects: {}
6
+ userData:
7
+ assetBundleName:
8
+ assetBundleVariant:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.github.asus4.texture-source",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "displayName": "TextureSource",
5
5
  "description": "Simplify WebCamera and test video handling for using Computer Vision in Unity",
6
6
  "unity": "2020.3",