Material is not stretching on Meshes
I used this script to draw some polygons: http://wiki.unity3d.com/index.php?title=Triangulator
I changed this part, to set the base texture coordinates of the mesh:
// Create the Vector3 vertices
Vector3[] vertices = new Vector3[vertices2D.Length];
Vector2[] uvs = new Vector2[vertices2D.Length];
for (int i = 0; i < vertices.Length; i++)
{
vertices[i] = new Vector3(vertices2D[i].x, vertices2D[i].y, 0);
uvs[i] = new Vector2(vertices2D[i].x, vertices2D[i].y);
}
msh.uv = uvs;
Now I set a material to the created GameObject, like this: polygonObject.GetComponent().material = blueMaterial;
The material of the GameObject has changed, but it now consists of thousands of small pictures of the desired one:
The picture isn't a small one (1600x1200). The size of the picture in the background is like (2600x1600). So there is clearly something wrong. How can I get this fixed, so the material of the GameObject fits the picture?
Answer by hexagonius · Nov 24, 2016 at 09:58 PM
Little rundown on UVs. for a mesh to know what to display it has normalized information about where to read pixels on a texture. since the size can be any size, some smart people thought that could be normalized. That means a value of 0,0 is the lower left of a texture and 1,1 is the upper right of a texture no matter the size.
what you need to do is apply values between 0 to 1. larger values just repeat the texture as you can see.
Thanks! I have written this method and it works now:
private Vector2[] sort(Vector2[] vertices2D)
{
float highestX = vertices2D[0].x;
float highestY = vertices2D[0].y;
float lowestX = vertices2D[0].x;
float lowestY = vertices2D[0].y;
for (int i = 0; i < vertices2D.Length; i++)
{
float x = vertices2D[i].x;
float y = vertices2D[i].y;
if (x > highestX) {
highestX = x;
}
if (x < lowestX)
{
lowestX = x;
}
if (y > highestY)
{
highestY = y;
}
if (y < lowestY)
{
lowestY = y;
}
}
Vector2[] uvs = new Vector2[vertices2D.Length];
float diffX = highestX - lowestX;
float diffY = highestY - lowestY;
for (int i = 0; i < uvs.Length; i++)
{
uvs[i] = new Vector2(1/ diffX*(vertices2D[i].x-lowestX), 1 / diffY * (vertices2D[i].y-lowestY));
}
return uvs;
}
Your answer
Follow this Question
Related Questions
How to add roughness to a mesh collider ? 0 Answers
How to partially change texture? 0 Answers
How to create a mesh for 2D maps with transparent parts, which is generated by software? 0 Answers
2 Models 1 Cracked How to make both look the same? (Material Problems) 1 Answer
Automatically assign hundred of textures to .obj file 0 Answers