TextureCaptureDemo.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using UnityEngine;
  2. //-----------------------------------------------------------------------------
  3. // Copyright 2012-2022 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] Shader _shader = null;
  13. [SerializeField] int _textureWidth = 1024;
  14. [SerializeField] int _textureHeight = 1024;
  15. [SerializeField] CaptureFromTexture _movieCapture = null;
  16. // State
  17. private Material _material;
  18. private RenderTexture _texture;
  19. private void Start()
  20. {
  21. _material = new Material(_shader);
  22. RenderTextureReadWrite readWrite = QualitySettings.activeColorSpace == ColorSpace.Gamma ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB;
  23. _texture = new RenderTexture(_textureWidth, _textureHeight, 0, RenderTextureFormat.ARGB32, readWrite);
  24. _texture.filterMode = FilterMode.Bilinear;
  25. _texture.Create();
  26. if (_movieCapture)
  27. {
  28. _movieCapture.SetSourceTexture(_texture);
  29. }
  30. }
  31. private void OnDestroy()
  32. {
  33. if (_material != null)
  34. {
  35. Material.Destroy(_material);
  36. _material = null;
  37. }
  38. if (_texture != null)
  39. {
  40. RenderTexture.Destroy(_texture);
  41. _texture = null;
  42. }
  43. }
  44. private void Update()
  45. {
  46. UpdateTexture();
  47. }
  48. private void UpdateTexture()
  49. {
  50. Graphics.Blit(Texture2D.whiteTexture, _texture, _material);
  51. }
  52. private void OnGUI()
  53. {
  54. if (_texture != null)
  55. {
  56. GUI.depth = 100;
  57. GUI.DrawTexture(new Rect(0f, 0f, Screen.width, Screen.height), _texture, ScaleMode.ScaleToFit, false);
  58. }
  59. }
  60. }
  61. }