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. Dismiss Notice

Question Using WindZones with entities

Discussion in 'Entity Component System' started by mexicanhatboy, Jul 13, 2023.

  1. mexicanhatboy

    mexicanhatboy

    Joined:
    Feb 5, 2019
    Posts:
    5
    Hi all,
    I hope this isn't an incredibly dense question, but I'm working on a forest visualisation in Unity. Since each tree should grow and show data (when interacted with) related to a simulation output, I wanted each game to be an indiviual GameObject and now, Entity (Since there will be hundreds of thousands of trees, the switch to ECS was necessary). I've mostly been having a good time with the new paradigm, but I've been unable to get entity-trees to interact with a WindZone. Trees created as GameObjects, in the main scene and directly from SpeedTree exports will interact with WindZones as expected, however any trees created in the subscene for use as entities will not, regardless of whether the Windzone is place in the main scene or the subscene. Does anyone have any insight?
    Thanks!
    Tom
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,975
    You'll have to set the windzone material properties yourself, since the tree entities and the windzones are unaware of each other. I suggest making a custom shader using shader graph to achieve wind animation.
     
  3. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    Or use old trick to make SpeedTree contained meshes that are not managed by unity be "wind aware". In GameObjects scene place one of a type (prefab) of each trees behind the camera and override its bounding boxes to be big to prevent frustum culling (actual meshes are still not visible). Each frame Unity speedtree integration will update wind animation parameters of those tree prefabs. You, with a special "read" script, read all speedtree wind parameters from those hidden behind camera tree's materials. Obtained parameter values then need to be written to the materials of actual visible trees. Of course this should be done only per one tree type and not for every tree instance. Here is a core of such script:
    Code (CSharp):
    1.  
    2. public void SetSpeedTreeWindParametersForMaterial(Material srcMat, Material dstMat)
    3. {
    4.     dstMat.SetVector(_ST_WindAnimation, srcMat.GetVector("_ST_WindAnimation"));
    5.     dstMat.SetVector(_ST_WindBranch, srcMat.GetVector("_ST_WindBranch"));
    6.     dstMat.SetVector(_ST_WindBranchAdherences, srcMat.GetVector("_ST_WindBranchAdherences"));
    7.     dstMat.SetVector(_ST_WindBranchAnchor, srcMat.GetVector("_ST_WindBranchAnchor"));
    8.     dstMat.SetVector(_ST_WindBranchTwitch, srcMat.GetVector(" _ST_WindBranchTwitch"));
    9.     dstMat.SetVector(_ST_WindBranchWhip, srcMat.GetVector("_ST_WindBranchWhip"));
    10.     dstMat.SetVector(_ST_WindFrondRipple, srcMat.GetVector("_ST_WindFrondRipple"));
    11.     dstMat.SetVector(_ST_WindGlobal, srcMat.GetVector("_ST_WindGlobal"));
    12.     dstMat.SetVector(_ST_WindLeaf1Ripple, srcMat.GetVector("_ST_WindLeaf1Ripple"));
    13.     dstMat.SetVector(_ST_WindLeaf1Tumble, srcMat.GetVector("_ST_WindLeaf1Tumble"));
    14.     dstMat.SetVector(_ST_WindLeaf1Twitch, srcMat.GetVector("_ST_WindLeaf1Twitch"));
    15.     dstMat.SetVector(_ST_WindLeaf2Ripple, srcMat.GetVector("_ST_WindLeaf2Ripple"));
    16.     dstMat.SetVector(_ST_WindLeaf2Tumble, srcMat.GetVector("_ST_WindLeaf2Tumble"));
    17.     dstMat.SetVector(_ST_WindLeaf2Twitch, srcMat.GetVector("_ST_WindLeaf2Twitch"));
    18.     dstMat.SetVector(_ST_WindTurbulences, srcMat.GetVector("_ST_WindTurbulences"));
    19.     dstMat.SetVector(_ST_WindVector, srcMat.GetVector("_ST_WindVector"));                      
    20. }
    21.  
     
    xVergilx likes this.
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Custom trees is the best solution. SpeedTree has a very poor performance if you push the numbers too high.

    Sampling windzone & pushing values per material works, but adding anything shader wise to the STs is a nightmare. Which, judging from the use case, you'd want to do anyway.
     
  5. mexicanhatboy

    mexicanhatboy

    Joined:
    Feb 5, 2019
    Posts:
    5
    OK thank you everyone! I will take a look at these solutions tomorrow at work :)