1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
| using UnityEngine; using UnityEngine.Rendering;
using Conditional = System.Diagnostics.ConditionalAttribute;
public class SRP1 : RenderPipeline { private RenderPipelineAsset _asset; CommandBuffer commandbuffer = new CommandBuffer() { name = "SRP1Command" };
SortingSettings sortingSetting; FilteringSettings filterSetting; Material errorMaterial; public SRP1(RenderPipelineAsset asset) { _asset = asset; } protected override void Render(ScriptableRenderContext context, Camera[] cameras) { GraphicsSettings.useScriptableRenderPipelineBatching = true; for (int i = 0; i < cameras.Length; i++) { Camera camera = cameras[i];
CameraClearFlags flag = camera.clearFlags; if (flag == CameraClearFlags.Skybox) { context.DrawSkybox(camera); } else { commandbuffer.ClearRenderTarget( flag <= CameraClearFlags.Depth, flag <= CameraClearFlags.Color, new Color(0.1f, 0.4f, 0.6f) ); context.ExecuteCommandBuffer(commandbuffer); commandbuffer.Clear(); }
context.SetupCameraProperties(camera); ShaderTagId shaderTagId = new ShaderTagId("SRP1Shader"); sortingSetting = new SortingSettings(camera) { criteria = SortingCriteria.CommonOpaque }; filterSetting = new FilteringSettings(RenderQueueRange.opaque); var drawingSetting = new DrawingSettings(shaderTagId, sortingSetting);
CullingResults cullingResults = new CullingResults(); if (camera.TryGetCullingParameters(out ScriptableCullingParameters p)) { cullingResults = context.Cull(ref p); context.DrawRenderers(cullingResults, ref drawingSetting, ref filterSetting); } DrawDefaultPipeline(context, camera, cullingResults); if (camera.clearFlags == CameraClearFlags.Skybox && RenderSettings.skybox != null) { context.DrawSkybox(camera); } context.Submit(); } } [Conditional("DEVELOPMENT_BUILD"), Conditional("UNITY_EDITOR")] void DrawDefaultPipeline(ScriptableRenderContext context, Camera camera, CullingResults cullingResults) { if (errorMaterial == null) { Shader errorShader = Shader.Find("Hidden/InternalErrorShader"); errorMaterial = new Material(errorShader) { hideFlags = HideFlags.HideAndDontSave }; } ShaderTagId shaderTagId = new ShaderTagId("ForwardBase");
var drawSettings = new DrawingSettings(shaderTagId,sortingSetting); drawSettings.SetShaderPassName(1, new ShaderTagId("PrepassBase")); drawSettings.SetShaderPassName(2, new ShaderTagId("Always")); drawSettings.SetShaderPassName(3, new ShaderTagId("Vertex")); drawSettings.SetShaderPassName(4, new ShaderTagId("VertexLMRGBM")); drawSettings.SetShaderPassName(5, new ShaderTagId("VertexLM")); drawSettings.overrideMaterial = errorMaterial; context.DrawRenderers(cullingResults, ref drawSettings, ref filterSetting); } }
|