So, I have decided to make a 3D Software in unity similar to Blender using OpenXR, I have recently tried adding vertex movement through the XR simple grab by Instantiating objects at vertices with the grab script. However, after adding this I have received the error
``IndexOutOfRangeException: Index was outside the bounds of the array.
ProceduralCube.faceVertices (System.Int32 dir, System.Single scale, UnityEngine.Vector3 facePos) (at Assets/ProceduralCube.cs:38)
ProceduralCube.MakeFace (System.Int32 dir, System.Single faceScale, UnityEngine.Vector3 facePos) (at Assets/ProceduralCube.cs:90)
ProceduralCube.MakeCube (System.Single cubeScale, UnityEngine.Vector3 cubePos) (at Assets/ProceduralCube.cs:85)
ProceduralCube.Update () (at Assets/ProceduralCube.cs:66)`
The Script is here if anyone can help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent( typeof(MeshFilter), typeof(MeshRenderer) )]
public class ProceduralCube : MonoBehaviour
{
public GameObject VertexBaseRenderedOBJ;
List<GameObject> Sphere = new List<GameObject>();
public Vector3[] Vertices =
{
new Vector3( 1, 1, 1),
new Vector3(-1, 1, 1),
new Vector3(-1,-1, 1),
new Vector3( 1,-1, 1),
new Vector3(-1, 1,-1),
new Vector3( 1, 1,-1),
new Vector3( 1,-1,-1),
new Vector3(-1,-1,-1)
};
public int[][] FaceTriangles = {
new int[] {0, 1, 2, 3},
new int[] {5, 0, 3, 6},
new int[] {4, 5, 6, 7},
new int[] {1, 4, 7, 2},
new int[] {5, 4, 1, 0},
new int[] {3, 2, 7, 6}
};
public Vector3[] faceVertices(int dir, float scale, Vector3 facePos) {
Vector3[] fv = new Vector3[4];
for (int i = 0; i < fv.Length; i++)
{
fv[i] = Vertices[FaceTriangles[dir] [i]] * scale + facePos;
}
return fv;
}
Mesh mesh;
public float scale = 1f;
public int posX,posY,posZ;
float adjScale;
List<Vector3> vertices;
List<int> triangles;
private void Awake()
{
mesh = GetComponent<MeshFilter>().mesh;
adjScale = scale * 0.5f;
}
private void Start()
{
}
private void Update()
{
RenderAllSpheres();
UpdateVertices();
MakeCube(adjScale, new Vector3((float) posX, (float) posY, (float) posZ) * scale);
UpdateMesh();
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.RecalculateNormals();
}
void MakeCube(float cubeScale, Vector3 cubePos)
{
vertices = new List<Vector3>();
triangles = new List<int>();
for (int i = 0; i < 6; i++)
{
MakeFace (i, cubeScale, cubePos);
}
}
void MakeFace(int dir, float faceScale, Vector3 facePos)
{
vertices.AddRange(faceVertices(dir, faceScale, facePos));
int vCount = vertices.Count;
triangles.Add(vCount - 4);
triangles.Add(vCount - 4 + 1);
triangles.Add(vCount - 4 + 2);
triangles.Add(vCount - 4);
triangles.Add(vCount - 4 + 2);
triangles.Add(vCount - 4 + 3);
}
void UpdateVertices() {
for (int i = 0; i < Vertices.Length; i++)
{
if(Vertices[i] == Sphere[i].transform.position) continue;
}
}
void RenderAllSpheres() {
while (Sphere.Count < Vertices.Length)
{
Sphere.Add(null);
}
for (int i = 0; i < Vertices.Length; i++)
{
if (Sphere[i] == null)
{
Sphere[i] = Instantiate(VertexBaseRenderedOBJ, Vertices[i], Quaternion.identity);
}
}
}
}