- Home /
UVs don't apply correctly on script-generated meshes
I'm having an issue with textures only applying to a small corner of some meshes I'm generating. The values I'm putting in scale from 0 to 1 as they should, and the meshes themselves are positioned correctly, but only one square segment is actually textured. I'm a bit lost as to what I'm doing wrong, and any help would be greatly appreciated. Full script is below (based on http://wiki.unity3d.com/index.php/CreatePlane), just attach to a GameObject. Most relevant bit starts at line 75.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ChunkManager : MonoBehaviour {
public float chunkSize = 256;
public int chunkSegs = 64;
private GameObject map;
private Dictionary<string, GameObject> chunks;
private int vGridCount;
private int vCount;
private int tCount;
private float scale;
private float hSize;
private float iSegs;
// Use this for initialization
void Start () {
chunks = new Dictionary<string, GameObject> ();
map = this.gameObject;
vGridCount = chunkSegs + 1;
vCount = vGridCount * vGridCount;
tCount = chunkSegs * chunkSegs * 6;
scale = chunkSize / (float)chunkSegs;
hSize = chunkSize / 2.0f;
iSegs = 1.0f / (float)chunkSegs;
for (int x = -1; x < 2; x++) {
for (int y = -1; y < 2; y++) {
CreateChunk (x, y);
}
}
}
// Update is called once per frame
void Update () {
}
// Creates a new chunk
void CreateChunk(float x, float z) {
CreateChunk (Mathf.FloorToInt (x / chunkSize), Mathf.FloorToInt (z / chunkSize));
}
string GetChunkId(int cx, int cz) {
return cx.ToString() + "-" + cz.ToString();
}
void CreateChunk(int cx, int cz) {
string chunkid = GetChunkId (cx, cz);
Debug.Log ("Creating chunk " + chunkid);
// Create the chunk object
GameObject chunk = new GameObject ();
chunk.name = "Chunk " + chunkid;
chunk.transform.parent = map.transform;
chunk.transform.position = new Vector3((float)cx * chunkSize, 0.0f, (float)cz * chunkSize);
// Build the mesh
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[vCount];
Vector2[] uvs = new Vector2[vCount];
int[] triangles = new int[tCount];
// Calculate uvs and vertices
int offset = 0;
float x, y;
for (y = 0.0f; y < vGridCount; y++) {
for (x = 0.0f; x < vGridCount; x++) {
vertices[offset] = new Vector3(x * scale - hSize, 0.0f, y * scale - hSize);
uvs[offset++] = new Vector2(x * iSegs, y * iSegs);
if (chunkid == "0-0" && y == 0.0f) {
Debug.Log (uvs[offset-1].x);
}
}
}
// Calculate triangles
offset = 0;
int ix, iy, a, b, c, d;
for (iy = 0; iy < chunkSegs; iy++) {
for (ix = 0; ix < chunkSegs; ix++) {
a = ( iy * vGridCount) + ix;
b = ((iy + 1) * vGridCount) + ix;
c = a + 1;
d = b + 1;
triangles[offset ] = a;
triangles[offset+1] = b;
triangles[offset+2] = c;
triangles[offset+3] = b;
triangles[offset+4] = d;
triangles[offset+5] = c;
}
}
// Update mesh
mesh.vertices = vertices;
mesh.uv = uvs;
mesh.triangles = triangles;
mesh.RecalculateNormals ();
mesh.RecalculateBounds ();
// Attach mesh filter and mesh renderer
MeshFilter meshFilter = (MeshFilter)chunk.AddComponent (typeof(MeshFilter));
MeshRenderer meshRenderer = (MeshRenderer)chunk.AddComponent (typeof(MeshRenderer));
chunk.AddComponent (typeof(BoxCollider));
meshFilter.sharedMesh = mesh;
meshRenderer.sharedMaterial = new Material(Shader.Find("Diffuse"));
chunks.Add (chunkid, chunk);
}
}
Is the small textured area showing the entire texture, or just the lower-left corner ... ? Is the rest a solid smear of colors based on the texture you currently have, or ... ? That would be caused by uv values past 1 and a clamped texture (but I don't think your UV values are past 1.)
I don't much care for how it assigns the shared material (why not just assign an existing material. or keep the one you have now?) I assume a texture being assigned later?
I'm just using a material with the default diffuse shader, no actual textures yet. At some point I'll be generating the textures to apply. The small corner is the only part that is visible - the rest is simply invisible, but unity shows the center of the object in the correct place. UV values go from 0 to 1 with even increments according to the debug log.
I've tried creating the material in Start
and using the same one for each chunk, but the result is the same.
E: I've added a picture to the original question.
I don't understand the picture. BUT. if you can't see the faces, UVs aren't the problem. A bad UV map will apply the wrong color, but will never not show a pixel. 100% of the time, the world's worst UV map will put some color from the texture, onto the pixel. The only way to hide a pixel is if the texture has transparency, and so does the shader (diffuse shaders do not.)
UVs aren't the problem. $$anonymous$$aybe the normals are bad (or you're just on the wrong side) or tris are messed up ... .
FYI, most people put on a checkerboard or a duck or something, just for testing. The default shader uses a white texture. So it's not like you're testing it "without a texture" first.
In the picture you can see one (of nine) of the chunks selected. The movement arrows are placed in the center of the object, but only the top right corner is visibly textured.
Found the problem though - I forgot to increment the offset when calculating my tris. Whoops.
Thanks for the help.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Change Material of quad in script c# unity 2d 1 Answer
Distribute terrain in zones 3 Answers
Splitting Procedurally Generated Mesh, Based On Height 0 Answers
How to highlight only one face of a Cube from script? 0 Answers