Search Unity

cloth with wind and physics?

Discussion in 'General Graphics' started by Deleted User, Apr 25, 2015.

  1. Deleted User

    Deleted User

    Guest

    How can i get wind blown cloth with physics (objects colliding realistically) in unity 5?
    I know how to make a vertex animation shader for wind blown cloth, but its not very realistic and would look crap when combined with physics.
     
  2. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,070
    This sounds like a good topic. I've yet to see much talk about cloth using U5.
     
  3. KingMatthew

    KingMatthew

    Joined:
    Jul 7, 2013
    Posts:
    166
    Unity got rid of interactive cloth, which is probably what you are looking for. The cloth you see now in Unity5 is specifically designed for clothes on characters or something similar.
    Maybe there is an Asset that has what you are looking for.
     
  4. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    652
    The new cloth works brilliantly and isn't limited to characters. It can be pushed around by any sphere or capsule collider, but it cannot exert a force back on anything.
     
    theANMATOR2b likes this.
  5. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,377
    Where would you start? Is there a tutorial anywhere?

    For instance, how about a square cloth covering a wooden crate or something like that. Or the cloth falling on the box?
     
  6. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    652
    Unfortunately you can't really make a box very well out of capsules and spheres, so that particular example wont work very well.

    All you do is add the cloth component to your object. Then if you want you can fix some of the vertices to their original position and/or add some colliders. There's some options for wind like forces as well.
     
  7. Max_Bol

    Max_Bol

    Joined:
    May 12, 2014
    Posts:
    123
    For the physic with other items (with other colliders than spheres and capsules), I can't really help.

    But a way of applying wind is to add to the Random Acceleration and External Acceleration of the cloth which will allow the cloth to move toward its local direction. Note that the random acceleration is really crappy for anything less than strong winds as it generate a new force way too often. As for the External Acceleration, it's mostly for things like constant winds/stream of force. If you want a gentle breeze or some indirect wind, you will mostly have to add a custom script that will allow the acceleration to be adjusted at the right speed.

    For example, I created this CSharp script this :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClothOnWind : MonoBehaviour {
    5.  
    6.     private Cloth clothMod;
    7.     private Vector3 TargetWindForce;
    8.  
    9.     public Vector3 MaxWindForce;
    10.     public float WindForceIntervals = 1;
    11.     public Vector3 NegativeWindForce;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         clothMod = this.GetComponent<Cloth> ();
    16.         clothMod.externalAcceleration = NegativeWindForce;
    17.         TargetWindForce = MaxWindForce;
    18.         if (WindForceIntervals <= 0) {
    19.             WindForceIntervals = 0.1f;
    20.         }
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update () {
    25.         clothMod.externalAcceleration = Vector3.MoveTowards (clothMod.externalAcceleration, TargetWindForce, WindForceIntervals * Time.deltaTime);
    26.  
    27.         if (clothMod.externalAcceleration == MaxWindForce) {
    28.             TargetWindForce = NegativeWindForce;
    29.         }
    30.         if (clothMod.externalAcceleration == NegativeWindForce) {
    31.             TargetWindForce = MaxWindForce;
    32.         }
    33.     }
    34. }
    35.  

    You just have to place this script on the GameObject with the cloth component.
    This script will make the external acceleration of your choice being alternated between its full power and its negative power so that it looks like the wind blows are inconsistent.
    If you wish, you could simplify it by making a direct uses of the cloth's settings for the external acceleration's maximum value (in the inspector), but I was damn lazy and decided to make it as an additional thing.

    If you really wish to make use of your wind component, you could make a script like this to look up for its position and orientation as well as wind force and add it onto to the external force. (Can't help you much on this one. Could do it, but my head is already all over my own project)

    If anyone is using this script, I highly suggest to add a tiny bit of random acceleration in the same direction as for the main external force just for the sake of making it less repetitive.

    = EDIT =
    I would like to add that with Unity 5 Cloth's Editable Constraints, you can also control easily how much each part of the cloth will bend with this script. For example, by making the "attached" part of the constraint (in the Cloth's Edit Constraint) at 0 and smoothly removing the constraint (from 0 to 1) towards the end of the cloths, this will allow a smooth and slight gliding effect on the cloth.

    WindEffect.png
     
    Last edited: Jul 22, 2015
    theANMATOR2b likes this.
  8. sniperisa

    sniperisa

    Joined:
    Sep 20, 2015
    Posts:
    14
    You mean something like this:


    The cloth looks like this:
    sails.jpg

    The wind force is applied like this:

    Code (CSharp):
    1. //Sail Controller class
    2. void SetWindForces()
    3.         {
    4.             if (windPower == null || windPower != windController.GetWindPower() || windDirection == null || windDirection != windController.GetWindDirection())
    5.             {
    6.                 Debug.Log("Set Wind in Sails");
    7.                 windPower = windController.GetWindPower();
    8.                 windDirection = windController.GetWindDirection();
    9.  
    10.                 float x = 0;
    11.                 float z = 0;
    12.  
    13.                 if (windDirection <= 90)
    14.                 {
    15.                     x = Mathf.Sin(windDirection*Mathf.Deg2Rad) * windPower;
    16.                     z = Mathf.Cos(windDirection*Mathf.Deg2Rad) * windPower;
    17.                 }
    18.                 else if (windDirection > 90 && windDirection <= 180)
    19.                 {
    20.                     z = Mathf.Sin((windDirection-90)*Mathf.Deg2Rad) * windPower * -1;
    21.                     x = Mathf.Cos((windDirection-90)*Mathf.Deg2Rad) * windPower;
    22.                 }
    23.                 else if (windDirection > 180 && windDirection <= 270)
    24.                 {
    25.                     x = Mathf.Sin((windDirection-180)*Mathf.Deg2Rad) * windPower * -1;
    26.                     z = Mathf.Cos((windDirection-180)*Mathf.Deg2Rad) * windPower * -1;
    27.                 }
    28.                 else
    29.                 {
    30.                     z = Mathf.Sin((windDirection-270)*Mathf.Deg2Rad) * windPower;
    31.                     x = Mathf.Cos((windDirection-270)*Mathf.Deg2Rad) * windPower * -1;
    32.                 }
    33.  
    34.  
    35.                 windForces = new Vector3(x, 0f, z);
    36.  
    37.                 foreach (WoP_Sail sail in sails)
    38.                 {
    39.                     sail.SetWindForces(windForces);
    40.                 }
    41.  
    42.             }
    43.         }
    44.  
    45. //------------------------------------------------------------
    46. //Sail Class
    47.  
    48.  
    49. public void SetWindForces(Vector3 wind)
    50.         {
    51.             windForces = wind;
    52.             if (sailCloth != null)
    53.             {
    54.                 sailCloth.externalAcceleration = windForces;
    55.             }
    56.         }
    57.  
    58.  
    With that solution I now have the following problem:
    If the sail mesh is double sided it works fine, if it is a one sided mesh the wind force is 180°. So the sail fills in the opposite direction.
    And it doesnt matter how the axis of the mesh are oriented
     
    imgodot likes this.
  9. imgodot

    imgodot

    Joined:
    Nov 29, 2013
    Posts:
    178
    sniperisa,
    Thanks for putting up those snippets; I appreciate your sharing.
    I am making a simplistic sailing simulator and your code helped me immensely.
    -- Paul
     
  10. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    227
unityunity