Skip to content

Commit

Permalink
Merge pull request #16 from lukakldiashvili/version/0.3
Browse files Browse the repository at this point in the history
Version 0.3
  • Loading branch information
lukakldiashvili authored Jun 1, 2024
2 parents 013e788 + f9bf34c commit 937dbf7
Show file tree
Hide file tree
Showing 31 changed files with 369 additions and 318 deletions.
File renamed without changes.
18 changes: 18 additions & 0 deletions Editor/Unified.UniversalBlur.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Unified.UniversalBlur.Editor",
"rootNamespace": "",
"references": [
"GUID:179652d7da581408b8baeed6637275e9",
"GUID:3eae0364be2026648bf74846acb8a731",
"GUID:15fc0a57446b3144c949da3e2b9737a9"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//Credits to: https://github.com/Unity-Technologies/Graphics

using System.Collections.Generic;
using Unified.UniversalBlur.Runtime;
using UnityEditor;
using UnityEditor.Rendering;

namespace Unified.Universal.Blur.Editor
namespace Unified.UniversalBlur.Editor
{
/// <summary>
/// Custom editor for FullScreenPassRendererFeature class responsible for drawing unavailable by default properties
Expand Down Expand Up @@ -47,20 +48,20 @@ public override void OnInspectorGUI()
DrawAdditionalProperties();
}

m_AffectedFeature.passIndex = m_PassIndexToUse;
m_AffectedFeature.PassIndex = m_PassIndexToUse;

EditorUtility.SetDirty(target);
}

