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 /
  • Help Room /
This question was closed May 25, 2018 at 01:07 AM by bpears for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by bpears · May 05, 2016 at 01:10 AM · meshverticesvertex

Change vertex position not working?

There are other things going on in the code but this part doesnt seem to be working. I am trying to get the vertex positions, rotate the object, then tell it to have its verts where it was previously, even though the rotation has been changed.

 verts = theMesh.vertices;
 theMesh.transform.localRotation = Quaternion.Euler(0,0,0);
 theMesh.vertices= verts;
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 Cherno · May 05, 2016 at 01:18 AM 0
Share

I guess the verts variable holds a reference to the vertices array, not an instanced copy.

avatar image bpears · May 05, 2016 at 01:27 AM 0
Share

@cherno I had a feeling. I tried to make another array and copy the copy, but that doesn't work. How can I get a clean copy? Do I have to make a copy of the mesh?

avatar image Cherno bpears · May 05, 2016 at 01:31 AM 0
Share

See my answer below :)

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · May 05, 2016 at 03:44 AM

Vertices are defined in local space. So the rotation of the object is irrelevant for the vertex data.

What you have to do is:

  • transform your vertices into worldspace before you rotate your object

  • rotate your object

  • transform the vertices back into local space

Example:

  verts = theMesh.vertices;
  for (int i = 0; i < verts.Length; i++)
      verts[i] = theMesh.transform.TransformPoint(verts[i]);
  theMesh.transform.localRotation = Quaternion.Euler(0,0,0);
  for (int i = 0; i < verts.Length; i++)
      verts[i] = theMesh.transform.InverseTransformPoint(verts[i]);
  theMesh.vertices = verts;

Note: your code seems a bit strange. What exactly is "theMesh"? If it's the actual Mesh instance, you can't get a "transform" from it since it's just a Mesh and not a GameObject. If it's actually a GameObject or MeshFilter you would have to use the mesh property to access the mesh.

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 bpears · May 05, 2016 at 10:45 AM 0
Share

Thank you. Will try this. Yeh the $$anonymous$$esh is the meshfilter mesh.

avatar image
1

Answer by Cherno · May 05, 2016 at 01:31 AM

