Skip to content

Commit

Permalink
Added support for 2023 and Discontinued support for 2020
Browse files Browse the repository at this point in the history
  • Loading branch information
S20817 committed Sep 21, 2023
1 parent a49c12b commit 411a799
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 74 deletions.
2 changes: 1 addition & 1 deletion Assets/Nova/Editor/Core/Scripts/ParticlesUberCommonGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private void CacheRenderersUsingThisMaterial(Material material)
{
_renderersUsingThisMaterial.Clear();

var renderers = Object.FindObjectsOfType(typeof(ParticleSystemRenderer)) as ParticleSystemRenderer[];
var renderers = Object.FindObjectsByType<ParticleSystemRenderer>(FindObjectsSortMode.None);
if (renderers == null) return;
foreach (var renderer in renderers)
if (renderer.sharedMaterial == material)
Expand Down
46 changes: 20 additions & 26 deletions Assets/Nova/Runtime/Core/Scripts/ApplyDistortionPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,26 @@ 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;
private RTHandle _distortedUvBufferRTHandle;
private RTHandle _tempRTHandle;

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(RTHandle distortedUvBufferRTHandle)
{
_renderer = renderer;
_distortedUvBufferIdentifier = distortedUvBufferIdentifier;
_distortedUvBufferRTHandle = distortedUvBufferRTHandle;
}

public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
Expand All @@ -51,29 +44,30 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData
{
return;
}

// todo:後で詳しくGUI周りを調査する
// マテリアルGUI描画時、なぜかここがNullでエラーが出てしまうので、とりあえずNullチェックを入れる
// ランタイム実行に影響がない
if (renderingData.cameraData.renderer.cameraColorTargetHandle.rt == null)
{
return;
}

var cmd = CommandBufferPool.Get();
cmd.Clear();
using (new ProfilingScope(cmd, _renderPassProfilingSampler))
{
var source = _renderer.cameraColorTarget;
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(_distortionBufferPropertyId, _distortedUvBufferRTHandle.nameID);
Blit(cmd, ref renderingData, _material);
}

context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}

public void Dispose()
{
CoreUtils.Destroy(_material);
}
}
}
25 changes: 13 additions & 12 deletions Assets/Nova/Runtime/Core/Scripts/DistortedUvBufferPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@ 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;

private RenderTargetIdentifier _renderTargetIdentifier;
private RTHandle _renderTargetRTHandle;

public DistortedUvBufferPass(string lightMode)
{
_filteringSettings = new FilteringSettings(_renderQueueRange);
renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
_shaderTagId = new ShaderTagId(lightMode);
}

public void Setup(RenderTargetIdentifier renderTargetIdentifier,
Func<RenderTargetIdentifier> getCameraDepthTargetIdentifier)

public void Setup(RTHandle renderTargetRTHandle)
{
_renderTargetIdentifier = renderTargetIdentifier;
_getCameraDepthTargetIdentifier = getCameraDepthTargetIdentifier;
_renderTargetRTHandle = renderTargetRTHandle;
}

public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor)
{
ConfigureTarget(_renderTargetIdentifier, _getCameraDepthTargetIdentifier.Invoke());
ConfigureTarget(_renderTargetRTHandle);
ConfigureClear(ClearFlag.Color, Color.gray);
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
Expand All @@ -46,12 +43,16 @@ public override void Execute(ScriptableRenderContext context, ref RenderingData

using (new ProfilingScope(cmd, _profilingSampler))
{
context.ExecuteCommandBuffer(cmd);
cmd.Clear();

var drawingSettings =
CreateDrawingSettings(_shaderTagId, ref renderingData, SortingCriteria.CommonTransparent);

#if UNITY_2023_1_OR_NEWER
var param = new RendererListParams(renderingData.cullResults, drawingSettings, _filteringSettings);
var renderList = context.CreateRendererList(ref param);
cmd.DrawRendererList(renderList);
#else
context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref _filteringSettings);
#endif
}
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
Expand Down
25 changes: 17 additions & 8 deletions Assets/Nova/Runtime/Core/Scripts/ScreenSpaceDistortion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class ScreenSpaceDistortion : ScriptableRendererFeature
private ApplyDistortionPass _applyDistortionPass;

private DistortedUvBufferPass _distortedUvBufferPass;
private RTHandle _distortedUvBufferRTHandle;

public override void Create()
{
Expand All @@ -32,21 +33,29 @@ public override void Create()
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (_applyDistortionShader == null || renderingData.cameraData.cameraType == CameraType.Reflection) return;
var cameraTargetDesciptor = renderingData.cameraData.cameraTargetDescriptor;

var distortedUvBufferFormat = SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGHalf)
? RenderTextureFormat.RGHalf
: RenderTextureFormat.DefaultHDR;
var distortedUvBuffer = RenderTexture.GetTemporary(cameraTargetDesciptor.width,
cameraTargetDesciptor.height, 0, distortedUvBufferFormat, RenderTextureReadWrite.Default,
cameraTargetDesciptor.msaaSamples);
var distortedUvBufferIdentifier = new RenderTargetIdentifier(distortedUvBuffer);

_distortedUvBufferPass.Setup(distortedUvBufferIdentifier, () => renderer.cameraDepthTarget);
_applyDistortionPass.Setup(renderer, distortedUvBufferIdentifier);
var desc = renderingData.cameraData.cameraTargetDescriptor;
desc.depthBufferBits = 0;
desc.colorFormat = distortedUvBufferFormat;
RenderingUtils.ReAllocateIfNeeded(ref _distortedUvBufferRTHandle, desc);

_distortedUvBufferPass.Setup(_distortedUvBufferRTHandle);
_applyDistortionPass.Setup(_distortedUvBufferRTHandle);
renderer.EnqueuePass(_distortedUvBufferPass);
renderer.EnqueuePass(_applyDistortionPass);
RenderTexture.ReleaseTemporary(distortedUvBuffer);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
_applyDistortionPass.Dispose();
RTHandles.Release(_distortedUvBufferRTHandle);
}
}
}
}
32 changes: 5 additions & 27 deletions Assets/Nova/Runtime/Core/Shaders/ParticlesApplyDistortion.shader
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,22 @@ Shader "Hidden/Nova/Particles/ApplyDistortion"
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma vertex Vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/Runtime/Utilities/Blit.hlsl"

TEXTURE2D(_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D(_ScreenSpaceUvTexture);
SAMPLER(sampler_ScreenSpaceUvTexture);
half4 _TintColor;

struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};

struct Varyings
{
float4 positionHCS : SV_POSITION;
float2 uv : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};

Varyings vert(Attributes IN)
{
Varyings OUT;
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = IN.uv;
return OUT;
}

half4 frag(Varyings IN) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(IN);
half2 dist = SAMPLE_TEXTURE2D(_ScreenSpaceUvTexture, sampler_ScreenSpaceUvTexture, IN.uv).xy;
const float2 uv = IN.texcoord;
half2 dist = SAMPLE_TEXTURE2D(_ScreenSpaceUvTexture, sampler_ScreenSpaceUvTexture, uv).xy;
dist = dist.xy * 2.0 - 1.0;
half4 col = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv + dist);
half4 col = SAMPLE_TEXTURE2D_X(_BlitTexture, sampler_LinearClamp, uv + dist);
return col;
}
ENDHLSL
Expand Down
2 changes: 2 additions & 0 deletions Assets/Nova/Runtime/Core/Shaders/ParticlesDistortion.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SAMPLER(sampler_FlowMap);
TEXTURE2D(_AlphaTransitionMap);
SAMPLER(sampler_AlphaTransitionMap);

CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
DECLARE_CUSTOM_COORD(_BaseMapOffsetXCoord);
DECLARE_CUSTOM_COORD(_BaseMapOffsetYCoord);
Expand Down Expand Up @@ -64,5 +65,6 @@ float _SoftParticlesIntensity;
float _DepthFadeNear;
float _DepthFadeFar;
float _DepthFadeWidth;
CBUFFER_END

#endif

0 comments on commit 411a799

Please sign in to comment.