Search Unity

Unity 5 Occlusion Culling Water Reflection flicker

Discussion in 'General Graphics' started by Kyle_L, Aug 13, 2015.

  1. Kyle_L

    Kyle_L

    Joined:
    Feb 8, 2015
    Posts:
    9
    So I've recently been having trouble with my Occlusion Culling disabling the objects when the player looks in the water's reflection, but when the player doesn't directly look at the water they partially re-enable.
    Any suggestions on how to fix this?



     
    Taurdenga likes this.
  2. FabrizioSpadaro

    FabrizioSpadaro

    Joined:
    Jul 10, 2012
    Posts:
    287
    For anyone out there, this is the fix.

    Replace this script into the water script

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. namespace UnityStandardAssets.Water
    7. {
    8.     [ExecuteInEditMode] // Make water live-update even when not in play mode
    9.     public class Water : MonoBehaviour
    10.     {
    11.         public enum WaterMode {
    12.             Simple = 0,
    13.             Reflective = 1,
    14.             Refractive = 2,
    15.         };
    16.         public WaterMode m_WaterMode = WaterMode.Refractive;
    17.         public bool m_DisablePixelLights = true;
    18.         public int m_TextureSize = 256;
    19.         public float m_ClipPlaneOffset = 0.07f;
    20.  
    21.         public bool m_TurnOffWaterOcclusion = true;
    22.  
    23.         public LayerMask m_ReflectLayers = -1;
    24.         public LayerMask m_RefractLayers = -1;
    25.  
    26.         private Dictionary<Camera, Camera> m_ReflectionCameras = new Dictionary<Camera, Camera>(); // Camera -> Camera table
    27.         private Dictionary<Camera, Camera> m_RefractionCameras = new Dictionary<Camera, Camera>(); // Camera -> Camera table
    28.  
    29.         private RenderTexture m_ReflectionTexture = null;
    30.         private RenderTexture m_RefractionTexture = null;
    31.         private WaterMode m_HardwareWaterSupport = WaterMode.Refractive;
    32.         private int m_OldReflectionTextureSize = 0;
    33.         private int m_OldRefractionTextureSize = 0;
    34.  
    35.         private static bool s_InsideWater = false;
    36.  
    37.         // This is called when it's known that the object will be rendered by some
    38.         // camera. We render reflections / refractions and do other updates here.
    39.         // Because the script executes in edit mode, reflections for the scene view
    40.         // camera will just work!
    41.         public void OnWillRenderObject()
    42.         {
    43.             if( !enabled || !GetComponent<Renderer>() || !GetComponent<Renderer>().sharedMaterial || !GetComponent<Renderer>().enabled )
    44.                 return;
    45.  
    46.             Camera cam = Camera.current;
    47.             if( !cam )
    48.                 return;
    49.  
    50.             // Safeguard from recursive water reflections.      
    51.             if( s_InsideWater )
    52.                 return;
    53.             s_InsideWater = true;
    54.  
    55.             // Actual water rendering mode depends on both the current setting AND
    56.             // the hardware support. There's no point in rendering refraction textures
    57.             // if they won't be visible in the end.
    58.             m_HardwareWaterSupport = FindHardwareWaterSupport();
    59.             WaterMode mode = GetWaterMode();
    60.  
    61.             Camera reflectionCamera, refractionCamera;
    62.             CreateWaterObjects( cam, out reflectionCamera, out refractionCamera );
    63.  
    64.             // find out the reflection plane: position and normal in world space
    65.             Vector3 pos = transform.position;
    66.             Vector3 normal = transform.up;
    67.  
    68.             // Optionally disable pixel lights for reflection/refraction
    69.             int oldPixelLightCount = QualitySettings.pixelLightCount;
    70.             if( m_DisablePixelLights )
    71.                 QualitySettings.pixelLightCount = 0;
    72.  
    73.             UpdateCameraModes( cam, reflectionCamera );
    74.             UpdateCameraModes( cam, refractionCamera );
    75.  
    76.             // Render reflection if needed
    77.             if( mode >= WaterMode.Reflective )
    78.             {
    79.                 // Reflect camera around reflection plane
    80.                 float d = -Vector3.Dot (normal, pos) - m_ClipPlaneOffset;
    81.                 Vector4 reflectionPlane = new Vector4 (normal.x, normal.y, normal.z, d);
    82.  
    83.                 Matrix4x4 reflection = Matrix4x4.zero;
    84.                 CalculateReflectionMatrix (ref reflection, reflectionPlane);
    85.                 Vector3 oldpos = cam.transform.position;
    86.                 Vector3 newpos = reflection.MultiplyPoint( oldpos );
    87.                 reflectionCamera.worldToCameraMatrix = cam.worldToCameraMatrix * reflection;
    88.  
    89.                 // Setup oblique projection matrix so that near plane is our reflection
    90.                 // plane. This way we clip everything below/above it for free.
    91.                 Vector4 clipPlane = CameraSpacePlane( reflectionCamera, pos, normal, 1.0f );
    92.                 reflectionCamera.projectionMatrix = cam.CalculateObliqueMatrix(clipPlane);
    93.  
    94.                 reflectionCamera.cullingMask = ~(1<<4) & m_ReflectLayers.value; // never render water layer
    95.                 reflectionCamera.targetTexture = m_ReflectionTexture;
    96.                 GL.SetRevertBackfacing (true);
    97.                 reflectionCamera.transform.position = newpos;
    98.                 Vector3 euler = cam.transform.eulerAngles;
    99.                 reflectionCamera.transform.eulerAngles = new Vector3(-euler.x, euler.y, euler.z);
    100.                 reflectionCamera.Render();
    101.                 reflectionCamera.transform.position = oldpos;
    102.                 GL.SetRevertBackfacing (false);
    103.                 GetComponent<Renderer>().sharedMaterial.SetTexture( "_ReflectionTex", m_ReflectionTexture );
    104.             }
    105.  
    106.             // Render refraction
    107.             if( mode >= WaterMode.Refractive )
    108.             {
    109.                 refractionCamera.worldToCameraMatrix = cam.worldToCameraMatrix;
    110.  
    111.                 // Setup oblique projection matrix so that near plane is our reflection
    112.                 // plane. This way we clip everything below/above it for free.
    113.                 Vector4 clipPlane = CameraSpacePlane( refractionCamera, pos, normal, -1.0f );
    114.                 refractionCamera.projectionMatrix = cam.CalculateObliqueMatrix(clipPlane);
    115.  
    116.                 refractionCamera.cullingMask = ~(1<<4) & m_RefractLayers.value; // never render water layer
    117.                 refractionCamera.targetTexture = m_RefractionTexture;
    118.                 refractionCamera.transform.position = cam.transform.position;
    119.                 refractionCamera.transform.rotation = cam.transform.rotation;
    120.                 refractionCamera.Render();
    121.                 GetComponent<Renderer>().sharedMaterial.SetTexture( "_RefractionTex", m_RefractionTexture );
    122.             }
    123.  
    124.             // Restore pixel light count
    125.             if( m_DisablePixelLights )
    126.                 QualitySettings.pixelLightCount = oldPixelLightCount;
    127.  
    128.             // Setup shader keywords based on water mode
    129.             switch( mode )
    130.             {
    131.             case WaterMode.Simple:
    132.                 Shader.EnableKeyword( "WATER_SIMPLE" );
    133.                 Shader.DisableKeyword( "WATER_REFLECTIVE" );
    134.                 Shader.DisableKeyword( "WATER_REFRACTIVE" );
    135.                 break;
    136.             case WaterMode.Reflective:
    137.                 Shader.DisableKeyword( "WATER_SIMPLE" );
    138.                 Shader.EnableKeyword( "WATER_REFLECTIVE" );
    139.                 Shader.DisableKeyword( "WATER_REFRACTIVE" );
    140.                 break;
    141.             case WaterMode.Refractive:
    142.                 Shader.DisableKeyword( "WATER_SIMPLE" );
    143.                 Shader.DisableKeyword( "WATER_REFLECTIVE" );
    144.                 Shader.EnableKeyword( "WATER_REFRACTIVE" );
    145.                 break;
    146.             }
    147.  
    148.             s_InsideWater = false;
    149.         }
    150.  
    151.  
    152.         // Cleanup all the objects we possibly have created
    153.         void OnDisable()
    154.         {
    155.             if( m_ReflectionTexture ) {
    156.                 DestroyImmediate( m_ReflectionTexture );
    157.                 m_ReflectionTexture = null;
    158.             }
    159.             if( m_RefractionTexture ) {
    160.                 DestroyImmediate( m_RefractionTexture );
    161.                 m_RefractionTexture = null;
    162.             }
    163.             foreach (KeyValuePair<Camera, Camera> kvp in m_ReflectionCameras)
    164.                 DestroyImmediate( (kvp.Value).gameObject );
    165.             m_ReflectionCameras.Clear();
    166.             foreach (KeyValuePair<Camera, Camera> kvp in m_RefractionCameras)
    167.                 DestroyImmediate( (kvp.Value).gameObject );
    168.             m_RefractionCameras.Clear();
    169.         }
    170.  
    171.  
    172.         // This just sets up some matrices in the material; for really
    173.         // old cards to make water texture scroll.
    174.         void Update()
    175.         {
    176.             if( !GetComponent<Renderer>() )
    177.                 return;
    178.             Material mat = GetComponent<Renderer>().sharedMaterial;
    179.             if( !mat )
    180.                 return;
    181.  
    182.             Vector4 waveSpeed = mat.GetVector( "WaveSpeed" );
    183.             float waveScale = mat.GetFloat( "_WaveScale" );
    184.             Vector4 waveScale4 = new Vector4(waveScale, waveScale, waveScale * 0.4f, waveScale * 0.45f);
    185.  
    186.             // Time since level load, and do intermediate calculations with doubles
    187.             double t = Time.timeSinceLevelLoad / 20.0;
    188.             Vector4 offsetClamped = new Vector4(
    189.                 (float)System.Math.IEEERemainder(waveSpeed.x * waveScale4.x * t, 1.0),
    190.                 (float)System.Math.IEEERemainder(waveSpeed.y * waveScale4.y * t, 1.0),
    191.                 (float)System.Math.IEEERemainder(waveSpeed.z * waveScale4.z * t, 1.0),
    192.                 (float)System.Math.IEEERemainder(waveSpeed.w * waveScale4.w * t, 1.0)
    193.             );
    194.  
    195.             mat.SetVector( "_WaveOffset", offsetClamped );
    196.             mat.SetVector( "_WaveScale4", waveScale4 );
    197.  
    198.             Vector3 waterSize = GetComponent<Renderer>().bounds.size;      
    199.             Vector3 scale = new Vector3( waterSize.x*waveScale4.x, waterSize.z*waveScale4.y, 1 );
    200.             Matrix4x4 scrollMatrix = Matrix4x4.TRS( new Vector3(offsetClamped.x,offsetClamped.y,0), Quaternion.identity, scale );
    201.             mat.SetMatrix( "_WaveMatrix", scrollMatrix );
    202.  
    203.             scale = new Vector3( waterSize.x*waveScale4.z, waterSize.z*waveScale4.w, 1 );
    204.             scrollMatrix = Matrix4x4.TRS( new Vector3(offsetClamped.z,offsetClamped.w,0), Quaternion.identity, scale );
    205.             mat.SetMatrix( "_WaveMatrix2", scrollMatrix );
    206.         }
    207.  
    208.         private void UpdateCameraModes( Camera src, Camera dest )
    209.         {
    210.             if( dest == null )
    211.                 return;
    212.             // set water camera to clear the same way as current camera
    213.             dest.clearFlags = src.clearFlags;
    214.             dest.backgroundColor = src.backgroundColor;      
    215.             if( src.clearFlags == CameraClearFlags.Skybox )
    216.             {
    217.                 Skybox sky = src.GetComponent(typeof(Skybox)) as Skybox;
    218.                 Skybox mysky = dest.GetComponent(typeof(Skybox)) as Skybox;
    219.                 if( !sky || !sky.material )
    220.                 {
    221.                     mysky.enabled = false;
    222.                 }
    223.                 else
    224.                 {
    225.                     mysky.enabled = true;
    226.                     mysky.material = sky.material;
    227.                 }
    228.             }
    229.             // update other values to match current camera.
    230.             // even if we are supplying custom camera&projection matrices,
    231.             // some of values are used elsewhere (e.g. skybox uses far plane)
    232.             dest.farClipPlane = src.farClipPlane;
    233.             dest.nearClipPlane = src.nearClipPlane;
    234.             dest.orthographic = src.orthographic;
    235.             dest.fieldOfView = src.fieldOfView;
    236.             dest.aspect = src.aspect;
    237.             dest.orthographicSize = src.orthographicSize;
    238.         }
    239.  
    240.         // On-demand create any objects we need for water
    241.         private void CreateWaterObjects( Camera currentCamera, out Camera reflectionCamera, out Camera refractionCamera )
    242.         {
    243.             WaterMode mode = GetWaterMode();
    244.  
    245.             reflectionCamera = null;
    246.             refractionCamera = null;
    247.  
    248.             if( mode >= WaterMode.Reflective )
    249.             {
    250.                 // Reflection render texture
    251.                 if( !m_ReflectionTexture || m_OldReflectionTextureSize != m_TextureSize )
    252.                 {
    253.                     if( m_ReflectionTexture )
    254.                         DestroyImmediate( m_ReflectionTexture );
    255.                     m_ReflectionTexture = new RenderTexture( m_TextureSize, m_TextureSize, 16 );
    256.                     m_ReflectionTexture.name = "__WaterReflection" + GetInstanceID();
    257.                     m_ReflectionTexture.isPowerOfTwo = true;
    258.                     m_ReflectionTexture.hideFlags = HideFlags.DontSave;
    259.                     m_OldReflectionTextureSize = m_TextureSize;
    260.  
    261.                 }
    262.  
    263.                 // Camera for reflection
    264.                 m_ReflectionCameras.TryGetValue(currentCamera, out reflectionCamera);
    265.                 if (!reflectionCamera) // catch both not-in-dictionary and in-dictionary-but-deleted-GO
    266.                 {
    267.                     GameObject go = new GameObject( "Water Refl Camera id" + GetInstanceID() + " for " + currentCamera.GetInstanceID(), typeof(Camera), typeof(Skybox) );
    268.                     reflectionCamera = go.GetComponent<Camera>();
    269.                     reflectionCamera.enabled = false;
    270.                     reflectionCamera.transform.position = transform.position;
    271.                     reflectionCamera.transform.rotation = transform.rotation;
    272.                     reflectionCamera.gameObject.AddComponent<FlareLayer>();
    273.                     go.hideFlags = HideFlags.HideAndDontSave;
    274.                     m_ReflectionCameras[currentCamera] = reflectionCamera;
    275.  
    276.                     if (m_TurnOffWaterOcclusion)
    277.                         reflectionCamera.useOcclusionCulling = false;
    278.                 }
    279.             }
    280.  
    281.             if( mode >= WaterMode.Refractive )
    282.             {
    283.                 // Refraction render texture
    284.                 if( !m_RefractionTexture || m_OldRefractionTextureSize != m_TextureSize )
    285.                 {
    286.                     if( m_RefractionTexture )
    287.                         DestroyImmediate( m_RefractionTexture );
    288.                     m_RefractionTexture = new RenderTexture( m_TextureSize, m_TextureSize, 16 );
    289.                     m_RefractionTexture.name = "__WaterRefraction" + GetInstanceID();
    290.                     m_RefractionTexture.isPowerOfTwo = true;
    291.                     m_RefractionTexture.hideFlags = HideFlags.DontSave;
    292.                     m_OldRefractionTextureSize = m_TextureSize;
    293.                 }
    294.  
    295.                 // Camera for refraction
    296.                 m_RefractionCameras.TryGetValue(currentCamera, out refractionCamera);
    297.                 if (!refractionCamera) // catch both not-in-dictionary and in-dictionary-but-deleted-GO
    298.                 {
    299.                     GameObject go = new GameObject( "Water Refr Camera id" + GetInstanceID() + " for " + currentCamera.GetInstanceID(), typeof(Camera), typeof(Skybox) );
    300.                     refractionCamera = go.GetComponent<Camera>();
    301.                     refractionCamera.enabled = false;
    302.                     refractionCamera.transform.position = transform.position;
    303.                     refractionCamera.transform.rotation = transform.rotation;
    304.                     refractionCamera.gameObject.AddComponent<FlareLayer>();
    305.                     go.hideFlags = HideFlags.HideAndDontSave;
    306.                     m_RefractionCameras[currentCamera] = refractionCamera;
    307.  
    308.                     if (m_TurnOffWaterOcclusion)
    309.                         refractionCamera.useOcclusionCulling = false;
    310.                 }
    311.             }
    312.         }
    313.  
    314.         private WaterMode GetWaterMode()
    315.         {
    316.             if( m_HardwareWaterSupport < m_WaterMode )
    317.                 return m_HardwareWaterSupport;
    318.             else
    319.                 return m_WaterMode;
    320.         }
    321.  
    322.         private WaterMode FindHardwareWaterSupport()
    323.         {
    324.             if( !SystemInfo.supportsRenderTextures || !GetComponent<Renderer>() )
    325.                 return WaterMode.Simple;
    326.  
    327.             Material mat = GetComponent<Renderer>().sharedMaterial;
    328.             if( !mat )
    329.                 return WaterMode.Simple;
    330.  
    331.             string mode = mat.GetTag("WATERMODE", false);
    332.             if( mode == "Refractive" )
    333.                 return WaterMode.Refractive;
    334.             if( mode == "Reflective" )
    335.                 return WaterMode.Reflective;
    336.  
    337.             return WaterMode.Simple;
    338.         }
    339.  
    340.         // Extended sign: returns -1, 0 or 1 based on sign of a
    341.         private static float sgn(float a)
    342.         {
    343.             if (a > 0.0f) return 1.0f;
    344.             if (a < 0.0f) return -1.0f;
    345.             return 0.0f;
    346.         }
    347.  
    348.         // Given position/normal of the plane, calculates plane in camera space.
    349.         private Vector4 CameraSpacePlane (Camera cam, Vector3 pos, Vector3 normal, float sideSign)
    350.         {
    351.             Vector3 offsetPos = pos + normal * m_ClipPlaneOffset;
    352.             Matrix4x4 m = cam.worldToCameraMatrix;
    353.             Vector3 cpos = m.MultiplyPoint( offsetPos );
    354.             Vector3 cnormal = m.MultiplyVector( normal ).normalized * sideSign;
    355.             return new Vector4( cnormal.x, cnormal.y, cnormal.z, -Vector3.Dot(cpos,cnormal) );
    356.         }
    357.  
    358.         // Calculates reflection matrix around the given plane
    359.         private static void CalculateReflectionMatrix (ref Matrix4x4 reflectionMat, Vector4 plane)
    360.         {
    361.             reflectionMat.m00 = (1F - 2F*plane[0]*plane[0]);
    362.             reflectionMat.m01 = (   - 2F*plane[0]*plane[1]);
    363.             reflectionMat.m02 = (   - 2F*plane[0]*plane[2]);
    364.             reflectionMat.m03 = (   - 2F*plane[3]*plane[0]);
    365.  
    366.             reflectionMat.m10 = (   - 2F*plane[1]*plane[0]);
    367.             reflectionMat.m11 = (1F - 2F*plane[1]*plane[1]);
    368.             reflectionMat.m12 = (   - 2F*plane[1]*plane[2]);
    369.             reflectionMat.m13 = (   - 2F*plane[3]*plane[1]);
    370.  
    371.             reflectionMat.m20 = (   - 2F*plane[2]*plane[0]);
    372.             reflectionMat.m21 = (   - 2F*plane[2]*plane[1]);
    373.             reflectionMat.m22 = (1F - 2F*plane[2]*plane[2]);
    374.             reflectionMat.m23 = (   - 2F*plane[3]*plane[2]);
    375.  
    376.             reflectionMat.m30 = 0F;
    377.             reflectionMat.m31 = 0F;
    378.             reflectionMat.m32 = 0F;
    379.             reflectionMat.m33 = 1F;
    380.         }
    381.  
    382.     }
    383. }
    384. }
     
    Last edited: Jun 6, 2016
  3. jeromeWork

    jeromeWork

    Joined:
    Sep 1, 2015
    Posts:
    429
    Awesome share thank you!
     
  4. ManuGamesDev

    ManuGamesDev

    Joined:
    Aug 30, 2018
    Posts:
    1
    This is the only useful answer to this topic in the whole internet!
     
    Taurdenga and FabrizioSpadaro like this.
  5. Stamblew

    Stamblew

    Joined:
    Sep 3, 2014
    Posts:
    22
    You, my friend, are a hero!
     
    FabrizioSpadaro likes this.
  6. flipper2017

    flipper2017

    Joined:
    Feb 25, 2017
    Posts:
    3
    you're a god :)
     
    FabrizioSpadaro likes this.