Skip to content

Commit

Permalink
fix flickering on volumetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
LGhassen committed Aug 25, 2020
1 parent 38e4311 commit 5e27dd8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Assets/Shaders/CompositeDeferredClouds.shader
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Shader "EVE/CompositeDeferredClouds" {
uniform sampler2D EVEDownscaledDepth;
float4 EVEDownscaledDepth_TexelSize;

uniform float rendererEnabled;

struct appdata_t {
float4 vertex : POSITION;
Expand All @@ -45,6 +46,9 @@ Shader "EVE/CompositeDeferredClouds" {
#else
o.pos = float4(v.vertex.x, v.vertex.y *_ProjectionParams.x, 1.0 , 1.0);
#endif

o.pos = rendererEnabled > 0.0 ? o.pos : float4(2.0, 2.0, 2.0, 1.0);

o.uv = ComputeScreenPos(o.pos);

return o;
Expand Down
18 changes: 4 additions & 14 deletions Atmosphere/DeferredVolumetricCloudsRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public static DeferredRendererToScreen DeferredRendererToScreen
private RenderTexture targetRT; //target RT, 1/4 screen res to save performance
private RenderTexture downscaledDepthRT;
Material downscaleDepthMaterial;
CommandBuffer clearTextureBuffer;

// pairs of volumetric clouds renderers and their materials, sorted by distance, for rendering farthest to closest
SortedList<float, Tuple<Renderer, Material>> renderersAdded = new SortedList<float, Tuple<Renderer, Material>>();
Expand Down Expand Up @@ -87,10 +86,6 @@ public void Initialize()

downscaleDepthMaterial = new Material(ShaderLoaderClass.FindShader("EVE/DownscaleDepth"));

clearTextureBuffer = new CommandBuffer();
clearTextureBuffer.SetRenderTarget(targetRT);
clearTextureBuffer.ClearRenderTarget(false, true, Color.black);

isInitialized = true;
}
}
Expand All @@ -101,8 +96,6 @@ public void EnableForThisFrame(MeshRenderer mr, Material mat)
{
if (!renderingEnabled)
{
targetCamera.RemoveCommandBuffer (CameraEvent.AfterForwardOpaque, clearTextureBuffer);

CommandBuffer cb = new CommandBuffer();

DeferredRendererToScreen.SetActive(true);
Expand All @@ -124,17 +117,15 @@ public void EnableForThisFrame(MeshRenderer mr, Material mat)

renderersAdded.Add(mr.gameObject.transform.position.magnitude, new Tuple<Renderer, Material>(mr, mat));


renderingEnabled = true;
}
}

void OnPreRender()
{
//sort cloud layers by decreasing distance to camera and render them farthest to closest
if (renderingEnabled)
{
foreach (var elt in renderersAdded.Reverse())
foreach (var elt in renderersAdded.Reverse()) //sort cloud layers by decreasing distance to camera and render them farthest to closest
{
CommandBuffer cb = new CommandBuffer();

Expand Down Expand Up @@ -165,9 +156,7 @@ void OnPostRender()
commandBuffersAdded.Clear();
renderersAdded.Clear();

//add a clear texture buffer, remove it when we restart rendering
targetCamera.AddCommandBuffer(CameraEvent.AfterForwardOpaque, clearTextureBuffer);

DeferredRendererToScreen.SetActive(false);
renderingEnabled = false;
}
}
Expand Down Expand Up @@ -227,6 +216,7 @@ public void Init()
shadowMR.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
shadowMR.receiveShadows = false;
shadowMR.enabled = true;
material.SetFloat(ShaderProperties.rendererEnabled_PROPERTY, 0f);

gameObject.layer = (int)Tools.Layer.Local;
}
Expand All @@ -243,7 +233,7 @@ public void SetDepthTexture(RenderTexture RT)

public void SetActive(bool active)
{
shadowMR.enabled = active;
material.SetFloat(ShaderProperties.rendererEnabled_PROPERTY, active ? 1f : 0f);
}

public void OnWillRenderObject()
Expand Down
5 changes: 5 additions & 0 deletions Utils/ShaderProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ public class ShaderProperties : MonoBehaviour
public static int _UniveralTime_PROPERTY { get { return _UniversalTime; } }
private static int _UniversalTime;

public static int rendererEnabled_PROPERTY { get { return _rendererEnabled; } }
private static int _rendererEnabled;

private void Awake()
{
_PosRotation = Shader.PropertyToID("_PosRotation");
Expand All @@ -68,6 +71,8 @@ private void Awake()
_ShadowBodies = Shader.PropertyToID("_ShadowBodies");

_UniversalTime = Shader.PropertyToID("_UniversalTime");

_rendererEnabled = Shader.PropertyToID("rendererEnabled");
}
}
}

0 comments on commit 5e27dd8

Please sign in to comment.