1

Hi I have this data coming from an API and I know it is a cube:

    {
  "position": {
    "itemSize": 3,
    "type": "Float32Array",
    "array": [
      0.5,
      0.5,
      0.5,
      0.5,
      0.5,
      -0.5,
      0.5,
      -0.5,
      0.5,
      0.5,
      -0.5,
      -0.5,
      -0.5,
      0.5,
      -0.5,
      -0.5,
      0.5,
      0.5,
      -0.5,
      -0.5,
      -0.5,
      -0.5,
      -0.5,
      0.5,
      -0.5,
      0.5,
      -0.5,
      0.5,
      0.5,
      -0.5,
      -0.5,
      0.5,
      0.5,
      0.5,
      0.5,
      0.5,
      -0.5,
      -0.5,
      0.5,
      0.5,
      -0.5,
      0.5,
      -0.5,
      -0.5,
      -0.5,
      0.5,
      -0.5,
      -0.5,
      -0.5,
      0.5,
      0.5,
      0.5,
      0.5,
      0.5,
      -0.5,
      -0.5,
      0.5,
      0.5,
      -0.5,
      0.5,
      0.5,
      0.5,
      -0.5,
      -0.5,
      0.5,
      -0.5,
      0.5,
      -0.5,
      -0.5,
      -0.5,
      -0.5,
      -0.5
    ],
    "normalized": false
  },
  "normal": {
    "itemSize": 3,
    "type": "Float32Array",
    "array": [
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      -1,
      0,
      0,
      -1
    ],
    "normalized": false
  },
  "uv": {
    "itemSize": 2,
    "type": "Float32Array",
    "array": [
      0,
      1,
      1,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      1,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      1,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      1,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      1,
      1,
      0,
      0,
      1,
      0,
      0,
      1,
      1,
      1,
      0,
      0,
      1,
      0
    ],
    "normalized": false
  }
}

How can I now create from these data a cube and its material in Unity? Is there a library or a built-in function?

Suisse
  • 3,467
  • 5
  • 36
  • 59

1 Answers1

2

First in general for parsing your JSON data into useful structure you need to have an according class structure and the use e.g. JsonUtility

[Serializable]
public class DataSet
{
    public int itemSize;
    public int[] array;
    public bool normalized;
}

[Serializable]
public class Example
{
    public DataSet position;
    public DataSet normal;
    public DataSet uv;
}

and do

var data = JsonUtility.FromJson<Example>(yourJsonString);

Next this data is still quite raw and we have to parse it further since we want valid vector values

var vertices = new List<Vector3>();

for(var i = 0; i < data.position.array.Length; i+=3)
{
    if(i+2 > data.position.array.Length - 1) break;

    var x = data.position.array[i];
    var y = data.position.array[i+1];
    var z = data.position.array[i+2];
    vertices.Add(new Vector3(x,y,z));
}

And similar for normals and UVs(just that here you use Vector2 instead)


Then to the main question: Since you already have positions (I assume vertices), normals and UVs I guess you are wondering where to get the triangles from.

UVs don't help here at all since they are only texture coordinates defining how the texture is later mapped on triangles.

Also the normals can't really help you here. A normal is just a direction and doesn't provide any helpful information about which three vertices together make up a triangle.

To get the triangles would only be possible if you know e.g. that always 3 sequential vertices make up a triangle - meaning your mesh has duplicate vertices.

Otherwise I'ld say it is not possible to know that triangles from your given data.

In Unity the triangles is simply a flat int[] where always three sequential elements are the indexes of the three vertices from the vertices array.

So under the one assumption that they are just consequent something like

var triangles = new int[vertices.Length]; 
for(var i = 0; i < triangles.Length; i++)
{
    triangles[i] = i;
}

Then you could simply put it all together and create a mesh like

var mesh = new Mesh();
mesh.vertices = vertices;
mesh.triangles = triangles;
// Etc
derHugo
  • 83,094
  • 9
  • 75
  • 115