ScreenOverlay.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using UnityEngine;
  3. namespace UnityStandardAssets.ImageEffects
  4. {
  5. [ExecuteInEditMode]
  6. [RequireComponent (typeof(Camera))]
  7. [AddComponentMenu ("Image Effects/Other/Screen Overlay")]
  8. public class ScreenOverlay : PostEffectsBase
  9. {
  10. public enum OverlayBlendMode
  11. {
  12. Additive = 0,
  13. ScreenBlend = 1,
  14. Multiply = 2,
  15. Overlay = 3,
  16. AlphaBlend = 4,
  17. }
  18. public OverlayBlendMode blendMode = OverlayBlendMode.Overlay;
  19. public float intensity = 1.0f;
  20. public Texture2D texture = null;
  21. public Shader overlayShader = null;
  22. private Material overlayMaterial = null;
  23. public override bool CheckResources ()
  24. {
  25. CheckSupport (false);
  26. overlayMaterial = CheckShaderAndCreateMaterial (overlayShader, overlayMaterial);
  27. if (!isSupported)
  28. ReportAutoDisable ();
  29. return isSupported;
  30. }
  31. void OnRenderImage (RenderTexture source, RenderTexture destination)
  32. {
  33. if (CheckResources() == false)
  34. {
  35. Graphics.Blit (source, destination);
  36. return;
  37. }
  38. Vector4 UV_Transform = new Vector4(1, 0, 0, 1);
  39. #if UNITY_WP8
  40. // WP8 has no OS support for rotating screen with device orientation,
  41. // so we do those transformations ourselves.
  42. if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
  43. UV_Transform = new Vector4(0, -1, 1, 0);
  44. }
  45. if (Screen.orientation == ScreenOrientation.LandscapeRight) {
  46. UV_Transform = new Vector4(0, 1, -1, 0);
  47. }
  48. if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
  49. UV_Transform = new Vector4(-1, 0, 0, -1);
  50. }
  51. #endif
  52. overlayMaterial.SetVector("_UV_Transform", UV_Transform);
  53. overlayMaterial.SetFloat ("_Intensity", intensity);
  54. overlayMaterial.SetTexture ("_Overlay", texture);
  55. Graphics.Blit (source, destination, overlayMaterial, (int) blendMode);
  56. }
  57. }
  58. }