- Home /
How can i achieve texturing of a dynamic generated Hex?
I have dynamically created a hex and i am trying to add a texture/material to it but it does not look good.
This is the code i am using:
List<Vector3> vertices;
List<int> triangles;
public float floorLevel = 0;
public Material material;
public void GenerateMesh()
{
MeshFilter meshFilter = gameObject.AddComponent<MeshFilter>();
Renderer renderer = gameObject.AddComponent<MeshRenderer>();
vertices = new List<Vector3>();
triangles = new List<int>();
Hex hex = new Hex(
new Node(new Vector3(-1, floorLevel, -.5f)),
new Node(new Vector3(-1, floorLevel, .5f)),
new Node(new Vector3(0, floorLevel, 1)),
new Node(new Vector3(1, floorLevel, .5f)),
new Node(new Vector3(1, floorLevel, -.5f)),
new Node(new Vector3(0, floorLevel, -1))
);
Vector2[] uv = new Vector2[]
{
new Vector2(0, .25f),
new Vector2(0, .75f),
new Vector2(.5f, 1),
new Vector2(1, .75f),
new Vector2(1, .25f),
new Vector2(.5f, 0)
};
TriangulateHex(hex);
Mesh mesh = new Mesh();
mesh.vertices = vertices.ToArray();
mesh.triangles = triangles.ToArray();
mesh.uv = uv;
mesh.RecalculateNormals();
meshFilter.mesh = mesh;
meshFilter.transform.rotation = Quaternion.Euler(0, 90f, 0);
renderer.material = material;
}
These are the images:
I want the hexagon texture to tile perfectly on the generated hexagon, but it tiles weirdly. Does anyone know what the problem is?
Thanks.
Later edit: Based on the comments bellow i tried to remake this hex, but still haven't managed to get it working
Here is a representation of what i did:
So based on this image i created the following vertex positions:
left: 0, .5f
top-left: .25f, 1
bottom-left: .25f, 0
top-right: .75f, 1
bottom-right: .75f, 0
right: 1, .5f
I have created the following triangles based on corner positions:
1, 2, 6
2, 5, 6
2, 3, 5
3, 4, 5
The hex renders ok.
I then try to add the UV mapping on the hex. These are the points i use for the mapping (same vertex positions as above):
0, .5f
.25f, 1
.75f, 1
1, .5f
.75f, 0
.25f, 0
Still the texture does not tile over the generated hex, it is skewed like in the image above, in a different way, but still not correctly tiled. What am i doing wrong?
Your uv mapping is correct, but your vertex positions look off. For a hex at 0,0 world position, it should have the same vertex coordinates as the uv map coordinates. If I tilt my head and look at your screenshot, I see an imperfect hex. Then the uv map looks the same (no matter where the vertices are in world space, of course).
So you are saying that the hex was built wrong, but the UV are correct? What is wrong? The vertex positions or the triangles?
Your answer
![](https://koobas.hobune.stream/wayback/20220612092726im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help mapping Square Texture to Hex Shape Mesh. 1 Answer
is it possible adding text in realtime using shadergraph? 0 Answers
Can I independently manipulate UVs of a multi-material object? 0 Answers
white texture gets yellowish and weird at a distance 0 Answers
Texture tiling difference between unlit and surface shader? 1 Answer