Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 41 additions & 24 deletions Assets/Post-process LWRP AO/Runtime/LWRPAO.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Reflection;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;

[Serializable]
[PostProcess(typeof(LWRPAORenderer), PostProcessEvent.BeforeTransparent, "Custom/LWRP Ambient Occlusion")]
[PostProcess(typeof(LWRPAORenderer), PostProcessEvent.BeforeStack, "Custom/LWRP Ambient Occlusion")]
public sealed class LWRPAO : PostProcessEffectSettings
{
}
Expand All @@ -24,33 +23,48 @@ private enum Pass
CompositionForward,
DebugOverlay
}

private bool IsInitialized;

private Type AmbientOcclusionRenderer;
private Type MultiScaleVO;

private MethodInfo CastRendererMethod;
private MethodInfo PreparePropertySheetMethod;
private MethodInfo PreparePropertySheetMethod;
private MethodInfo GenerateAOMapMethod;
private MethodInfo SetResourcesMethod;
private MethodInfo GetMethod;

private PostProcessResources PostProcessResources;
private PropertySheet PropertySheet;
private RenderTexture AmbientOnlyAO;
private RenderTargetIdentifier AmbientOnlyAOIdentifier;

private Assembly PostProcessAssembly = Assembly.GetAssembly(typeof(PostProcessVolume));


private void ResolveCastRendererMethod()
{
if (CastRendererMethod != null && PreparePropertySheetMethod != null)
if (IsInitialized)
{
return;
}
{
var m = typeof(PostProcessBundle).GetMethod("CastRenderer", BindingFlags.Instance | BindingFlags.NonPublic);
var method = m != null ? m.MakeGenericMethod(typeof(AmbientOcclusionRenderer)) : null;

AmbientOcclusionRenderer = PostProcessAssembly.GetType("UnityEngine.Rendering.PostProcessing.AmbientOcclusionRenderer");
MultiScaleVO = PostProcessAssembly.GetType("UnityEngine.Rendering.PostProcessing.MultiScaleVO");

GenerateAOMapMethod = MultiScaleVO.GetMethod("GenerateAOMap", BindingFlags.Public | BindingFlags.Instance);
SetResourcesMethod = MultiScaleVO.GetMethod("SetResources", BindingFlags.Public | BindingFlags.Instance);
GetMethod = AmbientOcclusionRenderer.GetMethod("Get", BindingFlags.Public | BindingFlags.Instance);

CastRendererMethod = method;
}
{
var m = typeof(MultiScaleVO).GetMethod("PreparePropertySheet", BindingFlags.Instance | BindingFlags.NonPublic);
CastRendererMethod = typeof(PostProcessBundle).GetMethod("CastRenderer", BindingFlags.Instance | BindingFlags.NonPublic)
.MakeGenericMethod(AmbientOcclusionRenderer);

PreparePropertySheetMethod = m;
}
PreparePropertySheetMethod = MultiScaleVO.GetMethod("PreparePropertySheet", BindingFlags.Instance | BindingFlags.NonPublic);

IsInitialized = true;
}

private void SetResources(PostProcessResources resources)
{
PostProcessResources = resources;
Expand All @@ -77,6 +91,8 @@ private void CheckAOTexture(PostProcessRenderContext context)
enableRandomWrite = true
};
AmbientOnlyAO.Create();

AmbientOnlyAOIdentifier = new RenderTargetIdentifier(AmbientOnlyAO);
}
}

Expand All @@ -87,7 +103,7 @@ private void PushDebug(PostProcessRenderContext context)
context.PushDebugOverlay(context.command, AmbientOnlyAO, PropertySheet, (int)Pass.DebugOverlay);
}
}

public override void Render(PostProcessRenderContext ctx)
{
ResolveCastRendererMethod();
Expand All @@ -100,15 +116,14 @@ public override void Render(PostProcessRenderContext ctx)
var aoSettings = (AmbientOcclusion) aoBundle.settings;
if (aoSettings.IsEnabledAndSupported(ctx) && CastRendererMethod != null)
{
var aoRenderer = (AmbientOcclusionRenderer) CastRendererMethod.Invoke(aoBundle, null);
var aoMethod = aoRenderer.Get();
if (aoMethod is MultiScaleVO)
var aoRenderer = CastRendererMethod.Invoke(aoBundle, null);
var aoMethod = GetMethod.Invoke(aoRenderer, new object[0]);

if (aoMethod.GetType() == MultiScaleVO)
{
var msvo = (MultiScaleVO) aoMethod;

cmd.BeginSample("Ambient Occlusion");
msvo.SetResources(ctx.resources);
PreparePropertySheetMethod.Invoke(msvo, new object[] { ctx });
SetResourcesMethod.Invoke(aoMethod, new object[] {ctx.resources});
PreparePropertySheetMethod.Invoke(aoMethod, new object[] { ctx });

SetResources(ctx.resources);
PreparePropertySheet(ctx, aoSettings);
Expand All @@ -124,11 +139,13 @@ public override void Render(PostProcessRenderContext ctx)
new Vector3(RenderSettings.fogDensity, RenderSettings.fogStartDistance, RenderSettings.fogEndDistance)
);
}

msvo.GenerateAOMap(cmd, cam, AmbientOnlyAO, null, false);
GenerateAOMapMethod.Invoke(aoMethod, new object[] {cmd, cam, AmbientOnlyAOIdentifier, null, false, false});
PushDebug(ctx);
cmd.SetGlobalTexture(ShaderIDsMSVOcclusionTexture, AmbientOnlyAO);

cmd.BlitFullscreenTriangle(ctx.source, ctx.destination);

cmd.BlitFullscreenTriangle(BuiltinRenderTextureType.None, ctx.destination, PropertySheet, (int)Pass.CompositionForward);
cmd.EndSample("Ambient Occlusion");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
{
"name": "Post-processLWRPAO",
"references": [
"com.unity.render-pipelines.core.Runtime",
"com.unity.postprocessing.Runtime"
]
}
{
"name": "Post-processLWRPAO",
"references": [
"Unity.RenderPipelines.Core.Runtime",
"Unity.Postprocessing.Runtime"
],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
}
7 changes: 7 additions & 0 deletions Assets/Post-process LWRP AO/package.json.meta

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

32 changes: 28 additions & 4 deletions Assets/Test/Lightweight/Cube Lightweight.mat
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_PrefabParentObject: {fileID: 0}
m_PrefabInternal: {fileID: 0}
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Cube Lightweight
m_Shader: {fileID: 4800000, guid: 933532a4fcc9baf4fa0491de14d08ed7, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
m_CustomRenderQueue: 2050
stringTagMap:
RenderType: Opaque
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BaseMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
Expand Down Expand Up @@ -66,13 +72,17 @@ Material:
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DstBlend: 0
- _EnvironmentReflections: 1
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _QueueOffset: 0
- _ReceiveShadows: 1
- _Smoothness: 0.5
- _SmoothnessTextureChannel: 0
- _SpecularHighlights: 1
- _SrcBlend: 1
Expand All @@ -81,6 +91,20 @@ Material:
- _WorkflowMode: 1
- _ZWrite: 1
m_Colors:
- _BaseColor: {r: 1, g: 1, b: 1, a: 1}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1}
- _SpecColor: {r: 0.19999996, g: 0.19999996, b: 0.19999996, a: 1}
--- !u!114 &200326388708793063
MonoBehaviour:
m_ObjectHideFlags: 11
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: d0353a89b1f911e48b9e16bdc9f2e058, type: 3}
m_Name:
m_EditorClassIdentifier:
version: 1
Loading