- Home /
 
multimaterial mesh, how to smooth fade between the materials?
I is making a game, like minecraft, that allows you to break or place blocks, but, using a Marching Cubes algoritm to create the mesh of the terrain. an a LOT of issues appear because is a Marching procedural mesh. One of this problems is the material asign, if I Have two materials, I need two submeshes, the triangles assign is solved but I dont know how to fade between the materials. A image to exemplificate:

( if the image not appears see in the bottom of the post, will have a append) how I can Fade this? the code of submesh SetTriangle is below:
 static function definirmateriais (data : float[,,],objeto : GameObject,mesha : Mesh,sub : int) {
 var verts : Vector3[]= mesha.vertices;
 var i : int;
 var tris : int[] = new int[mesha.triangles.Length];
 var C : int;
 var contador : int;
 var qbioma : int;
 // qbioma is a index material of terrain for current biome.
 qbioma = grade3D.qbioma;
 // loop once for all tris
 for(i=0;i<tris.Length/3;i++)
 {
     C=(i*3);
     // now Calculate the center of the triangle
     var centro : Vector3;
     centro = objeto.transform.TransformPoint((verts[mesha.triangles[C]] +verts[mesha.triangles[C+1]] +verts[mesha.triangles[C+2]])/3);
     // verify in the voxel data if in the triangle center position the index refer to material what is being seting the triangles
     if(fazerconta(centro,sub,data,qbioma))
     {
         for(var j=0;j<3;j++)
         tris[contador+j] = mesha.triangles[C+j];
         contador+=3;
     }
 }
 var trismenor : int[] = new int[contador];
 for(i=0;i<contador;i++)
 trismenor[i] = tris[i]; 
 // assign the triangles to the current submesh to color the terrain with this material
 mesha.SetTriangles(trismenor,sub);
 return(mesha);
 }
 
 static function fazerconta (centro : Vector3 , sub : int,data : float[,,],qbioma:int) {
 var booleano : boolean;
 
 // convert the float triangle position in a readable value in the voxel data
 var x : int=Mathf.FloorToInt(centro.x+1.5);
 var y : int=Mathf.FloorToInt(centro.y+0.5);
 var z : int=Mathf.FloorToInt(centro.z+1.5);
 var dat : float = data[x,y,z];
 
 // this if is to correct the issue of the triangles that pertents a air voxel ( 0 or >100) by painting this with the current biome material
 if(sub ==qbioma)
 {
 // verify if is this current material or is air
 if(dat>=sub && dat<=sub+1||dat>100||dat==0)
 booleano = true;
 }
 else
 {
 // verify if is this current material
 if(dat>=sub && dat<=sub+1&&dat!=0)
 booleano = true;
 }
 
 return booleano;
 }
 
               which is the approach needed to correct this issue, an make a nicely fade?
Download of the windows player : http://www.mediafire.com/download/ot6zedf4edcllyb/smoothcraftv003.rar
how to play: WASD walk,
E swicth the block edit offset,
T swicth the block edit scale,
holding SHIFT and clicking the arrow keys, put lines of blocks,
clicking broken blocks,
right-clicking put blocks,
numpad plus or numpad minus change the density of the puting block,
and 1,2,3,4 change the current block .
.
.
.
.
.
any help will be apreacited,
( sorry for the bad english, I am brazilian)
Answer by MakeCodeNow · Feb 17, 2014 at 02:22 AM
You have two options in this case. One is to render the given tri twice, with the second pass alpha-blending over the first pass with the amount of alpha based on vert colors or whatever else you find convenient.
You can create a new material/shader that internally blends between two materials/textures in the shader, again based on vertex colors or a constant color or whatever works for you. This approach will take less CPU and GPU time than the previous approach, but requires that you be at least a little comfortable with shader programming.
thanks very much for fast reply, I am interested in the second approach, a Shader to fade between two materials using vertex colors aplpha... this looks great,no uses transparency
,I also tried the first approach,but I no have find any shader that mask the transparency using vertex alpha. I need to create this for myself.
so... where I can find a nice shaderlab tutorial? or have you a shader that makes it?
Your answer
 
             Follow this Question
Related Questions
Do submeshes get sorted properly for transparent materials? 0 Answers
Remove triangle from submesh 0 Answers
Getting material from triangle returns wrong material 0 Answers
Fading multiple objects 0 Answers