Register  |  FAQ  |  Search  |  Memberlist  |  Usergroups  |  Log in 
Reply to topic
 Texture Splatting 
hde


Joined: 08 Oct 2007
Posts: 46
Reply with quote
For terrain generating i would need a special shader for Texture Splatting http://en.wikipedia.org/wiki/Texture_splatting . The shader needs to be use of minimum 4 textures for the terrain. Each texture should be variable tileable over the terrain. For the 4 textures we need 3 alpha maps for individual blending (which is usual packed in one texture with 3 rgb channels). I would write such a shader. But Directx 8 is really old and its difficult to find good ressouces for learning, examples, tutorials etc.

Thanks in advance
View user's profile Send private message
  
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
You can see the Texture Blending chapter of DirectX Document.

Here is a sample Texture Splatting shader:


Code:
texture tTX0;
texture tTX1;
texture tTX2;
texture tTX3;

technique T0
{
  pass P0
  {
  // stage0
  ColorOp[0] = SelectArg1;
  ColorArg1[0] = Texture;
  MinFilter[0]=Linear;
  MagFilter[0]=Linear;
  Texture[0] = <tTX0>;

  // stage1
  ColorOp[1] = BlendTextureAlpha;
  ColorArg1[1] = Texture;
  ColorArg2[1] = Current;
  TexCoordIndex[1]=0;
  MinFilter[1]=Linear;
  MagFilter[1]=Linear;
  Texture[1] = <tTX1>;

  // stage2
  ColorOp[2] = BlendTextureAlpha;
  ColorArg1[2] = Texture;
  ColorArg2[2] = Current;
  TexCoordIndex[2]=0;
  MinFilter[2]=Linear;
  MagFilter[2]=Linear;
  Texture[2] = <tTX2>;

  // stage3
  ColorOp[3] = BlendTextureAlpha;
  ColorArg1[3] = Texture;
  ColorArg2[3] = Current;
  TexCoordIndex[3]=0;
  MinFilter[3]=Linear;
  MagFilter[3]=Linear;
  Texture[3] = <tTX3>;

  // stage4
  ColorOp[4] = Disable;
  }
}
View user's profile Send private message
  
hde


Joined: 08 Oct 2007
Posts: 46
Reply with quote
Your support and the quality of your support is awsome ! Thanks

Only a little change or advise needed. The second stage is the Sand layer. The RGB UV part is completely right scaled - as in the terrain mesh. But the alpha layer must be scaled over the whole terrain. Could give me a hint to achieve this ?
Example Screesnshots from the original example in 3D World Studio and Awakening.


View user's profile Send private message
  
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
You need to provide separate uv coordinates for alpha map, this can be achieved by vertex shader. You also need separate alpha map, you can pack three alpha maps to one rgb texture.

Here is a sample shader for static mesh, you need to set the terrainSize vector to suitable value, it's used to compute uv coordinates for alpha map.

Code:
//--- system variables, player feed value ----

//texture tTXn (n = 0..7)

texture tTX0; //alpha map
texture tTX1;
texture tTX2;
texture tTX3;
texture tTX4;

matrix matWorld; //World Matrix
matrix matView;  //View Matrix
matrix matProj;  //Projection Matrix
matrix matTotal; //matWorld*matView*matProj;
matrix matWorldInv; //Inverse World Matrix
matrix matViewInv;  //Inverse View Matrix

vector terrainSize = ( 1280.0f, 1280.0f, 0.0f, 1.0f );



    // Definition of the vertex shader, declarations then assembly
    VertexShader vssplatting =
    decl
    {
   stream 0;
   float v0[3];       // Position
   d3dcolor v1;      // diffuse
   float v2[2];       // Texture Coord
    }
    asm
    {
   vs.1.0

   // c0-c3 contains composite transform matrix
   m4x4 oPos, v0, c0   // transform vertices by view/projection matrix

   
   rcp r1.x, c4.x
   rcp r1.y, c4.y

   mul oT0.xy, v0.xz, r1.xy
   mov oT1.xy, v2
   mov oT2.xy, v2
   mov oT3.xy, v2
   mov oT4.xy, v2
   };


   PixelShader pssplatting=
   asm
   {
   ps.1.4

   // Sample textures
   texld r0, t0 //three alpha maps packed in RGB
   texld r1, t1
   texld r2, t1
   texld r3, t1
   texld r4, t1

   // Combine the textures together based off of their alphamaps
   lrp r2, r0.r, r2, r1
   lrp r3, r0.g, r3, r2
   lrp r0, r0.b, r4, r3
   };



technique T0
{
  pass P0
  {
   Texture[0] = <tTX0>;
   Texture[1] = <tTX1>;
   Texture[2] = <tTX2>;
   Texture[3] = <tTX3>;
   Texture[4] = <tTX4>;

   MinFilter[0]=Linear;
   MagFilter[0]=Linear;
   MinFilter[1]=Linear;
   MagFilter[1]=Linear;
   MinFilter[2]=Linear;
   MagFilter[2]=Linear;
   MinFilter[3]=Linear;
   MagFilter[3]=Linear;
   MinFilter[4]=Linear;
   MagFilter[4]=Linear;

   VertexShader = <vssplatting>;
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <terrainSize>;

   PixelShader = <pssplatting>;
  }
}
View user's profile Send private message
  
arkikusi


Joined: 09 Dec 2009
Posts: 4
Reply with quote
This shader works in awakening 3.6 but it doesn't work in awakeningII.It is possible to arrange it. thanks
View user's profile Send private message
  
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
here it is ( for AwakeningII ):


Code:
//--- system variables, player feed value ----

//texture tTXn (n = 0..7)

texture tTX0; //alpha map
texture tTX1;
texture tTX2;
texture tTX3;
texture tTX4;

matrix matWorld; //World Matrix
matrix matView;  //View Matrix
matrix matProj;  //Projection Matrix
matrix matTotal; //matWorld*matView*matProj;
matrix matWorldInv; //Inverse World Matrix
matrix matViewInv;  //Inverse View Matrix

vector terrainSize = { 1280.0f, 1280.0f, 0.0f, 1.0f };



    // Definition of the vertex shader, declarations then assembly
    VertexShader vssplatting =

    asm
    {
   vs_1_1
   
   dcl_position v0
   dcl_texcoord v2

   // c0-c3 contains composite transform matrix
   m4x4 oPos, v0, c0   // transform vertices by view/projection matrix

   
   rcp r1.x, c4.x
   rcp r1.y, c4.y

   mul oT0.xy, v0.xz, r1.xy
   mov oT1.xy, v2
   mov oT2.xy, v2
   mov oT3.xy, v2
   mov oT4.xy, v2
   };


   PixelShader pssplatting=
   asm
   {
   ps_1_4

   // Sample textures
   texld r0, t0 //three alpha maps packed in RGB
   texld r1, t1
   texld r2, t1
   texld r3, t1
   texld r4, t1

   // Combine the textures together based off of their alphamaps
   lrp r2, r0.r, r2, r1
   lrp r3, r0.g, r3, r2
   lrp r0, r0.b, r4, r3
   };



technique T0
{
  pass P0
  {
   Texture[0] = <tTX0>;
   Texture[1] = <tTX1>;
   Texture[2] = <tTX2>;
   Texture[3] = <tTX3>;
   Texture[4] = <tTX4>;

   MinFilter[0]=Linear;
   MagFilter[0]=Linear;
   MinFilter[1]=Linear;
   MagFilter[1]=Linear;
   MinFilter[2]=Linear;
   MagFilter[2]=Linear;
   MinFilter[3]=Linear;
   MagFilter[3]=Linear;
   MinFilter[4]=Linear;
   MagFilter[4]=Linear;

   VertexShader = <vssplatting>;
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <terrainSize>;

   PixelShader = <pssplatting>;
  }
}



the AwakeningII will provide a editor for texture splatting in future.
View user's profile Send private message
  
arkikusi


Joined: 09 Dec 2009
Posts: 4
Reply with quote
Thank you for the readiness. It works very well. The Editor texture splatting is a good idea.
View user's profile Send private message
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic