浏览代码

Added custom renderer

/demo-work-customrenderer
Felipe Lira 5 年前
当前提交
d7feba3b
共有 10 个文件被更改,包括 364 次插入0 次删除
  1. 29
      Assets/Data/UniversalRP/CustomBoatDemoRenderer.asset
  2. 8
      Assets/Data/UniversalRP/CustomBoatDemoRenderer.asset.meta
  3. 8
      Assets/Rendering.meta
  4. 214
      Assets/Rendering/BoatRenderer.cs
  5. 11
      Assets/Rendering/BoatRenderer.cs.meta
  6. 40
      Assets/Rendering/BoatRendererData.cs
  7. 11
      Assets/Rendering/BoatRendererData.cs.meta
  8. 32
      Assets/Rendering/MainRenderPass.cs
  9. 11
      Assets/Rendering/MainRenderPass.cs.meta

29
Assets/Data/UniversalRP/CustomBoatDemoRenderer.asset


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 580815c7b01f60e429065cd2e33f7e1d, type: 3}
m_Name: Custom Boat Renderer Data
m_EditorClassIdentifier:
m_RendererFeatures:
- {fileID: 6778594283966551180}
postProcessData: {fileID: 11400000, guid: 41439944d30ece34e96484bdb6645b55, type: 2}
--- !u!114 &6778594283966551180
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: fc3f76324391a408d9c0770ca6fcb666, type: 3}
m_Name: WaterFX
m_EditorClassIdentifier:

8
Assets/Data/UniversalRP/CustomBoatDemoRenderer.asset.meta


fileFormatVersion: 2
guid: 9bab193ea15b8314184732322e8c9073
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 0
userData:
assetBundleName:
assetBundleVariant:

8
Assets/Rendering.meta


fileFormatVersion: 2
guid: cf5d23597296ed547841632296585938
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

214
Assets/Rendering/BoatRenderer.cs


using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class BoatRenderer : ScriptableRenderer
{
const int k_DepthStencilBufferBits = 32;
const string k_CreateCameraTextures = "Create Camera Texture";
ColorGradingLutPass m_ColorGradingLutPass;
MainLightShadowCasterPass m_MainLightShadowCasterPass;
PostProcessPass m_PostProcessPass;
PostProcessPass m_FinalPostProcessPass;
MainRenderPass m_MainRenderPass;
FinalBlitPass m_FinalBlitPass;
#if UNITY_EDITOR
SceneViewDepthCopyPass m_SceneViewDepthCopyPass;
#endif
RenderTargetHandle m_ActiveCameraColorAttachment;
RenderTargetHandle m_ActiveCameraDepthAttachment;
RenderTargetHandle m_CameraColorAttachment;
RenderTargetHandle m_CameraDepthAttachment;
RenderTargetHandle m_AfterPostProcessColor;
RenderTargetHandle m_ColorGradingLut;
ForwardLights m_ForwardLights;
public BoatRenderer(BoatRendererData data) : base(data)
{
Material blitMaterial = CoreUtils.CreateEngineMaterial(data.shaders.blitPS);
Material copyDepthMaterial = CoreUtils.CreateEngineMaterial(data.shaders.copyDepthPS);
m_MainLightShadowCasterPass = new MainLightShadowCasterPass(RenderPassEvent.BeforeRenderingShadows);
m_ColorGradingLutPass = new ColorGradingLutPass(RenderPassEvent.BeforeRenderingOpaques, data.postProcessData);
m_PostProcessPass = new PostProcessPass(RenderPassEvent.BeforeRenderingPostProcessing, data.postProcessData);
m_FinalPostProcessPass = new PostProcessPass(RenderPassEvent.AfterRenderingPostProcessing, data.postProcessData);
m_MainRenderPass = new MainRenderPass(RenderPassEvent.BeforeRenderingOpaques);
m_FinalBlitPass = new FinalBlitPass(RenderPassEvent.AfterRendering, blitMaterial);
#if UNITY_EDITOR
m_SceneViewDepthCopyPass = new SceneViewDepthCopyPass(RenderPassEvent.AfterRendering + 9, copyDepthMaterial);
#endif
// RenderTexture format depends on camera and pipeline (HDR, non HDR, etc)
// Samples (MSAA) depend on camera and pipeline
m_CameraColorAttachment.Init("_CameraColorTexture");
m_CameraDepthAttachment.Init("_CameraDepthAttachment");
m_AfterPostProcessColor.Init("_AfterPostProcessTexture");
m_ColorGradingLut.Init("_InternalGradingLut");
m_ForwardLights = new ForwardLights();
}
public override void Setup(ScriptableRenderContext context, ref RenderingData renderingData)
{
ref CameraData cameraData = ref renderingData.cameraData;
var cameraTargetDescriptor = cameraData.cameraTargetDescriptor;
bool mainLightShadows = m_MainLightShadowCasterPass.Setup(ref renderingData);
bool resolveShadowsInScreenSpace = mainLightShadows && renderingData.shadowData.requiresScreenSpaceShadowResolve;
// Depth prepass is generated in the following cases:
// - We resolve shadows in screen space
// - Scene view camera always requires a depth texture. We do a depth pre-pass to simplify it and it shouldn't matter much for editor.
// - If game or offscreen camera requires it we check if we can copy the depth from the rendering opaques pass and use that instead.
bool requiresDepthPrepass = renderingData.cameraData.isSceneViewCamera ||
(cameraData.requiresDepthTexture && (!CanCopyDepth(ref renderingData.cameraData)));
requiresDepthPrepass |= resolveShadowsInScreenSpace;
bool createColorTexture = RequiresIntermediateColorTexture(ref renderingData, cameraTargetDescriptor)
|| rendererFeatures.Count != 0;
// If camera requires depth and there's no depth pre-pass we create a depth texture that can be read
// later by effect requiring it.
bool createDepthTexture = cameraData.requiresDepthTexture && !requiresDepthPrepass;
bool postProcessEnabled = cameraData.postProcessEnabled;
bool requiresFinalPostProcessPass = postProcessEnabled &&
renderingData.cameraData.antialiasing == AntialiasingMode.FastApproximateAntialiasing;
m_ActiveCameraColorAttachment = (createColorTexture) ? m_CameraColorAttachment : RenderTargetHandle.CameraTarget;
m_ActiveCameraDepthAttachment = (createDepthTexture) ? m_CameraDepthAttachment : RenderTargetHandle.CameraTarget;
bool intermediateRenderTexture = createColorTexture || createDepthTexture;
if (intermediateRenderTexture)
CreateCameraRenderTarget(context, ref cameraData);
ConfigureCameraTarget(m_ActiveCameraColorAttachment.Identifier(), m_ActiveCameraDepthAttachment.Identifier());
for (int i = 0; i < rendererFeatures.Count; ++i)
{
rendererFeatures[i].AddRenderPasses(this, ref renderingData);
}
int count = activeRenderPassQueue.Count;
for (int i = count - 1; i >= 0; i--)
{
if (activeRenderPassQueue[i] == null)
activeRenderPassQueue.RemoveAt(i);
}
if (mainLightShadows)
EnqueuePass(m_MainLightShadowCasterPass);
if (postProcessEnabled)
{
m_ColorGradingLutPass.Setup(m_ColorGradingLut);
EnqueuePass(m_ColorGradingLutPass);
}
EnqueuePass(m_MainRenderPass);
if (postProcessEnabled)
{
if (requiresFinalPostProcessPass)
{
m_PostProcessPass.Setup(cameraTargetDescriptor, m_ActiveCameraColorAttachment, m_AfterPostProcessColor, m_ActiveCameraDepthAttachment, m_ColorGradingLut);
EnqueuePass(m_PostProcessPass);
m_FinalPostProcessPass.SetupFinalPass(m_AfterPostProcessColor);
EnqueuePass(m_FinalPostProcessPass);
}
else
{
m_PostProcessPass.Setup(cameraTargetDescriptor, m_ActiveCameraColorAttachment, RenderTargetHandle.CameraTarget, m_ActiveCameraDepthAttachment, m_ColorGradingLut);
EnqueuePass(m_PostProcessPass);
}
}
else if (m_ActiveCameraColorAttachment != RenderTargetHandle.CameraTarget)
{
m_FinalBlitPass.Setup(cameraTargetDescriptor, m_ActiveCameraColorAttachment);
EnqueuePass(m_FinalBlitPass);
}
}
public override void SetupLights(ScriptableRenderContext context, ref RenderingData renderingData)
{
m_ForwardLights.Setup(context, ref renderingData);
}
public override void SetupCullingParameters(ref ScriptableCullingParameters cullingParameters,
ref CameraData cameraData)
{
Camera camera = cameraData.camera;
cullingParameters.shadowDistance = Mathf.Min(cameraData.maxShadowDistance, camera.farClipPlane);
}
public override void FinishRendering(CommandBuffer cmd)
{
if (m_ActiveCameraColorAttachment != RenderTargetHandle.CameraTarget)
cmd.ReleaseTemporaryRT(m_ActiveCameraColorAttachment.id);
if (m_ActiveCameraDepthAttachment != RenderTargetHandle.CameraTarget)
cmd.ReleaseTemporaryRT(m_ActiveCameraDepthAttachment.id);
}
void CreateCameraRenderTarget(ScriptableRenderContext context, ref CameraData cameraData)
{
CommandBuffer cmd = CommandBufferPool.Get(k_CreateCameraTextures);
var descriptor = cameraData.cameraTargetDescriptor;
int msaaSamples = descriptor.msaaSamples;
if (m_ActiveCameraColorAttachment != RenderTargetHandle.CameraTarget)
{
bool useDepthRenderBuffer = m_ActiveCameraDepthAttachment == RenderTargetHandle.CameraTarget;
var colorDescriptor = descriptor;
colorDescriptor.depthBufferBits = (useDepthRenderBuffer) ? k_DepthStencilBufferBits : 0;
cmd.GetTemporaryRT(m_ActiveCameraColorAttachment.id, colorDescriptor, FilterMode.Bilinear);
}
if (m_ActiveCameraDepthAttachment != RenderTargetHandle.CameraTarget)
{
var depthDescriptor = descriptor;
depthDescriptor.colorFormat = RenderTextureFormat.Depth;
depthDescriptor.depthBufferBits = k_DepthStencilBufferBits;
depthDescriptor.bindMS = msaaSamples > 1 && !SystemInfo.supportsMultisampleAutoResolve && (SystemInfo.supportsMultisampledTextures != 0);
cmd.GetTemporaryRT(m_ActiveCameraDepthAttachment.id, depthDescriptor, FilterMode.Point);
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
bool RequiresIntermediateColorTexture(ref RenderingData renderingData, RenderTextureDescriptor baseDescriptor)
{
ref CameraData cameraData = ref renderingData.cameraData;
int msaaSamples = cameraData.cameraTargetDescriptor.msaaSamples;
bool isStereoEnabled = renderingData.cameraData.isStereoEnabled;
bool isScaledRender = !Mathf.Approximately(cameraData.renderScale, 1.0f);
bool isCompatibleBackbufferTextureDimension = baseDescriptor.dimension == TextureDimension.Tex2D;
bool requiresExplicitMsaaResolve = msaaSamples > 1 && !SystemInfo.supportsMultisampleAutoResolve;
bool isOffscreenRender = cameraData.camera.targetTexture != null && !cameraData.isSceneViewCamera;
bool isCapturing = cameraData.captureActions != null;
bool requiresBlitForOffscreenCamera = cameraData.postProcessEnabled || cameraData.requiresOpaqueTexture || requiresExplicitMsaaResolve;
if (isOffscreenRender)
return requiresBlitForOffscreenCamera;
return requiresBlitForOffscreenCamera || cameraData.isSceneViewCamera || isScaledRender || cameraData.isHdrEnabled ||
!isCompatibleBackbufferTextureDimension || !cameraData.isDefaultViewport || isCapturing || Display.main.requiresBlitToBackbuffer
|| (renderingData.killAlphaInFinalBlit && !isStereoEnabled);
}
bool CanCopyDepth(ref CameraData cameraData)
{
bool msaaEnabledForCamera = cameraData.cameraTargetDescriptor.msaaSamples > 1;
bool supportsTextureCopy = SystemInfo.copyTextureSupport != CopyTextureSupport.None;
bool supportsDepthTarget = RenderingUtils.SupportsRenderTextureFormat(RenderTextureFormat.Depth);
bool supportsDepthCopy = !msaaEnabledForCamera && (supportsDepthTarget || supportsTextureCopy);
// TODO: We don't have support to highp Texture2DMS currently and this breaks depth precision.
// currently disabling it until shader changes kick in.
//bool msaaDepthResolve = msaaEnabledForCamera && SystemInfo.supportsMultisampledTextures != 0;
bool msaaDepthResolve = false;
return supportsDepthCopy || msaaDepthResolve;
}
}

11
Assets/Rendering/BoatRenderer.cs.meta


fileFormatVersion: 2
guid: 45e2f51a3917e6941a41ceb69f5b1a58
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

40
Assets/Rendering/BoatRendererData.cs


using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
[CreateAssetMenu]
public class BoatRendererData : ScriptableRendererData
{
[ReloadGroup]
public sealed class ShaderResources
{
[Reload("Shaders/Utils/Blit.shader")]
public Shader blitPS;
[Reload("Shaders/Utils/CopyDepth.shader")]
public Shader copyDepthPS;
[Reload("Shaders/Utils/ScreenSpaceShadows.shader")]
public Shader screenSpaceShadowPS;
[Reload("Shaders/Utils/Sampling.shader")]
public Shader samplingPS;
}
[Reload("Runtime/Data/PostProcessData.asset")]
public PostProcessData postProcessData = null;
public ShaderResources shaders = null;
protected override ScriptableRenderer Create()
{
#if UNITY_EDITOR
if (!Application.isPlaying)
{
ResourceReloader.ReloadAllNullIn(this, UniversalRenderPipelineAsset.packagePath);
ResourceReloader.ReloadAllNullIn(postProcessData, UniversalRenderPipelineAsset.packagePath);
}
#endif
return new BoatRenderer(this);
}
}

11
Assets/Rendering/BoatRendererData.cs.meta


fileFormatVersion: 2
guid: 580815c7b01f60e429065cd2e33f7e1d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

32
Assets/Rendering/MainRenderPass.cs


using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class MainRenderPass : ScriptableRenderPass
{
FilteringSettings m_OpaqueFilteringSettings;
FilteringSettings m_TransparentFilteringSettings;
ShaderTagId m_UniversalForwardPass = new ShaderTagId("UniversalForward");
public MainRenderPass(RenderPassEvent renderPassEvent)
{
this.renderPassEvent = renderPassEvent;
m_OpaqueFilteringSettings = new FilteringSettings(RenderQueueRange.opaque);
m_TransparentFilteringSettings = new FilteringSettings(RenderQueueRange.transparent);
}
//public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
//{
// ConfigureTarget(BuiltinRenderTextureType.CurrentActive, BuiltinRenderTextureType.CurrentActive);
//}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
var opaqueDrawingSettings = CreateDrawingSettings(m_UniversalForwardPass, ref renderingData, SortingCriteria.CommonOpaque);
var transparentDrawingSettings = CreateDrawingSettings(m_UniversalForwardPass, ref renderingData, SortingCriteria.CommonOpaque);
context.DrawRenderers(renderingData.cullResults, ref opaqueDrawingSettings, ref m_OpaqueFilteringSettings);
context.DrawSkybox(renderingData.cameraData.camera);
context.DrawRenderers(renderingData.cullResults, ref transparentDrawingSettings, ref m_TransparentFilteringSettings);
}
}

11
Assets/Rendering/MainRenderPass.cs.meta


fileFormatVersion: 2
guid: ab0cf89a3f1c6f041a012ad1d75a117f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存