Skip to content

Commit

Permalink
冗長な処理を簡略化する
Browse files Browse the repository at this point in the history
  • Loading branch information
S20817 committed Sep 21, 2023
1 parent 7560c81 commit a25f4fa
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 33 deletions.
28 changes: 5 additions & 23 deletions Assets/Nova/Runtime/Core/Scripts/ApplyDistortionPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,24 @@ namespace Nova.Runtime.Core.Scripts
public sealed class ApplyDistortionPass : ScriptableRenderPass
{
private const string RenderPassName = nameof(ApplyDistortionPass);
private const string SrcToDestProfilingSamplerName = "SrcToDest";

private readonly bool _applyToSceneView;
private readonly int _distortionBufferPropertyId = Shader.PropertyToID("_ScreenSpaceUvTexture");
private readonly int _mainTexPropertyId = Shader.PropertyToID("_MainTex");
private readonly Material _material;
private readonly ProfilingSampler _srcToDestProfilingSampler;
private readonly ProfilingSampler _renderPassProfilingSampler;

private ScriptableRenderer _renderer;
private RenderTargetIdentifier _distortedUvBufferIdentifier;
private RenderTargetHandle _tempRenderTargetHandle;

public ApplyDistortionPass(bool applyToSceneView, Shader shader)
{
_applyToSceneView = applyToSceneView;
_srcToDestProfilingSampler = new ProfilingSampler(SrcToDestProfilingSamplerName);
_renderPassProfilingSampler = new ProfilingSampler(RenderPassName);
_tempRenderTargetHandle.Init("_TempRT");
_material = CoreUtils.CreateEngineMaterial(shader);
renderPassEvent = RenderPassEvent.BeforeRenderingPostProcessing;
}

public void Setup(ScriptableRenderer renderer, RenderTargetIdentifier distortedUvBufferIdentifier)
public void Setup(RenderTargetIdentifier distortedUvBufferIdentifier)
{
_renderer = renderer;
_distortedUvBufferIdentifier = distortedUvBufferIdentifier;
}

Expand All @@ -62,26 +54,16 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
}
var source = renderingData.cameraData.renderer.cameraColorTargetHandle.nameID;
#else
var source = _renderer.cameraColorTarget;
var source = renderingData.cameraData.renderer.cameraColorTarget;
#endif

var cmd = CommandBufferPool.Get();
cmd.Clear();
using (new ProfilingScope(cmd, _renderPassProfilingSampler))
{
var tempTargetDescriptor = renderingData.cameraData.cameraTargetDescriptor;
tempTargetDescriptor.depthBufferBits = 0;
cmd.GetTemporaryRT(_tempRenderTargetHandle.id, tempTargetDescriptor);

using (new ProfilingScope(cmd, _srcToDestProfilingSampler))
{
cmd.SetGlobalTexture(_mainTexPropertyId, source);
cmd.SetGlobalTexture(_distortionBufferPropertyId, _distortedUvBufferIdentifier);
Blit(cmd, source, _tempRenderTargetHandle.Identifier(), _material);
}

Blit(cmd, _tempRenderTargetHandle.Identifier(), source);
cmd.ReleaseTemporaryRT(_tempRenderTargetHandle.id);
cmd.SetGlobalTexture(_mainTexPropertyId, source);
cmd.SetGlobalTexture(_distortionBufferPropertyId, _distortedUvBufferIdentifier);
Blit(cmd, ref renderingData, _material);
}

context.ExecuteCommandBuffer(cmd);
Expand Down
8 changes: 2 additions & 6 deletions Assets/Nova/Runtime/Core/Scripts/DistortedUvBufferPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright 2021 CyberAgent, Inc.
// --------------------------------------------------------------

using System;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
Expand All @@ -15,7 +14,6 @@ public sealed class DistortedUvBufferPass : ScriptableRenderPass
private readonly ProfilingSampler _profilingSampler = new ProfilingSampler(ProfilerTag);
private readonly RenderQueueRange _renderQueueRange = RenderQueueRange.all;
private readonly ShaderTagId _shaderTagId;
private Func<RenderTargetIdentifier> _getCameraDepthTargetIdentifier;
private FilteringSettings _filteringSettings;

#if UNITY_2022_1_OR_NEWER
Expand All @@ -37,11 +35,9 @@ public void Setup(RTHandle renderTargetRTHandle)
_renderTargetRTHandle = renderTargetRTHandle;
}
#else
public void Setup(RenderTargetIdentifier renderTargetIdentifier,
Func<RenderTargetIdentifier> getCameraDepthTargetIdentifier)
public void Setup(RenderTargetIdentifier renderTargetIdentifier)
{
_renderTargetIdentifier = renderTargetIdentifier;
_getCameraDepthTargetIdentifier = getCameraDepthTargetIdentifier;
}
#endif

Expand All @@ -50,7 +46,7 @@ public override void Configure(CommandBuffer cmd, RenderTextureDescriptor camera
#if UNITY_2022_1_OR_NEWER
ConfigureTarget(_renderTargetRTHandle);
#else
ConfigureTarget(_renderTargetIdentifier, _getCameraDepthTargetIdentifier.Invoke());
ConfigureTarget(_renderTargetIdentifier);
#endif
ConfigureClear(ClearFlag.Color, Color.gray);
}
Expand Down
5 changes: 2 additions & 3 deletions Assets/Nova/Runtime/Core/Scripts/ScreenSpaceDistortion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingD
cameraTargetDescriptor.height, 0, distortedUvBufferFormat, RenderTextureReadWrite.Default,
cameraTargetDescriptor.msaaSamples);
var distortedUvBufferIdentifier = new RenderTargetIdentifier(distortedUvBuffer);

_distortedUvBufferPass.Setup(distortedUvBufferIdentifier, () => renderer.cameraDepthTarget);
_applyDistortionPass.Setup(renderer, distortedUvBufferIdentifier);
_distortedUvBufferPass.Setup(distortedUvBufferIdentifier);
_applyDistortionPass.Setup(distortedUvBufferIdentifier);
#endif
renderer.EnqueuePass(_distortedUvBufferPass);
renderer.EnqueuePass(_applyDistortionPass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Shader "Hidden/Nova/Particles/ApplyDistortion"
#pragma vertex Vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

#if UNITY_VERSION >= 202200
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"
#endif
Expand Down

0 comments on commit a25f4fa

Please sign in to comment.