Apply 1-9 materials on the same object, with alpha

I’ve made a system similar to MineCraft that uses simplex noise to generate a terrain of cubes. I can easily assign materials to cubes based on the values of simplex noise.
Now i want to add more materials to the same cubes, base on whats next to the other cube.

So for each cube im doing something similar to a shader, im checking all the cubes that are next to the current cube to determine what texture to use.

[1 0 0]
[1 X 0]
[1 0 0]

So the current cube is the X in the middle of the matrix. 0 represents Sand, 1 represents grass.

So from the start the material would be:

alt text

But then since grass is on the left of the cube i want to add another material with alpha on top of it, so it looks like this:

I have the original material Sand set to just be Diffuse. The materials i add on top of it, in the renderer.materials list, is Transparent/Diffuse.

But I seem to be getting a lot of jittering, where it switches between displaying the alpha texture and not.

Any idea how I can apply 9 materials with alpha on the same object without getting these render errors?

Or is there a way I can solve this with Math using only 1 texture? By rotating and looping through the matrix checking different scenarios?

I cannot reproduce your problem. If you create multiple materials for an object, they are rendered from top to bottom, and if all materials except the first use a shader from the “Transparent/” section, there should be no z-fighting (=“jittering”).

Are you sure you only have a single cube at every location, and not another cube (or another (sub-)mesh?) at the same position which only has the Sand material (or has a different materials list)? This is the only situation I can think of that would produce z-fighting.

EDIT: Ah, wait, now I can reproduce it!

It seems there is a bug (at least I can’t think of a reason) that will cause z-fighting if any consecutive materials in your materials array are identical, even if they use a “Transparent” shader.

So either make sure that all 9 materials are different, or if that is not possible you’ll need to attach a script to the object that brings the render queues back in order:

public class FixRenderQueues : MonoBehaviour {
	void Awake(){
		foreach (Renderer r in allRenderer)
			for(int i=0;i<r.materials.Length;i++)
				r.materials_.renderQueue = r.materials*.shader.renderQueue + i;*_

* }*
(with the script, press Play, so that the script will run once. It should then store the fixed queue values even when stopped, until you modify the object, or reload the scene)