WorldTracker_3DOF.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using System.Runtime.InteropServices;
  6. using System.Globalization;
  7. namespace Imagine.WebAR
  8. {
  9. [System.Serializable]
  10. public class Settings3DOF
  11. {
  12. public float armLength = 0.4f;
  13. public bool useExtraSmoothing = false;
  14. [Range(1,50)] public float smoothenFactor = 10;
  15. [Space][Range(.0001f, 0.1f)] public float angleSmoothFactor = 0.001f;
  16. [Range(0f, 0.05f)] public float angleDriftThreshold = 0.025f;
  17. }
  18. public partial class WorldTracker
  19. {
  20. private Vector3 targetPos3DOF;
  21. private Quaternion targetRot3DOF;
  22. void Awake_3DOF()
  23. {
  24. }
  25. void Start_3DOF()
  26. {
  27. // Input.multiTouchEnabled = true;
  28. var json = "{";
  29. json += "\"MODE\":\"3DOF\",";
  30. json += "\"CAM_START_HEIGHT\":" + cameraStartHeight.ToStringInvariantCulture() + ",";
  31. json += "\"ARM_LENGTH\":" + s3dof.armLength.ToStringInvariantCulture() + ",";
  32. json += "\"ANGLE_SMOOTH_FACTOR\":" + s3dof.angleSmoothFactor.ToStringInvariantCulture() + ",";
  33. json += "\"ANGLE_DRIFT_THRESHOLD\":" + s3dof.angleDriftThreshold.ToStringInvariantCulture() + ",";
  34. json += "\"USE_COMPASS\":" + (useCompass?"true":"false");
  35. json += "}";
  36. #if !UNITY_EDITOR && UNITY_WEBGL
  37. SetWebGLwTrackerSettings(json);
  38. #endif
  39. // if(!usePlacementIndicator){
  40. // //we need to correct rotation when object is autoplaced
  41. // //otherwise, object can end up behind the user
  42. // StartCoroutine(WaitAndInvoke(0.25f,()=>{
  43. // Debug.Log("Correcting rotation...");
  44. // var camForward = trackerCamera.transform.forward;
  45. // camForward.y = 0;
  46. // var lookRotation = Quaternion.LookRotation(camForward);
  47. // mainObject.transform.rotation = lookRotation;
  48. // }));
  49. // }
  50. }
  51. public void Update_3DOF()
  52. {
  53. if(s3dof.useExtraSmoothing){
  54. trackerCamera.transform.position = Vector3.Lerp(trackerCamera.transform.position, targetPos3DOF, Time.deltaTime * s3dof.smoothenFactor);
  55. trackerCamera.transform.rotation = Quaternion.Slerp(trackerCamera.transform.rotation, targetRot3DOF, Time.deltaTime * s3dof.smoothenFactor);
  56. //Debug.Log("Update Orbit Mode: " + targetPos3DOF + " " + targetRot3DOF);
  57. }
  58. }
  59. void UpdateCameraTransform_3DOF(string data)
  60. {
  61. var vals = data.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries);
  62. trackerCamRot.w = float.Parse(vals[0], CultureInfo.InvariantCulture);
  63. trackerCamRot.x = float.Parse(vals[1], CultureInfo.InvariantCulture);
  64. trackerCamRot.y = float.Parse(vals[2], CultureInfo.InvariantCulture);
  65. trackerCamRot.z = float.Parse(vals[3], CultureInfo.InvariantCulture);
  66. trackerCamPos.x = float.Parse(vals[4], CultureInfo.InvariantCulture);
  67. trackerCamPos.y = float.Parse(vals[5], CultureInfo.InvariantCulture);
  68. trackerCamPos.z = float.Parse(vals[6], CultureInfo.InvariantCulture);
  69. if(!s3dof.useExtraSmoothing){
  70. trackerCamera.transform.position = trackerCamPos;
  71. trackerCamera.transform.rotation = trackerCamRot;
  72. }
  73. else{
  74. targetPos3DOF = trackerCamPos;
  75. targetRot3DOF = trackerCamRot;
  76. }
  77. }
  78. void Place_3DOF(Vector3 pos){
  79. mainObject.transform.position = pos;
  80. }
  81. void Reset_3DOF()
  82. {
  83. #if !UNITY_EDITOR && UNITY_WEBGL
  84. WebGLResetOrigin();
  85. #endif
  86. }
  87. }
  88. }