Hexagon image material

I’ve created a hexagon using script

using UnityEngine;
using System.Collections;

public class someScript : MonoBehaviour {

public float height = 50f;
public float width = 50f;
public float depth = 0;
void Start () {
MeshFilter mf = GetComponent();
Mesh mesh = new Mesh();
mf.mesh = mesh;
Vector3[ ] vertices2 = new Vector3[6];
float x = 0;
float y = 0;
float r = 10;
float n = 6;
for (int i = 0; i < n; i++)
{
float xPos = x + r * Mathf.Cos(2 * Mathf.PI * i / n);
float zPos = y + r * Mathf.Sin(2 * Mathf.PI * i / n);
Debug.Log("xPos1 " + xPos);
Debug.Log("zPos1 " + zPos);
vertices2 = new Vector3(xPos, 0, zPos);
};

int[ ] tri2 = new int[12];
tri2[0] = 2;
tri2[1] = 1;
tri2[2] = 0;
tri2[3] = 3;
tri2[4] = 2;
tri2[5] = 0;
tri2[6] = 4;
tri2[7] = 3;
tri2[8] = 0;
tri2[9] = 5;
tri2[10] = 4;
tri2[11] = 0;
Vector3[ ] normals2 = new Vector3[6];
normals2[0] = -Vector3.forward;
normals2[1] = -Vector3.forward;
normals2[2] = -Vector3.forward;
normals2[3] = -Vector3.forward;
normals2[4] = -Vector3.forward;
normals2[5] = -Vector3.forward;

Vector2[ ] uv2 = new Vector2[6];
uv2[0] = new Vector2(0, 0);
uv2[1] = new Vector2(1, 0);
uv2[2] = new Vector2(0, 1);
uv2[3] = new Vector2(1, 1);
uv2[4] = new Vector2(1, 1);
uv2[5] = new Vector2(1, 1);
mesh.vertices = vertices2;
mesh.triangles = tri2;
mesh.normals = normals2;
mesh.uv = uv2;
}

}
When I render it with adding simple color material - it looks like this:


But when Im adding some image on it -

Half of the hexagon looks fine - but the other part is smoothed, any idea where can be the problem?

Texture mapping result decided by your uv texture coordinate.
As your code the last three uv of vertex both (1,1) so it will be looks like that.

1 Like

I’m not familiar with coding but I think the concept is same.
Everything modeled out needed to do UV mapping.
For your case. A planar UV mapping is perfect enough.
But I don’t know how to do UV mapping via scripting.
Usually just do in 3D software and import it in.

Thank you for the hint, yes, the problem was in uv, but Im not familiar in what the uv realy is, would be thankful for some link or info about it, thanks!
And how is it correct to set uv in hexagon to fit image correctly?

maximum what I was able to do is something like this from the image in preview


Would be glad for info hot get at least something similar :smile:

You mean kinda create mech in Blender and import it into Unity?

UVs are the x and y (u and v) position of a surface in texture space. For what is basically a 2d shape like this hexagon you can just apply the same positions you have for the vertices as the UVs and get decent results, though you may want to put them into 0 to 1 space instead of -1 to 1 space so the texturing isn’t tiled.

uv2 = new Vector2(vertices2.x * 0.5f + 0.5f, vertices2_.z * 0.5f + 0.5f);_

On an aside, why are all of your array names suffixed with the number 2? It’s a little confusing, especially since meshes actually do have a .uv2!

1 Like

Ya. If you are interested. Here’s some guide of UV unwrapping. =)
http://waylon-art.com/uvw_tutorial/uvwtut_01.html
http://sophiehoulden.com/tutorials/blender/unwrapTut.html