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

Get height at pos(x, z) of a mesh modified by shader

Discussion in 'Scripting' started by AndreiTache, Oct 24, 2019.

  1. AndreiTache

    AndreiTache

    Joined:
    Nov 8, 2014
    Posts:
    31
    Hi everyone, I'm working on adding water to my game.
    I made a shader for waves that moves the vertices up/down, but now I would like to get the height of the water at a certain position to use it for buoyancy calculations in another script.
    (I'm really new to shaders, so sorry if this is an easy question, I couldn't find the answer no matter how much googling I've done :))

    What I used to do was modify the mesh vertices and a mesh collider via script, then raycast down from the position to get the height, but because the mesh would be updating constantly, generating a mesh collider was really expensive, so I am looking for some alternatives.

    Thanks for the help =D
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    If the shader uses a formula for displacing the vertices, couldn't you use the same formula to calculate the water height?
     
  3. AndreiTache

    AndreiTache

    Joined:
    Nov 8, 2014
    Posts:
    31
    Yeah, actually, that makes sense. Guess I was just lazy and didn't want to rewrite the formula in c#
    Thanks :)
     
  4. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Yeah, code duplication is never cool, but I'm not sure how you could write it one place only. Maybe someone else has a good idea.
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,019
    maybe could take 1 pixel sample from wave depth texture at player position (using extra camera above or so)
    and then calculate height from that. (since you already know what is minimum and maximum height, can get Y value easily i think)

    there's also many free water shader systems (with waves and buoyancy), could look how they have solved it.