Last active
August 25, 2021 07:29
-
-
Save cnDelbert/0bf996b7d0af3e87ed1963691606057f to your computer and use it in GitHub Desktop.
Unity Tutorials from Catlink.
This file contains 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; | |
public class CameraRenderer | |
{ | |
private ScriptableRenderContext context; | |
private Camera camera; | |
private const string bufferName = "RenderCamera"; | |
private static ShaderTagId unlitShaderId = new ShaderTagId("SRPDefaultUnlit"); | |
private CullingResults cullingResults; | |
private static ShaderTagId[] legacyShaderTagIds = | |
{ | |
new ShaderTagId("Always"), | |
new ShaderTagId("ForwardBase"), | |
new ShaderTagId("PrepassBase"), | |
new ShaderTagId("Vertex"), | |
new ShaderTagId("VertexLMRGBM"), | |
new ShaderTagId("VertexLM") | |
}; | |
private static Material errMaterial; | |
private CommandBuffer buffer = new CommandBuffer() | |
{ | |
name = bufferName | |
}; | |
public void Render(ScriptableRenderContext context, Camera camera) | |
{ | |
this.context = context; | |
this.camera = camera; | |
if (!Cull()) | |
{ | |
return; | |
} | |
Setup(); | |
DrawVisibleGeometry(); | |
DrawUnsupportedShaders(); | |
Submit(); | |
} | |
void Setup() | |
{ | |
context.SetupCameraProperties(camera); | |
buffer.ClearRenderTarget(true, true, Color.clear); | |
buffer.BeginSample(bufferName); | |
ExecuteBuffer(); | |
} | |
void DrawVisibleGeometry() | |
{ | |
var sortingSettings = new SortingSettings(camera) | |
{ | |
criteria = SortingCriteria.CommonOpaque | |
}; | |
var drawingSettings = new DrawingSettings(unlitShaderId, sortingSettings); | |
var filterSettings = new FilteringSettings(RenderQueueRange.opaque); | |
context.DrawRenderers(cullingResults, ref drawingSettings, ref filterSettings); | |
context.DrawSkybox(camera); | |
sortingSettings.criteria = SortingCriteria.CommonTransparent; | |
drawingSettings.sortingSettings = sortingSettings; | |
filterSettings.renderQueueRange = RenderQueueRange.transparent; | |
context.DrawRenderers(cullingResults, ref drawingSettings, ref filterSettings); | |
} | |
void DrawUnsupportedShaders() | |
{ | |
if (errMaterial == null) | |
{ | |
errMaterial = new Material(Shader.Find("Hidden/InternalErrorShader")); | |
} | |
var drawingSettings = new DrawingSettings(legacyShaderTagIds[0], new SortingSettings(camera)) | |
{ | |
overrideMaterial = errMaterial | |
}; | |
for (var i = 1; i < legacyShaderTagIds.Length; i++) | |
{ | |
drawingSettings.SetShaderPassName(i, legacyShaderTagIds[i]); | |
} | |
var filteringSettings = FilteringSettings.defaultValue; | |
context.DrawRenderers(cullingResults, ref drawingSettings, ref filteringSettings); | |
} | |
void Submit() | |
{ | |
buffer.EndSample(bufferName); | |
ExecuteBuffer(); | |
context.Submit(); | |
} | |
void ExecuteBuffer() | |
{ | |
context.ExecuteCommandBuffer(buffer); | |
buffer.Clear(); | |
} | |
bool Cull() | |
{ | |
if (camera.TryGetCullingParameters(out ScriptableCullingParameters scp)) | |
{ | |
cullingResults = context.Cull(ref scp); | |
return true; | |
} | |
return false; | |
} | |
} |
This file contains 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; | |
public class CustomRenderPipeline : RenderPipeline | |
{ | |
private CameraRenderer renderer = new CameraRenderer(); | |
protected override void Render(ScriptableRenderContext context, Camera[] cameras) | |
{ | |
foreach (var camera in cameras) | |
{ | |
renderer.Render(context, camera); | |
} | |
} | |
} |
This file contains 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; | |
[CreateAssetMenu(menuName = "Rendering/CustomRenderPipeline")] | |
public class CustomRenderPipelineAsset : RenderPipelineAsset | |
{ | |
protected override RenderPipeline CreatePipeline() | |
{ | |
return new CustomRenderPipeline(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment