Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Madkrumper9 · Dec 29, 2020 at 06:44 PM · meshmeshcollidermesh verticesmesh manipulationadvanced

Flawed Mesh Manipulation Question!

I have the deforming mesh script curtesy of World of Zero (https://www.youtube.com/watch?v=l_2uGpjBMl4) and it seems to be pretty flawed. It doesn't work when I have it above a certain y value or when it's rotated. I'm looking to fix that but I suck at mesh manipulation.

Heres the deformable mesh script:

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 [RequireComponent(typeof(GeneratePlaneMesh))]
 public class DeformableMesh : MonoBehaviour
 {
 
     public float maximumDepression;
     public List<Vector3> originalVertices;
     public List<Vector3> modifiedVertices;
 
     private GeneratePlaneMesh plane;
 
     public void MeshRegenerated()
     {
         plane = GetComponent<GeneratePlaneMesh>();
         plane.mesh.MarkDynamic();
         originalVertices = plane.mesh.vertices.ToList();
         modifiedVertices = plane.mesh.vertices.ToList();
         Debug.Log("Mesh Regenerated");
     }
 
     public void AddDepression(Vector3 depressionPoint, float radius)
     {
         var worldPos4 = this.transform.worldToLocalMatrix * depressionPoint;
         var worldPos = new Vector3(worldPos4.x + transform.position.x, worldPos4.y, worldPos4.z + transform.position.z);
         Debug.Log(worldPos);
         for (int i = 0; i < modifiedVertices.Count; ++i)
         {
             var distance = (worldPos - (modifiedVertices[i] + Vector3.down * maximumDepression)).magnitude;
 
             //and if verticy height is higher than maximum height
             if (distance < radius)
             {
                 var newVert = originalVertices[i] + Vector3.down * maximumDepression;
                 // times depression per hit instead of maximum depression
 
                 modifiedVertices.RemoveAt(i);
                 modifiedVertices.Insert(i, newVert);
             }
         }
 
         plane.mesh.SetVertices(modifiedVertices);
         Debug.Log("Mesh Depressed");
     }
 }

I will be forever grateful if someone helps out. I've never had a question answered on this stupid site but 10+ hours have gone into this.

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 Eno-Khaon · Dec 29, 2020 at 08:04 PM

There are a few things that can be addressed here:

First, by this use of a radius away from the point(s) of contact, if the modified vertices have already been shoved far enough away (down?), then their neighboring vertices would become hard to reach using that radius. You could dig an incredibly deep pit (theoretically) and not be able to lower adjacent vertices if the height difference is currently greater than the provided radius (for better or worse). Similarly, depressions made are a fixed depth, but that seems to be the result of the video cutting the process short and not working things through to completion.

Next, regardless of whether the depression is scaled by the distance from the point of contact (it's currently not), an unnecessary square root calculation is making this whole process needlessly taxing.

 // Untested, but should be a more straight-forward approach to this
 // (Also, not having looked into how the video author generated the plane mesh,
 // this assumes that its vertices are centered around or near a local (0, 0, 0) )
 public void AddDepression(Vector3 depressionPoint, float radius)
 {
     // Simplified converting contact point to local position
     Vector3 contactPoint = plane.transform.InverseTransformPoint(depressionPoint);
     for(int i = 0; i < originalVertices.Count; i++)
     {
         float distance = (contactPoint - originalVertices[i]).sqrMagnitude;
         if(distance <= radius)
         {
             // Since changes are made locally to the mesh, this uses
             // Vector3.down rather than transform.down
             Vector3 newVertex = originalVertices + Vector3.down * maximumDepression;
             
             // Changed this because... why was it handled the way it was?
             modifiedVertices[i] = newVertex;
         }
     }
     plane.mesh.SetVertices(modifiedVertices);
 }


Unfortunately, this approach to handling ground deformation will always see significant performance hits as the vertex count/ground complexity increases. Ultimately, this leads most modern games to use a mainly- Shader-driven system for mostly-fixed deformations like snow.
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 Madkrumper9 · Dec 29, 2020 at 10:38 PM 0
Share

YES!! It works! I cannot thank you enough. Your the first person to help with the problem and not just provide a link.

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

128 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

Related Questions

Is it possible to enable Read/Write of a imported Mesh at Runtime? 0 Answers

Remove unnecessary vertices from plane mesh 1 Answer

Is there any way to get the optimized (cooked) mesh of a mesh collider? 0 Answers

How to manipulate(flatten) the Mesh of an object 0 Answers

Edit Mesh Edge to Match Overlapping Object or Obstruction 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