Register  |  FAQ  |  Search  |  Memberlist  |  Usergroups  |  Log in 
Reply to topic
 combine light + dot3 + bump + parallax mapping 
hde


Joined: 08 Oct 2007
Posts: 46
Reply with quote
Hello Vincent. It's possible to combine Lightmapping and dot3 or parallax mapping in a shader ? If yes have you already programmed such shader ? Due my travel through some other engines i was very impressed from the free Sauerbraten engine which is able to do such nice things.

By the way, i have noticed that parallaxx and dot3 only reacts properly to one light source. No more than one possible ?
View user's profile Send private message
  
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
Sure, you can freely combine these effects or add light source by writing shader; in fact, some dot3 shaders supports 2 lights. There is a lmBump.sha for lightmap bumpping in the QinEmperor demo.

If you want to add lightmapping to a dot3 shader, just follow a few steps to modify the shader:

1. Define a textrue for lightmap, and select lightmap in material's textures slot.

Code:
texture tTX2; //define a texture for lightmap, please ensure set light map to the 3rd  texture of material


2. Output lightmap UV coordinates in vertex shaders.

Code:
mov  oT2.xy, v2
mov  oT3.xy, v2


3. Blend the lightmap by Color Operator

Code:
  // stage3 (the stage number is not fixed)
  ColorOp[3] = Modulate; //or Modulate2x for brighter effect
  ColorArg1[3] = Texture;
  ColorArg2[3] = Current;
  Texture[3]=<tTX2>;


Here is a sample dot3 + lightmap shader for surfaces:


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

//texture tTXn (n = 0..7)
texture tTX0;
texture tTX1;
texture tTX2; //define a texture for lightmap, please ensure set light map to the 3rd  texture of material

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 _vecEye=(0.0f,1000.0f,0.0f,1.0f);
vector _vecLight=(0.0f,0.0f,0.0f,1.0f);




    // Definition of the vertex shader, declarations then assembly
    VertexShader vsspecular=
    decl
    {
   stream 0;
   float v0[3];       // Position
   float v1[2];      // uv0
   float v2[2];       // Texture Coord
   stream 1;
   float v8[3];   //s - tangent
   float v9[3];   //t - binormal
   float v7[3];   //n - vertex normal
    }
    asm
    {
   vs.1.0

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

   // v2 texture coordination
   mov  oT0.xy, v1
   mov  oT1.xy, v2

        //Output lightmap UV coordinates
        mov  oT2.xy, v2
        mov  oT3.xy, v2


   // c4 lpos
   add  r0, c4, -v0      // L = lpos - vertex, L in object space

   // c6 eye
   add  r1, c6, -v0   // VertexToEyeVector

   // normalize vector in r0
   dp3  r0.w, r0, r0      // r0.w = |r0|^2
   rsq  r0.w, r0.w         // r0.w = 1/sqrt(r0.w) = 1/|r1|
   mul  r0.xyz, r0, r0.w   // r0 - normalized L vector in texture space
   
   // normalize vector in r1
   dp3  r1.w, r1, r1      // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r1.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized E vector in texture space

   add  r0, r0, r1      // HalfVector

   // v8 Tangent,  x
   // v9 Binormal, y
   // v7 normal,   z
   dp3  r1.y, v8, r0
   dp3  r1.x, v9, -r0
   dp3  r1.z, v7, r0

   // r1 H vector in texture space

   // normalize vector in r1
   dp3  r1.w, r1, r1      // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r1.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized L vector in texture space

   // <-1, 1>  to  <0,1>
   // c5  0.5, 0.5, 0.5, 0.5
   mad  oD0, r1, c5, c5

   };



     // Definition of the vertex shader, declarations then assembly
    VertexShader vsdiffuse =
    decl
    {
   stream 0;
   float v0[3];       // Position
   float v1[2];      // uv0
   float v2[2];       // Texture Coord
   stream 1;
   float v8[3];   //s - tangent
   float v9[3];   //t - binormal
   float v7[3];   //n - vertex normal
    }
    asm
    {
   vs.1.0

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

   // v2 texture coordination
   mov  oT0.xy, v1
   mov  oT1.xy, v1

        //Output lightmap UV coordinates
        mov  oT2.xy, v2
        mov  oT3.xy, v2

   // c4 lpos
   add  r0, c4, -v0      // L = lpos - vertex, L in object space

   // v8 Tangent,  x
   // v9 Binormal, y
   // v7 normal,   z

   // matrix for object space to surface space
   // ( Tan.x  Tan.y  Tan.z ) = ( v3.x v3.y v3.z )   ( L.x )
   // ( Bin.x  Bin.y  Bin.z ) = ( v4.x v4.y v4.z ) * ( L.y )
   // ( nor.x  nor.y  nor.z ) = ( v1.x v1.y v1.z )   ( L.z )

   dp3  r1.y, v8, r0
   dp3  r1.x, v9, -r0
   dp3  r1.z, v7, r0


   // r1 L vector in texture space

   // normalize vector in r1
   dp3  r1.w, r1, r1      // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r0.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized L vector in texture space

   // <-1, 1>  to  <0,1>
   // c5  0.5, 0.5, 0.5, 0.5
   mad  oD0, r1, c5, c5

   };



