10
0

FadeInOut.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //***********************************************************
  2. // Filename: FadeInOut.cs
  3. // Author: Marco Fendrich, Moritz Kolvnebach
  4. // Last changes: Thursday, 9th of August 2018
  5. // Content: Controls the black fadeIn and fadeOut between teleports
  6. //***********************************************************
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. /// <summary>
  11. /// Controls the black fadeIn and fadeOut between teleports
  12. /// </summary>
  13. public class FadeInOut : MonoBehaviour {
  14. /// Material used to render the fade in/fade out quad
  15. [Tooltip("Material used to render the fade in/fade out quad.")]
  16. [SerializeField]
  17. private Material FadeMaterial;
  18. private Material FadeMaterialInstance;
  19. private int MaterialFadeID;
  20. // Mesh used for the fadeOut graphic
  21. private Mesh PlaneMesh;
  22. // The transparency factor of the fade graphic
  23. private float alpha =.0F;
  24. void Start ()
  25. {
  26. // Calculate the plane mesh used for "fade out" graphic on teleport
  27. PlaneMesh = new Mesh();
  28. Vector3[] verts = new Vector3[]
  29. {
  30. new Vector3(-1, -1, 0),
  31. new Vector3(-1, 1, 0),
  32. new Vector3(1, 1, 0),
  33. new Vector3(1, -1, 0)
  34. };
  35. int[] triangles = new int[] { 0, 1, 2, 0, 2, 3 };
  36. PlaneMesh.vertices = verts;
  37. PlaneMesh.triangles = triangles;
  38. PlaneMesh.RecalculateBounds();
  39. if (FadeMaterial != null)
  40. FadeMaterialInstance = new Material(FadeMaterial);
  41. // Set some standard variables
  42. MaterialFadeID = Shader.PropertyToID("_Fade");
  43. }
  44. private void OnPostRender()
  45. {
  46. // Position the fadeGraphic in front of the players eyes
  47. Matrix4x4 local = Matrix4x4.TRS(Vector3.forward * 0.3f, Quaternion.identity, Vector3.one);
  48. FadeMaterialInstance.SetPass(0);
  49. FadeMaterialInstance.SetFloat(MaterialFadeID, alpha);
  50. Graphics.DrawMeshNow(PlaneMesh, transform.localToWorldMatrix * local);
  51. }
  52. public void setAlphaValue(float sentAlpha) => alpha = sentAlpha;
  53. }