Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Rendering;
- using UnityEngine.Rendering.Universal;
- public class CausticsPass : ScriptableRenderPass
- {
- private const string profilerTag = "Caustics Pass";
- public Material causticsMaterial;
- public Material causticsMaterialVolume;
- private static Mesh mesh;
- private const float BIAS = 0.1f;
- CausticsFeature.CausticsSettings settings;
- public CausticsPass(CausticsFeature.CausticsSettings settings)
- {
- this.settings = settings;
- }
- public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
- {
- var cam = renderingData.cameraData.camera;
- if (cam.cameraType == CameraType.Preview || !causticsMaterial) return;
- var sunMatrix = Matrix4x4.TRS(Vector3.zero,
- Quaternion.Euler(settings.fixedDirection.x, settings.fixedDirection.y, settings.fixedDirection.z),
- Vector3.one);
- if (settings.direction == CausticsDirection.DirectionalLight)
- {
- sunMatrix = RenderSettings.sun != null
- ? RenderSettings.sun.transform.localToWorldMatrix
- : Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-45f, 45f, 0f), Vector3.one);
- }
- causticsMaterial.SetMatrix("_MainLightDirection", sunMatrix);
- causticsMaterialVolume.SetMatrix("_MainLightDirection", sunMatrix);
- CommandBuffer cmd = CommandBufferPool.Get(profilerTag);
- if (!mesh) mesh = GenerateQuad(1000f);
- var position = cam.transform.position;
- position.y = cam.transform.position.y > settings.waterLevel
- ? settings.waterLevel
- : cam.transform.position.y - BIAS;
- var matrix = Matrix4x4.TRS(position, Quaternion.identity, Vector3.one);
- cmd.DrawMesh(mesh, matrix, causticsMaterial, 0, 0);
- context.ExecuteCommandBuffer(cmd);
- CommandBufferPool.Release(cmd);
- }
- private static Mesh GenerateQuad(float size)
- {
- var m = new Mesh();
- size *= 0.5f;
- var verts = new[]
- {
- new Vector3(-size, 0f, -size),
- new Vector3(size, 0f, -size),
- new Vector3(-size, 0f, size),
- new Vector3(size, 0f, size)
- };
- var tris = new[]
- {
- 0, 2, 1,
- 2, 3, 1
- };
- m.vertices = verts;
- m.triangles = tris;
- return m;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement