Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Polymo · Jan 08, 2013 at 06:55 PM · terrainmeshcustom

custom foliage placement - need tips to increase performance

Hi,
i wrote a script which renders some foliage on a mesh terrain. The problem is, it drops the framerate from about 260 down to 90. I really dont like that..

I'm looking for ways to improve it or maybe write a new and better one.

I hope the following code can explain what it does because if i put that in words, it would be even longer:
(in short: Raycast from a grid around the player down to ground, get pixel, if pixel.green>0.5 draw a mesh with color from that pixel)

 void DrawFoliage () {
         if (draw == true) {
             Collider[] cols = Physics.OverlapSphere(transform.position, radius, groundLayer);
             if (cols.Length == 0) {
                 return;
             }
             
             foreach( Collider col in cols) {
                 if (col.renderer == null || col.renderer.material == null) {
                     return;
                 }
                 FoliageHolder fol = col.GetComponent<FoliageHolder>();
                 Texture2D tex = col.renderer.material.GetTexture("_MixTex") as Texture2D;
                 float strength = col.renderer.material.GetFloat("_LightMix");
                 
                 if (tex != null) {
                     for (int a=0;a<radius;a++) {
                         for (int b=0;b<radius;b++) {
                             Vector3 gpoint = transform.position+new Vector3((-radius/2)+b, 0,  (-radius/2)+a);
                             if (Vector3.Distance(gpoint, transform.position)<radius/2) {
                                 gpoint = new Vector3(Mathf.Round(gpoint.x),gpoint.y, Mathf.Round(gpoint.z));
                                 if (Physics.Raycast(gpoint+Vector3.up*(radius/2),Vector3.down, out hit, radius*2, groundLayer)) {
                                     Color fcol = tex.GetPixel(Mathf.RoundToInt(hit.textureCoord.x*tex.width), Mathf.RoundToInt(hit.textureCoord.y*tex.height));
                                     Debug.DrawRay(gpoint+Vector3.up*(radius/2), Vector3.down*(radius*2), fcol); 
                                     if (fol != null && fol.foliageMesh != null) {
                                         if (fcol.g>0.5f) {
                                             MaterialPropertyBlock propblock = new MaterialPropertyBlock();
                                             propblock.AddColor("_Emission", new Color(fcol.r*strength/2, fcol.g*strength/2, fcol.b*strength/2, 1.0f));
                                             Graphics.DrawMesh(fol.foliageMesh, hit.point, Quaternion.FromToRotation (transform.up, hit.normal), fol.foliageMaterial, 0,null,0,propblock);
                                         }
                                     }
                                 } else {
                                     Debug.DrawRay(gpoint+Vector3.up*(radius/2), Vector3.down*(radius*2), Color.red);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }

Thank you.

Comment
Add comment · Show 3
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 Polymo · Jan 09, 2013 at 03:05 PM 0
Share

hm, ok. i already fixed everything, it now performs very well. Another question that i stumbled upon: how do i scale a mesh drawn with Graphics.Draw$$anonymous$$esh()?

avatar image Polymo · Jan 09, 2013 at 11:48 PM 0
Share

i guess i have to write a special shader for that and input a matrix through the $$anonymous$$aterialBlockProperty?

avatar image Polymo · Jan 11, 2013 at 10:04 PM 0
Share

Any hint for me?

0 Replies

· Add your reply
  • Sort: 

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

8 People are following this question.

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

Related Questions

Lighting Banding Artifact 0 Answers

Questions about render distance in an open world game 0 Answers

How would i go about meshing over several objects? 1 Answer

Need Pointer: How to create 2D map in Unity? 1 Answer

Box Collider on Trees Are Not Rotating When Painted to Terrain 1 Answer


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