7 using System.Runtime.InteropServices;
17 private GameObject[] groupObjects = null;
20 private GameObject[] imageObjects = null;
23 private GameObject[] cameraObjects = null;
26 private GameObject[] meshObjects = null;
29 private GameObject viewCameraObject = null;
32 private Vector2 gameViewSize = Vector2.zero;
35 private float textureWidth = 0;
38 private float textureHeight = 0;
40 private const float vx = 2500.0f;
41 private const float vy = 1875.0f;
42 private const float vz = 5000.0f;
45 private const float scaleFactor = 0.85f;
47 internal void Create(Vector3[] vertices, Matrix4x4[] cameraMatrices, Material[] materials)
49 int materialsLength = materials.Length;
50 if (materialsLength == 0)
55 transform.localRotation = Camera.main.transform.localRotation;
57 textureWidth = materials[0].mainTexture.width;
58 textureHeight = materials[0].mainTexture.height;
60 GameObject mapViewerCameraObject = Resources.Load<GameObject>(
"Contents/MapViewerCamera");
62 groupObjects =
new GameObject[materialsLength];
63 cameraObjects =
new GameObject[materialsLength];
64 imageObjects =
new GameObject[materialsLength];
65 meshObjects =
new GameObject[materialsLength];
67 for (
int i = 0; i < materialsLength; i++)
69 groupObjects[i] =
new GameObject();
70 cameraObjects[i] = Instantiate(mapViewerCameraObject);
71 meshObjects[i] =
new GameObject();
72 imageObjects[i] = GameObject.CreatePrimitive(PrimitiveType.Quad);
74 groupObjects[i].name =
"Keyframe" + i;
75 cameraObjects[i].name =
"Camera";
76 meshObjects[i].name =
"Mesh";
77 imageObjects[i].name =
"Image";
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;
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];
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);
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);
100 meshObjects[i].transform.localPosition = Vector3.zero;
101 meshObjects[i].transform.localRotation = Quaternion.identity;
102 meshObjects[i].transform.localScale =
new Vector3(scaleFactor, scaleFactor, scaleFactor);
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);
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"));
123 if (gameViewSize.x > gameViewSize.y * textureWidth / textureHeight)
125 camera.fieldOfView = 2.0f * Mathf.Atan2(vy, vz) * Mathf.Rad2Deg;
129 camera.fieldOfView = 2.0f * Mathf.Atan2(vx * gameViewSize.y / gameViewSize.x, vz) * Mathf.Rad2Deg;
132 viewCameraObject.transform.localPosition = cameraObjects[0].transform.localPosition;
133 viewCameraObject.transform.localRotation = cameraObjects[0].transform.localRotation;
136 private Mesh CreateMapViewerMesh(
int idx, Vector3[] vertices)
138 int verticesLength = vertices.Length;
139 if (verticesLength == 0)
144 int size =
MapViewer.GetInstance().Create(idx);
150 int[] indices =
new int[size];
151 float[] texCoords =
new float[verticesLength * 2];
153 MapViewer.GetInstance().GetIndices(out indices[0]);
154 MapViewer.GetInstance().GetTexCoords(out texCoords[0]);
156 Vector2[] uvs =
new Vector2[verticesLength];
157 for (
int j = 0; j < verticesLength; j++)
159 uvs[j][0] = texCoords[2 * j + 0];
160 uvs[j][1] = texCoords[2 * j + 1];
163 Mesh mesh =
new Mesh();
164 mesh.vertices = vertices;
165 mesh.triangles = indices;
167 mesh.RecalculateNormals();
172 internal void Clear()
174 int childCount = gameObject.transform.childCount;
175 for (
int i = 0; i < childCount; i++)
177 DestroyImmediate(gameObject.transform.GetChild(0).gameObject);
180 if (viewCameraObject != null)
182 DestroyImmediate(viewCameraObject);
185 if (groupObjects != null)
187 int groupObjectsLength = groupObjects.Length;
188 for (
int i = 0; i < groupObjectsLength; i++)
190 DestroyImmediate(groupObjects[i]);
196 if (imageObjects != null)
198 int imageObjectsLength = imageObjects.Length;
199 for (
int i = 0; i < imageObjectsLength; i++)
201 DestroyImmediate(imageObjects[i]);
207 if (cameraObjects != null)
209 int cameraObjectsLength = cameraObjects.Length;
210 for (
int i = 0; i < cameraObjectsLength; i++)
212 DestroyImmediate(cameraObjects[i]);
215 cameraObjects = null;
218 if (meshObjects != null)
220 int meshObjectLength = meshObjects.Length;
221 for (
int i = 0; i < meshObjectLength; i++)
223 DestroyImmediate(meshObjects[i]);
230 internal void SetActiveImageObject(
int index)
232 if (cameraObjects == null || imageObjects == null)
237 int cameraObjectsLength = cameraObjects.Length;
238 for (
int i = 0; i < cameraObjectsLength; i++)
240 cameraObjects[i].SetActive(
true);
243 int imageObjectsLength = imageObjects.Length;
244 for (
int i = 0; i < imageObjectsLength; i++)
246 imageObjects[i].SetActive(
false);
249 imageObjects[index].SetActive(
true);
251 viewCameraObject.transform.localPosition = Vector3.zero;
252 viewCameraObject.transform.localRotation = Quaternion.identity;
255 internal void SetActiveMeshObject(
int index)
257 if (meshObjects == null)
262 int meshObjectsLength = meshObjects.Length;
263 for (
int i = 0; i < meshObjectsLength; i++)
265 meshObjects[i].SetActive(
false);
268 meshObjects[index].SetActive(
true);
270 viewCameraObject.transform.localPosition = cameraObjects[index].transform.localPosition;
271 viewCameraObject.transform.localRotation = cameraObjects[index].transform.localRotation;
274 internal void SetDeactiveImageObjects()
276 if (imageObjects == null)
281 int imageObjectsLength = imageObjects.Length;
282 for (
int i = 0; i < imageObjectsLength; i++)
284 if (imageObjects[i] != null)
286 imageObjects[i].SetActive(
false);
291 internal void SetDeactiveMeshObjects()
293 if (meshObjects == null)
298 int meshObjectLength = meshObjects.Length;
299 for (
int i = 0; i < meshObjectLength; i++)
301 if (meshObjects[i] != null)
303 meshObjects[i].SetActive(
false);
308 private void OnRenderObject()
310 if (viewCameraObject != null)
312 Vector2 tempGameViewSize = GetGameViewSize();
313 if (gameViewSize != tempGameViewSize)
315 gameViewSize = tempGameViewSize;
316 Camera camera = viewCameraObject.GetComponent<Camera>();
317 camera.aspect = gameViewSize.x / gameViewSize.y;
320 if (gameViewSize.x > gameViewSize.y * textureWidth / textureHeight)
322 camera.fieldOfView = 2.0f * Mathf.Atan2(vy, vz) * Mathf.Rad2Deg;
326 camera.fieldOfView = 2.0f * Mathf.Atan2(vx * gameViewSize.y / gameViewSize.x, vz) * Mathf.Rad2Deg;
332 private Vector2 GetGameViewSize()
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);
Map created by Visual SLAM renderer