I use this handy script I found online in all my projects to copy any kind of data.

 using System;
 using System.Reflection;//for copying objects
 using System.Linq;//for string[].Contains()
 using UnityEngine;//for print
 
 public class CopyData {
     
     /// <summary>
     /// Copies the data of one object to another. The target object gets properties of the first. 
     /// Any matching properties (by name) are written to the target.
     /// </summary>
     /// <param name="source">The source object to copy from</param>
     /// <param name="target">The target object to copy to</param>
     public static void CopyObjectData(object source, object target)
     {
         CopyObjectData(source, target, String.Empty, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
     }
     
     /// <summary>
     /// Copies the data of one object to another. The target object gets properties of the first. 
     /// Any matching properties (by name) are written to the target.
     /// </summary>
     /// <param name="source">The source object to copy from</param>
     /// <param name="target">The target object to copy to</param>
     /// <param name="excludedProperties">A comma delimited list of properties that should not be copied</param>
     /// <param name="memberAccess">Reflection binding access</param>
     public static void CopyObjectData(object source, object target, string excludedProperties, BindingFlags memberAccess)
     {
         string[] excluded = null;
         if (!string.IsNullOrEmpty(excludedProperties))
         {
             excluded = excludedProperties.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries);
         }
         
         MemberInfo[] miT = target.GetType().GetMembers(memberAccess);
 
         foreach (MemberInfo Field in miT)
         {
             string name = Field.Name;
             
             // Skip over excluded properties
             if (string.IsNullOrEmpty(excludedProperties) == false
                 && excluded.Contains(name))
             {
                 continue;
             }
             
             
             if (Field.MemberType == MemberTypes.Field)
             {
                 FieldInfo sourcefield = source.GetType().GetField(name);
                 if (sourcefield == null) { continue; }
                 
                 object SourceValue = sourcefield.GetValue(source);
                 ((FieldInfo)Field).SetValue(target, SourceValue);
             }
 
             else if (Field.MemberType == MemberTypes.Property)
             {
                 PropertyInfo piTarget = Field as PropertyInfo;
                 PropertyInfo sourceField = source.GetType().GetProperty(name, memberAccess);
                 if (sourceField == null) { continue; }
                 
                 if (piTarget.CanWrite && sourceField.CanRead)
                 {
                     object targetValue = piTarget.GetValue(target, null);
                     object sourceValue = sourceField.GetValue(source, null);
                     
                     if (sourceValue == null) { continue; }
                     
                     if (sourceField.PropertyType.IsArray
                         && piTarget.PropertyType.IsArray
                         && sourceValue != null ) 
                     {
                         CopyArray(source, target, memberAccess, piTarget, sourceField, sourceValue);
                     }
                     else
                     {
                         CopySingleData(source, target, memberAccess, piTarget, sourceField, targetValue, sourceValue);
                     }
                 }
             }
 
         }
     }
     
     private static void CopySingleData(object source, object target, BindingFlags memberAccess, PropertyInfo piTarget, PropertyInfo sourceField, object targetValue, object sourceValue)
     {
         //instantiate target if needed
         if (targetValue == null
             && piTarget.PropertyType.IsValueType == false
             && piTarget.PropertyType != typeof(string))
         {
             if (piTarget.PropertyType.IsArray)
             {
                 targetValue = Activator.CreateInstance(piTarget.PropertyType.GetElementType());
             }
             else
             {
                 targetValue = Activator.CreateInstance(piTarget.PropertyType);
             }
         }
         
         if (piTarget.PropertyType.IsValueType == false
             && piTarget.PropertyType != typeof(string))
         {
             CopyObjectData(sourceValue, targetValue, "", memberAccess);
             piTarget.SetValue(target, targetValue, null);
         }
         else
         {
             if (piTarget.PropertyType.FullName == sourceField.PropertyType.FullName)
             {
                 object tempSourceValue = sourceField.GetValue(source, null);
                 piTarget.SetValue(target, tempSourceValue, null);
             }
             else
             {
                 CopyObjectData(piTarget, target, "", memberAccess);
             }
         }
     }
     
     private static void CopyArray(object source, object target, BindingFlags memberAccess, PropertyInfo piTarget, PropertyInfo sourceField, object sourceValue)
     {
         int sourceLength = (int)sourceValue.GetType().InvokeMember("Length", BindingFlags.GetProperty, null, sourceValue, null);
         Array targetArray = Array.CreateInstance(piTarget.PropertyType.GetElementType(), sourceLength);
         Array array = (Array)sourceField.GetValue(source, null);
         
         for (int i = 0; i < array.Length; i++)
         {
             object o = array.GetValue(i);
             object tempTarget = Activator.CreateInstance(piTarget.PropertyType.GetElementType());
             CopyObjectData(o, tempTarget, "", memberAccess);
             targetArray.SetValue(tempTarget, i);
         }
         piTarget.SetValue(target, targetArray, null);
     }
 }
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 bpears · May 05, 2016 at 01:37 AM 0
Share

I'll give it a go, thanks!

avatar image Bunny83 · May 05, 2016 at 03:38 AM 0
Share

Uhm, that's pretty pointless ^^. Unity already returns a copy of the vertex array. The problem is that vertex position are defined in local space, not in worldspace. So the actual object rotation is applied in the shader via the model matrix which contains the position, rotation and scale of the object. When you rotate or move an object, it's vertex data won't change at all.

Follow this Question

Answers Answers and Comments

45 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

Related Questions

Why vertex positions appear (0.0, 0.0, 0.0) ? 0 Answers

moving vertices dynamicly relative to other Object 1 Answer

Point on a triangle. 1 Answer

How can I create a plane at any given vertex position? 0 Answers

How to get triangle index of meshe's desired part? 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