technique T0
{

  pass P1 // specular
  {
  // stage0
  ColorOp[0] = DotProduct3;

  // stage1
  ColorOp[1] = Modulate;
  ColorArg1[1] = Current;
  ColorArg2[1] = Current;

  // stage2
  ColorOp[2] = Modulate;
  ColorArg1[2] = Current;
  ColorArg2[2] = Current;

  // stage3  -- blend lightmap
  ColorOp[3] = Modulate;
  ColorArg1[3] = Texture;
  ColorArg2[3] = Current;
  Texture[3]=<tTX2>;

  // stage4
  ColorOp[4] = Disable;


   // vertex shader constant
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <_vecLight>;
   VertexShaderConstant[5] = (0.5f,0.5f,0.5f,0.5f);
   VertexShaderConstant[6] = <_vecEye>; // eye

   VertexShader = <vsspecular>;

  } // of pass1

  pass P2 //diffuse x texture
  {
  // stage0
  ColorOp[0] = DotProduct3;

  // stage1
  ColorOp[1] = Modulate;
  ColorArg1[1] = Texture;
  ColorArg2[1] = Current;

  Texture[1] = <tTX1>;


  // stage2   -- blend lightmap
  ColorOp[2] = Modulate;
  ColorArg1[2] = Texture;
  ColorArg2[2] = Current;
  Texture[2]=<tTX2>;

  // stage3
  ColorOp[3] = Disable;

  // alpha blend
  SrcBlend = One;
  DestBlend = One;
  AlphaBlendEnable = True;
  ZWriteEnable = False;
//  ZFunc = LessEqual;

   // vertex shader
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <_vecLight>;
   VertexShaderConstant[5] = (0.5f,0.5f,0.5f,0.5f);

   VertexShader = <vsdiffuse>;

  } // of pass2

} //of technique T0



// ---- T1 ----
technique T1
{

  pass P1 // specular
  {
  // stage0
  ColorOp[0] = DotProduct3;

  // stage1
  ColorOp[1] = Modulate;
  ColorArg1[1] = Current;
  ColorArg2[1] = Current;

  // stage2
  ColorOp[2] = Disable;


   // vertex shader constant
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <_vecLight>;
   VertexShaderConstant[5] = (0.5f,0.5f,0.5f,0.5f);
   VertexShaderConstant[6] = <_vecEye>; // eye

   VertexShader = <vsspecular>;

  } // of pass1


  pass P2
  {
  // stage0
  ColorOp[0] = Disable;
  // alpha blend
  SrcBlend = Zero;
  DestBlend = DestColor;
  AlphaBlendEnable = True;
  ZWriteEnable = False;
  }

  pass P3 //diffuse x texture
  {
  // stage0
  ColorOp[0] = DotProduct3;

  // stage1
  ColorOp[1] = Modulate;
  ColorArg1[1] = Texture;
  ColorArg2[1] = Current;

  Texture[1] = <tTX1>;

  // stage2
  ColorOp[2] = Disable;

  // alpha blend
  SrcBlend = One;
  DestBlend = One;
  AlphaBlendEnable = True;
  ZWriteEnable = False;
//  ZFunc = LessEqual;

   // vertex shader
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <_vecLight>;
   VertexShaderConstant[5] = (0.5f,0.5f,0.5f,0.5f);

   VertexShader = <vsdiffuse>;

  } // of pass3

} //of technique T1
View user's profile Send private message
  
Vincent


Joined: 25 Dec 2006
Posts: 296
Reply with quote
a shader for parallax mapping + Lightmapping, please ensure set light map to the 3rd texture of material.

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

//texture tTXn (n = 0..7)
texture tTX0;
texture tTX1;
texture tTX2;

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 _vecEye=(0.0f,1000.0f,0.0f,1.0f);
vector _vecLight=(0.0f,0.0f,0.0f,1.0f);
vector _vecLightColor=(1.0f,1.0f,1.0f,1.0f);


