Ok, noone answered, but I found some solutions so I post it.
I solved bug when changed shader for colored flats and cube from Transparent to Decal. I think, it’s not a solution of bug, but solution in my case. If someone knows the real solution, I will be glad to read.
Solution for performance problem was to generate own mesh by this Unity - Manual: Using meshes with C# scripts
Creator of variable shape mesh:
using UnityEngine;
using System.Collections;
public class MeshCreator : MonoBehaviour {
// create mesh prepared for exact count of flats
public Mesh CreateEmptyMesh(int flatsCount) {
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[flatsCount * 4];
mesh.normals = new Vector3[flatsCount * 4];
mesh.triangles = new int[flatsCount * 6];
mesh.uv = new Vector2[flatsCount * 4];
return mesh;
}
// add a single flat
public Mesh AddFlat(Mesh mesh, int index, Vector3 position) {
int index4 = index * 4;
int index6 = index * 6;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
int[] triangles = mesh.triangles;
Vector2[] uv = mesh.uv;
// create vertices for flat of size 1x1
vertices[index4] = new Vector3(-0.5f, 0, -0.5f) + position;
vertices[index4 + 1] = new Vector3(-0.5f, 0, 0.5f) + position;
vertices[index4 + 2] = new Vector3(0.5f, 0, 0.5f) + position;
vertices[index4 + 3] = new Vector3(0.5f, 0, -0.5f) + position;
// normal vectors pointing up for correct lighting
Vector3 normalUp = new Vector3(0, 1, 0);
normals[index4] = normalUp;
normals[index4 + 1] = normalUp;
normals[index4 + 2] = normalUp;
normals[index4 + 3] = normalUp;
// two triangles forming flat
triangles[index6] = index4;
triangles[index6 + 1] = index4 + 1;
triangles[index6 + 2] = index4 + 3;
triangles[index6 + 3] = index4 + 1;
triangles[index6 + 4] = index4 + 2;
triangles[index6 + 5] = index4 + 3;
// stretch texture to all flat
uv[index4] = new Vector2(0, 0);
uv[index4 + 1] = new Vector2(1, 0);
uv[index4 + 2] = new Vector2(1, 1);
uv[index4 + 3] = new Vector2(0, 1);
mesh.vertices = vertices;
mesh.normals = normals;
mesh.triangles = triangles;
mesh.uv = uv;
return mesh;
}
}
And now just create mesh of any shape, in this case rectangular mesh of 10x100 flats
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (MeshCreator))]
[RequireComponent (typeof (MeshFilter))]
public class RectangleMeshCreator : MonoBehaviour {
void Start () {
int width = 10;
int lenght = 100;
MeshCreator meshCreator = GetComponent<MeshCreator>();
MeshFilter meshFilter = GetComponent<MeshFilter>();
// create empty mesh of width * lenght size first
Mesh mesh = meshCreator.CreateEmptyMesh(width * lenght);
int index;
for (int x = 0; x < width; x++) {
for (int z = 0; z < lenght; z++) {
index = x * lenght + z;
mesh = meshCreator.AddFlat(mesh, index, new Vector3(x, 0, z));
}
}
// don't forget to recalculate boundaries
mesh.RecalculateBounds();
// and finally assign created mesh
meshFilter.mesh = mesh;
}
}
Fps is now high, even if I have many “flats of grid” on screen, and have no need to create multiple textures for another shapes of grid.