Last active
August 21, 2024 02:02
-
-
Save phi-lira/577bd1f7a0d65d1c7fb0e62bbbe37b38 to your computer and use it in GitHub Desktop.
Custom Pass injected by script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Rendering; | |
using UnityEngine.Rendering.Universal; | |
[ExecuteInEditMode] | |
public class CustomPass : MonoBehaviour | |
{ | |
public Mesh mesh; | |
public Material material; | |
DrawMeshPass m_DrawMeshPass; | |
void OnEnable() | |
{ | |
m_DrawMeshPass = new DrawMeshPass(); | |
m_DrawMeshPass.renderPassEvent = RenderPassEvent.AfterRendering; | |
RenderPipelineManager.beginCameraRendering += DrawMesh; | |
} | |
void OnDisable() | |
{ | |
RenderPipelineManager.beginCameraRendering -= DrawMesh; | |
} | |
void DrawMesh(ScriptableRenderContext context, Camera cam) | |
{ | |
if (mesh == null || material == null) return; | |
var renderer = cam.GetRenderer(); | |
m_DrawMeshPass.mesh = mesh; | |
m_DrawMeshPass.material = material; | |
m_DrawMeshPass.matrix = Matrix4x4.TRS(transform.position, transform.rotation, transform.lossyScale); | |
renderer.EnqueuePass(m_DrawMeshPass); | |
} | |
} | |
class DrawMeshPass : ScriptableRenderPass | |
{ | |
public Matrix4x4 matrix { get; set; } | |
public Mesh mesh { get; set; } | |
public Material material { get; set; } | |
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) | |
{ | |
CommandBuffer cb = CommandBufferPool.Get("DrawMesh"); | |
cb.DrawMesh(mesh, matrix, material); | |
context.ExecuteCommandBuffer(cb); | |
context.Submit(); | |
CommandBufferPool.Release(cb); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment