Hi guys can someone tell me why does my code not output anything or give any tips at all? (The materials work, something in this code is up as i have used cmd.blit before and it was okay) There’s almost no learning articles about the modern urp render passes, unity editor warned me to use blitter but after i switch everything is just a black screen and i cant get it to work
using UnityEngine.Rendering.Universal;
using UnityEngine.Rendering;
using UnityEngine;
using System;
public class CustomRenderPassFeature : ScriptableRendererFeature
{
class CustomRenderPass : ScriptableRenderPass
{
private Material raytraceMaterial;
private Material accumulateMaterial;
private RTHandle currRender;
private RTHandle prevRender;
private RTHandle resultTexture;
private bool First = true;
public bool Accumulate = true;
public int numAccumulatedFrames = 0;
private bool isSceneCam = false; // To track if we're in Scene Camera
public CustomRenderPass(Material raytraceMat, Material accumulateMat)
{
this.raytraceMaterial = raytraceMat;
this.accumulateMaterial = accumulateMat;
}
public override void OnCameraSetup(CommandBuffer cmd, ref RenderingData renderingData)
{
RenderTextureDescriptor camDescriptor = renderingData.cameraData.cameraTargetDescriptor;
// Ensure RTHandles are correctly allocated
if (currRender != null) currRender.Release();
currRender = RTHandles.Alloc(camDescriptor.width, camDescriptor.height, colorFormat: camDescriptor.graphicsFormat);
if (prevRender == null)
prevRender = RTHandles.Alloc(camDescriptor.width, camDescriptor.height, colorFormat: camDescriptor.graphicsFormat);
if (resultTexture == null)
resultTexture = RTHandles.Alloc(camDescriptor.width, camDescriptor.height, colorFormat: camDescriptor.graphicsFormat);
// Check if the camera is Scene View
isSceneCam = renderingData.cameraData.camera.cameraType == CameraType.SceneView;
// Debug: Check if any texture allocation failed
if (currRender == null || prevRender == null || resultTexture == null)
{
Debug.LogError("Render textures are not correctly allocated!");
}
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get("Ray Tracing Pass");
cmd.ClearRenderTarget(false, true, Color.black);
RTHandle source = renderingData.cameraData.renderer.cameraColorTargetHandle;
InitFrame(cmd);
if (Accumulate)
{
// Allocate a temporary texture for the previous frame copy
RTHandle prevFrameCopy = RTHandles.Alloc(renderingData.cameraData.cameraTargetDescriptor);
// Set the frame count in the raytracing material
raytraceMaterial.SetInt("NumRenderedFrames", numAccumulatedFrames);
// If it's the first frame, directly blit the source to the result texture
if (First)
{
cmd.SetRenderTarget(resultTexture.nameID);
Blitter.BlitCameraTexture(cmd, source, resultTexture, raytraceMaterial, 0);
First = false;
}
else
{
// Blit the result texture to prevFrameCopy (store the previous frame)
cmd.SetRenderTarget(prevFrameCopy.nameID);
Blitter.BlitCameraTexture(cmd, resultTexture, prevFrameCopy);
}
// Blit the current source to currRender using the raytrace material
cmd.SetRenderTarget(currRender.nameID);
Blitter.BlitCameraTexture(cmd, source, currRender, raytraceMaterial, 0);
// Set the textures for the accumulation shader
cmd.SetGlobalTexture("currRender", currRender.rt);
cmd.SetGlobalTexture("prevRender", prevFrameCopy.rt);
// Blit the accumulated result into the resultTexture
cmd.SetRenderTarget(resultTexture.nameID);
Blitter.BlitCameraTexture(cmd, currRender, resultTexture, accumulateMaterial, 0);
// Finally, blit the result back to the camera target
cmd.SetRenderTarget(source.nameID);
Blitter.BlitCameraTexture(cmd, resultTexture, source);
// Release the temporary frame copy texture
RTHandles.Release(prevFrameCopy);
numAccumulatedFrames++;
}
else
{
numAccumulatedFrames = 0;
cmd.SetRenderTarget(source.nameID);
Blitter.BlitCameraTexture(cmd, source, source, raytraceMaterial, 0);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public override void OnCameraCleanup(CommandBuffer cmd)
{
if (currRender != null) RTHandles.Release(currRender);
}
private void InitFrame(CommandBuffer cmd)
{
// Initialize frame-related resources here (if necessary)
}
}
CustomRenderPass m_ScriptablePass;
public Material raytraceMaterial;
public Material accumulateMaterial;
public bool rayTracingEnabled = true;
public bool useSceneView = false; // Toggle for scene view
public bool Accumulate = true;
public VisMode visMode = VisMode.Default;
public override void Create()
{
m_ScriptablePass = new CustomRenderPass(raytraceMaterial, accumulateMaterial)
{
Accumulate = Accumulate,
renderPassEvent = RenderPassEvent.AfterRenderingPostProcessing
};
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
// Enqueue the custom render pass
renderer.EnqueuePass(m_ScriptablePass);
}
public enum VisMode
{
Default,
// Additional modes can be added if needed
}
}
Im trying to make a raytracer from sebastian lague’s video but for some reason the blitting blurs/ruins or doesnt even display my texture at all, if anyone has a URP implementation of his raytracer i would be very thankful if i could learn from it, because im stuck on those render passes for eternity NOTHING WORKS