diff --git a/Assets/Nova/Editor/Core/Scripts/ParticlesUberCommonGUI.cs b/Assets/Nova/Editor/Core/Scripts/ParticlesUberCommonGUI.cs index c30ec79..40b9711 100644 --- a/Assets/Nova/Editor/Core/Scripts/ParticlesUberCommonGUI.cs +++ b/Assets/Nova/Editor/Core/Scripts/ParticlesUberCommonGUI.cs @@ -206,7 +206,7 @@ private void CacheRenderersUsingThisMaterial(Material material) { _renderersUsingThisMaterial.Clear(); - var renderers = Object.FindObjectsOfType(typeof(ParticleSystemRenderer)) as ParticleSystemRenderer[]; + var renderers = Object.FindObjectsByType(FindObjectsSortMode.None); if (renderers == null) return; foreach (var renderer in renderers) if (renderer.sharedMaterial == material) diff --git a/Assets/Nova/Runtime/Core/Scripts/ApplyDistortionPass.cs b/Assets/Nova/Runtime/Core/Scripts/ApplyDistortionPass.cs index 72f3962..53a81b4 100644 --- a/Assets/Nova/Runtime/Core/Scripts/ApplyDistortionPass.cs +++ b/Assets/Nova/Runtime/Core/Scripts/ApplyDistortionPass.cs @@ -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) @@ -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); + } } } \ No newline at end of file diff --git a/Assets/Nova/Runtime/Core/Scripts/DistortedUvBufferPass.cs b/Assets/Nova/Runtime/Core/Scripts/DistortedUvBufferPass.cs index 48168c2..4fbaf2c 100644 --- a/Assets/Nova/Runtime/Core/Scripts/DistortedUvBufferPass.cs +++ b/Assets/Nova/Runtime/Core/Scripts/DistortedUvBufferPass.cs @@ -15,10 +15,9 @@ public sealed class DistortedUvBufferPass : ScriptableRenderPass private readonly ProfilingSampler _profilingSampler = new ProfilingSampler(ProfilerTag); private readonly RenderQueueRange _renderQueueRange = RenderQueueRange.all; private readonly ShaderTagId _shaderTagId; - private Func _getCameraDepthTargetIdentifier; private FilteringSettings _filteringSettings; - - private RenderTargetIdentifier _renderTargetIdentifier; + + private RTHandle _renderTargetRTHandle; public DistortedUvBufferPass(string lightMode) { @@ -26,17 +25,15 @@ public DistortedUvBufferPass(string lightMode) renderPassEvent = RenderPassEvent.AfterRenderingTransparents; _shaderTagId = new ShaderTagId(lightMode); } - - public void Setup(RenderTargetIdentifier renderTargetIdentifier, - Func 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) @@ -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); diff --git a/Assets/Nova/Runtime/Core/Scripts/ScreenSpaceDistortion.cs b/Assets/Nova/Runtime/Core/Scripts/ScreenSpaceDistortion.cs index cecf298..20316a9 100644 --- a/Assets/Nova/Runtime/Core/Scripts/ScreenSpaceDistortion.cs +++ b/Assets/Nova/Runtime/Core/Scripts/ScreenSpaceDistortion.cs @@ -19,6 +19,7 @@ public sealed class ScreenSpaceDistortion : ScriptableRendererFeature private ApplyDistortionPass _applyDistortionPass; private DistortedUvBufferPass _distortedUvBufferPass; + private RTHandle _distortedUvBufferRTHandle; public override void Create() { @@ -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); + } } } } \ No newline at end of file diff --git a/Assets/Nova/Runtime/Core/Shaders/ParticlesApplyDistortion.shader b/Assets/Nova/Runtime/Core/Shaders/ParticlesApplyDistortion.shader index 63e265d..2af049d 100644 --- a/Assets/Nova/Runtime/Core/Shaders/ParticlesApplyDistortion.shader +++ b/Assets/Nova/Runtime/Core/Shaders/ParticlesApplyDistortion.shader @@ -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 diff --git a/Assets/Nova/Runtime/Core/Shaders/ParticlesDistortion.hlsl b/Assets/Nova/Runtime/Core/Shaders/ParticlesDistortion.hlsl index 6159812..fc40bc3 100644 --- a/Assets/Nova/Runtime/Core/Shaders/ParticlesDistortion.hlsl +++ b/Assets/Nova/Runtime/Core/Shaders/ParticlesDistortion.hlsl @@ -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); @@ -64,5 +65,6 @@ float _SoftParticlesIntensity; float _DepthFadeNear; float _DepthFadeFar; float _DepthFadeWidth; +CBUFFER_END #endif