Search code examples
c#unity-game-engineverticesmultiplication

How do i create multiple vertices from an object?


i am having rather a bit of trouble on how to multiply vertices from an object/model(Quad) across an axis(Z or X) like in Pic1 i gather the vertices from the object/model(Quad) then duplicate the vertices along an axis(but have it acceptable for triangles too), i'm having trouble doing this the results are in Pic2.

Would anyone know how to do this?

-Pic1.(what i am trying to achieve)

enter image description here

-Pic2.(the results i am getting)

enter image description here

And here is the code so you know what i have done.

using UnityEngine;
using System.Collections;

public class Extruder : MonoBehaviour {

public Mesh prefab;
public int length;
private Mesh mesh;

private Vector3[] vertices;
private Vector3[] mv;
// Use this for initialization
void Start()
{

    vertices = prefab.vertices;
    Generate();
}
// Update is called once per frame
void Generate () {

    vertices = new Vector3[(length)];
    for (int i = 0, x = 0; x <= length; i++, x++)
    {
        vertices[i] = new Vector3(0, 0, x);
    }
    mesh.vertices = vertices;
}
private void OnDrawGizmos()
{
    Gizmos.color = Color.black;
    for (int i = 0; i < vertices.Length; i++)
    {
        Gizmos.DrawSphere(vertices[i], 0.1f);
    }
}
}

Solution

  • EDIT: Resource on creating vertex arrays for Unity:

    These are both pretty good, and recent:

    http://jayelinda.com/modelling-by-numbers-part-1a/

    http://catlikecoding.com/unity/tutorials/procedural-grid/


    It's doing exactly what you asked.

    You said:

    Draw points along the Z-axis. (0,0,1), (0,0,2), (0,0,3), (0,0,4) .. etc

    I think what you wanted was:

    Draw points OFFSET FROM THE ORIGINAL by steps along the Z-axise. (x+0, y+0, z+1), (x+0, y+0, z+2), (x+0, y+0, z+3)... etc

    So, you've got a lot of code to write:

    1. Read the vertices from the mesh (where's your code to do this? You're not doing it yet)
    2. For each vertex, read the x, y, and z coords (ditto)
    3. For each vertex, add to the z coord but not the x and y

    You will, of course, only be able to do this once. After that, teh mesh is no longer a flat quad in the Z=0 plane, and your "extrusion" would extrude exponentially, and go horribly wrong. Probably you should check that the mesh has a maximum of 4 vertices before you start (except some quads have 6 vertices ...)

    HOWEVER.

    This is not how meshes work.

    You are missing approximately 20-100 lines of code, minimum. Look for some of the tutorials on procedural mesh generation.

    All the extra steps are NOT optional! If you miss them out, sometimes Unity will error, but usually it will silently corrupt the mesh, and/or break your lighting, and/or render nothing. There are no error messages for most of the steps if you forget to do them!

    At the VERY LEAST, you MUST HAVE:

    1. a Vector3 for every vertex
    2. a set of 3 ints for every triangle

    ...without that, it's literally impossible for Unity to do anything.

    You also need, in order to make Unity render it without crashes:

    1. A normal for every vertex
    2. A normalization routine that configures normals correctly (Unity does NOT do this automatically)
    3. A U,V for every vertex