- Home /
Unity procedural mesh generation UV texture flickering horizontal lines
Here's a build: https://dl.dropboxusercontent.com/u/140141632/temp/web.html If you move along the y axis (w/s) you'll notice that you'll see horizontal blue lines flicker in and out.
I'm using this sprite sheet: https://dl.dropboxusercontent.com/u/140141632/temp/spritesheet.png
I think the problem has something to do with the spritesheet because when I change the color of the blue sprites, it changes the color flicker.
Here is my code used for generating the mesh:
'using UnityEngine; using System.Collections;
[RequireComponent(typeof(MeshFilter))] [RequireComponent(typeof(MeshRenderer))] [RequireComponent(typeof(MeshCollider))]
public class Map : MonoBehaviour {
int width = 50, height = 50;
float textureWidth = 4, textureHeight = 4;
void Start () {
BuildMesh();
GenerateMap();
}
void Update () {
}
void BuildMesh() {
Vector3[] vertices = new Vector3[width * height * 4];
Vector3[] normals = new Vector3[width * height * 4];
Vector2[] uv = new Vector2[width * height * 4];
int[] triangles = new int[width * height * 2 * 3];
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
int tl = (y * width + x) * 4;
vertices[tl + 0] = new Vector3(x, -y, 0);
vertices[tl + 1] = new Vector3(x + 1, -y, 0);
vertices[tl + 2] = new Vector3(x + 1, -y - 1, 0);
vertices[tl + 3] = new Vector3(x, -y - 1, 0);
uv[tl + 0] = new Vector2(0.0f, 0.25f);
uv[tl + 1] = new Vector2(0.25f, 0.25f);
uv[tl + 2] = new Vector2(0.25f, 0.0f);
uv[tl + 3] = new Vector2(0.0f, 0.0f);
int tri_start = (y * width + x) * 6;
triangles[tri_start + 0] = tl + 0;
triangles[tri_start + 1] = tl + 2;
triangles[tri_start + 2] = tl + 3;
triangles[tri_start + 3] = tl + 0;
triangles[tri_start + 4] = tl + 1;
triangles[tri_start + 5] = tl + 2;
}
}
for(int i=0; i<width * height * 4; i++) {
normals[i] = Vector3.forward;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.triangles = triangles;
mesh.uv = uv;
MeshFilter mf = GetComponent<MeshFilter>();
mf.mesh = mesh;
}
void GenerateMap() {
float xOff = (float)Random.Range(1, 1000000);
float yOff = (float)Random.Range(1, 1000000);
// 0 = Black empty thing grass maybe; 1 = forest; 2 = moutain; 3 = water
int[,] tiles = new int[50,50];
for(int y=0; y<50; y++) {
for(int x=0; x<50; x++) {
tiles[x,y] = 0;
}
}
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
float noise = Mathf.PerlinNoise(x * 0.2f + xOff, y * 0.2f + yOff);
if(noise < 0.25f) {
tiles[x,y] = 2;
}
}
}
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
float noise = Mathf.PerlinNoise(x * 0.15f + xOff, y * 0.15f + yOff);
if(noise < 0.35f) {
tiles[x,y] = 1;
}
}
}
for(int y=0; y<height; y++) {
for(int x=0; x<width; x++) {
float noise = Mathf.PerlinNoise(x * 0.08f + xOff, y * 0.08f + yOff);
if(noise < 0.27f) {
tiles[x,y] = 3;
}
}
}
for(int y=0; y<50; y++) {
for(int x=0; x<50; x++) {
if(tiles[x,y] == 0) {
SetTile(x, y, 0, 0);
}
else if(tiles[x,y] == 1) {
SetTile(x, y, 0, 1);
}
else if(tiles[x,y] == 2) {
SetTile(x, y, 1, 0);
}
else if(tiles[x,y] == 3) {
SetTile(x, y, 1, 1);
}
}
}
}
void SetTile2(int x, int y) {
MeshFilter mf = GetComponent<MeshFilter>();
Vector2[] uv = mf.mesh.uv;
int tl = (y * width + x) * 4;
uv[tl + 0] = new Vector2(0.0f, 0.0f);
uv[tl + 1] = new Vector2(1.0f, 0.0f);
uv[tl + 2] = new Vector2(1.0f, 1.0f);
uv[tl + 3] = new Vector2(0.0f, 1.0f);
}
void SetTile(int x, int y, float tx, float ty) {
//Only works for square sprite sheets, no rectangles
ty = -ty;
MeshFilter mf = GetComponent<MeshFilter>();
Vector2[] uv = mf.mesh.uv;
int tl = (y * width + x) * 4;
float textureSize = 1f / textureHeight;
float startX = tx / textureWidth;
float startY = ty / textureHeight - textureSize;
uv[tl + 0] = new Vector2(startX + 0.0f, startY + textureSize);
uv[tl + 1] = new Vector2(startX + textureSize, startY + textureSize);
uv[tl + 2] = new Vector2(startX + textureSize, startY + 0.0f);
uv[tl + 3] = new Vector2(startX + 0.0f, startY + 0.0f);
mf.mesh.uv = uv;
}
}'
nobody will read that much code and nobody will click to links. using the "image" button post screen shots clearly showing your problem. regarding the code post the few relevant lines - if any
Your answer
Follow this Question
Related Questions
procedural texture uv error 0 Answers
How do we generate a UV mapping data for a complicated mesh? 0 Answers
Automatic UV Mapping for a Cube in C# 1 Answer
Delete Uv2 of a mesh? 1 Answer
Help Uv mapping !!! 1 Answer