Hi all!
I’m trying to do a script that creates a mesh from scratch. I follow Unity Reference Example 1:
I try to do a quad:
4 Vertices, 4 Normals, 4 colors, 4 uv (primary), 4 uv (secondary), 6 triangles
All are drawn correctly. But if I want add tangents (4) i get this strange error:
“Mesh.tangents is out of bounds. The supplied array needs to be the same size as the Mesh.vertices array.”
I checked with debug that vertices are 4 and tangent too.
What’s wrong? Is a strange bug of mi Unity version?
Thanks in advance
I’m very sorry.
It’s my fault. For explain better, I write a stupid example to post, with my big surprise, works like a charm. I’m pretty sure that the problem is elsewhere.
then, by way for apologies, i post the stupid script:
using UnityEngine;
using System.Collections;
using System.Linq;
public class ProceduralQuad : MonoBehaviour {
[SerializeField] Material material;
Mesh mesh;
Color32 c = Color.red;
Vector4 t = new Vector4(1, 0, 0, -1);
void Start () { mesh = new Mesh(); }
void Update () {
mesh.Clear();
mesh.vertices = new Vector3[]
{
transform.right * -.5f + transform.up * -.5f,
transform.right * -.5f + transform.up * .5f,
transform.right * .5f + transform.up * .5f,
transform.right * .5f + transform.up * -.5f
};
mesh.uv = new Vector2[]
{
new Vector2 (0,0),
new Vector2 (0,1),
new Vector2 (1,1),
new Vector2 (1,0)
};
mesh.uv2 = mesh.uv1;
mesh.colors32 = Enumerable.Repeat(c, mesh.vertexCount).ToArray();
mesh.triangles = new int[] { 0, 1, 2, 2, 3, 0 } ;
mesh.normals = Enumerable.Repeat(-Vector3.forward, mesh.vertexCount).ToArray();
mesh.tangents = Enumerable.Repeat(t, mesh.vertexCount).ToArray();
mesh.RecalculateBounds();
Graphics.DrawMesh(mesh, transform.position, transform.rotation, material, gameObject.layer);
}
}