com.github.asus4.texture-source 0.3.1 → 0.3.3

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.1",
60
+ "com.github.asus4.texture-source": "0.3.3",
61
61
  ...// other dependencies
62
62
  }
63
63
  }
@@ -47,7 +47,7 @@ namespace TextureSource
47
47
  #elif UNITY_IOS
48
48
  _ => shaderForARKit,
49
49
  #else
50
- _ => throw new NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
50
+ _ => throw new System.NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
51
51
  #endif
52
52
  };
53
53
  }
@@ -82,6 +82,21 @@ namespace TextureSource
82
82
 
83
83
  private void OnOcclusionFrameReceived(AROcclusionFrameEventArgs args)
84
84
  {
85
+ #if MODULE_ARFOUNDATION_6_1_ENABLED
86
+ var textures = args.externalTextures;
87
+ if (textures.Count == 0)
88
+ {
89
+ return;
90
+ }
91
+
92
+ var keywords = args.shaderKeywords;
93
+ SetMaterialKeywords(material, keywords.enabledKeywords, keywords.enabledKeywords);
94
+
95
+ foreach(var exTex in textures)
96
+ {
97
+ material.SetTexture(exTex.propertyId, exTex.texture);
98
+ }
99
+ #else
85
100
  if (args.textures.Count == 0)
86
101
  {
87
102
  return;
@@ -95,6 +110,7 @@ namespace TextureSource
95
110
  var tex = args.textures[i];
96
111
  material.SetTexture(args.propertyNameIds[i], tex);
97
112
  }
113
+ #endif // MODULE_ARFOUNDATION_6_1_ENABLED
98
114
  }
99
115
  }
100
116
  }
@@ -39,7 +39,7 @@ namespace TextureSource
39
39
  #elif UNITY_IOS
40
40
  _ => "Unlit/ARKitBackground",
41
41
  #else
42
- _ => throw new NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
42
+ _ => throw new System.NotSupportedException($"ARFoundationTextureSource is not supported on {Application.platform}"),
43
43
  #endif
44
44
  };
45
45
  return Shader.Find(shaderName);
@@ -137,7 +137,7 @@ namespace TextureSource
137
137
  lastUpdatedFrame = Time.frameCount;
138
138
  }
139
139
 
140
- protected static void SetMaterialKeywords(Material material, List<string> enabledKeywords, List<string> disabledKeywords)
140
+ protected static void SetMaterialKeywords(Material material, IReadOnlyList<string> enabledKeywords, IReadOnlyList<string> disabledKeywords)
141
141
  {
142
142
  if (enabledKeywords != null)
143
143
  {
@@ -2,7 +2,9 @@
2
2
  "name": "TextureSource",
3
3
  "rootNamespace": "",
4
4
  "references": [
5
- "GUID:a9420e37d7990b54abdef6688edbe313"
5
+ "GUID:a9420e37d7990b54abdef6688edbe313",
6
+ "GUID:92703082f92b41ba80f0d6912de66115",
7
+ "GUID:dc960734dc080426fa6612f1c5fe95f3"
6
8
  ],
7
9
  "includePlatforms": [],
8
10
  "excludePlatforms": [],
@@ -16,6 +18,11 @@
16
18
  "name": "com.unity.xr.arfoundation",
17
19
  "expression": "4.0.0",
18
20
  "define": "MODULE_ARFOUNDATION_ENABLED"
21
+ },
22
+ {
23
+ "name": "com.unity.xr.arfoundation",
24
+ "expression": "6.1.0",
25
+ "define": "MODULE_ARFOUNDATION_6_1_ENABLED"
19
26
  }
20
27
  ],
21
28
  "noEngineReferences": false
@@ -6,9 +6,13 @@
6
6
  _EnvironmentDepth("Texture", 2D) = "black" {}
7
7
  }
8
8
 
9
+ /////////////////////////////////////////////////////////////////////////////////////////////////
10
+ // GLSL shader for OpenGLES 3. GLES3 cannot use HLSL shader like Vulkan because of dependency on
11
+ // GL_OES_EGL_image_external_essl3 extension. Thus they are kept in different subshaders.
12
+ /////////////////////////////////////////////////////////////////////////////////////////////////
9
13
  SubShader
10
14
  {
11
- Name "ARCore Background (Before Opaques)"
15
+ Name "ARCore Background (Before Opaques) for GLES3"
12
16
  Tags
13
17
  {
14
18
  "Queue" = "Background"
@@ -31,22 +35,28 @@
31
35
 
32
36
  GLSLPROGRAM
33
37
 
34
- #pragma multi_compile_local __ ARCORE_ENVIRONMENT_DEPTH_ENABLED
35
- #pragma multi_compile_local __ TEXTURE_SOURCE_RAW_DISTANCE
36
-
37
38
  #pragma only_renderers gles3
38
39
 
40
+ #pragma multi_compile_local __ ARCORE_ENVIRONMENT_DEPTH_ENABLED
41
+ #pragma multi_compile_local __ ARCORE_IMAGE_STABILIZATION_ENABLED
42
+
39
43
  #include "UnityCG.glslinc"
40
44
 
41
45
  #ifdef SHADER_API_GLES3
42
46
  #extension GL_OES_EGL_image_external_essl3 : require
43
47
  #endif // SHADER_API_GLES3
44
48
 
49
+ #ifndef ARCORE_IMAGE_STABILIZATION_ENABLED
50
+ #define ARCORE_TEXCOORD_TYPE vec2
51
+ #else // ARCORE_IMAGE_STABILIZATION_ENABLED
52
+ #define ARCORE_TEXCOORD_TYPE vec3
53
+ #endif // !ARCORE_IMAGE_STABILIZATION_ENABLED
54
+
45
55
  // Device display transform is provided by the AR Foundation camera background renderer.
46
56
  uniform mat4 _UnityDisplayTransform;
47
57
 
48
58
  #ifdef VERTEX
49
- varying vec2 textureCoord;
59
+ varying ARCORE_TEXCOORD_TYPE textureCoord;
50
60
 
51
61
  void main()
52
62
  {
@@ -54,14 +64,22 @@
54
64
  // Transform the position from object space to clip space.
55
65
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
56
66
 
67
+ #ifdef ARCORE_IMAGE_STABILIZATION_ENABLED
68
+ textureCoord = gl_MultiTexCoord0.xyz;
69
+ #else
57
70
  // 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;
71
+ // _UnityDisplayTransform is provided as "Row Major" for all mobile platforms, so use the
72
+ // 'Row Vector * Matrix' operator. The GLSL '*' operator is overloaded to use a row vector
73
+ // when a matrix is left-multiplied by a vector. Refer to this doc for more information:
74
+ // https://en.wikibooks.org/wiki/GLSL_Programming/Vector_and_Matrix_Operations#Operators
75
+ textureCoord = (vec4(gl_MultiTexCoord0.x, gl_MultiTexCoord0.y, 1.0f, 0.0f) * _UnityDisplayTransform).xy;
76
+ #endif
59
77
  #endif // SHADER_API_GLES3
60
78
  }
61
79
  #endif // VERTEX
62
80
 
63
81
  #ifdef FRAGMENT
64
- varying vec2 textureCoord;
82
+ varying ARCORE_TEXCOORD_TYPE textureCoord;
65
83
  uniform samplerExternalOES _MainTex;
66
84
  uniform float _UnityCameraForwardScale;
67
85
 
@@ -70,17 +88,17 @@
70
88
  #endif // ARCORE_ENVIRONMENT_DEPTH_ENABLED
71
89
 
72
90
  #if defined(SHADER_API_GLES3) && !defined(UNITY_COLORSPACE_GAMMA)
73
- float GammaToLinearSpaceExact (float value)
91
+ float GammaToLinearSpaceExact(float value)
74
92
  {
75
93
  if (value <= 0.04045F)
76
94
  return value / 12.92F;
77
95
  else if (value < 1.0F)
78
- return pow((value + 0.055F)/1.055F, 2.4F);
96
+ return pow((value + 0.055F) / 1.055F, 2.4F);
79
97
  else
80
98
  return pow(value, 2.2F);
81
99
  }
82
100
 
83
- vec3 GammaToLinearSpace (vec3 sRGB)
101
+ vec3 GammaToLinearSpace(vec3 sRGB)
84
102
  {
85
103
  // Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
86
104
  return sRGB * (sRGB * (sRGB * 0.305306011F + 0.682171111F) + 0.012522878F);
@@ -88,7 +106,6 @@
88
106
  // Precise version, useful for debugging, but the pow() function is too slow.
89
107
  // return vec3(GammaToLinearSpaceExact(sRGB.r), GammaToLinearSpaceExact(sRGB.g), GammaToLinearSpaceExact(sRGB.b));
90
108
  }
91
-
92
109
  #endif // SHADER_API_GLES3 && !UNITY_COLORSPACE_GAMMA
93
110
 
94
111
  float ConvertDistanceToDepth(float d)
@@ -111,11 +128,16 @@
111
128
  void main()
112
129
  {
113
130
  #ifdef SHADER_API_GLES3
114
- vec3 result = texture(_MainTex, textureCoord).xyz;
131
+ #ifdef ARCORE_IMAGE_STABILIZATION_ENABLED
132
+ vec2 tc = textureCoord.xy / textureCoord.z;
133
+ #else
134
+ vec2 tc = textureCoord;
135
+ #endif
136
+ vec3 result = texture(_MainTex, tc).xyz;
115
137
  float depth = 1.0;
116
138
 
117
139
  #ifdef ARCORE_ENVIRONMENT_DEPTH_ENABLED
118
- float distance = texture(_EnvironmentDepth, textureCoord).x;
140
+ float distance = texture(_EnvironmentDepth, tc).x;
119
141
  depth = ConvertDistanceToDepth(distance);
120
142
  #endif // ARCORE_ENVIRONMENT_DEPTH_ENABLED
121
143
 
@@ -134,5 +156,154 @@
134
156
  }
135
157
  }
136
158
 
159
+ /////////////////////////////////////////////////////////////////////////////////////////////
160
+ // HLSL shader for Vulkan. It should be kept the same with GLES3 except for the syntax diff.
161
+ /////////////////////////////////////////////////////////////////////////////////////////////
162
+ SubShader
163
+ {
164
+ Name "ARCore Background (Before Opaques) for Vulkan"
165
+ Tags
166
+ {
167
+ "Queue" = "Background"
168
+ "RenderType" = "Background"
169
+ "ForceNoShadowCasting" = "True"
170
+ }
171
+
172
+ Pass
173
+ {
174
+ Name "AR Camera Background (ARCore)"
175
+ Cull Off
176
+ ZTest Always
177
+ ZWrite On
178
+ Lighting Off
179
+ LOD 100
180
+ Tags
181
+ {
182
+ "LightMode" = "Always"
183
+ }
184
+
185
+ HLSLPROGRAM
186
+
187
+ #pragma only_renderers vulkan
188
+
189
+ #pragma multi_compile_local __ ARCORE_ENVIRONMENT_DEPTH_ENABLED
190
+ #pragma multi_compile_local __ ARCORE_IMAGE_STABILIZATION_ENABLED
191
+
192
+ #include "UnityCG.cginc"
193
+
194
+ #pragma vertex vert
195
+ #pragma fragment frag
196
+
197
+ #ifndef ARCORE_IMAGE_STABILIZATION_ENABLED
198
+ #define ARCORE_TEXCOORD_TYPE float2
199
+ #else // ARCORE_IMAGE_STABILIZATION_ENABLED
200
+ #define ARCORE_TEXCOORD_TYPE float3
201
+ #endif // !ARCORE_IMAGE_STABILIZATION_ENABLED
202
+
203
+ // Device display transform is provided by the AR Foundation camera background renderer.
204
+ float4x4 _UnityDisplayTransform;
205
+
206
+ struct vertexInput
207
+ {
208
+ float4 vertex : POSITION;
209
+ float3 uv : TEXCOORD0;
210
+ };
211
+
212
+ struct v2f
213
+ {
214
+ float4 position : SV_POSITION;
215
+ ARCORE_TEXCOORD_TYPE textureCoord : TEXCOORD0;
216
+ };
217
+
218
+ v2f vert(vertexInput i)
219
+ {
220
+ v2f o;
221
+
222
+ // Transform the position from object space to clip space.
223
+ o.position = UnityObjectToClipPos(i.vertex.xyz);
224
+
225
+ #ifdef ARCORE_IMAGE_STABILIZATION_ENABLED
226
+ o.textureCoord = i.uv.xyz;
227
+ #else
228
+ // Remap the texture coordinates based on the device rotation.
229
+ // _UnityDisplayTransform is provided as "Row Major" for all mobile platforms, so use the
230
+ // 'Row Vector * Matrix' operator. In HLSL, with mul(x, y) if x is a vector, it treated
231
+ // as a row vector. For more information:
232
+ // https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-mul
233
+ o.textureCoord = mul(float4(i.uv.x, i.uv.y, 1.0f, 0.0f), _UnityDisplayTransform).xy;
234
+ #endif
235
+ return o;
236
+ }
237
+
238
+ sampler2D _MainTex;
239
+ float _UnityCameraForwardScale;
240
+
241
+ #ifdef ARCORE_ENVIRONMENT_DEPTH_ENABLED
242
+ sampler2D _EnvironmentDepth;
243
+ #endif // ARCORE_ENVIRONMENT_DEPTH_ENABLED
244
+
245
+ #ifndef UNITY_COLORSPACE_GAMMA
246
+ float3 GammaToLinearSpace(float3 sRGB)
247
+ {
248
+ // Approximate version from http://chilliant.blogspot.com.au/2012/08/srgb-approximations-for-hlsl.html?m=1
249
+ return sRGB * (sRGB * (sRGB * 0.305306011F + 0.682171111F) + 0.012522878F);
250
+ }
251
+ #endif // !UNITY_COLORSPACE_GAMMA
252
+
253
+ float ConvertDistanceToDepth(float d)
254
+ {
255
+ #if TEXTURE_SOURCE_RAW_DISTANCE
256
+ return d;
257
+ #else
258
+ d = _UnityCameraForwardScale > 0.0 ? _UnityCameraForwardScale * d : d;
259
+
260
+ float zBufferParamsW = 1.0 / _ProjectionParams.y;
261
+ float zBufferParamsY = _ProjectionParams.z * zBufferParamsW;
262
+ float zBufferParamsX = 1.0 - zBufferParamsY;
263
+ float zBufferParamsZ = zBufferParamsX * _ProjectionParams.w;
264
+
265
+ // Clip any distances smaller than the near clip plane, and compute the depth value from the distance.
266
+ return (d < _ProjectionParams.y) ? 1.0f : ((1.0 / zBufferParamsZ) * ((1.0 / d) - zBufferParamsW));
267
+ #endif // TEXTURE_SOURCE_RAW_DISTANCE
268
+ }
269
+
270
+ struct fragOutput
271
+ {
272
+ float4 color : SV_Target;
273
+ float depth : SV_Depth;
274
+ };
275
+
276
+ fragOutput frag(v2f i)
277
+ {
278
+ #ifdef ARCORE_IMAGE_STABILIZATION_ENABLED
279
+ float2 tc = i.textureCoord.xy / i.textureCoord.z;
280
+ #else
281
+ float2 tc = i.textureCoord;
282
+ #endif
283
+ float3 result = tex2D(_MainTex, tc).xyz;
284
+ float depth = 1.0;
285
+
286
+ #ifdef ARCORE_ENVIRONMENT_DEPTH_ENABLED
287
+ float distance = tex2D(_EnvironmentDepth, tc).x;
288
+ depth = ConvertDistanceToDepth(distance);
289
+ #endif // ARCORE_ENVIRONMENT_DEPTH_ENABLED
290
+
291
+ #ifndef UNITY_COLORSPACE_GAMMA
292
+ result = GammaToLinearSpace(result);
293
+ #endif // !UNITY_COLORSPACE_GAMMA
294
+
295
+ fragOutput o;
296
+
297
+ // o.color = float4(result, 1.0);
298
+ o.color = float4(result, depth);
299
+ o.depth = 1.0 - depth; // Unity Vulkan uses reverse Z.
300
+
301
+ return o;
302
+ }
303
+
304
+ ENDHLSL
305
+ }
306
+ }
307
+
137
308
  FallBack Off
138
309
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.github.asus4.texture-source",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "displayName": "TextureSource",
5
5
  "description": "Simplify WebCamera and test video handling for using Computer Vision in Unity",
6
6
  "unity": "2020.3",