123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class VisionSnapper : MonoBehaviour
- {
- public Material visionSnapperMaterial;
- public GameObject yRotator;
- public GameObject cameraObject;
- public float transitionTime = 1f;
- public float fadeTime = .1f;
- public float snappingAngle = 22.5f;
- public float speedThreshold = 50f;
-
- Vector3 savedRotation;
- Vector3 angularVelocity;
- bool inSnapRoutine = false;
- public bool yLocked = false;
- bool needToSaveRotation = true;
- // Start is called before the first frame update
- void Start()
- {
- if(cameraObject == null){
- cameraObject = Camera.main.gameObject;
- }
- if(visionSnapperMaterial == null){
- visionSnapperMaterial = Resources.Load("GingerVR-master/SicknessReductionTechniques/VisionSnapper/VisionSnapperMat") as Material;
- }
-
- yRotator.transform.position = cameraObject.transform.position;
- yRotator.transform.parent = cameraObject.transform.parent;
- cameraObject.transform.parent = yRotator.transform;
- savedRotation = cameraObject.transform.eulerAngles;
- StartCoroutine(DelayActivity());
- StartCoroutine(TrackAngularVelocity());
- }
- //if speed above threshold
- //begin fade out
- //lock cameraObject for however long
- IEnumerator DelayActivity(){
- inSnapRoutine = true;
- yield return new WaitForSeconds(1f);
- inSnapRoutine = false;
- }
-
- IEnumerator SnapRoutine(){
- yield return null;
- float direction;
- if(angularVelocity.y == 0){
- direction = 0;
- }else{
- direction = Mathf.Abs(angularVelocity.y) / angularVelocity.y;
- }
- int snaps = 0;
-
- float fadeOutProgress = 0f;
- //fade out image;
- while(fadeOutProgress < fadeTime){
- fadeOutProgress += Time.deltaTime;
-
- //fadeImage.color = new Color(0,0,0,fadeOutProgress/fadeTime);
- visionSnapperMaterial.SetFloat("_darkness", fadeOutProgress/fadeTime);
- yield return null;
- }
- visionSnapperMaterial.SetFloat("_darkness",1);
- yLocked = true;
- float waitProgress = 0f;
- //yLocked
-
- while(waitProgress < transitionTime){
- waitProgress += Time.deltaTime;
-
- if(waitProgress >= transitionTime && Mathf.Abs(angularVelocity.y) >= speedThreshold){
- waitProgress = 0f;
- snaps += 1;
- }
- yield return null;
- }
- //unyLocked
- yLocked = false;
- //rotate Y to match 22.5
-
- yRotator.transform.RotateAround(cameraObject.transform.position, new Vector3(0,1,0), snappingAngle*direction);
- //cameraObject.transform.Rotate(0,snappingAngle*direction,0);
- float fadeInProgress = 0f;
- while(fadeInProgress < fadeTime){
- fadeInProgress += Time.deltaTime;
- visionSnapperMaterial.SetFloat("_darkness",1 - (fadeOutProgress/fadeTime));
- yield return null;
- }
- visionSnapperMaterial.SetFloat("_darkness",0);
-
-
- inSnapRoutine = false;
- }
- // Update is called once per frame
- float checkTime = 0.1f;
- IEnumerator TrackAngularVelocity(){
- yield return null;
- Vector3 lastRotation = new Vector3(0,0,0);
- while(true){
- yield return new WaitForSeconds(checkTime);
- Vector3 delta = cameraObject.transform.localEulerAngles - lastRotation;
- lastRotation = cameraObject.transform.localEulerAngles;
- angularVelocity = delta / checkTime;
- }
- }
- void OnRenderImage(RenderTexture src, RenderTexture dst){
- RenderTexture renderTexture = RenderTexture.GetTemporary(src.width, src.height);
- Graphics.Blit(src, renderTexture); //copies source texture to destination texture
- RenderTexture tempTexture = RenderTexture.GetTemporary(src.width, src.height); //creates a quick temporary texture for calculations
- Graphics.Blit(renderTexture, tempTexture, visionSnapperMaterial);
- RenderTexture.ReleaseTemporary(renderTexture); //releases the temporary texture we got from GetTemporary
- renderTexture = tempTexture;
- Graphics.Blit(renderTexture, dst);
- RenderTexture.ReleaseTemporary(renderTexture);
- }
- void Update()
- {
- //angularVelocity = (cameraObject.transform.eulerAngles - savedRotation) / Time.deltaTime;
- //
-
- if(Mathf.Abs(angularVelocity.y) >= speedThreshold && !inSnapRoutine){
- inSnapRoutine = true;
- savedRotation = cameraObject.transform.eulerAngles;
- StartCoroutine(SnapRoutine());
- }
- //keep camera locked
- if(yLocked){
- //rotate around the inverse of current rotation
- yRotator.transform.RotateAround(cameraObject.transform.position, new Vector3(0,1,0),-cameraObject.transform.eulerAngles.y);
-
- //rotate around by savedRotation
- yRotator.transform.RotateAround(cameraObject.transform.position, new Vector3(0,1,0),savedRotation.y);
-
- }else{
- needToSaveRotation = true;
- }
- }
- void RotateCamera(float degrees){
- }
- }
|