Skip to content

Commit

Permalink
Support multiple cameras for UR
Browse files Browse the repository at this point in the history
Also do not disable UR if fails validation in edit mode.
  • Loading branch information
daleeidd committed Nov 10, 2022
1 parent 343d1f1 commit 9336989
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 69 deletions.
2 changes: 1 addition & 1 deletion crest/Assets/Crest/Crest/Scripts/Helpers/XRHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public static void Update(Camera camera)
SubsystemManager.GetInstances(_displayList);
#endif

if (!IsRunning || !IsSinglePass)
if (!camera.stereoEnabled || !IsSinglePass)
{
return;
}
Expand Down
29 changes: 27 additions & 2 deletions crest/Assets/Crest/Crest/Scripts/OceanRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ public Camera ViewCamera
}
}

public Camera ViewCameraExcludingSceneCamera
{
get
{
if (_camera != null)
{
return _camera;
}

return Camera.main;
}
set
{
_camera = value;
}
}

[Tooltip("The height where detail is focused is smoothed to avoid popping which is undesireable after a teleport. Threshold is in Unity units."), SerializeField]
float _teleportThreshold = 10f;
float _teleportTimerForHeightQueries = 0f;
Expand Down Expand Up @@ -612,6 +629,8 @@ void OnEnable()

Camera.onPreRender -= OnPreRenderCamera;
Camera.onPreRender += OnPreRenderCamera;
Camera.onPostRender -= OnPostRenderCamera;
Camera.onPostRender += OnPostRenderCamera;
}

void OnPreRenderCamera(Camera camera)
Expand All @@ -627,6 +646,12 @@ void OnPreRenderCamera(Camera camera)
}
}

void OnPostRenderCamera(Camera camera)
{
// Clean up for subsequent cameras or water could disappear.
UnderwaterRenderer.DisableOceanMaskKeywords();
}

internal void Rebuild()
{
enabled = false;
Expand Down Expand Up @@ -1043,8 +1068,7 @@ void WritePerFrameMaterialParams()
break;
case SurfaceSelfIntersectionFixMode.Automatic:
// Skip if UnderwaterRenderer is not full-screen (ocean will be clipped).
var skip = UnderwaterRenderer.Instance != null && UnderwaterRenderer.Instance.IsActive &&
UnderwaterRenderer.Instance._mode != UnderwaterRenderer.Mode.FullScreen;
var skip = UnderwaterRenderer.SkipSurfaceSelfIntersectionFixMode;
value = skip ? 0f : height < -2f ? 1f : height > 2f ? -1f : 0f;
break;
}
Expand Down Expand Up @@ -1435,6 +1459,7 @@ private void CleanUp()
_bufPerCascadeInstanceDataSource?.Dispose();

Camera.onPreRender -= OnPreRenderCamera;
Camera.onPostRender -= OnPostRenderCamera;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ void OnPreRenderUnderwaterEffect()
temporaryColorBuffer.name = "_CrestCameraColorTexture";

UpdatePostProcessMaterial(
this,
_mode,
_camera,
_underwaterEffectMaterial,
Expand Down Expand Up @@ -283,6 +284,7 @@ internal static void UpdateGlobals(Material oceanMaterial)
}

internal static void UpdatePostProcessMaterial(
UnderwaterRenderer renderer,
Mode mode,
Camera camera,
PropertyWrapperMaterial underwaterPostProcessMaterialWrapper,
Expand Down Expand Up @@ -340,14 +342,26 @@ bool setGlobalShaderData
}
}

Vector3 depthFogDensity;

if (!IsCullable)
{
depthFogDensity = dominantWaterBody == null
? OceanRenderer.Instance.OceanMaterial.GetVector(OceanRenderer.ShaderIDs.s_DepthFogDensity) * renderer._depthFogDensityFactor
: dominantWaterBody._overrideMaterial.GetVector(OceanRenderer.ShaderIDs.s_DepthFogDensity) * renderer._depthFogDensityFactor;
}
else
{
depthFogDensity = dominantWaterBody == null
? OceanRenderer.Instance.UnderwaterDepthFogDensity : dominantWaterBody.UnderwaterDepthFogDensity;
}

if (setGlobalShaderData)
{
Shader.SetGlobalVector(ShaderIDs.s_CrestDepthFogDensity, dominantWaterBody == null
? OceanRenderer.Instance.UnderwaterDepthFogDensity : dominantWaterBody.UnderwaterDepthFogDensity);
Shader.SetGlobalVector(ShaderIDs.s_CrestDepthFogDensity, depthFogDensity);
}

underwaterPostProcessMaterial.SetVector(OceanRenderer.ShaderIDs.s_DepthFogDensity, dominantWaterBody == null
? OceanRenderer.Instance.UnderwaterDepthFogDensity : dominantWaterBody.UnderwaterDepthFogDensity);
underwaterPostProcessMaterial.SetVector(OceanRenderer.ShaderIDs.s_DepthFogDensity, depthFogDensity);
}

// Enabling/disabling keywords each frame don't seem to have large measurable overhead
Expand All @@ -367,10 +381,11 @@ bool setGlobalShaderData
if (mode == Mode.FullScreen)
{
float seaLevel = OceanRenderer.Instance.SeaLevel;
var heightAboveWater = renderer != null ? renderer.HeightAboveWater : OceanRenderer.Instance.ViewerHeightAboveWater;

// We don't both setting the horizon value if we know we are going to be having to apply the effect
// full-screen anyway.
var forceFullShader = OceanRenderer.Instance.ViewerHeightAboveWater < -2f;
var forceFullShader = heightAboveWater < -2f;
if (!forceFullShader)
{
float maxOceanVerticalDisplacement = OceanRenderer.Instance.MaxVertDisplacement * 0.5f;
Expand Down
Loading

0 comments on commit 9336989

Please sign in to comment.