一、结构
using UnityEngine;
using UnityEngine.Rendering;
[CreateAssetMenu(menuName = "Rendering/Custom Render Pipeline")]
public partial class CustomRenderPipelineAsset : RenderPipelineAsset
{
protected override RenderPipeline CreatePipeline()
{
return new CustomRenderPipeline();
}
}
public partial class CustomRenderPipeline : RenderPipeline
{
protected override void Render(ScriptableRenderContext context, Camera[] cameras)
{
}
}
二、渲染
设置相机相关变量
//Schedules the setup of Camera specific global Shader variables.
_context.SetupCameraProperties(_camera);
Cull
private bool Cull(float maxShadowDistance)
{
if (_camera.TryGetCullingParameters(out ScriptableCullingParameters cullingParameters))
{
cullingParameters.shadowDistance = Mathf.Min(maxShadowDistance, _camera.farClipPlane);
_cullingResults = _context.Cull(ref cullingParameters);
return true;
}
return false;
}
渲染 UI
if (_camera.cameraType == CameraType.SceneView)
{
ScriptableRenderContext.EmitWorldGeometryForSceneView(_camera);
}
RenderDebug
_buffer.BeginSample(SampleName);
_buffer.EndSample(SampleName);
执行CommandBuffer
private void ExecuteBuffer()
{
_context.ExecuteCommandBuffer(_buffer);
_buffer.Clear();
}
设置渲染目标
- 需要与当前屏幕上的颜色进行混合是,需要使用 RenderBufferLoadAction.Load
_buffer.SetRenderTarget(
BuiltinRenderTextureType.CameraTarget,
finalBlendMode.destination == BlendMode.Zero ? RenderBufferLoadAction.DontCare : RenderBufferLoadAction.Load,
RenderBufferStoreAction.Store
);
设置绘制渲染目标中的那些区域
_buffer.SetViewport(_camera.pixelRect);
渲染不透明物体
SortingSettings sortingSettings = new SortingSettings(_camera)
{
criteria = SortingCriteria.CommonOpaque
};
DrawingSettings drawingSettings = new DrawingSettings(UnlitShaderTagId, sortingSettings);
drawingSettings.SetShaderPassName(1, LitShaderTagId);
FilteringSettings filteringSettings = new FilteringSettings(RenderQueueRange.opaque, renderingLayerMask: (uint) renderingLayerMask);
_context.DrawRenderers(_cullingResults, ref drawingSettings, ref filteringSettings);
渲染 skybox
_context.DrawSkybox(_camera);
渲染半透明物体
sortingSettings.criteria = SortingCriteria.CommonTransparent;
drawingSettings.sortingSettings = sortingSettings;
filteringSettings.renderQueueRange = RenderQueueRange.transparent;
_context.DrawRenderers(_cullingResults, ref drawingSettings, ref filteringSettings);
绘制不支持的 Shader
private static readonly ShaderTagId[] LegacyShaderTagIds = new[]
{
new ShaderTagId("Always"),
new ShaderTagId("ForwardBase"),
new ShaderTagId("PrepassBase"),
new ShaderTagId("Vertex"),
new ShaderTagId("VertexLMRGBM"),
new ShaderTagId("VertexLM")
};
partial void DrawUnsupportedShaders()
{
if (_errorMaterial == null)
{
_errorMaterial = new Material(Shader.Find("Hidden/InternalErrorShader"));
}
DrawingSettings drawingSettings = new DrawingSettings(LegacyShaderTagIds[0], new SortingSettings(_camera))
{
overrideMaterial = _errorMaterial
};
for (int i = 1; i < LegacyShaderTagIds.Length; i++)
{
drawingSettings.SetShaderPassName(i, LegacyShaderTagIds[i]);
}
FilteringSettings filteringSettings = FilteringSettings.defaultValue;
_context.DrawRenderers(_cullingResults, ref drawingSettings, ref filteringSettings);
}
渲染 Gizmos
_context.DrawGizmos(_camera, GizmoSubset.PreImageEffects);
_context.DrawGizmos(_camera, GizmoSubset.PostImageEffects);