Skip to content

Commit

Permalink
Conserve motion vectors between cameras, fix TAA on near camera blurr…
Browse files Browse the repository at this point in the history
…ing scaled planets
  • Loading branch information
LGhassen committed Jul 14, 2024
1 parent e68ac7e commit a70974d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
17 changes: 15 additions & 2 deletions scatterer/Effects/AntiAliasing/TemporalAntiAliasing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ enum Pass {SolverDilate, SolverNoDilate}
DepthTextureMode originalDepthTextureMode;
public bool checkOceanDepth = false;
public bool jitterTransparencies = false;
public bool resetMotionVectors = true;

private static CameraEvent TAACameraEvent = CameraEvent.AfterForwardAlpha; // BeforeImageEffects doesn't work well
private static int jitterProperty = Shader.PropertyToID("_Jitter");
private static int keepPreviousMotionVectorsProperty = Shader.PropertyToID("TAA_KeepPreviousMotionVectors");

private static CameraEvent TAACameraEvent = CameraEvent.AfterForwardAlpha; // BeforeImageEffects doesn't work well

bool firstFrame = true;

Expand Down Expand Up @@ -129,7 +133,7 @@ public void ConfigureJitteredProjectionMatrix()
targetCamera.projectionMatrix = GetJitteredProjectionMatrix(targetCamera);
targetCamera.useJitteredProjectionMatrixForTransparentRendering = jitterTransparencies;

temporalAAMaterial.SetVector("_Jitter", jitter); // TODO: shader properties
temporalAAMaterial.SetVector(jitterProperty, jitter);
}

RenderTexture CheckHistory(int id, CommandBuffer cmd)
Expand Down Expand Up @@ -221,6 +225,15 @@ public void OnPreCull()

m_ResetHistory = false;
firstFrame = false;

if (resetMotionVectors)
{
Shader.SetGlobalInt(keepPreviousMotionVectorsProperty, 0);
}
else
{
Shader.SetGlobalInt(keepPreviousMotionVectorsProperty, 1);
}
}
else
{
Expand Down
17 changes: 14 additions & 3 deletions scatterer/Scatterer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,27 @@ void Init()

if (mainSettings.useTemporalAntiAliasing)
{
TemporalAntiAliasing nearAA, farAA, scaledAA;
ShaderReplacer.Instance.LoadedShaders.TryGetValue("Scatterer/Internal-MotionVectors", out Shader customMotionVectorsShader);

if (customMotionVectorsShader != null)
{
GraphicsSettings.SetShaderMode(BuiltinShaderType.MotionVectors, BuiltinShaderMode.UseCustom);
GraphicsSettings.SetCustomShader(BuiltinShaderType.MotionVectors, customMotionVectorsShader);
}

TemporalAntiAliasing nearAA, farAA, scaledAA;

nearAA = nearCamera.gameObject.AddComponent<TemporalAntiAliasing>();
nearAA.checkOceanDepth = mainSettings.useOceanShaders;
nearAA.resetMotionVectors = false;
antiAliasingScripts.Add(nearAA);

if (!unifiedCameraMode && farCamera)
{
farAA = farCamera.gameObject.AddComponent<TemporalAntiAliasing>();
farAA.checkOceanDepth = mainSettings.useOceanShaders;
antiAliasingScripts.Add(farAA);
farAA.resetMotionVectors = false;
antiAliasingScripts.Add(farAA);
}

// Cap the ridiculous clip plane (1e9) of the scaledCamera otherwise we get invalid motion vectors
Expand Down Expand Up @@ -639,7 +649,8 @@ public void AddTAAToInternalCamera(CameraManager.CameraMode cameraMode)
if(internalTAA == null)
{
internalTAA = internalCamera.gameObject.AddComponent<TemporalAntiAliasing>();
antiAliasingScripts.Add(internalTAA);
internalTAA.resetMotionVectors = false;
antiAliasingScripts.Add(internalTAA);
}
}
}
Expand Down

0 comments on commit a70974d

Please sign in to comment.