//---- user variables ---
vector vecOffset=(-0.015f, -0.015f, -0.015f, 1.0f);


     // Definition of the vertex shader, declarations then assembly
    VertexShader vsdiffuse =
    decl
    {
   stream 0;
   float v0[3];       // Position
   float v1[2];      // uv0
   float v2[2];       // Texture Coord
   stream 1;
   float v8[3];   //s - tangent
   float v9[3];   //t - binormal
   float v7[3];   //n - vertex normal
    }
    asm
    {
   vs.1.0

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

   // v1 texture coordination
   mov  oT0.xy, v1
   mov  oT1.xy, v1
   mov  oT2.xy, v2

   //------ L vector ----
   // c4 lpos
   add  r0, c4, -v0      // L = lpos - vertex, L in object space

   // v8 Tangent,  x
   // v9 Binormal, y
   // v7 normal,   z

   // matrix for object space to surface space
   dp3  r1.y, v8, r0
   dp3  r1.x, v9, -r0
   dp3  r1.z, v7, r0

   // r1 L vector in texture space

   // normalize vector in r1
   dp3  r1.w, r1, r1      // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r0.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized L vector in texture space

   // <-1, 1>  to  <0,1>
   // c5  0.5, 0.5, 0.5, 0.5
   mad  oD0, r1, c5, c5

   //------- Eye vector ---
   // c6 eye
   add  r0, c6, -v0        // V = VertexToEyeVector = eye - vertex
   // V to Tangent Space
   dp3  r1.y, v8, r0
   dp3  r1.x, v9, -r0
   dp3  r1.z, v7, r0

   // normalize vector in r1
   dp3  r1.w, r1, r1       // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r0.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized V vector in Tangent Space

   mov oT3.xyz, r1

   //------- Half vector ---
   // c4 lpos
   add  r0, c4, -v0      // L = lpos - vertex, L in object space

   // c6 eye
   add  r1, c6, -v0   // VertexToEyeVector

   // normalize vector in r0
   dp3  r0.w, r0, r0      // r0.w = |r0|^2
   rsq  r0.w, r0.w         // r0.w = 1/sqrt(r0.w) = 1/|r1|
   mul  r0.xyz, r0, r0.w   // r0 - normalized L vector in texture space
   
   // normalize vector in r1
   dp3  r1.w, r1, r1      // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r1.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized E vector in texture space

   add  r0, r0, r1      // HalfVector

   // v8 Tangent,  x
   // v9 Binormal, y
   // v7 normal,   z
   dp3  r1.y, v8, r0
   dp3  r1.x, v9, -r0
   dp3  r1.z, v7, r0

   // r1 H vector in texture space

   // normalize vector in r1
   dp3  r1.w, r1, r1      // r1.w = |r1|^2
   rsq  r1.w, r1.w         // r1.w = 1/sqrt(r1.w) = 1/|r1|
   mul  r1.xyz, r1, r1.w   // r1 - normalized L vector in texture space

   // <-1, 1>  to  <0,1>
   // c5  0.5, 0.5, 0.5, 0.5
   mad  oD1, r1, c5, c5

   };




   PixelShader psparallax14 =
   asm
   {
      ps.1.4

// r0 - base
// r1 - normal/height map (height in alpha)

// t0 - base & normal & height
// t3 - eye


texcrd   r0.xyz, t0             // base texture coordination
texld    r1, t0                 // normal/height

texcrd   r2.xyz, t3                 // eye

mul     r3.rgb, r1_bx2.aaa, c1      // scale

//mad     r3.rgb, r3, r2_bx2, r0  // newtexcoord = height * eye + texcoord
mad     r3.rgb, r3, r2, r0  // newtexcoord = height * eye + texcoord

phase

texld    r0, r3 // base map
texld    r1, r3 // normal map
texld    r2, t2 // light map

dp3_sat r0.a, r1_bx2, v1_bx2 // N.H
dp3_sat r1.rgb, r1_bx2, v0_bx2 // N.L
mul      r1.rgb, r1, c2 //light color
+mul    r0.a, r0.a, r0.a       // N.H^2

mul    r0.a, r0.a, r0.a       // N.H^4
mul    r0.a, r0.a, r0.a       // N.H^8
mul    r0.a, r0.a, r0.a       // N.H^16

mad    r0, r0, r1, r0.a  // N.L * base map + N.H^16


mul      r0, r0, r2 //blend lightmap
//mul_x2      r0, r0, r2  //or use this line to replace previous for brighter effect



   };       


technique T0
{
  pass P1
  {
  Texture[0] = <tTX1>;
  Texture[1] = <tTX0>;
  Texture[2] = <tTX2>;

   // vertex shader
   VertexShaderConstant[0] = <matTotal>;   // World*View*Proj Matrix
   VertexShaderConstant[4] = <_vecLight>;
   VertexShaderConstant[5] = (0.5f,0.5f,0.5f,0.5f);
   VertexShaderConstant[6] = <_vecEye>; // eye

   VertexShader = <vsdiffuse>;

   PixelShaderConstant[1] = <vecOffset>;
   PixelShaderConstant[2] = <_vecLightColor>;
   PixelShader = <psparallax14>;
  }
} //of technique T0



//--- T1 ----
technique T1
{
  pass P1
  {
  Texture[0] = <tTX1>;
  }

} //of technique T1
View user's profile Send private message
  
hde


Joined: 08 Oct 2007
Posts: 46
Reply with quote
Oh my god thx Vincent. But please don't await that i understand this shader language Wink
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