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

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
@@ -1,7 +1,46 @@
1
1
  # Texture Source
2
2
 
3
+ [![upm](https://img.shields.io/npm/v/com.github.asus4.texture-source?label=upm)](https://www.npmjs.com/package/com.github.asus4.texture-source)
4
+
3
5
  TextureSource is a utility that provides a consistent API to get the texture from various sources.
4
6
 
7
+ ![virtual-texture](https://github.com/asus4/TextureSource/assets/357497/e52f80d2-b1be-4cfa-81f7-76cdafe271bc)
8
+
9
+ ## Example API Usage
10
+
11
+ ```c#
12
+ using TextureSource;
13
+ using UnityEngine;
14
+
15
+ [RequireComponent(typeof(VirtualTextureSource))]
16
+ public class TextureSourceSample: MonoBehaviour
17
+ {
18
+ private void Start()
19
+ {
20
+ // Listen to OnTexture event from VirtualTextureSource
21
+ // Also able to bind in the inspector
22
+ if (TryGetComponent(out VirtualTextureSource source))
23
+ {
24
+ source.OnTexture.AddListener(OnTexture);
25
+ }
26
+ }
27
+
28
+ private void OnDestroy()
29
+ {
30
+ if (TryGetComponent(out VirtualTextureSource source))
31
+ {
32
+ source.OnTexture.RemoveListener(OnTexture);
33
+ }
34
+ }
35
+
36
+ public void OnTexture(Texture texture)
37
+ {
38
+ // Do whatever 🥳
39
+ // You don't need to think about webcam texture rotation.
40
+ }
41
+ }
42
+ ```
43
+
5
44
  ## Install via UPM
6
45
 
7
46
  Add the following setting to `Packages/manifest.json`
@@ -18,7 +57,7 @@ Add the following setting to `Packages/manifest.json`
18
57
  }
19
58
  ],
20
59
  "dependencies": {
21
- "com.github.asus4.texture-source": "0.2.1",
60
+ "com.github.asus4.texture-source": "0.3.0",
22
61
  ...// other dependencies
23
62
  }
24
63
  }
@@ -30,29 +69,37 @@ After installing the library, attach `VirtualTextureSource` to the GameObject.
30
69
 
