MaxstARSDK  3.5.0
AbstractCameraBackgroundBehaviour.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using UnityEngine.Rendering;
7 using System.Collections;
8 using System.Linq;
9 using System;
10 using System.Runtime.InteropServices;
11 
12 namespace maxstAR
13 {
18  public class AbstractCameraBackgroundBehaviour : MonoBehaviour
19  {
20  public Shader RGBBackgroundShader;
21  public Shader YuvBackgroundShader;
22  public Shader AndroidYuvBackgroundShader;
23  public Color32 pauseColor = new Color32(255, 255, 255, 255);
24  private static AbstractCameraBackgroundBehaviour instance = null;
25 
26  internal static AbstractCameraBackgroundBehaviour Instance
27  {
28  get
29  {
30  if (instance == null)
31  {
32  instance = FindObjectOfType<AbstractCameraBackgroundBehaviour>();
33  }
34 
35  return instance;
36  }
37  }
38 
39  private int cameraWidth = 0;
40  private int cameraHeight = 0;
41  private Texture2D rgbTexture = null;
42  private bool keepRendering = false;
43 
44  private Texture2D yTexture = null;
45  private Texture2D uvTexture = null;
46 
47  void Awake()
48  {
49  Debug.Log("Current graphics device type is " + SystemInfo.graphicsDeviceType + ".");
50 
51  if (Application.platform == RuntimePlatform.Android ||
52  Application.platform == RuntimePlatform.IPhonePlayer)
53  {
54  MaxstAR.SetScreenOrientation((int)Screen.orientation);
55  }
56  else
57  {
58  MaxstAR.SetScreenOrientation((int)ScreenOrientation.LandscapeLeft);
59  }
60  }
61 
62  void OnApplicationPause(bool pause)
63  {
64  if (pause)
65  {
66  StopRendering();
67  }
68  else
69  {
70  StartRendering();
71  }
72  }
73 
74  void OnEnable()
75  {
76  StartRendering();
77  }
78 
79  void OnDisable()
80  {
81  StopRendering();
82  }
83 
84  void OnDestroy()
85  {
86  StopRendering();
87  }
88 
89  private void StartRendering()
90  {
91  GetComponent<Renderer>().material.SetFloat("pauseMode", 0.0f);
92  Debug.Log("StartRendering");
93  keepRendering = true;
94  }
95 
96  private void StopRendering()
97  {
98  GetComponent<Renderer>().material.SetFloat("pauseMode", 1.0f);
99  Debug.Log("StopRendering");
100  keepRendering = false;
101  }
102 
103  private void CreateCameraTexture(int imageWidth, int imageHeight, ColorFormat imageFormat)
104  {
105  if(rgbTexture != null || yTexture != null) {
106  return;
107  }
108 
109  if(imageFormat == ColorFormat.RGB888) {
110  rgbTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.RGB24, false);
111  GetComponent<Renderer>().material = new Material(RGBBackgroundShader);
112  GetComponent<Renderer>().material.mainTexture = rgbTexture;
113  } else if(imageFormat == ColorFormat.YUV420sp) {
114  yTexture = new Texture2D(imageWidth, imageHeight, TextureFormat.R8, false);
115  uvTexture = new Texture2D(imageWidth / 2, imageHeight / 2, TextureFormat.RG16, false);
116 
117  if (Application.platform == RuntimePlatform.Android) {
118  GetComponent<Renderer>().material = new Material(AndroidYuvBackgroundShader);
119  } else {
120  GetComponent<Renderer>().material = new Material(YuvBackgroundShader);
121  }
122 
123  GetComponent<Renderer>().material.SetTexture("_YTex", yTexture);
124  GetComponent<Renderer>().material.SetTexture("_UVTex", uvTexture);
125  }
126 
127  Mesh mesh = GetComponent<MeshFilter>().mesh;
128  Vector3[] vertices = mesh.vertices;
129  Vector2[] uvs = new Vector2[vertices.Length];
130 
131  uvs[0].x = 0; uvs[0].y = 1;
132  uvs[1].x = 1; uvs[1].y = 0;
133  uvs[2].x = 1; uvs[2].y = 1;
134  uvs[3].x = 0; uvs[3].y = 0;
135  mesh.uv = uvs;
136 
137  GetComponent<Renderer>().material.SetColor("_PauseColor", pauseColor);
138  GetComponent<Renderer>().material.renderQueue = 1500;
139  Debug.Log("Create background texture");
140  }
141 
142  private void Update()
143  {
145  TrackedImage image = state.GetImagePtr();
146 
147  if (image != null && image.GetWidth() > 0 && image.GetHeight() > 0)
148  {
149  CreateCameraTexture(image.GetWidth(), image.GetHeight(), image.GetFormat());
150 
151  if (keepRendering)
152  {
153  UpdateInternal(image);
154  }
155  }
156  }
157 
158  void UpdateInternal(TrackedImage image)
159  {
160  IntPtr cameraFrameBuffer = image.GetDataPtr();
161 
162  if (cameraFrameBuffer != IntPtr.Zero)
163  {
164  switch(image.GetFormat()) {
165  case ColorFormat.RGB888:
166  {
167  if(rgbTexture != null) {
168  rgbTexture.LoadRawTextureData(cameraFrameBuffer, image.GetWidth() * image.GetHeight() * 3);
169  rgbTexture.Apply();
170  }
171  }
172  break;
173  case ColorFormat.YUV420sp:
174  {
175  if(yTexture != null) {
176  IntPtr yPtr;
177  IntPtr uvPtr;
178  unsafe
179  {
180  byte* pointer = (byte*)cameraFrameBuffer.ToPointer();
181  yPtr = (IntPtr)pointer;
182  pointer += image.GetWidth() * image.GetHeight();
183  uvPtr = (IntPtr)pointer;
184  }
185 
186  yTexture.LoadRawTextureData(yPtr, image.GetWidth() * image.GetHeight());
187  yTexture.Apply();
188  uvTexture.LoadRawTextureData(uvPtr, image.GetWidth() * image.GetHeight() / 2);
189  uvTexture.Apply();
190  }
191  }
192  break;
193  }
194 
195  }
196  }
197 
198  internal bool RenderingEnabled()
199  {
200  return keepRendering;
201  }
202  }
203 }
int GetHeight()
Get height
int GetWidth()
Get width
static void SetScreenOrientation(int orientation)
Notify screen orientation chagned
Definition: MaxstAR.cs:33
Container for individual tracking information
Set device environment
Definition: MaxstAR.cs:15
ColorFormat GetFormat()
Image format
TrackingState GetTrackingState()
Get saved TrackingState value
Handles native background rendering. Background rendering includes camera image, feature point...
ColorFormat
Camera image format
Definition: ColorFormat.cs:11
image data which is used for tracker and rendering
Definition: TrackedImage.cs:13
static TrackerManager GetInstance()
Get TrackerManager instance
Control AR Engine (Singletone)