Ripple.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Ripple : MonoBehaviour
  5. {
  6. public Camera mainCamera;
  7. public RenderTexture InteractiveRT;//交互RT
  8. public RenderTexture PrevRT; //上一幀
  9. public RenderTexture CurrentRT;//當前幀
  10. public RenderTexture TempRT; //臨時RT
  11. public Shader DrawShader; //繪製Shader
  12. public Shader RippleShader;//漣漪計算Shader
  13. public Shader AddShader;
  14. private Material RippleMat;
  15. private Material DrawMat;
  16. private Material AddMat;
  17. [Range(0, 1.0f)]
  18. public float DrawRadius = 0.2f;
  19. public int TextureSize = 512;
  20. // Start is called before the first frame update
  21. void Start()
  22. {
  23. //TODO獲取相機
  24. mainCamera = Camera.main.GetComponent<Camera>();
  25. CurrentRT = CreateRT();
  26. PrevRT = CreateRT();
  27. TempRT = CreateRT();
  28. DrawMat = new Material(DrawShader);
  29. RippleMat = new Material(RippleShader);
  30. //AddMat = new Material(AddShader);
  31. GetComponent<Renderer>().material.mainTexture = CurrentRT;
  32. Application.targetFrameRate = 120;
  33. }
  34. public RenderTexture CreateRT()
  35. {
  36. RenderTexture rt = new RenderTexture(TextureSize, TextureSize, 0, RenderTextureFormat.RFloat);
  37. //rt.Create();
  38. return rt;
  39. }
  40. /// <summary>
  41. /// 繪製
  42. /// </summary>
  43. /// <param name="x"></param>
  44. /// <param name="y"></param>
  45. /// <param name="radius">半徑</param>
  46. private void DrawAt(float x, float y, float radius)
  47. {
  48. //原來的貼圖
  49. DrawMat.SetTexture("_SourceTex", CurrentRT);
  50. //繪製的位置和大小
  51. DrawMat.SetVector("_Pos", new Vector4(x, y, radius));
  52. //提交
  53. Graphics.Blit(null, TempRT, DrawMat);
  54. //進行交換
  55. RenderTexture rt = TempRT;
  56. TempRT = CurrentRT;
  57. CurrentRT = rt;
  58. }
  59. private Vector3 lastPos;
  60. // Update is called once per frame
  61. void Update()
  62. {
  63. //TODO鼠標左鍵按下時射綫檢測
  64. if (Input.GetMouseButton(0))
  65. {
  66. Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
  67. RaycastHit hit;//射綫碰撞信息
  68. if (Physics.Raycast(ray, out hit))
  69. {
  70. if ((hit.point - lastPos).sqrMagnitude > 0.1f)
  71. {
  72. lastPos = hit.point;
  73. //TODO:繪製圖案
  74. DrawAt(hit.textureCoord.x, hit.textureCoord.y, DrawRadius);
  75. }
  76. }
  77. }
  78. ////TODO:添加物体交互
  79. //AddMat.SetTexture("_Tex1", InteractiveRT);
  80. //AddMat.SetTexture("_Tex2", CurrentRT);
  81. //Graphics.Blit(null, TempRT, AddMat);
  82. ////把结果交换到CurrentRT
  83. //RenderTexture rt0 = TempRT;
  84. //TempRT = CurrentRT;
  85. //CurrentRT = rt0;
  86. //TODO:計算漣漪
  87. RippleMat.SetTexture("_PrevRT", PrevRT);
  88. RippleMat.SetTexture("_CurrentRT", CurrentRT);
  89. Graphics.Blit(null, TempRT, RippleMat);
  90. //TODO 交換到 CurrentRT上
  91. Graphics.Blit(TempRT, PrevRT);
  92. //交換
  93. RenderTexture rt = PrevRT;
  94. PrevRT = CurrentRT;
  95. CurrentRT = rt;
  96. }
  97. }