Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Water plane and flip texture.

Discussion in 'Scripting' started by digiplayer, Apr 1, 2009.

  1. digiplayer

    digiplayer

    Joined:
    Apr 1, 2009
    Posts:
    4
    I am having trouble trying to show water reflections while under the water. Does anyone know how to flip the texture so that while under water you can see the reflections? Any help appreciated.
     
  2. andreasng

    andreasng

    Joined:
    Aug 15, 2011
    Posts:
    5
    Why no one have adressed this is beyond me. The problem is still there even with water4!

    anyone?
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Because it doesn't make any sense. What exactly are you reflecting when you're under water? Water surface reflection is caused by light rays bouncing off the water. When you're underwater the light is being reflected away from you so you wouldn't see a reflection anyway.
     
  4. andreasng

    andreasng

    Joined:
    Aug 15, 2011
    Posts:
    5
    i see . the same way you can put gravity to -1 (which "makes no sense") the same way i want my water-surface to work underneath.

    wrong. if you have been under water at any time in your life you would have seen that reflections is the same if not more visible from below the water-surface than from above.

    this image speaks for itself.
    http://s2.desktopia.net/wp-content/uploads/walls/thumbs/Underwater-Pool-575x431.jpg

    even this one speaks for itself.
    http://www.safariegypt.com/egypt_hotels/cairo_hotels/Images/hyatt/p36388p010.jpg
     
    Last edited: Mar 2, 2012
  5. andreasng

    andreasng

    Joined:
    Aug 15, 2011
    Posts:
    5
    aaaaanyway.....

    i made a really hacky fix to this problem, but i think this is on unity's hands not the users.

    I "declared" this (or whatever it's called i'm not a coder)

    Code (csharp):
    1. _Flip("Flip", Float) = 1.0
    then I fixed this spot

    Code (csharp):
    1. // shading for fresnel term
    2. worldNormal.xz *= _FresnelScale;
    3. half refl2Refr = Fresnel(viewVector, worldNormal, FRESNEL_BIAS, FRESNEL_POWER) * _Flip;
    it makes the water NOT akwardly bright when viewed from below but refractive/reflective.

    I think it has something to do with the way unity calculates the fresnel. When camera angle is below the water surface the fresnel just takes over instead of only taking over when the angles are close, and then go back to nothing when the angles are steep.

    Does anyone have a better solution?
     
    Last edited: Mar 2, 2012
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Wow. Searching the basement to find a lost post in a locked filing cabinet in a disused lavatory with a sign on it saying "Beware of the Leopard".

    Andreasng: As this post was SOOOO old, it was probably OK for you to start a new post.

    I believe the question is:

    "When underwater, how can I make the surface look like real water from underneath?"

    Some images of the water surface can be found here: http://tinyurl.com/84djjbo ... some of which I have posted below:



    This is not an easy task, but one, if it's solved, may be useful to many people.

    A challenge to the entrepreneur to get a package together for the Asset Store?
     
  7. andreasng

    andreasng

    Joined:
    Aug 15, 2011
    Posts:
    5
    yeah i guess i could just start a new thread - on the other hand it's newer appreciated to have 10 threads with people wanting the same thing. HAHAHA

    Anyway... It ISSSSS and easy task. Unity team, and anyone with experience in shading would be able to "flip" the numbers for the water4 shader in no time.

    All I want is the already working reflection and refraction to work both above and below.
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you simply want the reflection for the water plane but from below, you can use two water planes:



    This was created in a few minutes by:
    • Creating a new stock Unity Terrain and sculpting it into a ring.
    • Adding a water plane and positioning it (demo shows both basic and pro water)
    • Duplicating the water plane and setting the transform.scale.y on the second plane to -1.0

    The underwater fog is a simple script:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Underwater : MonoBehaviour {
    5.     public float waterLevel;
    6.     public Transform waterPlane;    //  Testing
    7.     private bool isUnderwater;
    8.     private Color normalColor;
    9.     private Color underwaterColor;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         normalColor = new Color (0.5f, 0.5f, 0.5f, 0.5f);
    14.         underwaterColor = new Color (0.22f, 0.65f, 0.77f, 0.5f);
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if ((transform.position.y < waterLevel) != isUnderwater) {
    20.             isUnderwater = transform.position.y < waterLevel;
    21.             if (isUnderwater) SetUnderwater ();
    22.             if (!isUnderwater) SetNormal ();
    23.         }
    24.     }
    25.  
    26.     void SetNormal () {
    27.         RenderSettings.fogColor = normalColor;
    28.         RenderSettings.fogDensity = 0.01f;
    29.  
    30.         //  Testing
    31.         waterPlane.localScale = new Vector3 (waterPlane.localScale.x, 1.0f, waterPlane.localScale.z);
    32.     }
    33.  
    34.     void SetUnderwater () {
    35.         RenderSettings.fogColor = underwaterColor;
    36.         RenderSettings.fogDensity = 0.1f;
    37.  
    38.         //  Testing
    39.         waterPlane.localScale = new Vector3 (waterPlane.localScale.x, -1.0f, waterPlane.localScale.z);
    40.     }
    41. }
    42.  
    Out of curiosity I tested flipping a single water plane when under the water level, which worked as well, but I'm completely unclear if doing this has any value at all... I was just curious. You can delete the lines marked "Testing"
     
    LaTigresaBlanca likes this.
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I saw a comment on YouTube re: this thread...

    First off, reply to the thread, not the YouTube clip please. YouTube is hosting the clip, but YouTube is not part of the Unity Forum.

    Please check your code, and don't forget to fill in all of the public data in the inspector. If the water plane is not triggering your fog, then most likely you have forgotten to set your water level value in the inspector.
     
  10. Zanyblax

    Zanyblax

    Joined:
    Jul 25, 2015
    Posts:
    2
    Do you attach this to the water? @Adam Buckner
     
  11. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    If you look at the "Update", this script needs to be attached to something that's related the the camera/player. When the "transform.position" of the GameObject with the script on it is "less than" the "water level" then the script changes the fog level, etc..

    Be aware that this is a very very simple script and will proably need to be made more complex to actaully work properly in a game project. This script simply illustrates the idea and is a proof of concept.
     
  12. Zanyblax

    Zanyblax

    Joined:
    Jul 25, 2015
    Posts:
    2
    Thanks!
     
  13. AurorasMercy

    AurorasMercy

    Joined:
    Feb 19, 2014
    Posts:
    16
    Does anyone have a fix for the flipped water plane in Unity 5?
    I tried Adam's method and several other methods for flipping the duplicate plane, but it never shows from below.
     
  14. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Which water system are you using? There are several to choose from now, in Standard Assets. Some of the more advanced ones may not allow flipping as they are far more complicated and include things like mesh deformation.

    The Unity "Pro" water should allow flipping, but the two water planes my affect the other's rendering. What you may want to try is the above script with the active flip. I have successfully done this flip technique (just now) using WaterProDaytime and WaterProNightime prefabs from Standard Assets. I believe that the "Water4 samples will not flip successfully.
     
  15. AurorasMercy

    AurorasMercy

    Joined:
    Feb 19, 2014
    Posts:
    16
    Thank you Adam, I was using the "Water 4 Simple", so that must have been the problem.

    Thanks to your tip, I was able to get the Pro water looking great from underneath!

    It wasn't quite the look I wanted on top of the water though (water 4 looks much more like an ocean), so I used the pro water from beneath, and the water 4 on top of that (right side up), and it works beautifully!
     
  16. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
  17. Frennle

    Frennle

    Joined:
    Dec 15, 2016
    Posts:
    2


    I'm sorry, but I'm new to unity... and i really didn't understand that..... could you plase run me through the steps of adding the script ( what do i add it too??)

    cheers, frennle
     
  18. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    You should probably get a little more familiar with unity before trying these intermediate tricks. Try going to the learn tab and doing several of the tutorial projects.

    This script checks the transform position of the GameObject it is attached to to do the flipping. Be aware that this script is proof of concept only and will require further work to be production ready.
     
  19. Frennle

    Frennle

    Joined:
    Dec 15, 2016
    Posts:
    2
    ok, thanks :)