12 using System.Collections.Generic;
13 using System.Runtime.InteropServices;
25 private WebCamTexture
w;
32 private float aspect = 1.38188006064344f;
63 WebCamDevice[] devices= WebCamTexture.devices;
64 if (devices.Length > 0)
66 w=
new WebCamTexture(screen_width, screen_height, 15);
70 this._bg_panel=GameObject.Find(
"Plane");
71 this._bg_panel.renderer.material.mainTexture=
w;
76 this._bg_panel_corner_points = GameObject.Find (
"TrackingCornerPoints");
78 cornerPoints =
new Texture2D(screen_width, screen_height);
80 Color c =
new Color(255, 255, 255, 0);
81 for (
int i = 0; i<data.Length; i++)
86 cornerPoints.SetPixels(data);
89 this._bg_panel_corner_points.renderer.material.mainTexture =
cornerPoints;
95 Debug.LogError(
"No Webcam.");
98 #if GYRO || GYRO_WITH_TRANSLATION
100 gyroCam.enabled =
true;
114 StreamReader fileReader = null;
115 string logFile = Application.persistentDataPath +
"/log.txt";
118 if (File.Exists(logFile))
120 fileReader =
new StreamReader(logFile);
122 logFileData = fileReader.ReadToEnd();
147 if (textureOk && !planeRotationFound)
149 this._buf = (Color32[])this.w.GetPixels32 ();
150 m_PixelsHandle = GCHandle.Alloc (this._buf, GCHandleType.Pinned);
152 m_PixelsHandle.Free ();
167 Debug.Log(
"WebTexture not initialized");
187 if(message ==
"Tracker_ResetMM")
192 else if(message.StartsWith(
"Corners"))
194 userMessagesText = message;
198 Debug.Log(
"Message from plugin: " + message);
211 int [] corners =
new int[size];
212 Marshal.Copy(pointer, corners, 0, size);
226 Color c =
new Color(255, 255, 255, 0);
227 for (
int i = 0; i<data.Length; i++)
232 for(
int i = 0; i < corners.Length; i++)
234 data[corners[i]] = Color.red;
237 cornerPoints.SetPixels(data);
238 cornerPoints.Apply();
253 planeRotation.x = r1;
254 planeRotation.y = r2;
255 planeRotation.z = r3;
257 planeTranslation.x = (float)t2;
258 planeTranslation.y = (float)t1;
265 this._ground_plane.SetActive (
true);
266 this._ground_plane.transform.eulerAngles = planeRotation + this._bg_panel.transform.eulerAngles;
270 #if GYRO || GYRO_WITH_TRANSLATION
272 Vector3 planePosition = this._bg_panel.transform.position;
274 if (planePosition.x > 0)
275 planePosition.x -= 1000f;
277 planePosition.x += 1000f;
279 if (planePosition.y > 0)
280 planePosition.y -= 1000f;
282 planePosition.y += 1000f;
284 if (planePosition.z > 0)
285 planePosition.z -= 1000f;
287 planePosition.z += 1000f;
291 Debug.Log (
"Unity_ Plane found");
296 #if GYRO || GYRO_WITH_TRANSLATION
297 this._ground_plane.transform.position = planePosition;
298 orginalPlainePosition = planePosition;
301 orginalPlainePosition = this._ground_plane.transform.position;
307 #if GYRO_WITH_TRANSLATION
308 planeTranslation.x *= 4140f;
309 planeTranslation.y *= -3000f;
310 if(!gyroCam.GyroAngleDetected)
312 this._ground_plane.transform.position = orginalPlainePosition +
planeTranslation;
318 if((
float)t3 < 1.75f)
322 planeTranslation.x *= 4140f * offset;
323 planeTranslation.y *= -3000f * offset;
324 this._ground_plane.transform.position = orginalPlainePosition +
planeTranslation;
329 this._ground_plane.transform.eulerAngles = planeRotation + this._bg_panel.transform.eulerAngles;
346 yield
return new WaitForSeconds(1f);
348 Debug.Log (
"AutoEnd finished");
362 logFileData += message;
374 userMessagesText = message;
383 if( GUI.Button(
new Rect(Screen.width*0.9f, 0, Screen.width*0.1f, Screen.width*0.1f),
"Exit"))
385 Debug.Log(
"Unity_ App exits");
389 if( GUI.Button(
new Rect(Screen.width - (Screen.width*0.3f),(Screen.height-(Screen.width*0.3f)),Screen.width*0.3f,Screen.width*0.3f),
"Start Plane Detection"))
391 Debug.Log(
"Unity_ Tracking started");
392 planeRotationFound =
false;
393 this._ground_plane.SetActive(
false);
394 Camera.main.transform.eulerAngles = Vector3.zero;
399 if( GUI.Button(
new Rect(0,(Screen.height-(Screen.width*0.3f)),Screen.width*0.3f,Screen.width*0.3f),
"Stop Plane Detection"))
401 Debug.Log(
"Unity_ Tracking stoped");
402 planeRotationFound =
false;
404 this._ground_plane.SetActive(
false);
405 Camera.main.transform.eulerAngles = Vector3.zero;
412 text =
"\nMessages: "+userMessagesTextInstr+
"\n"+userMessagesText+
"\n" +
userMessagesText1;
420 GUI.Label (
new Rect (0f, 0f, 200, 200),
"Current tracking state: " +
current_State.ToString());
421 GUI.Label (
new Rect (0, 100, 400, 400), text);
432 string logFile = Application.persistentDataPath +
"/log.txt";
433 var sr = File.CreateText(logFile);
434 sr.WriteLine (logFileData);
445 List<Vector2> ret =
new List<Vector2>();
447 string[] coordinates = array.Split(
new char[]{
';'});
449 foreach(
string s
in coordinates)
451 string[] coor = s.Split(
new char[]{
','});
454 int x = Convert.ToInt32(coor[0]);
455 int y = Convert.ToInt32(coor[1]);
456 if(x<screen_width+1 && x>0 && y<screen_height+1 && y>0)
458 ret.Add(
new Vector2(x, y));
void Logging(string message)
Function for logging data on Application.persistentDataPath + "/log.txt"
Vector3 orginalPlainePosition
SlamFlexWrapper.SendLogDelegate mSendLogDelegate
SlamFlexWrapper.SendArrayPointsDelegate mSendArrayPointsDelegate
List< Vector2 > ParseCoordinates(string array)
delegate void SendLogDelegate(string text)
Definition of delegate used from plugin to send log to Unity3D
delegate void SendPoseDelegate(float r1, float r2, float r3, double t1, double t2, double t3)
Definition of delegate used from plugin to send pose to Unity3D
delegate void SendStringDelegate(string s)
Definition of delegate used from plugin to send string to Unity3D
SlamFlexWrapper.DetectionState current_State
void Awake()
Function for unity3D code initialization
SlamFlexWrapper.SendStringDelegate mSendStringDelegate
IEnumerator AutoEnd()
Function for starting SLAMflex until enough points for generation of map is found ...
SlamFlexWrapper.SendPoseDelegate mSendPoseDelegate
string userMessagesTextInstr
void SetCornerTexturePoints(int[] corners)
Function for drawing corners on separate texture
void UserMessages(string message)
Function for presentation of user messages on GUI
DetectionState
Enum for state of SLAM detection process
void OnGUI()
Function for unity3D GUI
GameObject _bg_panel_corner_points
Gyroscope-controlled camera for iPhone Android revised 2.26.12
int transform(const BasicImage< S > &in, BasicImage< T > &out, const TooN::Matrix< 2 > &M, const TooN::Vector< 2 > &inOrig, const TooN::Vector< 2 > &outOrig, const T defaultValue=T())
IEnumerator CheckTheValidityOfTexture()
Function for yield of unity3D update until webtexture is initialized
void ReciveArrayPointsFromPlugin(IntPtr pointer, int size)
Function for communication between plugin and Unity, plugin sends array of points (corners) to Unity ...
void OnApplicationQuit()
Function for unity3D OnApplicationQuit event
SlamFlexWrapper implements interface to native implementation and public interface for use inside C# ...
static void StartSlam(SendStringDelegate ssd, SendPoseDelegate spd, SendLogDelegate sLogd, SendArrayPointsDelegate arrayPoints)
Starts SLAM detection
delegate void SendArrayPointsDelegate(IntPtr pointer, int size)
Definition of delegate used from plugin to send array of points to Unity3D
void ReciveStringFromPlugin(string message)
Function for communication between plugin and Unity, plugin sends plain string
static DetectionState SetNewFrame(IntPtr pointer, int width, int height)
Send image data to plugin for SLAM detection
static void StopSlam()
Stops SLAM detection
static void StartPlaneDetection()
Initiate SLAM detection
SlamFlexAdapter is example how setup SLAMflex library, setup image data from webtexture and every fra...
void RecivePoseFromPlugin(float r1, float r2, float r3, double t1, double t2, double t3)
Function for reciving pose from plugin, rotation in euler angles and translation
void Start()
Function for unity3D code initialization