9 using System.Runtime.InteropServices;
19 private int keyframeIndex = 0;
25 get {
return keyframeIndex; }
28 keyframeIndex = value;
34 private bool showMesh =
false;
40 get {
return showMesh; }
49 private bool autoCamera =
false;
55 get {
return autoCamera; }
64 private int maxKeyframeCount = 0;
70 get {
return maxKeyframeCount; }
71 set { maxKeyframeCount = value; }
75 private bool transparent =
false;
81 get {
return transparent; }
87 SetTransparent(transparent);
92 private Quaternion[] cameraRotations = null;
95 private Matrix4x4[] cameraMatrices = null;
98 private Vector3[] vertices = null;
101 private Material[] materials = null;
111 public bool Load(
string fileName)
113 if (!ReadMap(fileName))
118 Debug.Log(
"Read Json Success");
121 if (mapRendererBehaviour == null)
126 mapRendererBehaviour.Clear();
127 mapRendererBehaviour.Create(vertices, cameraMatrices, materials);
129 SetTransparent(transparent);
135 private bool ReadMap(
string fileName)
138 if (!
MapViewer.GetInstance().Initialize(fileName))
143 IntPtr jsonPtr = MapViewer.GetInstance().GetJson();
144 string json = Marshal.PtrToStringAnsi(jsonPtr);
148 Debug.Log(
"Map is not opened");
152 Map3D map3D = JsonReader.Deserialize<Map3D>(json);
154 int width = map3D.width;
155 int height = map3D.height;
156 maxKeyframeCount = map3D.imageCount;
158 vertices =
new Vector3[map3D.vertexCount];
159 for (
int i = 0; i < map3D.vertexCount; i++)
161 vertices[i] =
new Vector3(map3D.vertices[i].x, -map3D.vertices[i].y, map3D.vertices[i].z);
164 cameraMatrices =
new Matrix4x4[maxKeyframeCount];
165 for (
int i = 0; i < maxKeyframeCount; i++)
167 for (
int j = 0; j < 16; j++)
169 cameraMatrices[i][j] = map3D.poseMatrices[i][j];
171 cameraMatrices[i] = cameraMatrices[i].inverse;
174 cameraRotations =
new Quaternion[maxKeyframeCount];
175 for (
int i = 0; i < maxKeyframeCount; i++)
177 cameraRotations[i] = MatrixUtils.InvQuaternionFromMatrix(cameraMatrices[i]);
178 Vector3 tempR = cameraRotations[i].eulerAngles;
181 cameraRotations[i] = Quaternion.Euler(tempR);
184 Shader color = Shader.Find(
"Unlit/Texture");
185 materials =
new Material[maxKeyframeCount];
186 for (
int i = 0; i < maxKeyframeCount; i++)
188 materials[i] =
new Material(color);
189 materials[i].mainTexture = GetCameraTexture(i, width, height);
195 private void SetTransparent(
bool transparent)
197 if (materials == null)
202 Shader alpha = Shader.Find(
"Unlit/Transparent");
203 Shader color = Shader.Find(
"Unlit/Texture");
205 int materialsLength = materials.Length;
206 for (
int i = 0; i < materialsLength; i++)
208 if (transparent ==
true)
210 materials[i].shader = alpha;
214 materials[i].shader = color;
219 private Texture2D GetCameraTexture(
int index,
int width,
int height)
221 int size = width * height;
222 byte[] image =
new byte[size];
223 MapViewer.GetInstance().GetImage(index, out image[0]);
226 byte[] rawTextureData =
new byte[size * 4];
227 for (
int j = 0; j < size; j++)
229 rawTextureData[j * 4 + 0] = alpha;
230 rawTextureData[j * 4 + 1] = image[j];
231 rawTextureData[j * 4 + 2] = image[j];
232 rawTextureData[j * 4 + 3] = image[j];
235 Texture2D texture =
new Texture2D(width, height, TextureFormat.ARGB32,
false);
236 texture.LoadRawTextureData(rawTextureData);
247 if (showMesh ==
true)
249 mapRendererBehaviour.SetDeactiveImageObjects();
250 mapRendererBehaviour.SetActiveMeshObject(keyframeIndex);
254 mapRendererBehaviour.SetDeactiveMeshObjects();
255 mapRendererBehaviour.SetActiveImageObject(keyframeIndex);
266 const float distanceWeight = 1.0f;
267 const float angleWeight = 10.0f;
269 float minWeightSum = 999999.0f;
271 Vector3 tPosition = position;
272 Quaternion tQuaternion = quaternion;
274 for (
int i = 0; i < maxKeyframeCount; i++)
276 Matrix4x4 cameraMatrix = cameraMatrices[i];
277 Vector3 cameraPosition =
new Vector3(cameraMatrix.m03, cameraMatrix.m13, cameraMatrix.m23);
279 float calcDistance = Vector3.Distance(tPosition, cameraPosition);
280 float calcAngle = Quaternion.Angle(tQuaternion, cameraRotations[i]);
281 float calcWeightSum = distanceWeight * calcDistance + angleWeight * (Mathf.Abs(calcAngle));
283 if (minWeightSum > calcWeightSum)
285 minWeightSum = calcWeightSum;
bool AutoCamera
Change the view point of the scene view. scene viewpoint in editor. Select the keyframe closest to th...
int KeyframeIndex
Change the keyframe number of imported 3D map.
bool Load(string fileName)
Read map file and create keyframe and mappoint as Unity3d object.
Handles 3D map file for authoring. Map controller includes mappoint controller and keyframe controlle...
int MaxKeyframeCount
Get the number of keyframes for the imported map.
void ApplyViewCamera(Vector3 position, Quaternion quaternion)
Select the keyframe closest to the scene view.
bool Transparent
Changes the loaded map to translucent state.
Map created by Visual SLAM renderer
void UpdateMapViewer()
Updated the control changes of the authoring tool in the scene view.
bool ShowMesh
Reconstruct map point cloud to 3D for authoring.