Search Unity

How to mark the vertices on a mesh and make the markers follow them as you apply transformations

Discussion in 'Scripting' started by aliens9889, Apr 28, 2018.

  1. aliens9889

    aliens9889

    Joined:
    Nov 21, 2014
    Posts:
    6


    Hi everybody I want to replicate this example, but I don't know where to start, If you could help with some script, info, tutorial, project, or anything that help me and let me do this same thing I'll really appreciate it.

    Thank you very much!!
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Well, they're probably using Gizmos.DrawCube for the markers, and I think they're looping through the vertices of the mesh. The transformations are simple. The transformations are simple, just use transform.TransformPoint() on each of them.
    So, the pseudo code looks something like this:
    Code (CSharp):
    1. Mesh mesh;
    2.  
    3. void OnDrawGizmosSelected() //Draw the markers, if the object is selected
    4.     Gizmos.color = Color.red
    5.     for each vertex in the Mesh
    6.         worldPos = transform.TransformPoint(vertex)
    7.         Gizmos.DrawCube(worldPos, Vector3.one * 0.1f)
    8.     endfor
    9. end
     
  3. aliens9889

    aliens9889

    Joined:
    Nov 21, 2014
    Posts:
    6
    Hi gorbit99 How are you? On your code there are some things that I don't understand for example:

    in the for each with the mesh I will have to use mesh.vertices?
    the worldPos would be a Vector3 or Transform?
    the method as well would going to Start() or Update() ?
    And you use Mesh only, there is a initialize that would be necessary for this?
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Learn C# and then watch the official unity tutorials. I just can't help you if you don't have the basic knowledge required
     
  5. aliens9889

    aliens9889

    Joined:
    Nov 21, 2014
    Posts:
    6
    I'm asking because checking the code I got this as a error I made the adjustment necessaries but I don't know what I missing

    Code (CSharp):
    1. ArgumentNullException: Argument cannot be null.
    2. Parameter name: _unity_self
    3. UnityEngine.Mesh.GetAllocArrayFromChannel[Vector3] (InternalShaderChannel channel, InternalVertexChannelType format, Int32 dim) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Mesh.cs:54)
    4. UnityEngine.Mesh.GetAllocArrayFromChannel[Vector3] (InternalShaderChannel channel) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Mesh.cs:68)
    5. UnityEngine.Mesh.get_vertices () (at /Users/builduser/buildslave/unity/build/Runtime/Export/Mesh.cs:145)
    6. MarkVertexFour.Start () (at Assets/Code/MarkVertexFour.cs:16)
     
  6. aliens9889

    aliens9889

    Joined:
    Nov 21, 2014
    Posts:
    6
    I updated the code this way

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MarkVertexFour : MonoBehaviour {
    6.  
    7.     Mesh mesh;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.            
    13.         Gizmos.color = Color.red;
    14.         mesh = GetComponent<Mesh>();
    15.  
    16.         foreach (Vector3 vertex in mesh.vertices) {
    17.             Vector3    worldPos = transform.TransformPoint(vertex);
    18.             Gizmos.DrawCube(worldPos, Vector3.one * 0.1f);
    19.         }
    20.    
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.        
    26.     }
    27.  
    28. }
    29.  
    And this is showed up on console and I'm not sure what could I missing

    Screen Shot 2018-05-01 at 11.52.31.png
     
  7. aliens9889

    aliens9889

    Joined:
    Nov 21, 2014
    Posts:
    6
    Any suggestion? About What am I missing? Please
     
  8. enghqii

    enghqii

    Joined:
    Sep 4, 2015
    Posts:
    5
    Seems like your mesh doesn't have any vertex.