Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by dan_wipf · Dec 03, 2018 at 10:18 PM · shadertreeperformance optimizationvertex shaderwind

Vertex Animation Shader Performance (tree vegetation)

Hi, my problem is not the Shader, but the Performance is horrible. Like if i have a scene with Custom Trees and they’re Vertex animated for Wind.

is it usefull to put the animation parameter in a cginc file? or how can i get a good performance boost? like animate the parameter in c# or something..

thanks dan


Shader:


 Shader "TreeWind/Wind_Main" {
 
  
 
     Properties {
 
         _MainTex ("Main Texture", 2D) = "white" {}
         _BumpMap ("Normalmap", 2D) = "bump" {}
         _Color ("Color", Color) = (1,1,1,1)
         _AlphaCutoff("Alpha Cutoff",range(0,1)) = 0.03 
 
         _wind_dir ("Wind Direction", Vector) = (0.5,0.05,0.5,0)
 
 
         _tree_sway("Tree Sway Offset", range(0,1)) = 0.2
         _tree_sway_disp ("Tree Sway Displacement", range(0,1)) = 0.3
         _tree_sway_speed ("Tree Sway Speed", range(0,10)) = 1
         _wind_size ("Wind Wave Size", range(50,5)) = 15
 
     }
 
  
 
     SubShader {
         Tags {
             //"Queue"="AlphaTest"
             "RenderType"="Opaque"
         }
         LOD 400
         cull off
 
         CGPROGRAM
         #include "TerrainEngine.cginc"
         #pragma target 5.0
 
         #pragma surface surf Lambert vertex:vert addshadow fullforwardshadows 
 
 
 
             //Declared Variables
 
             float4 _wind_dir;
             float _wind_size;
             float _tree_sway_speed;
             float _tree_sway_disp;
             float _tree_sway;
             fixed _AlphaCutoff;
 
 
             sampler2D _BumpMap;
             sampler2D _MainTex;
 
             fixed4 _Color;
 
 
 
                 //Structs
 
                 struct Input {
 
                     float2 uv_MainTex;
                     float2 uv_BumpMap;
                     float3 viewDir;
                     
 
                 };
 
 
 
                 // Vertex Manipulation Function
 
                 void vert (inout appdata_full i) {
 
 
                      //Gets the vertex's World Position 
 
                     float3 worldPos = mul (unity_ObjectToWorld, i.vertex).xyz;
 
 
                     //Tree Movement and Wiggle
 
                     i.vertex.x += (cos(_Time.z * _tree_sway_speed + (worldPos.x/_wind_size) + (sin(_Time.z * _tree_sway * _tree_sway_speed + (worldPos.x/_wind_size)) * _tree_sway) ) + 1)/2 * _tree_sway_disp * _wind_dir.x * (i.vertex.y / 10) + 
                     cos(_Time.w * i.vertex.x * _tree_sway + (worldPos.x/_wind_size)) * _tree_sway * _wind_dir.x * i.color.b;
 
                     i.vertex.z += (cos(_Time.z * _tree_sway_speed + (worldPos.z/_wind_size) + (sin(_Time.z * _tree_sway * _tree_sway_speed + (worldPos.z/_wind_size)) * _tree_sway) ) + 1)/2 * _tree_sway_disp * _wind_dir.z * (i.vertex.y / 10) + 
                     cos(_Time.w * i.vertex.z * _tree_sway + (worldPos.x/_wind_size)) * _tree_sway * _wind_dir.z * i.color.b;
 
                     i.vertex.y += cos(_Time.z * _tree_sway_speed + (worldPos.z/_wind_size)) * _tree_sway_disp * _wind_dir.y * (i.vertex.y / 10);
 
 
 
                     //Branches Movement
 
                     i.vertex.y += sin(_Time.w * _tree_sway_speed + _wind_dir.x + (worldPos.z/_wind_size)) * _tree_sway  * i.color.r;
 
 
                 }
 
                 // Surface Shader
 
                 void surf (Input IN, inout SurfaceOutput o) {
                     
                     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
                     
                     o.Albedo = c.rgb;
                     o.Alpha = c.a;
                     o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
                     clip(tex2D(_MainTex, IN.uv_MainTex).a * c.a - _AlphaCutoff);
 
 
                 }
         ENDCG
         }
     Fallback "Legacy Shaders/Bumped Diffuse"
 } 
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Arycama · Aug 01, 2018 at 12:54 PM

Does disabling the vertex animation improve framerate? If not, how many trees do you have and how many vertices are you rendering? Each vertex you are trying to render will need to run the vertex shader code. More vertices = less performance.

A cginc does nothing for performance. It is used so you don't have to re-write code that you want to share between different shaders. All shaders get compiled down to assembly code before being run, so the GPU has no concept of a cginc file.

If you post the vertex shader code I can tell you if there's any optimizations you can make.

Comment
Add comment · 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 dan_wipf · Aug 02, 2018 at 05:42 AM 0
Share

@Arycama Hi, thanks for the Answer! Im working on a LOD System with quite a lot trees in the Scene. Lod0 = +-7000 verts, Lod +- 6000 verts, billboard 16 verts.

yes if there's no "Wind" if I set the speed to 0 fps are increasing. Well I'm just curious if it's cheaper to calculate Wind movement outside of the Shader or if it's ok like im doing it now(improbable).


I'd like to add some tumbling to the shader(branches)


you can get a $$anonymous$$i test scene here at GitHub https://github.com/danwipf/TreeShader

ShaderCode I provide in my Question.

avatar image
2

Answer by CarpeFunSoftware · Dec 05, 2018 at 12:44 PM

It's heavily math bound, the rest is pretty "normal". So the math on each vertex is killing your performance.

Thoughts: 1) Pre-calc things rather than re-calc. Make a work vector for worldPos / _wind_size and use vector based math to do the divide. Use that result instead of calcing each factor (vector math is faster). You seem to be using that expression (worldPox.x/_wind_size) several times...calc it once. 2) Consider using a lookup table for sin and cos if you think it will help. 3) similar things for other parts...I.vertex.y/10 appears several times. Do the math once, then use the resulting variable. Maybe time.z * tree_sway_speed could be passed in as a uniform once. For example. Calc things in C# if you can set it once per frame, calc it once in the shader if you must (vert specific stuff). Never calc things multiple times. Some compilers may optimize repeats out, but some may not. So do it yourself. Also, where possible, multiply first, then add. MAD methodology.

4) Consider a completely different approach that just calcs a result for all trees in a table, and then "just looks up" the individual tree's values...like there's 10 variations, and each tree is one of 10 status values. Kind of like a flipbook where you're just using an index. Then you won't have to do the math N times (if you have 1000 trees, with 7000 verts, that's a lot of math). 5) Make sure you cull trees you don't need (you indicated billboards, good job)

Just some thoughts. :)

Comment
Add comment · 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 SunnyChow · Dec 06, 2018 at 08:25 AM 0
Share

agree. especially point 2

avatar image
0

Answer by hector_gutierrezc_tvj32015 · Jun 28, 2018 at 01:51 PM

The best ones i have seen have a cginc, also they work with a wind zone

Comment
Add comment · 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 dan_wipf · Jun 28, 2018 at 02:22 PM 0
Share

have you some reference? @hector_gutierrezc_tvj32015

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.

Follow this Question

Answers Answers and Comments

138 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 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

Nature/Leaf shader gaps between mesh edges 0 Answers

Add new vertex attributes for a shader,Create vertex attributes 0 Answers

After displacing through shader, mesh is same when accessed through script. 2 Answers

Does dynamic batching and vertex shader work together? 1 Answer

Mesh Particle Transparency 0 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