Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Water and LayerCullDistance

Discussion in 'General Graphics' started by Maxi77, Apr 16, 2015.

  1. Maxi77

    Maxi77

    Joined:
    Mar 3, 2015
    Posts:
    15
    Hi,

    Im using Water4 in my scene with both reflection and refraction. The Water plane is covering a large area, and that is generating alot of drawcalls, when there are many objects in the cameras frustrum. My goal is to improve the performance by reducing the amount of objects that are reflected and refracted.

    First af few detalis:
    Unity: 5.0.1f1
    Script: Water.cs
    Water Mode: Refractive
    Texture Size: 512

    I know that i can set what layers are reflected/refracted, and im already already filtering objects that way.

    Problem:
    There are a lot of smaller objects in the scene(stones, trees and other kind og details), that I have put on specific layer. I have set LayerCullDistance on the main camera, so that the details in the specific layer are culled at way before the farClipPlane. The problem is that the Water still reflects the details, even if the main camera is not rendering them.

    I have tried to set the LayerCullDIstance for the reflection camera, but that does not Work well. If i set af specific distance for a given layer, then the objects in that layer does not get reflected at all. Even if i set the distance to the same as the farclipPlane. Below are my current code, where i have added the line just below where the cullingmask i set.


    Code (CSharp):
    1.             // Render reflection if needed
    2.             if (mode >= WaterMode.Reflective)
    3.             {      
    4.                 // Reflect camera around reflection plane
    5.                 float d = -Vector3.Dot(normal, pos) - clipPlaneOffset;
    6.                 Vector4 reflectionPlane = new Vector4(normal.x, normal.y, normal.z, d);
    7.  
    8.                 Matrix4x4 reflection = Matrix4x4.zero;
    9.                 CalculateReflectionMatrix(ref reflection, reflectionPlane);
    10.                 Vector3 oldpos = cam.transform.position;
    11.                 Vector3 newpos = reflection.MultiplyPoint(oldpos);
    12.                 reflectionCamera.worldToCameraMatrix = cam.worldToCameraMatrix * reflection;
    13.  
    14.                 // Setup oblique projection matrix so that near plane is our reflection
    15.                 // plane. This way we clip everything below/above it for free.
    16.                 Vector4 clipPlane = CameraSpacePlane(reflectionCamera, pos, normal, 1.0f);
    17.                 reflectionCamera.projectionMatrix = cam.CalculateObliqueMatrix(clipPlane);
    18.  
    19.                 reflectionCamera.cullingMask = ~(1 << 4) & reflectLayers.value; // never render water layer
    20.                 reflectionCamera.layerCullDistances = cam.layerCullDistances;
    21.                 reflectionCamera.targetTexture = m_ReflectionTexture;
    22.                 GL.invertCulling = true;
    23.                 reflectionCamera.transform.position = newpos;
    24.                 Vector3 euler = cam.transform.eulerAngles;
    25.                 reflectionCamera.transform.eulerAngles = new Vector3(-euler.x, euler.y, euler.z);
    26.                 reflectionCamera.Render();
    27.                 reflectionCamera.transform.position = oldpos;
    28.                 GL.invertCulling = false;
    29.                 GetComponent<Renderer>().sharedMaterial.SetTexture("_ReflectionTex", m_ReflectionTexture);
    30.             }
    Is this a bug or am I doing something wrong?
     
  2. Maxi77

    Maxi77

    Joined:
    Mar 3, 2015
    Posts:
    15
    I played around with this for a while, but havent found a solution. I have managed to set the farclipplane of the reflectioncamera, but it does not effect what gets reflected. I quess it has something to do with fact, that the script set a custom projectionmatrix.

    As far as i know, the only option is to Work with the cullingmask. Maby write some code that move objects between different "distance"-layers. I realy dont like that solution.

    I would appreciate it if anyone can explain why the layerculldistance has problems with custom projectionmatrix.