Hi , it s a quite noobish question i guess , but i’ve been at it for 3 days without success.
I am doing a classic generative grid mesh and some Perlin. I have troubles with a class .
i have a vetex class that is working perfectly:
public class Vertex {
public Vector3 position;
public Vector3 normal;
public Vector2 uv;
public Color rock;
public float influenceTexture ;
public float influenceTerrain ;
public Zone zone;
public int ID;
public Vertex(){
position = new Vector3();
normal = new Vector3();
uv = new Vector2();
rock = Color.grey;
influenceTexture =0;
influenceTerrain =0;
zone = Zone.empty;
ID = 0;
}
}
public enum Zone
{
empty,sand,water,greens,moss,bamboo,rock,tree,plate,unique,house,other
}
i want to make 2 class one Grid class that hold (1 instance of the second class) Vertices that is equal to an array of (class)Vertex.
I want that array of vertex to be possible to call from the grid or the vertices classes whereever is ok.
My idea is to be able to get the array positions[ ] of the set of vertex by calling grid.vertices.positions[ ]
but also be able to get one position by doing grid.vertices.arrayofvertex[someint].position or grid.arrayofvertex[someint].position
(grid is an instance of the Grid class and so on)
i ve tried both approches :
public class GridA {
public Vertices vertices{ get; set;}
public Grid() {
vertices = new Vertices ();//vertexArray);
}
}
public class VerticesA{
public Vertex[] array{ get; set;}
public Vector3[] positions{ get; set;}
public Vector3[] normals{ get; set;}
public Vector2[] uvs{ get; set;}
public Color[] rocks{get; set;}
public float[] influencesTexture{get; set;}
public float[] influencesTerrain{get; set;}
public Zone[] zones{get; set;}
public int[] ids{get; set;}
public Vertices() {
array = new Vertex[(Garden.data.resolutionX + 1) * (Garden.data.resolutionY + 1)];
positions = new Vector3[array.Length];
normals = new Vector3[array.Length];
uvs = new Vector2[array.Length];
rocks = new Color[array.Length];
influencesTexture = new float[array.Length];
influencesTerrain = new float[array.Length];
zones = new Zone[array.Length];
ids = new int[array.Length];
for (int v = 0, z = 0; z <= Garden.data.resolutionY; z++) {
for (int x = 0; x <= Garden.data.resolutionX; x++, v++) {
array [v] = new Vertex ();
positions [v] = array [v].position;
normals [v] = array [v].normal;
uvs [v] = array [v].uv;
rocks [v] = array [v].rock;
influencesTexture [v] = array [v].influenceTexture;
influencesTerrain [v] = array [v].influenceTerrain;
zones [v] = array [v].zone;
ids [v] = array [v].ID;
}
}
}
}
and
public class GridB {
public Vertices vertices{ get; set;}
public Vertex[] vertexArray{ get; set;}
public Grid() {
vertexArray = new Vertex[(Garden.data.resolutionX + 1) * (Garden.data.resolutionY + 1)];
for (int v = 0, z = 0; z <= Garden.data.resolutionY; z++) {
for (int x = 0; x <= Garden.data.resolutionX; x++, v++) {
vertexArray [v] = new Vertex ();
}
}
vertices = new Vertices(vertexArray);
}
public Grid(int gridResolutionX,int gridResolutionY) {
vertexArray = new Vertex[(gridResolutionX + 1) * (gridResolutionY + 1)];
for (int v = 0, z = 0; z <= Garden.data.resolutionY; z++) {
for (int x = 0; x <= Garden.data.resolutionX; x++, v++) {
vertexArray [v] = new Vertex ();
}
}
vertices = new Vertices(vertexArray);
}
}
public class Vertices{
public Vector3[] positions{ get; set;}
public Vector3[] normals{ get; set;}
public Vector2[] uvs{ get; set;}
public Color[] rocks{get; set;}
public float[] influencesTexture{get; set;}
public float[] influencesTerrain{get; set;}
public Zone[] zones{get; set;}
public int[] ids{get; set;}
public Vertices(Vertex[] actualArray) {
positions = new Vector3[actualArray.Length];
normals = new Vector3[actualArray.Length];
uvs = new Vector2[actualArray.Length];
rocks = new Color[actualArray.Length];
influencesTexture = new float[actualArray.Length];
influencesTerrain = new float[actualArray.Length];
zones = new Zone[actualArray.Length];
ids = new int[actualArray.Length];
for (int v = 0, z = 0; z <= Garden.data.resolutionY; z++) {
for (int x = 0; x <= Garden.data.resolutionX; x++, v++) {
positions [v] = actualArray [v].position;
normals [v] = actualArray [v].normal;
uvs [v] = actualArray [v].uv;
rocks [v] = actualArray [v].rock;
influencesTexture [v] = actualArray [v].influenceTexture;
influencesTerrain [v] = actualArray [v].influenceTerrain;
zones [v] = actualArray [v].zone;
ids [v] = actualArray [v].ID;
}
}
}
}
in GridA approach i would get 1 vertex by going grid.vertices.array[theIndexOfTheVertexINeed] but i also could grab all the vertices positions like so : grid.vertices.positons = anArrayOfVector3;
in GridB approach i do the same with another syntax grid.vertexArray[theIndexOfTheVertexINeed] for one vertex , and still grid.vertices.positons = anArrayOfVector3; for the positions
Either syntax is fin for me , but it doesn’t work because what i want to do in the Vertices class is not working .
When i iterate though the double for loops , it creates copy of the values in the array , not references . and i dont know how to do it differently .
What i dont want to do is to have to put everything in a update to make them constantly equal to each other .
when i write to arrays like positions[ ] in the vertices class , everything’s fine , but if i try to get one vertex.position somewhere else it is not equal to what i wrote inside the positions[ ]
i hope it is clear . My question could be asked as , how do i make references (pointer?) to the same variable
so that if i write grid.vertices.positions[10] = new Vector3(1f,2f,3f); and somewhere else i try to get the vertex position doing Debug.Log(grid.vertexArray[10].position); i get 1,2,3 and not 0,0,0
