The custom Render Pass I wrote in Unity6 is not working. In Framedebugger, I can see that the Pass has been successfully inserted, the Shader and parameters have been successfully read, but the entire screen is gray. In Framedebugger, it shows that the camera color I read is a 2x2 white texture, and in the Render Graph Viewer, it displays “This is an Unsafe Render Pass. Only Raster Render Passes can be merged”
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.RenderGraphModule;
using UnityEngine.Rendering.RenderGraphModule.Util;
using UnityEngine.Rendering.Universal;
using UnityEngine.Splines;
public class CloudPass : ScriptableRenderPass
{
#region StaticValues
private static readonly int boundsMinId = Shader.PropertyToID("_boundsMin");
private static readonly int boundsMaxId = Shader.PropertyToID("_boundsMax");
private static readonly int stepCountId = Shader.PropertyToID("_stepCount");
private static readonly int BaseNoiseTexId = Shader.PropertyToID("_BaseNoiseTex");
private static readonly int NoiseScaleId = Shader.PropertyToID("_NoiseScale");
private static readonly int NoiseOffsetId = Shader.PropertyToID("_NoiseOffset");
private static readonly int CloudDensityId = Shader.PropertyToID("_CloudDensity");
private static readonly int CloudDensityPowId = Shader.PropertyToID("_CloudDensityPow");
private static readonly int AbsorptionId = Shader.PropertyToID("_Absorption");
private static readonly int LightAbsorptionId = Shader.PropertyToID("_LightAbsorption");
private static readonly int LightAbsorptionToSunId = Shader.PropertyToID("_LightAbsorptionToSun");
private static readonly int phaseParamsId = Shader.PropertyToID("_phaseParams");
private static readonly int colAId = Shader.PropertyToID("_colA");
private static readonly int colBId = Shader.PropertyToID("_colB");
private static readonly int colorOffset1Id = Shader.PropertyToID("_colorOffset1");
private static readonly int colorOffset2Id = Shader.PropertyToID("_colorOffset2");
private static readonly int darknessThresholdId = Shader.PropertyToID("_darknessThreshold");
private static readonly int WeatherMapId = Shader.PropertyToID("_WeatherMap");
private static readonly int WeatherMapSTId = Shader.PropertyToID("_WeatherMapST");
private static readonly int CloudHeightId = Shader.PropertyToID("_CloudHeight");
private static readonly int DetailNoiseTexId = Shader.PropertyToID("_DetailNoiseTex");
private static readonly int DetailNoiseScaleId = Shader.PropertyToID("_DetailNoiseScale");
private static readonly int DetailNoiseOffsetId = Shader.PropertyToID("_DetailNoiseOffset");
private static readonly int DetailNoiseWeightId = Shader.PropertyToID("_DetailNoiseWeight");
private static readonly int CloudIntensityId = Shader.PropertyToID("_CloudIntensity");
private static readonly int DetailNoiseIntensityId = Shader.PropertyToID("_DetailNoiseIntensity");
private static readonly int BlueNoiseTexId = Shader.PropertyToID("_BlueNoiseTex");
private static readonly int BlueNoiseTexSTId = Shader.PropertyToID("_BlueNoiseTexST");
#endregion
private const string tempRTName = "_CloudTexture";
private const string PassName = "CloudPass";
private CloudSettings defaultSettings;
private Material material;
private RenderTextureDescriptor cloudTextureDescriptor;
private class PassData
{
internal TextureHandle CloudTexture; //声明包含渲染通道使用的资源类
}
public CloudPass(Material material, CloudSettings defaultSettings)
{
this.material = material;
this.defaultSettings = defaultSettings;
cloudTextureDescriptor = new RenderTextureDescriptor(Screen.width, Screen.height, RenderTextureFormat.Default, 0);
}
private void UpdateSettings() //传参
{
if (material == null) return;
// Use the Volume settings or the default settings if no Volume is set.
var volumeComponent = VolumeManager.instance.stack.GetComponent<CloudVolume>();
material.SetVector(boundsMinId, volumeComponent.boundsMin.value);
material.SetVector(boundsMaxId, volumeComponent.boundsMax.value);
material.SetFloat(stepCountId, volumeComponent.stepCount.value);
material.SetTexture(BaseNoiseTexId, volumeComponent.BaseNoiseTex.value);
material.SetVector(NoiseScaleId, volumeComponent.BaseNoiseScale.value);
material.SetVector(NoiseOffsetId, volumeComponent.BaseNoiseOffset.value);
material.SetFloat(CloudDensityId, volumeComponent.CloudDensity.value);
material.SetFloat(CloudDensityPowId, volumeComponent.CloudDensityPower.value);
material.SetFloat(AbsorptionId, volumeComponent.Absorption.value);
material.SetFloat(LightAbsorptionId, volumeComponent.LightAbsorption.value);
material.SetFloat(LightAbsorptionToSunId, volumeComponent.LightAbsorptionToSun.value);
material.SetVector(phaseParamsId, volumeComponent.phaseParams.value);
material.SetColor(colAId, volumeComponent.ColorMid.value);
material.SetColor(colBId, volumeComponent.ColorDark.value);
material.SetFloat(colorOffset1Id, volumeComponent.ColorMidOffset.value);
material.SetFloat(colorOffset2Id, volumeComponent.ColorDarkOffset.value);
material.SetFloat(darknessThresholdId, volumeComponent.DarknessThreshold.value);
material.SetTexture(WeatherMapId, volumeComponent.WeatherMap.value);
material.SetVector(WeatherMapSTId, volumeComponent.WeatherMapST.value);
material.SetFloat(CloudHeightId, volumeComponent.CloudHeight.value);
material.SetTexture(DetailNoiseTexId, volumeComponent.DetailNoiseTex.value);
material.SetVector(DetailNoiseScaleId, volumeComponent.DetailNoiseScale.value);
material.SetVector(DetailNoiseOffsetId, volumeComponent.DetailNoiseOffset.value);
material.SetFloat(DetailNoiseWeightId, volumeComponent.DetailNoiseWeight.value);
material.SetFloat(CloudIntensityId, volumeComponent.CloudIntensity.value);
material.SetFloat(DetailNoiseIntensityId, volumeComponent.DetailNoiseIntensity.value);
material.SetTexture(BlueNoiseTexId, volumeComponent.BlueNoiseTex.value);
material.SetVector(BlueNoiseTexSTId, volumeComponent.BlueNoiseTexST.value);
}
public override void RecordRenderGraph(RenderGraph renderGraph, ContextContainer frameData)
{
RasterGraphContext context = new RasterGraphContext();
UniversalResourceData resourceData = frameData.Get<UniversalResourceData>();
UniversalCameraData cameraData = frameData.Get<UniversalCameraData>();
// The following line ensures that the render pass doesn't blit
// from the back buffer.
if (resourceData.isActiveTargetBackBuffer)
return;
// Set the blur texture size to be the same as the camera target size.
cloudTextureDescriptor.width = cameraData.cameraTargetDescriptor.width;
cloudTextureDescriptor.height = cameraData.cameraTargetDescriptor.height;
cloudTextureDescriptor.depthBufferBits = 0;
TextureHandle srcCamColor = resourceData.activeColorTexture;
TextureHandle dst = UniversalRenderer.CreateRenderGraphTexture(renderGraph, cloudTextureDescriptor, tempRTName, false);
// Update the blur settings in the material
UpdateSettings();
// This check is to avoid an error from the material preview in the scene
if (!srcCamColor.IsValid() || !dst.IsValid())
return;
//RenderGraphUtils.AddCopyPass(renderGraph,srcCamColor, dst);
//RenderGraphUtils.AddCopyPass(renderGraph,dst, srcCamColor);
// The AddBlitPass method adds a vertical blur render graph pass that blits from the source texture (camera color in this case) to the destination texture using the first shader pass (the shader pass is defined in the last parameter).
//Blitter.BlitTexture();
RenderGraphUtils.BlitMaterialParameters cmd_Cloud = new(srcCamColor, dst, material,0);
renderGraph.AddBlitPass(cmd_Cloud, PassName);
renderGraph.AddCopyPass(dst, srcCamColor);
}
}
