- Home /
Help mapping Square Texture to Hex Shape Mesh.
I want to map a Square Texture to a Hex Shaped Mesh. I have the Mesh with 4 Triangle & 6 Vertices. I want to do this using Code, dynamically.
Here is the Code I used.
Script Name : "Plance.cs"
using UnityEngine;
using System.Collections;
public class Plane : MonoBehaviour
{
public Vector3[] Vertices;
public Vector2[] UV;
public int[] Triangles;
void MeshSetup ()
{
float halfHeight = 0;
Vertices = new Vector3[] {
new Vector3 (-1, halfHeight, 0),
new Vector3 (-.5f, halfHeight, Mathf.Sqrt (3) / -2),
new Vector3 (.5f, halfHeight, Mathf.Sqrt (3) / 2),
new Vector3 (-.5f, halfHeight, Mathf.Sqrt (3) / 2),
new Vector3 (.5f, halfHeight, Mathf.Sqrt (3) / -2),//
new Vector3 (1, halfHeight, 0)//
};
UV = new Vector2[] {
new Vector2 (0.5F, 0.87F),
new Vector2 (0.87F, 0.13F),
new Vector2 (0.13F, 0.87F),
new Vector2 (0.87F, 0.87F),
new Vector2 (0.13F, 0.13F),
new Vector2 (0.5F, 0.13F)
};
Triangles = new int[] {
0,3,1,
3,2,1,
1,2,4,
4,2,5
};
}
void Start ()
{
MeshSetup ();
Mesh stuff = new Mesh ();
gameObject.AddComponent<MeshFilter> ().mesh = stuff;
stuff.vertices = Vertices;
stuff.triangles = Triangles;
stuff.uv = UV;
}
}
I have attached this Script to an Empty Game Object. It has a Material attached to it with the Texture.
I have attached the Exported Package.
Click Here to Download Unity Package
I need it to look like this :
This is how it looks like now :
Immediate responses are appreciated.
Answer by karlog · Nov 14, 2013 at 06:14 AM
I have fixed it. The following code fixes the issue when having the material with tiling x=-1 and y=1.
Here is the Code : using UnityEngine; using System.Collections;
public class Plane : MonoBehaviour
{
public Vector3[] Vertices;
public Vector2[] UV;
public int[] Triangles;
void MeshSetup ()
{
float halfHeight = 0;
Vertices = new Vector3[] {
// new Vector3 (0, halfHeight, 0),
new Vector3 (-.5f, halfHeight, Mathf.Sqrt (3) / 2),
new Vector3 (-1f, halfHeight, 0),
new Vector3 (-.5f, halfHeight, Mathf.Sqrt (3) / -2),//
new Vector3 (.5F, halfHeight,Mathf.Sqrt (3) / -2),//
new Vector3 (1F, halfHeight, 0),
new Vector3 (.5f, halfHeight, Mathf.Sqrt (3) / 2),
};
UV = new Vector2[] {
// new Vector2 (0.5F, 0.5F),
new Vector2 (0.75F, 0.87F),
new Vector2 (1F, 0.5F),
new Vector2 (0.75F, 0.13F),
new Vector2 (0.25F, 0.13F),
new Vector2 (0F, 0.5F),
new Vector2 (0.25F, 0.87F),
};
Triangles = new int[] {
2,4,3,4,2,5,1,5,2,0,5,1,
// 0,1,5,1,2,5,2,4,5,2,3,4,
// 1,6,0,6,5,0,5,4,0,4,3,0,3,2,0,2,1,0,
// 1,2,0,2,3,0,3,4,0,4,5,0,5,6,0,6,1,0,
// 0,3,2,0,2,1,0,1,6,0,6,5,0,5,4,0,4,3
// 0,4,3,0,3,2,0,2,1,0,1,6,0,6,5,0,5,4
// 0,1,6,0,6,5,0,5,4,0,4,3,0,3,2,0,2,1,
};
}
void Start ()
{
MeshSetup ();
Mesh stuff = new Mesh ();
gameObject.AddComponent<MeshFilter> ().mesh = stuff;
stuff.vertices = Vertices;
stuff.triangles = Triangles;
stuff.uv = UV;
}
}
Your answer
Follow this Question
Related Questions
How can i achieve texturing of a dynamic generated Hex? 0 Answers
Get texture with UV Mesh Mapping 0 Answers
3DS Max Seams from Unity Cubes 2 Answers
Render Texture out from a 3D Object 1 Answer
Hexagon grid UV mapping 0 Answers