• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Hardik-somani · Jun 04, 2015 at 02:27 PM · unity 4.6verticesmeshfilter

hello... anyone know how to store mesh vertices in vector3 array and whenever i want,i asign to object for mesh formation purpose.i am begginer in unity3d games please help..in advance Thank's..!

folloing the code of oject deformation....i want object reformation after deformation

Code:

public class CarDamage2 : MonoBehaviour { public float maxMoveDelta = 1.0f; // maximum distance one vertice moves per explosion (in meters) public float maxCollisionStrength = 50.0f; public float YforceDamp = 0.1f; // 0.0 - 1.0 public float demolutionRange = 2f; public float impactDirManipulator = 0.0f; public MeshFilter[] optionalMeshList;

 private MeshFilter[] meshfilters;
 private float sqrDemRange;

 Vector3 [] oldVerts=new Vector3[100];
 private Mesh[] myMesh;
 Vector3[] vertices;
 Vector3[] normals;

 public void Start()
 {
     //maxMoveDelta = Mathf.Clamp01(maxMoveDelta);        
     if(optionalMeshList.Length>0)
         meshfilters = optionalMeshList;
     else
         meshfilters = GetComponentsInChildren<MeshFilter>();
         
     sqrDemRange = demolutionRange*demolutionRange;
         
     /* doesnt work, strange physic behaviour
     for(int j=0; j<meshfilters.Length; ++j)
     {
            if(!meshfilters[j].transform.collider)
             {
             meshfilters[j].gameObject.AddComponent<BoxCollider>();        
             meshfilters[j].transform.collider.isTrigger = true;    
         }
     }*/
     oldVertices ();
 }
 
 public void OnCollisionEnter( Collision collision ) 
 {
     Vector3 colRelVel = collision.relativeVelocity;
     colRelVel.y *= YforceDamp;
     
     Vector3 colPointToMe = transform.position - collision.contacts[0].point;

     // Dot = angle to collision point, frontal = highest damage, strip = lowest damage
     float colStrength = colRelVel.magnitude * Vector3.Dot(collision.contacts[0].normal, colPointToMe.normalized);

     OnMeshForce( collision.contacts[0].point, Mathf.Clamp01(colStrength/maxCollisionStrength) );
     //Debug.DrawLine(collision.contacts[0].point, transform.position, Color.red);
     //Debug.Break();
 }


 // if called by SendMessage(), we only have 1 param
 public void OnMeshForce( Vector4 originPosAndForce )
 {
     OnMeshForce( (Vector3)originPosAndForce, originPosAndForce.w );
 }

 public void OnMeshForce( Vector3 originPos, float force )
 {
     // force should be between 0.0 and 1.0
     force = Mathf.Clamp01(force);
     
     for(int j=0; j<meshfilters.Length; ++j)
     {
         Vector3 [] verts = meshfilters[j].mesh.vertices;

         for (int i=0;i<verts.Length;++i)
         {
             Vector3 scaledVert = Vector3.Scale( verts[i], transform.localScale );                        
             Vector3 vertWorldPos = meshfilters[j].transform.position + (meshfilters[j].transform.rotation * scaledVert);
             Vector3 originToMeDir = vertWorldPos - originPos;
             Vector3 flatVertToCenterDir = transform.position - vertWorldPos;
             flatVertToCenterDir.y = 0.0f;
                
             // 0.5 - 1 => 45° to 0°  / current vertice is nearer to exploPos than center of bounds
             if( originToMeDir.sqrMagnitude < sqrDemRange ) //dot > 0.8f )
             {
                 float dist = Mathf.Clamp01(originToMeDir.sqrMagnitude/sqrDemRange);
                 float moveDelta = force * (1.0f-dist) * maxMoveDelta;

                 Vector3 moveDir = Vector3.Slerp(originToMeDir, flatVertToCenterDir, impactDirManipulator).normalized * moveDelta;

                 verts[i] += Quaternion.Inverse(transform.rotation)*moveDir;

     
                 /*Debug.DrawRay(vertWorldPos, moveDir, Color.red);
                 Debug.DrawLine(vertWorldPos, transform.position, Color.green);
                 Debug.Break();*/
             }

         }
    
         meshfilters[j].mesh.vertices = verts;
         meshfilters[j].mesh.RecalculateBounds();
     }
 } 

 public void oldVertices()
 {

     /*for (int j=0; j<meshfilters.Length; ++j)
     {
         oldVerts = meshfilters [j].mesh.vertices;
         Debug.Log("J:" +j);
     }*/
 }

 public void setMesh()
 {
     /*
     Debug.Log("Call set Mesh");
     for (int j=0; j<meshfilters.Length; ++j)
     {        
         meshfilters[j].mesh.vertices = oldVerts;
         meshfilters[j].mesh.RecalculateBounds();
     }*/
 
 }

}

Comment
Add comment · Show 2
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 Cherno · Jun 04, 2015 at 02:33 PM 0
Share

Please fix your title. Things like "I'm a beginner" and "thanks" belong into the main body, not in the title line. Also please format your code. The first couplbe of lines are not formatted as such. It helps people read and understand.

avatar image Glurth · Jun 04, 2015 at 02:39 PM 0
Share

I would recommend more details about what the problem with all this code actually is, where in the code it lies, what it does now as opposed to what it should do, etc...

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Mesh Vertices Welding 0 Answers

Why vertex positions appear (0.0, 0.0, 0.0) ? 1 Answer

How to calculate the percentage of a mesh visible by a camera? 2 Answers

Getting the world position of mesh vertices? 2 Answers

Mesh.vertices are all 0? 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