-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUnityARVideo.cs
119 lines (95 loc) · 3.6 KB
/
UnityARVideo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Rendering;
namespace UnityEngine.XR.iOS
{
public class UnityARVideo : MonoBehaviour
{
public Material m_ClearMaterial;
private CommandBuffer m_VideoCommandBuffer;
private Texture2D _videoTextureY;
private Texture2D _videoTextureCbCr;
private Matrix4x4 _displayTransform;
private bool bCommandBufferInitialized;
public void Start()
{
UnityARSessionNativeInterface.ARFrameUpdatedEvent += UpdateFrame;
bCommandBufferInitialized = false;
}
void UpdateFrame(UnityARCamera cam)
{
_displayTransform = new Matrix4x4();
_displayTransform.SetColumn(0, cam.displayTransform.column0);
_displayTransform.SetColumn(1, cam.displayTransform.column1);
_displayTransform.SetColumn(2, cam.displayTransform.column2);
_displayTransform.SetColumn(3, cam.displayTransform.column3);
}
void InitializeCommandBuffer()
{
m_VideoCommandBuffer = new CommandBuffer();
m_VideoCommandBuffer.Blit(null, BuiltinRenderTextureType.CurrentActive, m_ClearMaterial);
GetComponent<Camera>().AddCommandBuffer(CameraEvent.BeforeForwardOpaque, m_VideoCommandBuffer);
bCommandBufferInitialized = true;
}
void OnDestroy()
{
if (m_VideoCommandBuffer != null) {
GetComponent<Camera>().RemoveCommandBuffer(CameraEvent.BeforeForwardOpaque, m_VideoCommandBuffer);
}
UnityARSessionNativeInterface.ARFrameUpdatedEvent -= UpdateFrame;
bCommandBufferInitialized = false;
}
#if !UNITY_EDITOR && UNITY_IOS
public void OnPreRender()
{
ARTextureHandles handles = UnityARSessionNativeInterface.GetARSessionNativeInterface().GetARVideoTextureHandles();
if (handles.IsNull())
{
return;
}
if (!bCommandBufferInitialized) {
InitializeCommandBuffer ();
}
Resolution currentResolution = Screen.currentResolution;
// Texture Y
if (_videoTextureY == null) {
_videoTextureY = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height,
TextureFormat.R8, false, false, (System.IntPtr)handles.TextureY);
_videoTextureY.filterMode = FilterMode.Bilinear;
_videoTextureY.wrapMode = TextureWrapMode.Repeat;
m_ClearMaterial.SetTexture("_textureY", _videoTextureY);
}
// Texture CbCr
if (_videoTextureCbCr == null) {
_videoTextureCbCr = Texture2D.CreateExternalTexture(currentResolution.width, currentResolution.height,
TextureFormat.RG16, false, false, (System.IntPtr)handles.TextureCbCr);
_videoTextureCbCr.filterMode = FilterMode.Bilinear;
_videoTextureCbCr.wrapMode = TextureWrapMode.Repeat;
m_ClearMaterial.SetTexture("_textureCbCr", _videoTextureCbCr);
}
_videoTextureY.UpdateExternalTexture(handles.TextureY);
_videoTextureCbCr.UpdateExternalTexture(handles.TextureCbCr);
m_ClearMaterial.SetMatrix("_DisplayTransform", _displayTransform);
}
#else
public void SetYTexure(Texture2D YTex)
{
_videoTextureY = YTex;
}
public void SetUVTexure(Texture2D UVTex)
{
_videoTextureCbCr = UVTex;
}
public void OnPreRender()
{
if (!bCommandBufferInitialized) {
InitializeCommandBuffer ();
}
m_ClearMaterial.SetTexture("_textureY", _videoTextureY);
m_ClearMaterial.SetTexture("_textureCbCr", _videoTextureCbCr);
m_ClearMaterial.SetMatrix("_DisplayTransform", _displayTransform);
}
#endif
}
}