MaxstARSDK  3.5.0
MapRendererBehaviour.cs
1 /*==============================================================================
2 Copyright 2017 Maxst, Inc. All Rights Reserved.
3 ==============================================================================*/
4 
5 using UnityEngine;
6 using System.Collections;
7 using System.Runtime.InteropServices;
8 
9 namespace maxstAR
10 {
14  public class MapRendererBehaviour : MonoBehaviour
15  {
16  [SerializeField]
17  private GameObject[] groupObjects = null;
18 
19  [SerializeField]
20  private GameObject[] imageObjects = null;
21 
22  [SerializeField]
23  private GameObject[] cameraObjects = null;
24 
25  [SerializeField]
26  private GameObject[] meshObjects = null;
27 
28  [SerializeField]
29  private GameObject viewCameraObject = null;
30 
31  [SerializeField]
32  private Vector2 gameViewSize = Vector2.zero;
33 
34  [SerializeField]
35  private float textureWidth = 0;
36 
37  [SerializeField]
38  private float textureHeight = 0;
39 
40  private const float vx = 2500.0f;
41  private const float vy = 1875.0f;
42  private const float vz = 5000.0f;
43 
44  // TODO
45  private const float scaleFactor = 0.85f;
46 
47  internal void Create(Vector3[] vertices, Matrix4x4[] cameraMatrices, Material[] materials)
48  {
49  int materialsLength = materials.Length;
50  if (materialsLength == 0)
51  {
52  return;
53  }
54 
55  transform.localRotation = Camera.main.transform.localRotation;
56 
57  textureWidth = materials[0].mainTexture.width;
58  textureHeight = materials[0].mainTexture.height;
59 
60  GameObject mapViewerCameraObject = Resources.Load<GameObject>("Contents/MapViewerCamera");
61 
62  groupObjects = new GameObject[materialsLength];
63  cameraObjects = new GameObject[materialsLength];
64  imageObjects = new GameObject[materialsLength];
65  meshObjects = new GameObject[materialsLength];
66 
67  for (int i = 0; i < materialsLength; i++)
68  {
69  groupObjects[i] = new GameObject();
70  cameraObjects[i] = Instantiate(mapViewerCameraObject);
71  meshObjects[i] = new GameObject();
72  imageObjects[i] = GameObject.CreatePrimitive(PrimitiveType.Quad);
73 
74  groupObjects[i].name = "Keyframe" + i;
75  cameraObjects[i].name = "Camera";
76  meshObjects[i].name = "Mesh";
77  imageObjects[i].name = "Image";
78 
79  groupObjects[i].transform.parent = transform;
80  cameraObjects[i].transform.parent = groupObjects[i].transform;
81  meshObjects[i].transform.parent = groupObjects[i].transform;
82  imageObjects[i].transform.parent = groupObjects[i].transform;
83 
84  cameraObjects[i].layer = LayerMask.NameToLayer("Ignore Raycast");
85  meshObjects[i].AddComponent(typeof(MeshFilter));
86  meshObjects[i].AddComponent(typeof(MeshRenderer));
87  meshObjects[i].GetComponent<MeshFilter>().mesh = CreateMapViewerMesh(i, vertices);
88  meshObjects[i].GetComponent<Renderer>().material = materials[i];
89  imageObjects[i].GetComponent<Renderer>().material = materials[i];
90 
91  groupObjects[i].transform.localPosition = Vector3.zero;
92  groupObjects[i].transform.localRotation = Quaternion.identity;
93  groupObjects[i].transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
94 
95  Matrix4x4 m = MatrixUtils.GetUnityPoseMatrix(cameraMatrices[i]);
96  cameraObjects[i].transform.localRotation = MatrixUtils.QuaternionFromMatrix(m);
97  cameraObjects[i].transform.localPosition = MatrixUtils.PositionFromMatrix(m);
98  cameraObjects[i].transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
99 
100  meshObjects[i].transform.localPosition = Vector3.zero;
101  meshObjects[i].transform.localRotation = Quaternion.identity;
102  meshObjects[i].transform.localScale = new Vector3(scaleFactor, scaleFactor, scaleFactor);
103 
104  imageObjects[i].transform.localPosition = new Vector3(0.0f, 0.0f, 10.0f);
105  imageObjects[i].transform.localRotation = Quaternion.identity;
106  imageObjects[i].transform.localScale = new Vector3(10.0f, 10.0f * -textureHeight / textureWidth, 10.0f);
107  }
108 
109  gameViewSize = GetGameViewSize();
110  viewCameraObject = new GameObject();
111  viewCameraObject.name = "ViewCamera";
112  viewCameraObject.transform.parent = transform;
113  Camera camera = viewCameraObject.AddComponent<Camera>();
114  camera.ResetProjectionMatrix();
115  camera.nearClipPlane = 0.03f;
116  camera.farClipPlane = 10000.0f;
117  camera.aspect = gameViewSize.x / gameViewSize.y;
118  camera.clearFlags = CameraClearFlags.SolidColor;
119  camera.backgroundColor = Color.black;
120  camera.cullingMask &= ~(1 << LayerMask.NameToLayer("Ignore Raycast"));
121 
123  if (gameViewSize.x > gameViewSize.y * textureWidth / textureHeight)
124  {
125  camera.fieldOfView = 2.0f * Mathf.Atan2(vy, vz) * Mathf.Rad2Deg;
126  }
127  else
128  {
129  camera.fieldOfView = 2.0f * Mathf.Atan2(vx * gameViewSize.y / gameViewSize.x, vz) * Mathf.Rad2Deg;
130  }
131 
132  viewCameraObject.transform.localPosition = cameraObjects[0].transform.localPosition;
133  viewCameraObject.transform.localRotation = cameraObjects[0].transform.localRotation;
134  }
135 
136  private Mesh CreateMapViewerMesh(int idx, Vector3[] vertices)
137  {
138  int verticesLength = vertices.Length;
139  if (verticesLength == 0)
140  {
141  return null;
142  }
143 
144  int size = MapViewer.GetInstance().Create(idx);
145  if (size == 0)
146  {
147  return null;
148  }
149 
150  int[] indices = new int[size];
151  float[] texCoords = new float[verticesLength * 2];
152 
153  MapViewer.GetInstance().GetIndices(out indices[0]);
154  MapViewer.GetInstance().GetTexCoords(out texCoords[0]);
155 
156  Vector2[] uvs = new Vector2[verticesLength];
157  for (int j = 0; j < verticesLength; j++)
158  {
159  uvs[j][0] = texCoords[2 * j + 0];
160  uvs[j][1] = texCoords[2 * j + 1];
161  }
162 
163  Mesh mesh = new Mesh();
164  mesh.vertices = vertices;
165  mesh.triangles = indices;
166  mesh.uv = uvs;
167  mesh.RecalculateNormals();
168 
169  return mesh;
170  }
171 
172  internal void Clear()
173  {
174  int childCount = gameObject.transform.childCount;
175  for (int i = 0; i < childCount; i++)
176  {
177  DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
178  }
179 
180  if (viewCameraObject != null)
181  {
182  DestroyImmediate(viewCameraObject);
183  }
184 
185  if (groupObjects != null)
186  {
187  int groupObjectsLength = groupObjects.Length;
188  for (int i = 0; i < groupObjectsLength; i++)
189  {
190  DestroyImmediate(groupObjects[i]);
191  }
192 
193  groupObjects = null;
194  }
195 
196  if (imageObjects != null)
197  {
198  int imageObjectsLength = imageObjects.Length;
199  for (int i = 0; i < imageObjectsLength; i++)
200  {
201  DestroyImmediate(imageObjects[i]);
202  }
203 
204  imageObjects = null;
205  }
206 
207  if (cameraObjects != null)
208  {
209  int cameraObjectsLength = cameraObjects.Length;
210  for (int i = 0; i < cameraObjectsLength; i++)
211  {
212  DestroyImmediate(cameraObjects[i]);
213  }
214 
215  cameraObjects = null;
216  }
217 
218  if (meshObjects != null)
219  {
220  int meshObjectLength = meshObjects.Length;
221  for (int i = 0; i < meshObjectLength; i++)
222  {
223  DestroyImmediate(meshObjects[i]);
224  }
225 
226  meshObjects = null;
227  }
228  }
229 
230  internal void SetActiveImageObject(int index)
231  {
232  if (cameraObjects == null || imageObjects == null)
233  {
234  return;
235  }
236 
237  int cameraObjectsLength = cameraObjects.Length;
238  for (int i = 0; i < cameraObjectsLength; i++)
239  {
240  cameraObjects[i].SetActive(true);
241  }
242 
243  int imageObjectsLength = imageObjects.Length;
244  for (int i = 0; i < imageObjectsLength; i++)
245  {
246  imageObjects[i].SetActive(false);
247  }
248 
249  imageObjects[index].SetActive(true);
250 
251  viewCameraObject.transform.localPosition = Vector3.zero;
252  viewCameraObject.transform.localRotation = Quaternion.identity;
253  }
254 
255  internal void SetActiveMeshObject(int index)
256  {
257  if (meshObjects == null)
258  {
259  return;
260  }
261 
262  int meshObjectsLength = meshObjects.Length;
263  for (int i = 0; i < meshObjectsLength; i++)
264  {
265  meshObjects[i].SetActive(false);
266  }
267 
268  meshObjects[index].SetActive(true);
269 
270  viewCameraObject.transform.localPosition = cameraObjects[index].transform.localPosition;
271  viewCameraObject.transform.localRotation = cameraObjects[index].transform.localRotation;
272  }
273 
274  internal void SetDeactiveImageObjects()
275  {
276  if (imageObjects == null)
277  {
278  return;
279  }
280 
281  int imageObjectsLength = imageObjects.Length;
282  for (int i = 0; i < imageObjectsLength; i++)
283  {
284  if (imageObjects[i] != null)
285  {
286  imageObjects[i].SetActive(false);
287  }
288  }
289  }
290 
291  internal void SetDeactiveMeshObjects()
292  {
293  if (meshObjects == null)
294  {
295  return;
296  }
297 
298  int meshObjectLength = meshObjects.Length;
299  for (int i = 0; i < meshObjectLength; i++)
300  {
301  if (meshObjects[i] != null)
302  {
303  meshObjects[i].SetActive(false);
304  }
305  }
306  }
307 
308  private void OnRenderObject()
309  {
310  if (viewCameraObject != null)
311  {
312  Vector2 tempGameViewSize = GetGameViewSize();
313  if (gameViewSize != tempGameViewSize)
314  {
315  gameViewSize = tempGameViewSize;
316  Camera camera = viewCameraObject.GetComponent<Camera>();
317  camera.aspect = gameViewSize.x / gameViewSize.y;
318 
320  if (gameViewSize.x > gameViewSize.y * textureWidth / textureHeight)
321  {
322  camera.fieldOfView = 2.0f * Mathf.Atan2(vy, vz) * Mathf.Rad2Deg;
323  }
324  else
325  {
326  camera.fieldOfView = 2.0f * Mathf.Atan2(vx * gameViewSize.y / gameViewSize.x, vz) * Mathf.Rad2Deg;
327  }
328  }
329  }
330  }
331 
332  private Vector2 GetGameViewSize()
333  {
334  System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
335  System.Reflection.MethodInfo GetSizeOfMainGameView = T.GetMethod("GetSizeOfMainGameView",
336  System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
337  System.Object Res = GetSizeOfMainGameView.Invoke(null, null);
338 
339  return (Vector2)Res;
340  }
341  }
342 }
Map created by Visual SLAM renderer