31
70
  ![virtual-texture](https://github.com/asus4/TextureSource/assets/357497/e52f80d2-b1be-4cfa-81f7-76cdafe271bc)
32
71
 
33
- Then, right-click on the project panel and create the TexureSource scriptable object that you want to use. You can set different sources for the Editor and Runtime.
72
+ Then, right-click on the project panel and create the TextureSource scriptable object that you want to use. You can set different sources for the Editor and Runtime.
34
73
 
35
74
  ![scriptable-object](https://github.com/asus4/TextureSource/assets/357497/6c4862e2-5298-4f4e-8cd5-076d54d46db8)
36
75
 
37
76
  Currently provides the following sources:
38
77
 
39
- #### WebCam Texture Source
78
+ ### WebCam Texture Source
40
79
 
41
80
  Includes collecting device rotation.
42
81
 
43
82
  ![webcam-texture-source](https://github.com/asus4/TextureSource/assets/357497/407f7372-b214-4ba9-9093-2b39755b905b)
44
83
 
45
- #### Video Texture Source
84
+ ### Video Texture Source
46
85
 
47
86
  Useful when using test videos only in the Editor.
48
87
 
49
88
  ![video-texture-source](https://github.com/asus4/TextureSource/assets/357497/8e38ed1a-d2d8-4e16-9fc4-e5d4c6d0a888)
50
89
 
51
- #### AR Foundation Texture Source
90
+ ### Image Texture Source
91
+
92
+ Test with static images.
93
+
94
+ `OnTexture` event is invoked every frame if the `sendContinuousUpdate` is enabled.
95
+
96
+ ![image-texture-source](https://github.com/asus4/TextureSource/assets/357497/3d7eef4b-40c5-40b4-8403-b70f394ce938)
97
+
98
+ ### AR Foundation Texture Source
52
99
 
53
100
  Provides AR camera texture access. It supports both ARCore/ARKit.
54
101
 
55
- ![ar-faoundation-texture-source](https://github.com/asus4/TextureSource/assets/357497/5ac82a8a-0554-41a2-b9ef-c03ebd60c6ff)
102
+ ![ar-foundation-texture-source](https://github.com/asus4/TextureSource/assets/357497/5ac82a8a-0554-41a2-b9ef-c03ebd60c6ff)
56
103
 
57
104
  ## Acknowledgement
58
105
 
@@ -1,7 +1,5 @@
1
1
  #pragma kernel TextureTransform
2
2
 
3
- #include "Common.hlsl"
4
-
5
3
  Texture2D<float4> _InputTex;
6
4
  RWTexture2D<float4> _OutputTex;
7
5
  uint2 _OutputTexSize;
@@ -22,10 +20,6 @@ void TextureTransform (uint2 id : SV_DispatchThreadID)
22
20
 
23
21
  float4 c = _InputTex.SampleLevel(linearClampSampler, uv, 0);
24
22
 
25
- #ifndef UNITY_NO_LINEAR_COLORSPACE
26
- c.rgb = FastSRGBToLinear(c.rgb);
27
- #endif // !UNITY_COLORSPACE_GAMMA
28
-
29
23
  _OutputTex[id] = any(uv < 0) || any(uv > 1)
30
24
  ? float4(0, 0, 0, 1)
31
25
  : c;
@@ -6,6 +6,9 @@ namespace TextureSource
6
6
  using UnityEngine;
7
7
  using UnityEngine.XR.ARFoundation;
8
8
 
9
+ /// <summary>
10
+ /// Source from ARFoundation
11
+ /// </summary>
9
12
  [CreateAssetMenu(menuName = "ScriptableObject/Texture Source/ARFoundation", fileName = "ARFoundationTextureSource")]
10
13
  public sealed class ARFoundationTextureSource : BaseTextureSource
11
14
  {
@@ -2,6 +2,9 @@ namespace TextureSource
2
2
  {
3
3
  using UnityEngine;
4
4
 
5
+ /// <summary>
6
+ /// Abstract class for the source.
7
+ /// </summary>
5
8
  public abstract class BaseTextureSource : ScriptableObject, ITextureSource
6
9
  {
7
10
  public abstract bool DidUpdateThisFrame { get; }
@@ -2,6 +2,9 @@ namespace TextureSource
2
2
  {
3
3
  using UnityEngine;
4
4
 
5
+ /// <summary>
6
+ /// Interface for the source
7
+ /// </summary>
5
8
  public interface ITextureSource
6
9
  {
7
10
  bool DidUpdateThisFrame { get; }
@@ -0,0 +1,58 @@
1
+ namespace TextureSource
2
+ {
3
+ using UnityEngine;
4
+
5
+ /// <summary>
6
+ /// Source from image texture
7
+ /// </summary>
8
+ [CreateAssetMenu(menuName = "ScriptableObject/Texture Source/Image", fileName = "ImageTextureSource")]
9
+ public class ImageTextureSource : BaseTextureSource
10
+ {
11
+ [SerializeField]
12
+ private Texture[] textures = default;
13
+
14
+ [SerializeField]
15
+ private bool sendContinuousUpdate = false;
16
+
17
+ public override bool DidUpdateThisFrame
18
+ {
19
+ get
20
+ {
21
+ if (sendContinuousUpdate)
22
+ {
23
+ return true;
24
+ }
25
+
26
+ bool updated = isUpdated;
27
+ isUpdated = false;
28
+ return updated;
29
+ }
30
+ }
31
+
32
+ public override Texture Texture => textures[currentIndex];
33
+
34
+ private int currentIndex = 0;
35
+ private bool isUpdated = false;
36
+
37
+ public override void Start()
38
+ {
39
+ if (textures.Length == 0)
40
+ {
41
+ Debug.LogError("No texture is set");
42
+ return;
43
+ }
44
+ isUpdated = true;
45
+ }
46
+
47
+ public override void Stop()
48
+ {
49
+ isUpdated = false;
50
+ }
51
+
52
+ public override void Next()
53
+ {
54
+ currentIndex = (currentIndex + 1) % textures.Length;
55
+ isUpdated = true;
56
+ }
57
+ }
58
+ }
@@ -0,0 +1,11 @@
1
+ fileFormatVersion: 2
2
+ guid: b51cafa6a901147349bf2af15e41f6a5
3
+ MonoImporter:
4
+ externalObjects: {}
5
+ serializedVersion: 2
6
+ defaultReferences: []
7
+ executionOrder: 0
8
+ icon: {instanceID: 0}
9
+ userData:
10
+ assetBundleName:
11
+ assetBundleVariant:
@@ -3,6 +3,9 @@ namespace TextureSource
3
3
  using System;
4
4
  using UnityEngine;
5
5
 
6
+ /// <summary>
7
+ /// Transforms 2D texture with any arbitrary matrix
8
+ /// </summary>
6
9
  public class TextureTransformer : IDisposable
7
10
  {
8
11
  private static readonly int _InputTex = Shader.PropertyToID("_InputTex");
package/Runtime/Utils.cs CHANGED
@@ -2,6 +2,9 @@ namespace TextureSource
2
2
  {
3
3
  using UnityEngine;
4
4
 
5
+ /// <summary>
6
+ /// Internal utility functions
7
+ /// </summary>
5
8
  internal static class Utils
6
9
  {
7
10
  public static void GetTargetSizeScale(
@@ -4,6 +4,9 @@ namespace TextureSource
4
4
  using UnityEngine;
5
5
  using UnityEngine.Video;
6
6
 
7
+ /// <summary>
8
+ /// Source from video player
9
+ /// </summary>
7
10
  [CreateAssetMenu(menuName = "ScriptableObject/Texture Source/Video", fileName = "VideoTextureSource")]
8
11
  public class VideoTextureSource : BaseTextureSource
9
12
  {
@@ -5,7 +5,7 @@ namespace TextureSource
5
5
  using UnityEngine.Scripting;
6
6
 
7
7
  /// <summary>
8
- /// Virtual Texture Source
8
+ /// Invokes texture update event from the provided texture source ScriptableObject asset.
9
9
  /// </summary>
10
10
  public class VirtualTextureSource : MonoBehaviour
11
11
  {
@@ -4,30 +4,32 @@ namespace TextureSource
4
4
  using System.Linq;
5
5
  using UnityEngine;
6
6
 
7
+ /// <summary>
8
+ /// Source from WebCamTexture
9
+ /// </summary>
7
10
  [CreateAssetMenu(menuName = "ScriptableObject/Texture Source/WebCam", fileName = "WebCamTextureSource")]
8
11
  public sealed class WebCamTextureSource : BaseTextureSource
9
12
  {
10
- [Flags]
11
- public enum WebCamKindFlag
13
+ /// <summary>
14
+ /// Facing direction of the camera
15
+ /// </summary>
16
+ public enum CameraFacing
12
17
  {
13
- WideAngle = 1 << 0,
14
- Telephoto = 1 << 1,
15
- ColorAndDepth = 1 << 2,
16
- UltraWideAngle = 1 << 3,
17
- }
18
-
19
- [Flags]
20
- public enum FacingFlag
21
- {
22
- Front = 1 << 0,
23
- Back = 1 << 1,
18
+ Front,
19
+ Back,
24
20
  }
25
21
 
26
22
  [SerializeField]
27
- private WebCamKindFlag kindFilter = WebCamKindFlag.WideAngle | WebCamKindFlag.Telephoto | WebCamKindFlag.UltraWideAngle;
23
+ [Tooltip("Priorities of Camera Facing Direction")]
24
+ private CameraFacing[] facingPriorities = new CameraFacing[] {
25
+ CameraFacing.Back, CameraFacing.Front
26
+ };
28
27
 
29
28
  [SerializeField]
30
- private FacingFlag facingFilter = FacingFlag.Front | FacingFlag.Back;
29
+ [Tooltip("Priorities of WebCamKind")]
30
+ private WebCamKind[] kindPriority = new WebCamKind[] {
31
+ WebCamKind.WideAngle, WebCamKind.Telephoto, WebCamKind.UltraWideAngle,
32
+ };
31
33
 
32
34
  [SerializeField]
33
35
  private Vector2Int resolution = new Vector2Int(1270, 720);
@@ -57,16 +59,16 @@ namespace TextureSource
57
59
  private int lastUpdatedFrame = -1;
58
60
  private bool isFrontFacing;
59
61
 
60
- public WebCamKindFlag KindFilter
62
+ public CameraFacing[] FacingPriorities
61
63
  {
62
- get => kindFilter;
63
- set => kindFilter = value;
64
+ get => facingPriorities;
65
+ set => facingPriorities = value;
64
66
  }
65
67
 
66
- public FacingFlag FacingFilter
68
+ public WebCamKind[] KindPriorities
67
69
  {
68
- get => facingFilter;
69
- set => facingFilter = value;
70
+ get => kindPriority;
71
+ set => kindPriority = value;
70
72
  }
71
73
 
72
74
  public Vector2Int Resolution
@@ -85,7 +87,24 @@ namespace TextureSource
85
87
 
86
88
  public override void Start()
87
89
  {
88
- devices = WebCamTexture.devices.Where(IsMatchFilter).ToArray();
90
+ static CameraFacing GetFacing(WebCamDevice device)
91
+ {
92
+ return device.isFrontFacing ? CameraFacing.Front : CameraFacing.Back;
93
+ }
94
+
95
+ // Sort with facing, then kind
96
+ devices = WebCamTexture.devices
97
+ .Where(d => facingPriorities.Contains(GetFacing(d)) && kindPriority.Contains(d.kind))
98
+ .OrderBy(d => Array.IndexOf(facingPriorities, GetFacing(d)))
99
+ .ThenBy(d => Array.IndexOf(kindPriority, d.kind))
100
+ .ToArray();
101
+
102
+ if (devices.Length == 0)
103
+ {
104
+ Debug.LogError("No available camera found for the given priorities. Falling back to the default.");
105
+ devices = WebCamTexture.devices;
106
+ }
107
+
89
108
  StartCamera(currentIndex);
90
109
  }
91
110
 
@@ -159,23 +178,5 @@ namespace TextureSource
159
178
  lastUpdatedFrame = Time.frameCount;
160
179
  return transformer.Texture;
161
180
  }
162
-
163
- private bool IsMatchFilter(WebCamDevice device)
164
- {
165
- WebCamKindFlag kind = device.kind switch
166
- {
167
- WebCamKind.WideAngle => WebCamKindFlag.WideAngle,
168
- WebCamKind.Telephoto => WebCamKindFlag.Telephoto,
169
- WebCamKind.ColorAndDepth => WebCamKindFlag.ColorAndDepth,
170
- WebCamKind.UltraWideAngle => WebCamKindFlag.UltraWideAngle,
171
- _ => throw new NotImplementedException($"Unknown WebCamKind: {device.kind}"),
172
- };
173
- FacingFlag facing = device.isFrontFacing
174
- ? FacingFlag.Front
175
- : FacingFlag.Back;
176
-
177
- return kindFilter.HasFlag(kind)
178
- && facingFilter.HasFlag(facing);
179
- }
180
181
  }
181
182
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com.github.asus4.texture-source",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "displayName": "TextureSource",
5
5
  "description": "Simplify WebCamera and test video handling for using Computer Vision in Unity",
6
6
  "unity": "2020.3",
@@ -11,6 +11,7 @@
11
11
  ],
12
12
  "documentationUrl": "https://github.com/asus4/TextureSource/tree/main",
13
13
  "changelogUrl": "https://github.com/asus4/TextureSource/releases",
14
+ "license": "MIT",
14
15
  "licensesUrl": "https://github.com/asus4/TextureSource/blob/main/Packages/com.github.asus4.texture-source/LICENSE",
15
16
  "author": {
16
17
  "name": "Koki Ibukuro",
@@ -1,8 +0,0 @@
1
- // Ported from RenderPipeLine-Core, to support both URP and Built-in RP in one shader
2
- // com.unity.render-pipelines.core copyright © 2020 Unity Technologies ApS
3
- // Licensed under the Unity Companion License for Unity-dependent projects--see [Unity Companion License](http://www.unity3d.com/legal/licenses/Unity_Companion_License).
4
-
5
- float3 FastSRGBToLinear(float3 c)
6
- {
7
- return c * (c * (c * 0.305306011 + 0.682171111) + 0.012522878);
8
- }
@@ -1,7 +0,0 @@
1
- fileFormatVersion: 2
2
- guid: d73b9c13ee77b4bb48a8ff20c23c9709
3
- ShaderIncludeImporter:
4
- externalObjects: {}
5
- userData:
6
- assetBundleName:
7
- assetBundleVariant: