You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6 KiB

  1. using UnityEngine;
  2. using System.Collections;
  3. public class BackgroundController : MonoBehaviour {
  4. public float transistionSpeed = 1f;
  5. public float waitTime = 10f;
  6. private Material mat;
  7. private float lastTransition = 0.0f;
  8. private bool inverseDirection = false;
  9. // Use this for initialization
  10. void Start () {
  11. mat = GetComponent<Renderer>().material;
  12. }
  13. // Update is called once per frame
  14. void FixedUpdate () {
  15. if (Time.time - lastTransition >= waitTime) {
  16. lastTransition = Time.time;
  17. StartCoroutine(transition(transistionSpeed));
  18. }
  19. }
  20. private IEnumerator transition(float time) {
  21. float transitionStart = -1;
  22. float transitionEnd = 1;
  23. if (inverseDirection) {
  24. mat.SetFloat("_InvertMap", 1);
  25. }else {
  26. mat.SetFloat("_InvertMap", 0);
  27. }
  28. float elapsedTime = 0;
  29. float transitionFrac = transitionStart;
  30. while ((elapsedTime/ time) <=1) {
  31. transitionFrac = Mathf.Lerp(transitionStart, transitionEnd, (elapsedTime / time));
  32. transitionFrac = (inverseDirection)? transitionFrac : -transitionFrac;
  33. mat.SetFloat("_TimeNode", transitionFrac);
  34. elapsedTime += Time.deltaTime;
  35. yield return new WaitForEndOfFrame();
  36. }
  37. if (inverseDirection) {
  38. mat.SetFloat("_TimeNode", 1);
  39. } else {
  40. mat.SetFloat("_TimeNode", -1);
  41. }
  42. inverseDirection = !inverseDirection;
  43. }
  44. }