I am translating a script that generates a hexagonal mesh into C# from java and I ran into this issue with the uvs, it works fine except when I try to assign uvs and I get the above error on the line reading "uvVerts = v * uvRadius;" at line 69. Any help would be appreciated.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshCollider))]
public class HexGrid : MonoBehaviour {
-
public int columns = 100;*
-
public int rows = 50;*
-
public float tileSize = 1.0f;*
-
public Material material;*
-
private Color colors;*
-
private Mesh mesh; // = new Mesh();*
-
private Vector2 uvVerts; //= new Vector2[6];*
-
private float uvRadius;*
-
private Vector2 uvDefault;*
-
public int uvRows = 2; // Number of rows in the texture*
-
public int uvCols = 2; // Number of cols in the texture*
-
// Use this for initialization*
-
void Start () {*
-
mesh = new Mesh();*
-
Vector2[] uvVerts = new Vector2[6];*
-
float uvRadius = 1.0f / uvCols / 2.0f;*
-
Vector2 uvDefault = new Vector2(uvRadius, uvRadius);*
-
renderer.material = material;*
-
BuildMesh();*
-
// Set all the cells to different colors*
-
for (int i = 0; i < rows; i++) {*
-
for (var j = 0; j < columns; j++) {*
-
SetUV(i,j,i%2,j%2);*
-
}*
-
}*
-
}*
-
public void BuildMesh() {*
-
//mesh.normals = normals;*
-
// Assign our mesh to our filter/renderer/collider*
-
MeshFilter mesh_filter = GetComponent<MeshFilter>();*
-
MeshRenderer mesh_renderer = GetComponent<MeshRenderer>();*
-
MeshCollider mesh_collider = GetComponent<MeshCollider>();*
-
mesh = new Mesh();*
-
mesh_filter.mesh = mesh;*
_ float vertSpacing = 2.0f * Mathf.Cos(30.0f * Mathf.Deg2Rad) * tileSize;_
_ float horzSpacing = tileSize + Mathf.Sin(30.0f * Mathf.Deg2Rad) * tileSize;_
_ Vector3 currPos = new Vector3(-columns / 2.0f * horzSpacing, 0.0f, rows / 2.0f * vertSpacing);_
-
//layout vertices of a single hex*
-
Vector3[] hexVerts = new Vector3[6];*
-
hexVerts[0] = Vector3.zero;*
-
Vector3 v = Vector3.right;*
-
for (int i = 0; i < 6; i++) {*
hexVerts = v * tileSize;
uvVerts = v * uvRadius; //THIS LINE
_ v = Quaternion.AngleAxis(60.0f, -Vector3.down) * v;
* }*_
* //create the vertices*
_ Vector3 vertices = new Vector3[rows * columns * 6];
* int currVert = 0;
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
for (int k = 0; k < hexVerts.Length; k++) {
vertices[currVert++] = hexVerts[k] + currPos;
}
currPos.z-= vertSpacing;
}
currPos.x += horzSpacing;
currPos.z = rows / 2.0f * vertSpacing;
if (i % 2 == 0)
currPos.z -= vertSpacing / 2.0f;
}
mesh.vertices = vertices;
Debug.Log(“vertices done”);*_
* //create the triangles*
* int current = 0;*
_ int triangles = new int[rows * columns * 4 * 3];
for (int i = 0; i < columns * rows * 6; i += 6) {
* triangles[current++] = i+0;
triangles[current++] = i+1;
triangles[current++] = i+5;*_
* triangles[current++] = i+5;*
* triangles[current++] = i+1;*
* triangles[current++] = i+4;*
* triangles[current++] = i+4;*
* triangles[current++] = i+1;*
* triangles[current++] = i+2;*
* triangles[current++] = i+2;*
* triangles[current++] = i+3;*
* triangles[current++] = i+4;*
* }*
* mesh.triangles = triangles;*
* // Setup a based set of UV coordinates*
* Vector2[] uvs = new Vector2[vertices.Length];*
* for (int i = 0; i < vertices.Length; i += 6) {*
* for (int j = 0; j < 6; j++) {*
* uvs[i+j] = uvVerts[j] + uvDefault;*
* }*
* }*
* mesh.uv = uvs;*
* mesh.colors = colors;*
* mesh.RecalculateBounds();*
* mesh.RecalculateNormals();*
* mesh_filter.mesh = mesh;
mesh_collider.sharedMesh = mesh;
_ Debug.Log (“Done Mesh!”);
}*_
* public void SetUV(int row, int col, int atlasRow, int atlasCol) {*
* if (row < 0 || row >= rows) return;*
* if (col < 0 || col >= columns) return;*
* if (atlasRow < 0 || atlasRow >= uvRows) return;*
* if (atlasCol < 0 || atlasCol >= uvCols) return;*
* Vector2[] uvs = mesh.uv;*
_ int Base = col * rows * 6 + row * 6;
Vector2 offset = new Vector2(atlasCol * uvRadius * 2.0f + uvRadius, atlasRow * uvRadius * 2.0f + uvRadius);
* for (int i = 0; i < 6; i++) {
uvs[Base + i] = uvVerts + offset;
}
mesh.uv = uvs;
}
}*_