- Home /
how to get all points along a mesh
Hi all; I have a problem at the moment in which I'm trying to get all the points along a mesh object I've created using a c# script. Any advise will be greatly received. Here is the screenshot of the mesh:
Code to do all this:
using UnityEngine;
using System.Collections;
public class CreatePlaneMesh : MonoBehaviour {
public float width = 50f;
public float height = 50f;
public Material mat; // material variabl
// Use this for initialization
void Start () {
gameObject.AddComponent<MeshFilter>();
MeshFilter mf = GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
mf.mesh = mesh;
//vertices
Vector3[] vertices = new Vector3[4]; //create 4 slots to store vectors to create plane
vertices[0] = new Vector3 (width, 0, 0);
vertices[1] = new Vector3 (width, 0, height);
vertices[2] = new Vector3 (0, 0, height);
vertices[3] = new Vector3 (0, 0, 0);
mesh.vertices = vertices;
//get all vertex positions of the mesh object
mesh = GetComponent<MeshFilter>().mesh;
Vector3[] v = mesh.vertices;
int i = 0; while (i < v.Length){
v[i]+= Vector3.up * Time.deltaTime;
i++;
}
vector3[] worldPOS = new Vector3[4];
for(int j=0; j < 4; j++) {
worldPOS = new vector3(transform.Transformpoint(verices[j])));
}
mesh.vertices = vertices;
mesh.RecalculateBounds();
//
int[] indices = new int[6];
indices[0] = 0; indices[1] = 3; indices[2] = 2;
indices[3] = 0; indices[4] = 2; indices[5] = 1;
mesh.SetIndices(indices, MeshTopology.Triangles, 0);
gameObject.AddComponent<MeshRenderer>();
MeshRenderer mr = GetComponent<MeshRenderer>();
mr.material.color = Color.green;
mesh.RecalculateNormals();
Error message: The type 'UnityEngine.Vector3' does not contain a constructor that takes '1' arguments
What do you mean by "all the points"? You clearly have them all now, in lines 23-26.
Do you mean the verts in world space? In that case, just use the standard way: planeA.transformDirection(someVert);
I mean points by obtaining all the vertex positions in the world space as I'm looking to do some sub-dividing of spaces for a procedural terrain project I'm currently working on. So the idea would be to obtain a random position along one of the edges of the mesh and then find another random position on the opposite edge so I can draw a line to simulate the partitioning. The idea is to keep repeating the process until I have enclosed spaces in which I can place objects such as buildings, trees and so on. Thanks for your response, I will try your suggestion.
Answer by robertbu · Jan 23, 2014 at 06:33 PM
The verties in a mesh are in local coordinates. To convert local coordinaes to world coordinates use Transform.TransformPoint():
Vector3 worldPos = transform.TransformPoint(v[i]);
Is your question answered? If so please tick the answer as solution to mark this question as answered:
I ran into an array out of index error then trying to insert the line into the code
Can you edit your question to include he changed code? And can you include the error message from the console? Note that this line must be executed inside your 'while' loop for each point in the array. If you execute it outside the 'while' loop, then 'i' will be beyond the end of the array.
Question edited - lines 38-41. Error message at the bottom.