VisionSnapper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class VisionSnapper : MonoBehaviour
  6. {
  7. public Material visionSnapperMaterial;
  8. public GameObject yRotator;
  9. public GameObject cameraObject;
  10. public float transitionTime = 1f;
  11. public float fadeTime = .1f;
  12. public float snappingAngle = 22.5f;
  13. public float speedThreshold = 50f;
  14. Vector3 savedRotation;
  15. Vector3 angularVelocity;
  16. bool inSnapRoutine = false;
  17. public bool yLocked = false;
  18. bool needToSaveRotation = true;
  19. // Start is called before the first frame update
  20. void Start()
  21. {
  22. if(cameraObject == null){
  23. cameraObject = Camera.main.gameObject;
  24. }
  25. if(visionSnapperMaterial == null){
  26. visionSnapperMaterial = Resources.Load("GingerVR-master/SicknessReductionTechniques/VisionSnapper/VisionSnapperMat") as Material;
  27. }
  28. yRotator.transform.position = cameraObject.transform.position;
  29. yRotator.transform.parent = cameraObject.transform.parent;
  30. cameraObject.transform.parent = yRotator.transform;
  31. savedRotation = cameraObject.transform.eulerAngles;
  32. StartCoroutine(DelayActivity());
  33. StartCoroutine(TrackAngularVelocity());
  34. }
  35. //if speed above threshold
  36. //begin fade out
  37. //lock cameraObject for however long
  38. IEnumerator DelayActivity(){
  39. inSnapRoutine = true;
  40. yield return new WaitForSeconds(1f);
  41. inSnapRoutine = false;
  42. }
  43. IEnumerator SnapRoutine(){
  44. yield return null;
  45. float direction;
  46. if(angularVelocity.y == 0){
  47. direction = 0;
  48. }else{
  49. direction = Mathf.Abs(angularVelocity.y) / angularVelocity.y;
  50. }
  51. int snaps = 0;
  52. float fadeOutProgress = 0f;
  53. //fade out image;
  54. while(fadeOutProgress < fadeTime){
  55. fadeOutProgress += Time.deltaTime;
  56. //fadeImage.color = new Color(0,0,0,fadeOutProgress/fadeTime);
  57. visionSnapperMaterial.SetFloat("_darkness", fadeOutProgress/fadeTime);
  58. yield return null;
  59. }
  60. visionSnapperMaterial.SetFloat("_darkness",1);
  61. yLocked = true;
  62. float waitProgress = 0f;
  63. //yLocked
  64. while(waitProgress < transitionTime){
  65. waitProgress += Time.deltaTime;
  66. if(waitProgress >= transitionTime && Mathf.Abs(angularVelocity.y) >= speedThreshold){
  67. waitProgress = 0f;
  68. snaps += 1;
  69. }
  70. yield return null;
  71. }
  72. //unyLocked
  73. yLocked = false;
  74. //rotate Y to match 22.5
  75. yRotator.transform.RotateAround(cameraObject.transform.position, new Vector3(0,1,0), snappingAngle*direction);
  76. //cameraObject.transform.Rotate(0,snappingAngle*direction,0);
  77. float fadeInProgress = 0f;
  78. while(fadeInProgress < fadeTime){
  79. fadeInProgress += Time.deltaTime;
  80. visionSnapperMaterial.SetFloat("_darkness",1 - (fadeOutProgress/fadeTime));
  81. yield return null;
  82. }
  83. visionSnapperMaterial.SetFloat("_darkness",0);
  84. inSnapRoutine = false;
  85. }
  86. // Update is called once per frame
  87. float checkTime = 0.1f;
  88. IEnumerator TrackAngularVelocity(){
  89. yield return null;
  90. Vector3 lastRotation = new Vector3(0,0,0);
  91. while(true){
  92. yield return new WaitForSeconds(checkTime);
  93. Vector3 delta = cameraObject.transform.localEulerAngles - lastRotation;
  94. lastRotation = cameraObject.transform.localEulerAngles;
  95. angularVelocity = delta / checkTime;
  96. }
  97. }
  98. void OnRenderImage(RenderTexture src, RenderTexture dst){
  99. RenderTexture renderTexture = RenderTexture.GetTemporary(src.width, src.height);
  100. Graphics.Blit(src, renderTexture); //copies source texture to destination texture
  101. RenderTexture tempTexture = RenderTexture.GetTemporary(src.width, src.height); //creates a quick temporary texture for calculations
  102. Graphics.Blit(renderTexture, tempTexture, visionSnapperMaterial);
  103. RenderTexture.ReleaseTemporary(renderTexture); //releases the temporary texture we got from GetTemporary
  104. renderTexture = tempTexture;
  105. Graphics.Blit(renderTexture, dst);
  106. RenderTexture.ReleaseTemporary(renderTexture);
  107. }
  108. void Update()
  109. {
  110. //angularVelocity = (cameraObject.transform.eulerAngles - savedRotation) / Time.deltaTime;
  111. //
  112. if(Mathf.Abs(angularVelocity.y) >= speedThreshold && !inSnapRoutine){
  113. inSnapRoutine = true;
  114. savedRotation = cameraObject.transform.eulerAngles;
  115. StartCoroutine(SnapRoutine());
  116. }
  117. //keep camera locked
  118. if(yLocked){
  119. //rotate around the inverse of current rotation
  120. yRotator.transform.RotateAround(cameraObject.transform.position, new Vector3(0,1,0),-cameraObject.transform.eulerAngles.y);
  121. //rotate around by savedRotation
  122. yRotator.transform.RotateAround(cameraObject.transform.position, new Vector3(0,1,0),savedRotation.y);
  123. }else{
  124. needToSaveRotation = true;
  125. }
  126. }
  127. void RotateCamera(float degrees){
  128. }
  129. }