WorldTracker_6DOF.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Globalization;
  5. using UnityEngine;
  6. using System.Runtime.InteropServices;
  7. using UnityEngine.UI;
  8. using UnityEngine.Events;
  9. namespace Imagine.WebAR
  10. {
  11. [System.Serializable]
  12. public class Settings6DOF
  13. {
  14. //public bool repositionOnTouch = false;
  15. public enum DepthMode {SCALE_AS_DEPTH, Z_AS_DEPTH_EXPERIMENTAL};
  16. public DepthMode depthMode = DepthMode.SCALE_AS_DEPTH;
  17. // public enum AngleMode {PRECISE_BUT_SKEWED, ACCURATE_BUT_LERPED}
  18. //public Vector3 camOffset;
  19. [Range(300, 600)] public int maxPixels = 450;
  20. [Range(10, 200)] public int maxPoints = 120;
  21. [Range(0,1)] public float targetConfidence = 0.1f;
  22. [Space][Range(50, 250)] public int clampPixelDrift = 50;
  23. [Range(0.05f,0.25f)] public float clampScaleDrift = 0.1f;
  24. [Space][Range(0.00001f, 0.001f)] public float minPointQuality = 0.0001f;
  25. [Range(0.001f, 0.1f)] public float minGoodPointQuality = 0.002f;
  26. [Range(1,20)] public float minCoplanarFactor = 3;
  27. [Space][Range(.0001f, 0.01f)] public float angleSmoothFactor = 0.001f;
  28. // public AngleMode angleMode = AngleMode.PRECISE_BUT_SKEWED;
  29. [Range(0f, 0.05f)] public float angleDriftThreshold = 0.025f;
  30. [Space] public bool poseCorrectionEnabled = true;
  31. [Range(250, 1000)] public int poseCorrectionInterval = 500;
  32. public UnityEvent<float> OnConfidence;
  33. }
  34. public partial class WorldTracker
  35. {
  36. [DllImport("__Internal")] private static extern void WebGLPlaceOrigin(string camPosStr);
  37. private float startZ;
  38. void Awake_6DOF()
  39. {
  40. }
  41. void Start_6DOF()
  42. {
  43. var camPos = new Vector3(origPos.x, 0, origPos.z);
  44. startZ = (mainObject.transform.position - camPos).magnitude;
  45. var dmode = "";
  46. if(s6dof.depthMode == Settings6DOF.DepthMode.SCALE_AS_DEPTH)
  47. dmode = "SCALE";
  48. else if(s6dof.depthMode == Settings6DOF.DepthMode.Z_AS_DEPTH_EXPERIMENTAL)
  49. dmode = "Z";
  50. var json = "{";
  51. json += "\"MODE\":\"3DOF\","; //<--3DOF for placement mode only
  52. json += "\"DEPTHMODE\":\"" + dmode + "\",";
  53. json += "\"START_Z\":" + startZ.ToStringInvariantCulture() + ",";
  54. json += "\"CAM_START_HEIGHT\":" + cameraStartHeight.ToStringInvariantCulture() + ",";
  55. json += "\"ARM_LENGTH\":" + s3dof.armLength.ToStringInvariantCulture() + ",";
  56. json += "\"MAX_PIXELS\":" + s6dof.maxPixels + ",";
  57. json += "\"MAX_POINTS\":" + s6dof.maxPoints + ",";
  58. json += "\"TARGET_CONFIDENCE\":" + s6dof.targetConfidence.ToStringInvariantCulture() + ",";
  59. json += "\"CLAMP_PIXEL_DRIFT\":" + s6dof.clampPixelDrift + ",";
  60. json += "\"CLAMP_SCALE_DRIFT\":" + s6dof.clampScaleDrift.ToStringInvariantCulture() + ",";
  61. json += "\"ANGLE_SMOOTH_FACTOR\":" + s6dof.angleSmoothFactor.ToStringInvariantCulture() + ",";
  62. // json += "\"ANGLE_MODE\":\"" + s6dof.angleMode.ToString() + "\",";
  63. json += "\"ANGLE_DRIFT_THRESHOLD\":" + s6dof.angleDriftThreshold.ToStringInvariantCulture() + ",";
  64. json += "\"POSE_CORRECTION_ENABLED\":" + (s6dof.poseCorrectionEnabled ? "true" : "false") + ",";
  65. json += "\"POSE_CORRECTION_INTERVAL\":" + s6dof.poseCorrectionInterval + ",";
  66. json += "\"MIN_POINT_QUALITY\":" + s6dof.minPointQuality.ToStringInvariantCulture() + ",";
  67. json += "\"MIN_GOOD_POINT_QUALITY\":" + s6dof.minGoodPointQuality.ToStringInvariantCulture() + ",";
  68. json += "\"MIN_COPLANAR_FACTOR\":" + s6dof.minCoplanarFactor.ToStringInvariantCulture() + ",";
  69. json += "\"USE_COMPASS\":" + (useCompass?"true":"false");
  70. json += "}";
  71. #if !UNITY_EDITOR && UNITY_WEBGL
  72. SetWebGLwTrackerSettings(json);
  73. #endif
  74. }
  75. public void Update_6DOF()
  76. {
  77. if (usePlacementIndicator && !placementIndicatorSettings.placed)
  78. {
  79. Update_3DOF();
  80. }
  81. }
  82. void UpdateCameraTransform_6DOF(string data)
  83. {
  84. var ar = ((float)Screen.width) / Screen.height;
  85. if (usePlacementIndicator && !placementIndicatorSettings.placed)
  86. {
  87. UpdateCameraTransform_3DOF(data);
  88. }
  89. else
  90. {
  91. var vals = data.Split(new string[]{","}, System.StringSplitOptions.RemoveEmptyEntries);
  92. trackerCamRot.w = float.Parse(vals[0], CultureInfo.InvariantCulture);
  93. trackerCamRot.x = float.Parse(vals[1], CultureInfo.InvariantCulture);
  94. trackerCamRot.y = float.Parse(vals[2], CultureInfo.InvariantCulture);
  95. trackerCamRot.z = float.Parse(vals[3], CultureInfo.InvariantCulture);
  96. trackerCamPos.x = float.Parse(vals[4], CultureInfo.InvariantCulture);
  97. trackerCamPos.y = float.Parse(vals[5], CultureInfo.InvariantCulture);
  98. trackerCamPos.z = float.Parse(vals[6], CultureInfo.InvariantCulture);
  99. trackerCamera.transform.position = trackerCamPos;
  100. trackerCamera.transform.rotation = trackerCamRot;
  101. trackerMainObjectScale = float.Parse(vals[7], CultureInfo.InvariantCulture) * Vector3.one;
  102. mainObject.transform.localScale = trackerMainObjectScale;
  103. s6dof.OnConfidence?.Invoke(float.Parse(vals[8], CultureInfo.InvariantCulture));
  104. }
  105. }
  106. void Place_6DOF(){
  107. mainObject.transform.position = Vector3.zero;
  108. var startCamPos = new Vector3(origPos.x, 0, origPos.z);
  109. trackerCamera.transform.position = startCamPos;
  110. #if UNITY_WEBGL && !UNITY_EDITOR
  111. var json = "{";
  112. json += "\"MODE\":\"6DOF\"" + ",";
  113. json += "\"START_Z\":" + startZ.ToStringInvariantCulture();
  114. json += "}";
  115. SetWebGLwTrackerSettings(json);
  116. var camPosStr = startCamPos.x + "," + startCamPos.y + "," + startCamPos.z;
  117. WebGLPlaceOrigin(camPosStr);
  118. #endif
  119. }
  120. void Reset_6DOF()
  121. {
  122. #if UNITY_WEBGL && !UNITY_EDITOR
  123. SetWebGLwTrackerSettings("{\"MODE\":\"3DOF\"}");
  124. #endif
  125. }
  126. }
  127. }