TextureCaptureDemo.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2012-2017 RenderHeads Ltd. All rights reserved.
  4. //-----------------------------------------------------------------------------
  5. namespace RenderHeads.Media.AVProMovieCapture.Demos
  6. {
  7. /// <summary>
  8. /// Animates a procedural texture effect driven by a shader
  9. /// </summary>
  10. public class TextureCaptureDemo : MonoBehaviour
  11. {
  12. [SerializeField]
  13. private int _textureWidth = 1024;
  14. [SerializeField]
  15. private int _textureHeight = 1024;
  16. [SerializeField]
  17. private CaptureFromTexture _movieCapture;
  18. // State
  19. private Material _material;
  20. private RenderTexture _texture;
  21. private void Start()
  22. {
  23. Shader plasmaShader = Resources.Load<Shader>("AVProMovieCapture_Plasma");
  24. _material = new Material(plasmaShader);
  25. _texture = new RenderTexture(_textureWidth, _textureHeight, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
  26. _texture.filterMode = FilterMode.Bilinear;
  27. _texture.Create();
  28. if (_movieCapture)
  29. {
  30. _movieCapture.SetSourceTexture(_texture);
  31. }
  32. }
  33. private void OnDestroy()
  34. {
  35. if (_material != null)
  36. {
  37. Material.Destroy(_material);
  38. _material = null;
  39. }
  40. if (_texture != null)
  41. {
  42. RenderTexture.Destroy(_texture);
  43. _texture = null;
  44. }
  45. }
  46. private void Update()
  47. {
  48. UpdateTexture();
  49. }
  50. private void UpdateTexture()
  51. {
  52. Graphics.Blit(Texture2D.whiteTexture, _texture, _material);
  53. }
  54. private void OnGUI()
  55. {
  56. if (_texture != null)
  57. {
  58. GUI.depth = 100;
  59. GUI.DrawTexture(new Rect(Screen.width / 2f - _texture.width / 2f, Screen.height / 2f - _texture.height / 2f, _texture.width, _texture.height), _texture, ScaleMode.ScaleToFit, false);
  60. }
  61. }
  62. }
  63. }