Skip to content

Commit

Permalink
Do not disable renderer
Browse files Browse the repository at this point in the history
Fixes missing SH and missing shadows etc.
  • Loading branch information
daleeidd committed Aug 12, 2021
1 parent 5bd09e3 commit f44b954
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ public class ApplyUnderwaterFogToTransparent : MonoBehaviour
[SerializeField]
internal int _shaderPass;

bool _isEnabled;
public bool IsEnabled => _isEnabled;

void OnEnable()
{
if (TryGetComponent(out _renderer) && !s_Renderers.Contains(this))
{
// If the shader has other passes (like shadows) then this will stop them from working.
_renderer.enabled = false;
_isEnabled = _renderer.enabled;
s_Renderers.Add(this);
}
}
Expand All @@ -36,14 +39,14 @@ void LateUpdate()
var maxWaterHeight = OceanRenderer.Instance.SeaLevel + OceanRenderer.Instance.MaxVertDisplacement;
// TODO: Throws exceptions when renderer is disabled for ParticleSystem.
// TODO: Probably a better to check this.
_renderer.enabled = _renderer.bounds.ClosestPoint(transform.position + Vector3.down * 10000f).y > maxWaterHeight;
_isEnabled = _renderer.enabled && !(_renderer.bounds.ClosestPoint(transform.position + Vector3.down * 10000f).y > maxWaterHeight);
}

void OnDisable()
{
if (_renderer != null && s_Renderers.Contains(this))
{
_renderer.enabled = true;
_isEnabled = false;
s_Renderers.Remove(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void OnPreRenderUnderwaterEffect()
foreach (var input in ApplyUnderwaterFogToTransparent.s_Renderers)
{
// Disabled renderer means we control the rendering.
if (!input._renderer.enabled && GeometryUtility.TestPlanesAABB(_cameraFrustumPlanes, input._renderer.bounds))
if (input.IsEnabled && GeometryUtility.TestPlanesAABB(_cameraFrustumPlanes, input._renderer.bounds))
{
_registry.Add(Vector3.Distance(_camera.transform.position, input.transform.position), input);
}
Expand All @@ -135,7 +135,7 @@ void OnPreRenderUnderwaterEffect()

// NOTE:
// We only need to do this so we can set a white texture for when there is no texture. Otherwise,
// _MainTex is already setup.
// _MainTex is already setup. Could replace with a keyword instead.

// Set _MainTex so we can get the alpha channel for blending.
var texture = renderer.sharedMaterial.HasProperty("_MainTex") ? renderer.sharedMaterial.GetTexture("_MainTex") : null;
Expand All @@ -150,27 +150,25 @@ void OnPreRenderUnderwaterEffect()
}

// Add missing probe data.
LightProbeUtility.SetSHCoefficients(renderer.gameObject.transform.position, _materialPropertyBlock);
// LightProbeUtility.SetSHCoefficients(renderer.gameObject.transform.position, _materialPropertyBlock);
renderer.SetPropertyBlock(_materialPropertyBlock);

int shaderPass;

if (input._highQuality)
{
// Render into temporary render texture so the effect shader will have colour to work with. I could not
// work out how to use GPU blending to apply the underwater fog correctly.
CopyTexture(_underwaterEffectCommandBuffer, temporaryColorBuffer, _camera);
_underwaterEffectCommandBuffer.SetRenderTarget(temporaryColorBuffer, 0, CubemapFace.Unknown, -1);
shaderPass = 2;
}
else
{
_underwaterEffectCommandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
shaderPass = 1;
}

_underwaterEffectCommandBuffer.DrawRenderer(renderer, renderer.sharedMaterial, submeshIndex: 0, shaderPass: input._shaderPass);

int shaderPass = input._highQuality ? 2 : 1;

// Render the fog and apply to camera target.
_underwaterEffectCommandBuffer.SetRenderTarget(BuiltinRenderTextureType.CameraTarget, 0, CubemapFace.Unknown, -1);
_underwaterEffectCommandBuffer.DrawRenderer(renderer, _underwaterEffectMaterial.material, submeshIndex: 0, shaderPass);
Expand Down

0 comments on commit f44b954

Please sign in to comment.