private void DrawAdditionalProperties()
{
List<string> selectablePasses;
bool isMaterialValid = m_AffectedFeature.passMaterial != null;
bool isMaterialValid = m_AffectedFeature.PassMaterial != null;
selectablePasses = isMaterialValid ? GetPassIndexStringEntries(m_AffectedFeature) : new List<string>() { "No material" };

// If material is invalid 0'th index is selected automatically, so it stays on "No material" entry
// It is invalid index, but FullScreenPassRendererFeature wont execute until material is valid
var choiceIndex = EditorGUILayout.Popup("Pass Index", m_AffectedFeature.passIndex, selectablePasses.ToArray());
var choiceIndex = EditorGUILayout.Popup("Pass Index", m_AffectedFeature.PassIndex, selectablePasses.ToArray());

m_PassIndexToUse = choiceIndex;

Expand All @@ -69,10 +70,10 @@ private void DrawAdditionalProperties()
private List<string> GetPassIndexStringEntries(UniversalBlurFeature component)
{
List<string> passIndexEntries = new List<string>();
for (int i = 0; i < component.passMaterial.passCount; ++i)
for (int i = 0; i < component.PassMaterial.passCount; ++i)
{
// "Name of a pass (index)" - "PassAlpha (1)"
string entry = $"{component.passMaterial.GetPassName(i)} ({i})";
string entry = $"{component.PassMaterial.GetPassName(i)} ({i})";
passIndexEntries.Add(entry);
}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion UnifiedUniversalBlur/Scripts.meta → Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions Runtime/BlurPassData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using UnityEngine;

namespace Unified.UniversalBlur.Runtime
{
internal struct BlurPassData : IDisposable
{
internal Material EffectMaterial;
internal int PassIndex;

public float Downsample;
public float Intensity;
public float Scale;
public int Iterations;

public RenderTextureDescriptor Descriptor;

public
// #if UNITY_2022_1_OR_NEWER
// RTHandle
// #else
RenderTexture
// #endif
RT1, RT2;

public void Dispose()
{
if (RT1 != null)
RT1.Release();

if (RT2 != null)
RT2.Release();
}
}
}
3 changes: 3 additions & 0 deletions Runtime/BlurPassData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Runtime/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;

namespace Unified.UniversalBlur.Runtime
{
public static class Helpers
{
// Derived from RenderingUtils.RTHandleNeedsReAlloc
public static bool HasDescriptorChanged(RenderTextureDescriptor descriptorA, RenderTextureDescriptor descriptorB, bool scaled)
{
if (descriptorA.useDynamicScale != scaled)
return true;
if (!scaled && (descriptorA.width != descriptorB.width || descriptorA.height != descriptorB.height))
return true;
return
descriptorA.depthBufferBits != descriptorB.depthBufferBits ||
(descriptorA.depthBufferBits == (int)DepthBits.None &&
descriptorA.graphicsFormat != (GraphicsFormat)descriptorB.colorFormat) ||
descriptorA.dimension != descriptorB.dimension ||
descriptorA.enableRandomWrite != descriptorB.enableRandomWrite ||
descriptorA.useMipMap != descriptorB.useMipMap ||
descriptorA.autoGenerateMips != descriptorB.autoGenerateMips ||
descriptorA.msaaSamples != descriptorB.msaaSamples ||
descriptorA.bindMS != descriptorB.bindMS ||
descriptorA.useDynamicScale != descriptorB.useDynamicScale ||
descriptorA.memoryless != descriptorB.memoryless;
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Helpers.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Runtime/ScaleBlurWith.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Unified.UniversalBlur.Runtime
{
public enum ScaleBlurWith
{
Disabled,
ScreenHeight,
ScreenWidth,
}
}
3 changes: 3 additions & 0 deletions Runtime/ScaleBlurWith.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions Runtime/UniversalBlurFeature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace Unified.UniversalBlur.Runtime
{
public class UniversalBlurFeature : ScriptableRendererFeature
{
private enum InjectionPoint
{
BeforeRenderingTransparents = RenderPassEvent.BeforeRenderingTransparents,
BeforeRenderingPostProcessing = RenderPassEvent.BeforeRenderingPostProcessing,
AfterRenderingPostProcessing = RenderPassEvent.AfterRenderingPostProcessing
}

[field: SerializeField, HideInInspector] public int PassIndex { get; set; } = 0;

[field: Header("Blur Settings")]
[field: SerializeField] private InjectionPoint injectionPoint = InjectionPoint.AfterRenderingPostProcessing;

[field: Space]
[field: Range(0f, 1f)] [field: SerializeField] public float Intensity { get; set; } = 1.0f;

[Range(1f, 10f)] [SerializeField] private float downsample = 2.0f;
[Range(1, 20)] [SerializeField] private int iterations = 6;
[Range(0f, 5f)] [SerializeField] private float scale = .5f;
[SerializeField] private ScaleBlurWith scaleBlurWith;
[SerializeField] private float scaleReferenceSize = 1080f;

[SerializeField]
[HideInInspector]
[Reload("Shaders/KawaseBlur.shader")]
private Shader shader;

private readonly ScriptableRenderPassInput _requirements = ScriptableRenderPassInput.Color;

public Material PassMaterial => _material;

// Hidden by scope because of incorrect behaviour in the editor
private bool disableInSceneView = true;

private Material _material;
private UniversalBlurPass _fullScreenPass;
private bool _injectedBeforeTransparents;

/// <inheritdoc/>
public override void Create()
{
_fullScreenPass = new UniversalBlurPass();
_fullScreenPass.renderPassEvent = (RenderPassEvent)injectionPoint;

ScriptableRenderPassInput modifiedRequirements = _requirements;

var requiresColor = (_requirements & ScriptableRenderPassInput.Color) != 0;
_injectedBeforeTransparents = injectionPoint <= InjectionPoint.BeforeRenderingTransparents;

if (requiresColor && !_injectedBeforeTransparents)
{
modifiedRequirements ^= ScriptableRenderPassInput.Color;
}

_fullScreenPass.ConfigureInput(modifiedRequirements);
}

/// <inheritdoc/>
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (!TrySetShadersAndMaterials())
{
Debug.LogErrorFormat("{0}.AddRenderPasses(): Missing material. {1} render pass will not be added.", GetType().Name, name);
return;
}

var passData = GetBlurPassData();

_fullScreenPass.Setup(passData, renderingData);

renderer.EnqueuePass(_fullScreenPass);
}

/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
_fullScreenPass?.Dispose();
CoreUtils.Destroy(_material);
}

private bool TrySetShadersAndMaterials()
{
if (shader == null)
{
shader = Shader.Find("Unified/KawaseBlur");
}

if (_material == null && shader != null)
_material = CoreUtils.CreateEngineMaterial(shader);
return _material != null;
}

private float CalculateScale() => scaleBlurWith switch
{
ScaleBlurWith.ScreenHeight => scale * (Screen.height / scaleReferenceSize),
ScaleBlurWith.ScreenWidth => scale * (Screen.width / scaleReferenceSize),
_ => scale
};

private BlurPassData GetBlurPassData()
{
return new BlurPassData()
{
EffectMaterial = _material,
Downsample = downsample,
Intensity = Intensity,
PassIndex = PassIndex,
Scale = CalculateScale(),
Iterations = iterations,
};
}
}
}
File renamed without changes.
Loading

0 comments on commit 937dbf7

Please sign in to comment.