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
1
Question by UnityNoob123 · Mar 17, 2018 at 02:42 AM · meshoptimizationverticesdeformation

Vertex Mesh Optimization Question

Hey thanks for checking out my question! I currently have a grenade script that will pass in variables to my terrain mesh deformation script and loop through all the vertices in the terrain mesh and modify those that are within a certain radius. However, I was wondering if there was a way to avoid looping through all the vertices and just grab the vertices within the grenade radius? I noticed that with a raycasthit, you can retrieve the index for the triangle, but I cannot seem to find something like that for overlapshpere. Any help is appreciated, thanks.

 public class Grenade : MonoBehaviour {
 
     public float explosionDelay;
     public float explosionRadius;
     public float explosionPower;
     public LayerMask effectLayer;
 
     void Start () {
         StartCoroutine(Explode());
     }
 
     IEnumerator Explode() {
         yield return new WaitForSeconds(explosionDelay);
         Collider[] _hit = Physics.OverlapSphere(transform.position, explosionRadius, effectLayer);
         for (int i = 0; i < _hit.Length; i++) {
             if (_hit[i].GetComponent<DeformTerrain>())
             _hit[i].GetComponent<DeformTerrain>().DeformMesh(transform.position, explosionRadius, explosionPower);
         }
         Destroy(gameObject);
     }
 }
 
 public class DeformTerrain : MonoBehaviour {
 
     private MeshFilter meshf;
     private Vector3[] verts;
 
     public void DeformMesh(Vector3 _Position, float _radius, float _power) {
         meshf = GetComponent<MeshFilter>();
         verts = meshf.mesh.vertices;
         for (int i = 0; i < verts.Length; i++) {
             Vector3 worldVert = transform.TransformPoint(verts[i]);
             float _dist = Vector3.Distance(worldVert, _Position);
             if (_dist < _radius) {
                 verts[i].y -= _power * (_dist/2); //This makes it a bit more rounded
             }
         }
         Destroy(GetComponent<MeshCollider>());
         meshf.mesh.vertices = verts;
         gameObject.AddComponent<MeshCollider>();
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 17, 2018 at 02:58 AM

Well, it depends on how your vertices are arranged in the array. If it follows some logic you could work out a sub region of your vertices array. However if you have no idea how the vertices a distributed in the array you have no other way than iteration over all. Though there is a major improvement to your code: Do not transform ever single vertex into worldspace. Instead just transform your "_Position" into the local space of the terrain object. That way you can work with local space coordinates. Also you may want to check the squared distance for slightly better performance.

 public void DeformMesh(Vector3 _Position, float _radius, float _power)
 {
     meshf = GetComponent<MeshFilter>();
     verts = meshf.mesh.vertices;
     Vector3 pos = transform.InverseTransformPoint(_Position);
     float r2 = _radius * _radius;
     for (int i = 0; i < verts.Length; i++)
     {
         float _dist = (verts[i] - pos).sqrMagnitude;
         if (_dist < r2)
             verts[i].y -= _power * (Mathf.Sqrt(_dist)/2);
     }
     Destroy(GetComponent<MeshCollider>());
     meshf.mesh.vertices = verts;
     gameObject.AddComponent<MeshCollider>();
 }
Comment
Add comment · Show 2 · 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 Bunny83 · Mar 17, 2018 at 03:05 AM 0
Share

Note that the biggest performance killer is your $$anonymous$$eshCollider. However to further improve your script you may want to pull those two lines into the Start method of your "DeformTerrain " class:

 meshf = GetComponent<$$anonymous$$eshFilter>();
 verts = meshf.mesh.vertices;

Of course this would only work when this is the only script that is manipulating the terrain mesh because in this case we don't need to read the vertices back every time we deform the mesh.

avatar image UnityNoob123 · Mar 17, 2018 at 03:34 AM 0
Share

Awesome, thanks for your help.

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

89 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

Related Questions

Making holes into a mesh 1 Answer

How to reduce polygon count? I have meshes from pointclouds. 0 Answers

How to find smallest length between hit point and vertices 1 Answer

Why is my mesh peeling off when I try to deform it during runtime? 1 Answer

Ways of modificating mesh triangles 3 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