Search Unity

Using Tree gameobjects vs Unity's built in system

Discussion in 'Scripting' started by jc_lvngstn, May 22, 2013.

  1. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    No apologies needed. All good here.

    OK, today we are setting up the unity asset server at my home offices and then we have to complete some left over GUI for our user accounts and software licensing drm stuff.

    Tomorrow all day is about taking our geometry brush code and making it a plug-in. I'll post you on the progress.
     
  2. BoBo-san

    BoBo-san

    Joined:
    Jan 29, 2011
    Posts:
    11
    This thread is interesting.
    Thank for the tip and benchmark.

    If someone are still looking for billboard shader. here you go.


    Code (csharp):
    1.  
    2. Shader "Custom/Billboard" {
    3.     Properties {
    4.         _MainTex ("Base (RGBA)", 2D) = "white" {}
    5.         _Size ("Size", Vector) = (1,1,0,0)
    6.     }
    7.     SubShader {
    8.         Tags { "RenderType"="Transparent" "Queue" = "Transparent"}
    9.         LOD 200
    10.        
    11.         Blend SrcAlpha OneMinusSrcAlpha
    12.         Cull Off
    13.         CGINCLUDE
    14.         #include "UnityCG.cginc"
    15.        
    16.         sampler2D _MainTex;
    17.         float4 _MainTex_ST;
    18.         float4 _Size;
    19.        
    20.        
    21.         struct v2f
    22.         {
    23.             float4 pos : SV_POSITION;
    24.             fixed2 uv : TEXCOORD0;
    25.         };
    26.  
    27.         v2f vert(appdata_base v)
    28.         {
    29.             v2f o;
    30.            
    31.             /** Billboard Code **/
    32.             float4 viewPos = mul(UNITY_MATRIX_MV, float4(0,0,0,1));  
    33.            
    34.             float2 factor = v.texcoord.xy - float2(0.5f,0.5f);
    35.            
    36.             viewPos.xy += _Size.xy*factor;
    37.            
    38.             o.pos = mul(UNITY_MATRIX_P,viewPos);
    39.             /** Billboard Code **/
    40.            
    41.             o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
    42.            
    43.             return o;
    44.         }
    45.        
    46.         fixed4 frag(v2f i) : COLOR
    47.         {
    48.             return tex2D(_MainTex, i.uv);
    49.         }
    50.        
    51.         ENDCG
    52.        
    53.        
    54.         Pass
    55.         {
    56.             CGPROGRAM
    57.             #pragma vertex vert
    58.             #pragma fragment frag
    59.             ENDCG  
    60.         }
    61.     }
    62.     FallBack "Diffuse"
    63. }
    64.  
    Assign this shader to plane mesh and you should get your billboard.
    The code for billboard is in vertex shader function "vert".
    This shader only render billboard, no fog or light calculation.
    The size of billboard is controled by _Size property.
    Plane's uv should range from 0 to 1.
    To avoid camera frustum culling (Because Unity don't have knowledge about billboard size in shader), you need to adjust your plane's scale to match billboard size and rotation to get maximum AABB. Rotation in 45 degree in X and Y axis should be okay.

    PS. I don't know why this shader cannot get dynamic batching.
     
    Last edited: May 29, 2013
  3. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Thank you, will try it out : )
     
  4. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    larsbertram1: On the topics of shaders, how much work would it be to add bump map support and receive shadows to the tree leaf shader?
    Would it be similar to the modifications you did to the bark shader or are they very different?
    Also, would that impact performance a lot?
     
  5. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
  6. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you are talking about the ambient occlusion shaders?
    well i do not use them but the tree creator ones: those come with bump/spec and translucency right out of the box.
    adding this to the ambient occlusion shader is not really difficult i guess but would force you to rework all your models as leave planes have to be double sided.
    so i would suggest to go with the tree creator shaders first.

    lars
     
  7. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you are talking about the ambient occlusion shaders?
    well i do not use them but the tree creator ones: those come with bump/spec and translucency right out of the box.
    adding this to the ambient occlusion shader is not really difficult i guess but would force you to rework all your models as leave planes have to be double sided.
    so i would suggest to go with the tree creator shaders first.

    lars
     
  8. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Hmm, ok. When i tried to use that shader the whole model just turned into a big mess, you have any idea why thats happening? Workes fine with the Occlusion shader. Also, why do i have to rework my models? The occlusion shader already makes my planes double sided. Last question, do the tree creation shaders allow shadows to be cast on the model like your custom bark shader?
     
  9. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    So...sorry for jumping in, in the middle of a conversation that frankly...I am a noob on. But...ideally, what features (from shaders) would we have on the trees?

    Shadows
    Bumping
    Ambient occlusion
    Translucency
    Wind

    All on non-unity trees. Right? Lars, doesn't your shaders offer these features? Maybe except for occlusion, I'd have to go back and look.
     
  10. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    bending data has to be baked into vertex.colors and a second uv set which is quite a task to do manually.
    so i would sugguest to create your trees using the built in tree creator…
    working on an imposter system you will have to decide which kind of trees it should support – at least for the first version:
    - imported ones using the occlusion shaders: bad bending, no shadows on the leaves, no bumpmapping, spec lighting and translucency.
    - trees generated using the built in tree creator: better bending, shadows on the leaves, bumpmapping, spec lighting and translucency.

    geometry has to be double sided to work with the tree creator shader. the occllusion shader simply does not cull the planes -> no real lighting, no shadows.

    [/QUOTE]

    yes.

    lars
     
  11. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    you will only have those features if you bake all needed information (especially about bending and ambient occlusion) into your models which is as said before a lot of work.
    then you could simply use the tree creator shaders.

    my advanced foliage shaders do offer all these features but also need specially prepared models of course.
    but they have not been optimized to render trees but rather small plants and bushes. and you will need your own wind function to make them bend.

    lars
     
  12. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Ok, thanx for the explanation. Problem is im dealing with plants and stuff so its not possible to use the tree creator and im not sure i want to spend a lot of time setting them up, also i have no idea at all on how to do that. Is there a guide on that somewhere if i for example would like to use your foliage shader lars? So i could try setting up before buying the package if i decide i want it?
     
  13. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    As far as vertex baking for wind...if I understand correctly, it's just applying certain colors to the vertices to indicate wind "strength". Couldn't a simple script apply the values, depending on height or something? It wouldn't be fancy bending...but it might be decent.
     
  14. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    I agree, height and or radius from center would work just fine. That could be set up to happen automatically in Unity. I would rather have something that worked and was easy to set up instead of something that was as detailed as I see Lars' shaders are and didn't use them because it was too difficult and time consuming to use ( I bought the one he recommended the other day and tried it out today)

    BTW, I should be able to post a version of the geometry brush tonight. I'll ask my coder about the vertex color utility.
     
    Last edited: May 30, 2013
  15. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    Geometry Brush Day 01
    Alright, here's what I have so far.
    Not as user friendly as what I have in my game but it works. All the source code is there if you want to add to it and or edit it.
    I would appreciate any efforts given back to this forum thread and not turned into a product in the asset store.
    Have fun..


    https://www.dropbox.com/s/2t6qbm3vd8y696i/GeometryBrushPlugin.rar

    >You can only make one brush at a time
    >You cannot save your brushes
    >Does not support multiple layers of geometry
    >We need to make a reference gadget for the brush scale
    >In addition to add and delete geometry. I would also like to see a soft scale tool.

    Please add any other ideas you may have.
     
  16. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    Thank you for the props. I hope to have more videos up later this week.
     
  17. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Dont worry, i dont think anyone here at least would post it on the asset store.
    Great work so far, thanx a lot : )
     
  18. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    well, it is a bit more than just vertex colors: you will also have to setup a second uv.
    i think one might write a script which would apply all needed values but:
    - you would need one mesh including different submeshes (trunk, branches 1st level, branches 2nd level, leaves) in a specific hierarchy as input in order to tell the script how to handle the different vertices
    - it should be controllable in order to setup different values for each tree

    lars
     
  19. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Sounds like a big job there.
    I think it would be enough for my needs with only alpha cutout + bump + self shadows.
    No need for special setup to do that right?
     
  20. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    it is a big job indeed.
    and in case you do not need any bending you would have to tweak the tree creator shaders and remove the vertex wind animation part from them.

    lars
     
  21. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    All of that could be done with a script in the 3d package
     
  22. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    what package do you mean?
     
  23. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    I think he is talking about a future tree system package.
     
  24. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    How are you creating the second UV set? It only takes seconds to do in maya, max, and modo. Also FYI. making a second uv set doubles the size of your model If you have many different trees that could be a considerable amount of memory. Each additional uv set adds to the size of the model per the original size.

    Why exactly do you need a second uv set? and why cant all of this motion (vertex animation) cant be baked into the model from the 3d package?
    Just curious.
     
  25. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    Maya, Max, Modo all have built in scripting frameworks
     
  26. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    I was wondering if there was any additional functionality to that geometry brush plug-in that you guys wanted besides what I mentioned. My coder will be in shortly and I wanted to have a task list set up for him.
     
  27. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    I have not had the time to look at it but what i think are the basic stuff are:
    -Add and remove
    -Configurable size and density
    -Ability to place single items with precision
    -Random rotations and also some random colors (as options)
    -Ability to adapt rotation to slope for smaller stuff like bushes and stuff?
     
  28. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    I can't understand why you need a second UV as well.
    You don't need it if you only want to move the vertexes of the mesh.

    As for the wind effects, there are basically 2.
    1. leaf trembling. This needs vertex painting or it can be achieved based on height distance from the mesh base and on distance from center on horizontal plane.
    2. slow waving. This needs nothing and can be realized based only on the height from the base of the mesh.
     
  29. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    -Add and remove > both are already there.I was also going to add a soft scale like we have in my game.
    -Configurable size and density > both are already there. I do need a visual representation of the brush itself though.
    -Ability to place single items with precision > Not sure what this is for or how it would be different then what Unity already does. Please elaborate.
    -Random rotations and also some random colors (as options) > Random rotation for all or one axis is already in. Also align to normal is there too.
    Already supported ( inadvertently) Random colors would be on the side of the user. Just make as many color variations as you like and add them to the brush.
    -Ability to adapt rotation to slope for smaller stuff like bushes and stuff? > Yes, Align to normal.. all good.
     
  30. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317

    I agree, I am going to look into this. As for my project, everything I am doing happens at run-time. I am not baking any lighting into anything. Everything in my game is destructible too. The characters and effects can change the terrain, the foliage, the trees, all dynamic.

    I would think your tank game would need to have dynamic destruction too. Also being able to lay tank tracks down on the terrain or cut a path through the bushes, mow down tress and what not, as the user moves?

    I bought Lars' terrain decals. I was going to take a look at those when I have time to see if they would work for some of my needs as described as well.
     
  31. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    i was talking about the original tree creator shaders and the way they need all information to be stored.
    they use vertex colors and the 2nd uv set to store bending information and ambient occlusion. but in case you edit the shaders and drop ambient occlusion all these information could be stored just in the vertex colors.

    of course you do not need to apply these information in unity and can also do this in your 3d app but that is a quite painfull task.
    so a script/editor within unity would really help a lot. and i would prefer this solution over any 3d app script as it would work independently from any other app and let you test the results immediately.

    @magmaltese:
    you won’t be able to calculate the distance between vertex pos and mesh origin when using static batching so all this has to be stored per vertex (color or uv).

    lars
     
  32. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    Understood, Thank you..
    I was looking at the problem as if I had made the models. I don't usually use those tree creators. I can use them in my game as I have set it up, but I have seen that they have way too many polys for me and I want the user to be able to cut the the trees down.
     
  33. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    don’t you have trees from the big environment package in your project?those should be tree created with the tree creator.

    they do have a higher poly count by nature as they use (and do have to use) double sided geometry on the leaves – up to 2500 tris for a common tree should be ok. it is not alway the triangle count that limits performance but also fill rate and a lot of trees on the asset store are not really fill rate optimized.

    lars
     
  34. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    Yes, You are right. I do have those in there. I put them in as a test to see how it would work. Those trees are not destructible though. I don't know if I will keep them in there or not. All the procedural trees in my game are from the asset store. I haven't made any of my own using the unity stuff. I was hoping to be able to get some made that will move properly and also be destructible.
     
    Last edited: May 30, 2013
  35. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Ah I see what you mean now. Wasn't thinking about static batching: when I have stuff that is not completely static in my project, I consider it dynamic in all aspects.

    Having a system like this, that can animate trees while still having them static batched would be great... but still of no use to me, as my trees are destructible :(
     
  36. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
  37. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    We will : )
     
  38. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    hi matt,

    very nice update. and just another feature request:
    - disable/stop painting when pressing "alt" in order to let the user rotate within the scene view window.
    - add random rotation on (local) y-axis (even if "align to normal" is checked).

    lars
     
    Last edited: May 31, 2013
  39. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    This is very nice. I didn't check "Prevent overlap" and created a tree skyscraper in one click ;)
    I'll do some more testing today.
     

    Attached Files:

  40. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Hahaha, very nice : D
     
  41. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    Yes,, fun huh? That feature is intentional (or should I say happy accident) . In case you want to stack rocks or whatever..
     
    Last edited: May 31, 2013
  42. HeavyMcD

    HeavyMcD

    Joined:
    Nov 4, 2012
    Posts:
    317
    I agree, will do. Thanks
     
  43. jc_lvngstn

    jc_lvngstn

    Joined:
    Jul 19, 2006
    Posts:
    1,508
    Something else I like about this...if you want to paint grass, the grass will automatically seemed thin around trees, because of the tree collider. It's just a slight side effect, but a nice one. Try painting some trees, then smaller green cubes to simulate grass. You will see the "grass" mostly falls between trees.

    But I like the clean, friendly UI. It's intuitive. Many assets on the store are...well, confusing to say the least.
     
  44. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,900
    some pretty interesting news i think:

    (http://forum.unity3d.com/threads/184032-Umbra-3-2)

    that will probably speed up tree rendering in a dramatic way!

    lars
     
  45. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Saw that as well, looks very nice!
    Problem is that it might break with random tree rotations?
     
  46. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Wow, that geometry brush editor is awesome, heavyh2o!

    Is there anything I can do to help with the billboard issues? Can you just use virror's screen-capture code to loop through a folder of trees and create png's for each one? Or is there something more that you need for the billboard generation?
     
  47. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Depends on how ppl want to do it, my script works very well for "offline" generation of the textures, but if its needed for runtime i guess render to texture would be a better option since its a lot faster. I have been busy with my own game for the last few days and also i had some strange issues with the LOD groups, they wont show my billboard at all : / So if someone could do some experiments with them it would be nice. Also planning on writing a tree replacement script some day : p
     
  48. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I can't really think of a situation where you'd need runtime generation of billboards. Unless you're dynamically building procedural trees at runtime, your trees are going to be meshes saved in your assets folder, so it seems like you'd want the billboard textures premade as well. I do think a render to texture system would be cleaner though; you could just run it in the editor or have it update the billboard while you're working on your tree instead of having to actually run in play mode and take a bunch of screenshots. It would only work in Pro though, in that case. Sounds like most of us in this thread have Pro though, so maybe that's fine.

    I've been running up against a bunch of new annoying things with Unity's terrain trees in the last week or two (like trying to place decals on trees... apparently their mesh data is hidden in some weird way and not accessible) and I'm going to take the plunge and start switching over to mesh trees. I'll see what I can do with billboard impostors and let you guys know.
     
  49. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    So you probably can't fully understand the increase in performance that impostors allow.

    Yes, you can pre-make impostors and use crossed planes (as I wrote in a post before in this thread) but you occur in very lower quality than real time impostors.
    First of all, being not runtime generated, they won't show up real time shadows (and not even anything similar correct to the lightmapped ones...).
    Second, they usually show quite clearly the line of contact of the planes.



    Real time generated impostors, if well coded, cannot be discerned by the normal mesh.
    In practice, they allow you to render trees (or any far mesh) only every X frames instead than at each frame, and replace the whole mesh with a single billboard.
    This means minimal triangle count and minimal overdraw with maximal visual quality.

    That's why we "need" runtime generation of billboards.

    You can find this interestning.



    I think that modern engines should all implement impostors as part of their optimization system.
    Not only for trees, but for ANY mesh.
    In example, the last lod of an object could ALWAYS be an impostor instead than be culled off.
    Generally, having a system where you can re-render objects every X frames instead than every frame, just based on their distance from the camera and the change in view angle (which is what defines the visual change of static meshes) of the camera would make a huge impact in performance.
     
  50. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Innnnteresting. I thought you meant dynamically creating them at level load rather than actually dynamically recreating them every X frames. A system like this would be awesome, but is also a lot more work than what I was envisioning... ;) It would definitely make a good asset for the asset store though.