Advertisement
Guest User

Untitled

a guest
Mar 21st, 2022
574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Rendering;
  3. using UnityEngine.Rendering.Universal;
  4.  
  5.  
  6. public class CausticsPass : ScriptableRenderPass
  7. {
  8. private const string profilerTag = "Caustics Pass";
  9.  
  10. public Material causticsMaterial;
  11. public Material causticsMaterialVolume;
  12. private static Mesh mesh;
  13.  
  14. private const float BIAS = 0.1f;
  15.  
  16. CausticsFeature.CausticsSettings settings;
  17.  
  18. public CausticsPass(CausticsFeature.CausticsSettings settings)
  19. {
  20. this.settings = settings;
  21. }
  22.  
  23. public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
  24. {
  25. var cam = renderingData.cameraData.camera;
  26.  
  27. if (cam.cameraType == CameraType.Preview || !causticsMaterial) return;
  28.  
  29. var sunMatrix = Matrix4x4.TRS(Vector3.zero,
  30. Quaternion.Euler(settings.fixedDirection.x, settings.fixedDirection.y, settings.fixedDirection.z),
  31. Vector3.one);
  32. if (settings.direction == CausticsDirection.DirectionalLight)
  33. {
  34. sunMatrix = RenderSettings.sun != null
  35. ? RenderSettings.sun.transform.localToWorldMatrix
  36. : Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-45f, 45f, 0f), Vector3.one);
  37. }
  38.  
  39.  
  40. causticsMaterial.SetMatrix("_MainLightDirection", sunMatrix);
  41. causticsMaterialVolume.SetMatrix("_MainLightDirection", sunMatrix);
  42.  
  43. CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
  44.  
  45. if (!mesh) mesh = GenerateQuad(1000f);
  46. var position = cam.transform.position;
  47. position.y = cam.transform.position.y > settings.waterLevel
  48. ? settings.waterLevel
  49. : cam.transform.position.y - BIAS;
  50. var matrix = Matrix4x4.TRS(position, Quaternion.identity, Vector3.one);
  51. cmd.DrawMesh(mesh, matrix, causticsMaterial, 0, 0);
  52.  
  53. context.ExecuteCommandBuffer(cmd);
  54. CommandBufferPool.Release(cmd);
  55. }
  56.  
  57. private static Mesh GenerateQuad(float size)
  58. {
  59. var m = new Mesh();
  60.  
  61. size *= 0.5f;
  62.  
  63. var verts = new[]
  64. {
  65. new Vector3(-size, 0f, -size),
  66. new Vector3(size, 0f, -size),
  67. new Vector3(-size, 0f, size),
  68. new Vector3(size, 0f, size)
  69. };
  70.  
  71. var tris = new[]
  72. {
  73. 0, 2, 1,
  74. 2, 3, 1
  75. };
  76.  
  77. m.vertices = verts;
  78. m.triangles = tris;
  79.  
  80. return m;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement