• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by gfgood · Nov 07, 2016 at 11:20 PM · shadergravitywaterfloatinglow poly

Low Poly Water with Proper Buoyancy

Hi all,

So first, let me start off by saying I'm super new to Unity. I have a background in video production and a specialization in After Effects with expressions, so I'm some what familiar with using code to relate objects.

As a first foray, I'm trying to make a low- poly duck float in low poly water.

I have found a shader online that is essentially this: http://www.battlemaze.com/?p=153 (I think in my hunt a found a newer version somewhere). From what I can tell, I guess it's manipulating vertices to form the waves.

My question is how to make my duck float on the top of the waves. I've seen some people do something or another with rigid body wheels, but I'm unclear on how to even make the ducks gravity respect the waves so that it sits on top (or just a little bit under it).

Any help would be greatly appreciated!

Thanks!

George

Comment

People who like this

0 Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gfgood · Nov 07, 2016 at 11:24 PM 0
Share

Also, from further research, it seems like my approach may be fundamentally flawed. If I'm understanding correctly, the shader is only making it look like its moving, but the object isn't. Therefore, any collision with it won't work.

I've also read that actually manipulating a mesh is super render intensive, and I do want this to eventually run on an oculus.

avatar image Namey5 gfgood · Nov 08, 2016 at 04:47 AM 0
Share

It depends. If your scene is moderately small and is low poly, then you could probably get away with actual code based mesh displacement. It is less straight forward than shader displacement however.

avatar image gfgood Namey5 · Nov 09, 2016 at 12:22 AM 0
Share

Is this essentially what you're suggesting?

http://catlikecoding.com/unity/tutorials/mesh-deformation/

I really don't have a reference of what small is since this is my first project. It will basically be a small low poly pond surrounded by a lot of other static objects with a bunch of ducks instantiating. It is also being made for Oculus so the assumption is that any use would have a pretty beefy computer. Any suggestions on the best way to figure out if I can get away with it?

Show more comments
avatar image gfgood · Nov 09, 2016 at 05:44 AM 0
Share

@namey5 For whatever reason, I can't reply to your most recent reply.

First off, thanks! I'm glad to see this forum is a responsive community and I really appreciate your help! If you have seen the video that @conman79 posted above, that looks like any interesting way to handle buoyancy (thanks to you as well!)

I'd love to see what you come up with! At this point, I definitely need some hand holding, but rest assured I spend my time researching to make sure I really understand whats going on and don't just rely on people to solve all my problems ;)

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by conman79 · Nov 09, 2016 at 01:20 AM

Realistic buoyancy is quite difficult to simulate. Even more difficult if your water is based on a shader. Since you specifically mentioned a duck, here's a talk about buoyancy using a duck as an example!

https://www.youtube.com/watch?v=OOeA0pJ8Y2s

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by Namey5 · Nov 09, 2016 at 05:58 AM

Ok, so after a bit I've developed the system I was talking about, so you should be able to test this on whatever setup you want.

The first script is for the actual water object. I've set it up with static variables, and as such it will only work with one water object in the scene. Basically, you attach this to, say, a plane or whatever water object you want and it will deform the mesh based on Unity's Perlin Noise algorithm. I'd suggest removing the collider from the water object.

 using UnityEngine;
 using System.Collections;
 
 public class WaterDeformation : MonoBehaviour 
 {
     
     public static Mesh mesh;            //The water mesh
     public static Transform water;        //The water transform
     
     public float deformAmount = 1;        //Amount to deform the water
     public float scale = 2.5f;            //How fine the displacement is
     public float speed = 1;                //The speed of waves
     
     private Vector2 time = Vector2.zero;    //The actual speed offset
     
     // Use this for initialization
     void Start () 
     {
         //Set the water and mesh variables at start
         water = transform;
         mesh = GetComponent<MeshFilter>().mesh;
     }
     
     // Update is called once per frame
     void Update () 
     {
         time = new Vector2 (Time.time, Time.time) * speed;            //Set up speed offset for deformation
         
         Vector3[] vertices = mesh.vertices;                //Create a variable for the vertices beforehand
         
         for (int i = 0; i < vertices.Length; i++) {
             vertices[i] = Deform (vertices[i]);                //For every vertice, deform the Y position
         }
         
         mesh.vertices = vertices;                    //Re-assign the vertices
         mesh.RecalculateNormals ();                    //Recalculate the normals so the object doesn't look flat
         GetComponent<MeshFilter>().mesh = mesh;        //Re-assign the mesh to the filter
     }
     
     Vector3 Deform (Vector3 v)            //Takes a Vector3
     {
         v.y = Mathf.PerlinNoise (v.x / scale + time.x, v.z / scale + time.y) * deformAmount;            //Distort the vertice's Y position based off its X and Z positions + time
         return v;            //Return the offset vertice position
     }
 }

The second script you attach to any objects you want to have buoyancy. The thing about this is, I've never tried to calculate buoyancy myself, so instead what it does is it adds force upwards if the object is below the water at the nearest point.

 using UnityEngine;
 using System.Collections;
 
 public class Buoyancy : MonoBehaviour 
 {
 
     public float buoyancy = 20;            //Buoyancy force
     public float viscosity = 20;                  //How easily an object can move through the water
     
     private Rigidbody rb;                //Rigidbody attached to this object
 
     // Use this for initialization
     void Start () 
     {
         rb = GetComponent<Rigidbody>();            //Set the rigidbody at startup
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         Vector3[] vertices = WaterDeformation.mesh.vertices;            //Find the water's vertices
         Vector3[] worldVerts = new Vector3[vertices.Length];            //Create a new array to store world space vertex positions
         
         for (int i = 0; i < vertices.Length; i++) {
             worldVerts[i] = WaterDeformation.water.TransformPoint (vertices[i]);        //For every vertex, transform the position into world space
         }
         
         Vector3 nearestVert = NearestVertice (transform.position, worldVerts);            //Find the nearest vertice to this object
         
         if (transform.position.y < nearestVert.y)    {    //If this object is below the nearest vertice
             rb.AddForce (Vector3.up * buoyancy);        //Apply force upwards
                     rb.velocity /= ((viscosity / 100) + 1);           //Slow the objects movements when in water
             }
     }
     
     Vector3 NearestVertice (Vector3 pos, Vector3[] verts)            //Takes a position and a position array
     {
         Vector3 nearestVert = Vector3.zero;            //Create the initial nearestVert variable and initialise it
         float minDist = 100;                        //Declare the min dist (can be whatever you want, something large though)
         
         for (int i = 0; i < verts.Length; i++) {        //For every vertice
             if (Vector3.Distance (pos, verts[i]) < minDist) {        //If the vertice is closer than the one before it
                 nearestVert = verts[i];                                //Set the nearest vertice variable
                 minDist = Vector3.Distance (pos, verts[i]);            //Update the minDist
             }
         }
         
         return nearestVert;            //Return the nearest vertice
     }
 }
Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gfgood · Nov 14, 2016 at 03:32 AM 0
Share

This is awesome @namey5 !!!

I think I'll do some work on the buoyancy over time, but this is definitely a great start!

Thanks a ton!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

97 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shader Graph low poly water normals 1 Answer

How can i get the y world position of a vertex at a given x and z position from a shader graph? 1 Answer

Water shader change 0 Answers

Help with grabpass shader for 2D water effect. 2 Answers

Basic Water shader error message 6 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges