В коде генерируется хекс грид. Все расчеты вроде правильные, но вот в самой игре текстура не отрисовывается. Может кто то сможет найти ошибку, потому что я ее не вижу.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class HexRender : MonoBehaviour
{ List<Vector3> vertices = new List<Vector3>();
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
private Mesh _mesh;
private MeshFilter _meshFilter;
private MeshRenderer _meshRenderer;
private List<Face> _faces;
public Material Material;
public float innerSize;
public float outerSize;
public float height;
public bool isFlatTopped;
private int point = 1;
private void Awake()
{
_meshRenderer = GetComponent<MeshRenderer>();
_meshFilter = GetComponent<MeshFilter>();
_mesh = new Mesh();
_mesh.name = "hex";
_meshFilter.mesh = _mesh;
_meshRenderer.material = Material;
}
public struct Face
{
public List<Vector3> vertices { get; private set; }
public List<int> triangles { get; private set; }
public List<Vector2> uvs { get; private set; }
public Face(List<Vector3> vertices, List<int> triangles, List<Vector2> uvs)
{
this.vertices = vertices;
this.triangles = triangles;
this.uvs = uvs;
}
}
private void OnEnable()
{
DrawMesh();
}
//public void OnValidate()
// {
// if (Application.isPlaying)
// {
// DrawMesh();
// }
// }
public void DrawMesh()
{
DrawFaces();
CombineFaces();
}
private void DrawFaces()
{
_faces = new List<Face>();
for (int i = 0; i < 6; i++)
{
_faces.Add(CreateFace(innerSize, outerSize, height / 2f, height / 2f, i));
}
for (int i = 0; i < 6; i++)
{
_faces.Add(CreateFace(innerSize, outerSize, -height / 2f, -height / 2f, i, true));
}
for (int i = 0; i < 6; i++)
{
_faces.Add(CreateFace(innerSize, innerSize, height / 2f, height / 2f, i, true));
}
for (int i = 0; i < 6; i++)
{
_faces.Add(CreateFace(outerSize, outerSize, height / 2f, height / 2f, i, false));
}
}
private void CombineFaces()
{
for (int i = 0; i < _faces.Count; i++)
{
vertices.AddRange(_faces[i].vertices);
uvs.AddRange(_faces[i].uvs);
int offset = (4 * i);
foreach (int tris in _faces[i].triangles)
{
triangles.Add(tris + offset);
}
}
Debug.Log(vertices[0]);
_mesh.vertices = vertices.ToArray();
Debug.Log(_mesh);
_mesh.triangles = triangles.ToArray();
_mesh.uv = uvs.ToArray();
_mesh.RecalculateNormals();
}
private Face CreateFace(float innerRad, float outerRad, float SideA, float SideB, int point, bool reverse = false)
{
Vector3 pointA = GetPoint(innerRad, SideB, point);
Vector3 pointB = GetPoint(innerRad, SideB, (point < 5) ? point + 1 : 0);
Vector3 pointC = GetPoint(outerRad, SideA, (point < 5) ? point + 1 : 0);
Vector3 pointD = GetPoint(outerRad, SideA, point);
List<Vector3> vertices = new List<Vector3>() {pointA, pointB, pointC, pointD};
List<int> triangles = new List<int>() {0, 1, 2, 2, 3, 0};
List<Vector2> uvs = new List<Vector2>()
{
new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1),
new Vector2(0, 1)
};
if (reverse)
{
vertices.Reverse();
}
return new Face(vertices,triangles,uvs);
}
protected Vector3 GetPoint(float size, float height, int index)
{
float angle_deg = isFlatTopped ? 60 * index : 60 * index - 30;
float angle_rag = Mathf.PI / 180f * angle_deg;
return new Vector3((size * Mathf.Cos(angle_rag)), height, (size * Mathf.Sin(angle_rag)));
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}