Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualLPVOID

Posted 08 September 2013 - 09:52 AM

for more information

I past the Terrain.fx

///////////////////////////纹理/////////////////////////////////////////////////
Texture2D  txDetail_Water:DETAIL_WATER;
Texture2D  txDetail_Stone:DETAIL_STONE;
Texture2D  txDetail_Sand:DETAIL_SAND;
Texture2D  txDetail_Grass:DETAIL_GRASS;
Texture2D  txDetail_Bush:DETAIL_BUSH;
Texture2D  txDetail_Tree:DETAIL_TREE;
Texture2D  txDetail_Rock:DETAIL_ROCK;
Texture2D  txDetail_City:DETAIL_CITY;
Texture2D  txDetail_Snow:DETAIL_SNOW;

Texture2D txSuface:SURFACETEX;
Texture2D txDiff:DIFFUSETEX;
Texture2D txHeight:HEIGHTTEX;

Texture2D WaterNormal:TEX_WATER_N;

//water normal ,tbn space to model space空间转换到模型空间
static  float3x3 M33_TBN_To_M = { 1.0f,0.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,1.0f,0.0f };
//地表材质
//water  0   0.000   0
//stone  1   0.125   32
//sand   2   0.250   64
//Grass  3   0.375   96
//bush   4   0.500   128
//Tree   5   0.625   160
//Rock   6   0.750   192
//City   7   0.875   224
//snow   8   1.000   255
//细节贴图的近处和远处差值//blend the draft tex and detail
static float2 detailMinMax=float2(200.0f,1000.0f);

SamplerState SamHeight
{
    Texture = (txHeight);
    Filter = ANISOTROPIC;
    //Filter = MIN_MAG_MIP_POINT;
    AddressU = Mirror;
    AddressV = Mirror;
    MaxAnisotropy=4;
};
SamplerState SamTexture
{
    Filter = ANISOTROPIC;
    AddressU = Wrap;
    AddressV = Wrap;
    MaxAnisotropy=8;
};
SamplerState PointSample
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Wrap;
    AddressV = Wrap;
};



///////////////////////////参数//////////////////////////////////////////////////
cbuffer transformDate
{
	float4x4 m44_V:VIEW;//
	float4x4 m44_WV_NOSCALE:WV_NOSCALE;
	float4x4 m44_WV:WORLDVIEW;
	float4x4 m44_WVP:WORLDVIEWPROJ;
	float3    v3_CamPos:CAMPOS;
	float4	 exPP:EXTEND_PP;//DetailRepeat  HeigntMapWidth areaWidth,segs
	float4	exPP_1:EXTEND_PP_1;//scaleFnK,scaleFnB,fTime,mMTimeLoop(Mtime在0-2Pi变化)
//terrain height= tex.r* scaleFnK+scaleFnB;
	float4x4 m44_WVP_L:WORLDVIEWPROJ_L;
	float2 WaveUVOffset0:W_UV_0;
	float2 WaveUVOffset1:W_UV_1;
	float   WaveUVRepeat:W_UV_REPEAT;
}

struct VS_IN
{
   float4 posL: POSITION;
   float3 nor: NORMAL;
   float2 texCoor:TEXCOORD;
};

struct PS_IN_MAIN
{
   float4 pos:SV_POSITION;
   float3 norView:NORMAL;
   float2 texCoor:TEXCOORD0;
   float4 posView:TEXCOORD1;
};

struct PS_OUT_MAIN
{
	float4 G0 :		SV_Target0;//texture
	float4 G1 :		SV_Target1;//nor
	float4 G2:		SV_Target2;//pos
};
struct PS_IN_L
{
	float4 pos:SV_POSITION;
};

float3 GetModelPos(in float2 UV)
{
	float x=(UV.x-0.5)*exPP.z;
	float z=(0.5-UV.y)*exPP.z;
	float y=tex2Dlod(SamHeight, float4(UV, 0,0)).r*exPP_1.x+exPP_1.y;
	return float3(x,y,z);
}
/////////////光照 VS//////////////
PS_IN_L VS_Light(VS_IN vIn)
{
    PS_IN_L vOut=(PS_IN_L) 0;
    float4 tranformedPos=vIn.posL;
    tranformedPos.y=tex2Dlod(SamHeight, float4(vIn.texCoor, 0,0)).r*exPP_1.x+exPP_1.y;
   vOut.pos=mul(tranformedPos,m44_WVP_L);
	return vOut;
}
///////////光照PS////////////////
//只写深度缓冲,因此不输出//only need z,no output
void PS_Light(PS_IN_L pIn)
{
	
}
///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//9种细节贴图则将1/8=0.125,得到9个颜色分布,靠近
static float surfaceTypeDistance=0.125f;
/////////主体VS////////////
PS_IN_MAIN		VS_Main(VS_IN vIn)
{
	PS_IN_MAIN vOut=(PS_IN_MAIN) 0;
	float UVOffset=1/(exPP.w-1);
        float4 tranformedPos=vIn.posL;
   	float3 PosC=GetModelPos(vIn.texCoor);
        tranformedPos.y=PosC.y;
        //Compute normal
   	float3 PosL=GetModelPos(vIn.texCoor+float2(-UVOffset,0));
	float3 PosR=GetModelPos(vIn.texCoor+float2(UVOffset,0));
	float3 PosUp=GetModelPos(vIn.texCoor+float2(0,-UVOffset));
	float3 PosDown=GetModelPos(vIn.texCoor+float2(0,UVOffset));
	float3 dirUp=PosUp-PosC;
	float3 dirRight=PosR-PosC;
	float3 dirDown=PosDown-PosC;
	float3 dirLeft=PosL-PosC;
	float3 nor=normalize(cross(dirUp,dirRight));
	 nor+=normalize(cross(dirRight,dirDown));
	nor+=normalize(cross(dirDown,dirLeft));
	nor+=normalize(cross(dirLeft,dirUp)); 
	nor=normalize(nor);
	vOut.norView=normalize(mul(nor,(float3x3)m44_V));

   vOut.texCoor=vIn.texCoor;
   vOut.pos=mul(tranformedPos,m44_WVP);
   vOut.posView=mul(tranformedPos,m44_WV);
   return vOut;
}
////////主体PS////////////
PS_OUT_MAIN PS_Main(PS_IN_MAIN pIn)
{
	PS_OUT_MAIN pOut=(PS_OUT_MAIN) 0;
	// float3  normalColor= txNor.Sample(SamTexture, pIn.texCoor).xyz*2-1; 
	 pOut.G1.xyz=pIn.norView;//mul(normalize(normalColor),(float3x3)m44_V);
	 //pOut.G1.xyz=normalColor; 
	 pOut.G1.w=0;
	 
 // float3 worldNor=normalize(tex2Dlod(SamNor, float4(vIn.texCoor, 0,0))*2-1);
   ///纹理混合
   //(x>=y true)
   float detailUVRepeat=10.0f;
   float3 texD_0=txDetail_Water.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_1=txDetail_Stone.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_2=txDetail_Sand.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*100).rgb;
   float3 texD_3=txDetail_Grass.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_4=txDetail_Bush.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_5=txDetail_Tree.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_6=txDetail_Rock.Sample(SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_7=txDetail_City.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_8=txDetail_Snow.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;

   float surfValue=txSuface.Sample(SamTexture, pIn.texCoor).r;
   //确定surface纹理定义的种类和混合
   //surface color blend
   float tempV=surfValue/0.125f;

   int intPart=(int)floor(tempV);
   float floatPart=frac(tempV);
   float3 detailMix;
   switch(intPart)
   {
	case 0:
		{
			detailMix=lerp(texD_0,texD_1,floatPart);
			float3 wN_0=WaterNormal.Sample(SamTexture,(pIn.texCoor+WaveUVOffset0)*WaveUVRepeat).xyz*2-1;
			float3 wN_1=WaterNormal.Sample(SamTexture,(pIn.texCoor-WaveUVOffset1)*WaveUVRepeat).xyz*2-1;
			float3 wN=normalize(wN_0+wN_1);
			wN=mul(wN,M33_TBN_To_M);
			wN=mul(wN,(float3x3)m44_V);
			float3 OkN=normalize(pIn.norView*0.7+wN*0.3);
			pOut.G1.xyz=OkN;
		}
		break;
	case 1:
	     detailMix=lerp(texD_1,texD_2,floatPart);
		break;
	case 2:
		detailMix=lerp(texD_2,texD_3,floatPart);
		break;
	case 3:
	    detailMix=lerp(texD_3,texD_4,floatPart);
		break;
	case 4:
		detailMix=lerp(texD_4,texD_5,floatPart);
		break;
	case 5:
		detailMix=lerp(texD_5,texD_6,floatPart);
		break;
	case 6:
		detailMix=lerp(texD_6,texD_7,floatPart);
		break;
	case 7:
		detailMix=lerp(texD_7,texD_8,floatPart);
		break;
	case 8:
		detailMix=texD_8;
		break;
   }
   float3 diffTex=txDiff.Sample(SamTexture,pIn.texCoor).rgb;
   float rateOfDiffTex=saturate((pIn.posView.z-detailMinMax.x)/(detailMinMax.y-detailMinMax.x));
   pOut.G0.xyz=lerp(detailMix,diffTex,rateOfDiffTex);//
   pOut.G0.w=1.0;//alpha
   pOut.G2=pIn.posView;
   pOut.G2.w=intPart;//材质ID
   return pOut;
}
///////////////////////////////////////////////////


BlendState NoBlend
{
    BlendEnable[0] = FALSE;
};

DepthStencilState dsState
{
   DepthFunc = LESS_EQUAL;
   DepthEnable = TRUE;
   DepthWriteMask =1;
};

//RasterizerState rsSolid
//{
//FillMode = Solid;
//CullMode = 3;
//};

technique10 main
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Main() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Main() ) );
       SetDepthStencilState(dsState, 0);
       // SetRasterizerState(rsSolid);
       SetBlendState( NoBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
   } 
}
technique10 Lighting
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Light() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Light() ) );
   } 
}

#3LPVOID

Posted 08 September 2013 - 09:50 AM

for more information

I past the Terrain.fx

///////////////////////////纹理/////////////////////////////////////////////////
Texture2D  txDetail_Water:DETAIL_WATER;
Texture2D  txDetail_Stone:DETAIL_STONE;
Texture2D  txDetail_Sand:DETAIL_SAND;
Texture2D  txDetail_Grass:DETAIL_GRASS;
Texture2D  txDetail_Bush:DETAIL_BUSH;
Texture2D  txDetail_Tree:DETAIL_TREE;
Texture2D  txDetail_Rock:DETAIL_ROCK;
Texture2D  txDetail_City:DETAIL_CITY;
Texture2D  txDetail_Snow:DETAIL_SNOW;

Texture2D txSuface:SURFACETEX;
Texture2D txDiff:DIFFUSETEX;
Texture2D txHeight:HEIGHTTEX;

Texture2D WaterNormal:TEX_WATER_N;

//water normal ,tbn space to model space空间转换到模型空间
static  float3x3 M33_TBN_To_M = { 1.0f,0.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,1.0f,0.0f };
//地表材质
//water  0   0.000   0
//stone  1   0.125   32
//sand   2   0.250   64
//Grass  3   0.375   96
//bush   4   0.500   128
//Tree   5   0.625   160
//Rock   6   0.750   192
//City   7   0.875   224
//snow   8   1.000   255
//细节贴图的近处和远处差值//blend the draft tex and detail
static float2 detailMinMax=float2(200.0f,1000.0f);

SamplerState SamHeight
{
	Texture = (txHeight);
     Filter = ANISOTROPIC;
    //Filter = MIN_MAG_MIP_POINT;
    AddressU = Mirror;
    AddressV = Mirror;
    MaxAnisotropy=4;
};
SamplerState SamTexture
{
    Filter = ANISOTROPIC;
    AddressU = Wrap;
    AddressV = Wrap;
    MaxAnisotropy=8;
};
SamplerState PointSample
{
	Filter = MIN_MAG_MIP_POINT;
	AddressU = Wrap;
    AddressV = Wrap;
};



///////////////////////////参数//////////////////////////////////////////////////
cbuffer transformDate
{
	float4x4 m44_V:VIEW;//
	float4x4 m44_WV_NOSCALE:WV_NOSCALE;
	float4x4 m44_WV:WORLDVIEW;
	float4x4 m44_WVP:WORLDVIEWPROJ;
	float3    v3_CamPos:CAMPOS;
	float4	 exPP:EXTEND_PP;//DetailRepeat  HeigntMapWidth areaWidth,segs
	float4	exPP_1:EXTEND_PP_1;//scaleFnK,scaleFnB,fTime,mMTimeLoop(Mtime在0-2Pi变化)
//terrain height= tex.r* scaleFnK+scaleFnB;
	float4x4 m44_WVP_L:WORLDVIEWPROJ_L;
	float2 WaveUVOffset0:W_UV_0;
	float2 WaveUVOffset1:W_UV_1;
	float   WaveUVRepeat:W_UV_REPEAT;
}

struct VS_IN
{
   float4 posL: POSITION;
   float3 nor: NORMAL;
   float2 texCoor:TEXCOORD;
};

struct PS_IN_MAIN
{
   float4 pos:SV_POSITION;
	  float3 norView:NORMAL;
   float2 texCoor:TEXCOORD0;
   float4 posView:TEXCOORD1;
};

struct PS_OUT_MAIN
{
	float4 G0 :		SV_Target0;//texture
	float4 G1 :		SV_Target1;//nor
	float4 G2:		SV_Target2;//pos
};
struct PS_IN_L
{
	float4 pos:SV_POSITION;
};

float3 GetModelPos(in float2 UV)
{
	float x=(UV.x-0.5)*exPP.z;
	float z=(0.5-UV.y)*exPP.z;
	float y=tex2Dlod(SamHeight, float4(UV, 0,0)).r*exPP_1.x+exPP_1.y;
	return float3(x,y,z);
}
/////////////光照 VS//////////////
PS_IN_L VS_Light(VS_IN vIn)
{
	PS_IN_L vOut=(PS_IN_L) 0;
    float4 tranformedPos=vIn.posL;
    tranformedPos.y=tex2Dlod(SamHeight, float4(vIn.texCoor, 0,0)).r*exPP_1.x+exPP_1.y;
   vOut.pos=mul(tranformedPos,m44_WVP_L);
	return vOut;
}
///////////光照PS////////////////
//只写深度缓冲,因此不输出//only need z,no output
void PS_Light(PS_IN_L pIn)
{
	
}
///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//9种细节贴图则将1/8=0.125,得到9个颜色分布,靠近
static float surfaceTypeDistance=0.125f;
/////////主体VS////////////
PS_IN_MAIN		VS_Main(VS_IN vIn)
{
	PS_IN_MAIN vOut=(PS_IN_MAIN) 0;
	float UVOffset=1/(exPP.w-1);
   float4 tranformedPos=vIn.posL;
   	float3 PosC=GetModelPos(vIn.texCoor);
    tranformedPos.y=PosC.y;
        //Compute normal
   	float3 PosL=GetModelPos(vIn.texCoor+float2(-UVOffset,0));
	float3 PosR=GetModelPos(vIn.texCoor+float2(UVOffset,0));
	float3 PosUp=GetModelPos(vIn.texCoor+float2(0,-UVOffset));
	float3 PosDown=GetModelPos(vIn.texCoor+float2(0,UVOffset));
	float3 dirUp=PosUp-PosC;
	float3 dirRight=PosR-PosC;
	float3 dirDown=PosDown-PosC;
	float3 dirLeft=PosL-PosC;
	float3 nor=normalize(cross(dirUp,dirRight));
	 nor+=normalize(cross(dirRight,dirDown));
	nor+=normalize(cross(dirDown,dirLeft));
	nor+=normalize(cross(dirLeft,dirUp)); 
	nor=normalize(nor);
	vOut.norView=normalize(mul(nor,(float3x3)m44_V));

   vOut.texCoor=vIn.texCoor;
   vOut.pos=mul(tranformedPos,m44_WVP);
   vOut.posView=mul(tranformedPos,m44_WV);
   return vOut;
}
////////主体PS////////////
PS_OUT_MAIN PS_Main(PS_IN_MAIN pIn)
{
	PS_OUT_MAIN pOut=(PS_OUT_MAIN) 0;
	// float3  normalColor= txNor.Sample(SamTexture, pIn.texCoor).xyz*2-1; 
	 pOut.G1.xyz=pIn.norView;//mul(normalize(normalColor),(float3x3)m44_V);
	 //pOut.G1.xyz=normalColor; 
	 pOut.G1.w=0;
	 
 // float3 worldNor=normalize(tex2Dlod(SamNor, float4(vIn.texCoor, 0,0))*2-1);
   ///纹理混合
   //(x>=y true)
   float detailUVRepeat=10.0f;
   float3 texD_0=txDetail_Water.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_1=txDetail_Stone.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_2=txDetail_Sand.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*100).rgb;
   float3 texD_3=txDetail_Grass.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_4=txDetail_Bush.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_5=txDetail_Tree.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_6=txDetail_Rock.Sample(SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_7=txDetail_City.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_8=txDetail_Snow.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;

   float surfValue=txSuface.Sample(SamTexture, pIn.texCoor).r;
   //确定surface纹理定义的种类和混合
   //surface color blend
   float tempV=surfValue/0.125f;

   int intPart=(int)floor(tempV);
   float floatPart=frac(tempV);
   float3 detailMix;
   switch(intPart)
   {
	case 0:
		{
			detailMix=lerp(texD_0,texD_1,floatPart);
			float3 wN_0=WaterNormal.Sample(SamTexture,(pIn.texCoor+WaveUVOffset0)*WaveUVRepeat).xyz*2-1;
			float3 wN_1=WaterNormal.Sample(SamTexture,(pIn.texCoor-WaveUVOffset1)*WaveUVRepeat).xyz*2-1;
			float3 wN=normalize(wN_0+wN_1);
			wN=mul(wN,M33_TBN_To_M);
			wN=mul(wN,(float3x3)m44_V);
			float3 OkN=normalize(pIn.norView*0.7+wN*0.3);
			pOut.G1.xyz=OkN;
		}
		break;
	case 1:
	     detailMix=lerp(texD_1,texD_2,floatPart);
		break;
	case 2:
		detailMix=lerp(texD_2,texD_3,floatPart);
		break;
	case 3:
	    detailMix=lerp(texD_3,texD_4,floatPart);
		break;
	case 4:
		detailMix=lerp(texD_4,texD_5,floatPart);
		break;
	case 5:
		detailMix=lerp(texD_5,texD_6,floatPart);
		break;
	case 6:
		detailMix=lerp(texD_6,texD_7,floatPart);
		break;
	case 7:
		detailMix=lerp(texD_7,texD_8,floatPart);
		break;
	case 8:
		detailMix=texD_8;
		break;
   }
   float3 diffTex=txDiff.Sample(SamTexture,pIn.texCoor).rgb;
   float rateOfDiffTex=saturate((pIn.posView.z-detailMinMax.x)/(detailMinMax.y-detailMinMax.x));
   pOut.G0.xyz=lerp(detailMix,diffTex,rateOfDiffTex);//
   pOut.G0.w=1.0;//alpha
   pOut.G2=pIn.posView;
   pOut.G2.w=intPart;//材质ID
   return pOut;
}
///////////////////////////////////////////////////


BlendState NoBlend
{
    BlendEnable[0] = FALSE;
};

DepthStencilState dsState
{
   DepthFunc = LESS_EQUAL;
   DepthEnable = TRUE;
   DepthWriteMask =1;
};

//RasterizerState rsSolid
//{
//FillMode = Solid;
//CullMode = 3;
//};

technique10 main
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Main() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Main() ) );
       SetDepthStencilState(dsState, 0);
	  // SetRasterizerState(rsSolid);
	   SetBlendState( NoBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
   } 
}
technique10 Lighting
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Light() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Light() ) );
   } 
}

#2LPVOID

Posted 08 September 2013 - 09:48 AM

for more information

I past the Terrain.fx

///////////////////////////纹理/////////////////////////////////////////////////
Texture2D  txDetail_Water:DETAIL_WATER;
Texture2D  txDetail_Stone:DETAIL_STONE;
Texture2D  txDetail_Sand:DETAIL_SAND;
Texture2D  txDetail_Grass:DETAIL_GRASS;
Texture2D  txDetail_Bush:DETAIL_BUSH;
Texture2D  txDetail_Tree:DETAIL_TREE;
Texture2D  txDetail_Rock:DETAIL_ROCK;
Texture2D  txDetail_City:DETAIL_CITY;
Texture2D  txDetail_Snow:DETAIL_SNOW;

Texture2D txSuface:SURFACETEX;
Texture2D txDiff:DIFFUSETEX;
Texture2D txHeight:HEIGHTTEX;

Texture2D WaterNormal:TEX_WATER_N;

//water normal ,tbn space to model space空间转换到模型空间
static  float3x3 M33_TBN_To_M = { 1.0f,0.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,1.0f,0.0f };
//地表材质
//water  0   0.000   0
//stone  1   0.125   32
//sand   2   0.250   64
//Grass  3   0.375   96
//bush   4   0.500   128
//Tree   5   0.625   160
//Rock   6   0.750   192
//City   7   0.875   224
//snow   8   1.000   255
//细节贴图的近处和远处差值//blend the draft tex and detail
static float2 detailMinMax=float2(200.0f,1000.0f);

SamplerState SamHeight
{
	Texture = (txHeight);
     Filter = ANISOTROPIC;
    //Filter = MIN_MAG_MIP_POINT;
    AddressU = Mirror;
    AddressV = Mirror;
    MaxAnisotropy=4;
};
SamplerState SamTexture
{
    Filter = ANISOTROPIC;
    AddressU = Wrap;
    AddressV = Wrap;
    MaxAnisotropy=8;
};
SamplerState PointSample
{
	Filter = MIN_MAG_MIP_POINT;
	AddressU = Wrap;
    AddressV = Wrap;
};



///////////////////////////参数//////////////////////////////////////////////////
cbuffer transformDate
{
	float4x4 m44_V:VIEW;//
	float4x4 m44_WV_NOSCALE:WV_NOSCALE;
	float4x4 m44_WV:WORLDVIEW;
	float4x4 m44_WVP:WORLDVIEWPROJ;
	float3    v3_CamPos:CAMPOS;
	float4	 exPP:EXTEND_PP;//DetailRepeat  HeigntMapWidth areaWidth,segs
	float4	exPP_1:EXTEND_PP_1;//scaleFnK,scaleFnB,fTime,mMTimeLoop(Mtime在0-2Pi变化)
//terrain height= tex.r* scaleFnK+scaleFnB;
	float4x4 m44_WVP_L:WORLDVIEWPROJ_L;
	float2 WaveUVOffset0:W_UV_0;
	float2 WaveUVOffset1:W_UV_1;
	float   WaveUVRepeat:W_UV_REPEAT;
}

struct VS_IN
{
   float4 posL: POSITION;
   float3 nor: NORMAL;
   float2 texCoor:TEXCOORD;
};

struct PS_IN_MAIN
{
   float4 pos:SV_POSITION;
	  float3 norView:NORMAL;
   float2 texCoor:TEXCOORD0;
   float4 posView:TEXCOORD1;
};

struct PS_OUT_MAIN
{
	float4 G0 :		SV_Target0;//texture
	float4 G1 :		SV_Target1;//nor
	float4 G2:		SV_Target2;//pos
};
struct PS_IN_L
{
	float4 pos:SV_POSITION;
};

float3 GetModelPos(in float2 UV)
{
	float x=(UV.x-0.5)*exPP.z;
	float z=(0.5-UV.y)*exPP.z;
	float y=tex2Dlod(SamHeight, float4(UV, 0,0)).r*exPP_1.x+exPP_1.y;
	return float3(x,y,z);
}
/////////////光照 VS//////////////
PS_IN_L VS_Light(VS_IN vIn)
{
	PS_IN_L vOut=(PS_IN_L) 0;
    float4 tranformedPos=vIn.posL;
    tranformedPos.y=tex2Dlod(SamHeight, float4(vIn.texCoor, 0,0)).r*exPP_1.x+exPP_1.y;
   vOut.pos=mul(tranformedPos,m44_WVP_L);
	return vOut;
}
///////////光照PS////////////////
//只写深度缓冲,因此不输出//only need z,no output
void PS_Light(PS_IN_L pIn)
{
	
}
///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//9种细节贴图则将1/8=0.125,得到9个颜色分布,靠近
static float surfaceTypeDistance=0.125f;
/////////主体VS////////////
PS_IN_MAIN		VS_Main(VS_IN vIn)
{
	PS_IN_MAIN vOut=(PS_IN_MAIN) 0;
	float UVOffset=1/(exPP.w-1);
   float4 tranformedPos=vIn.posL;
   	float3 PosC=GetModelPos(vIn.texCoor);
    tranformedPos.y=PosC.y;
        //Compute normal
   	float3 PosL=GetModelPos(vIn.texCoor+float2(-UVOffset,0));
	float3 PosR=GetModelPos(vIn.texCoor+float2(UVOffset,0));
	float3 PosUp=GetModelPos(vIn.texCoor+float2(0,-UVOffset));
	float3 PosDown=GetModelPos(vIn.texCoor+float2(0,UVOffset));
	float3 dirUp=PosUp-PosC;
	float3 dirRight=PosR-PosC;
	float3 dirDown=PosDown-PosC;
	float3 dirLeft=PosL-PosC;
	float3 nor=normalize(cross(dirUp,dirRight));
	 nor+=normalize(cross(dirRight,dirDown));
	nor+=normalize(cross(dirDown,dirLeft));
	nor+=normalize(cross(dirLeft,dirUp)); 
	nor=normalize(nor);
	vOut.norView=normalize(mul(nor,(float3x3)m44_V));

   vOut.texCoor=vIn.texCoor;
   vOut.pos=mul(tranformedPos,m44_WVP);
   vOut.posView=mul(tranformedPos,m44_WV);
   return vOut;
}
////////主体PS////////////
PS_OUT_MAIN PS_Main(PS_IN_MAIN pIn)
{
	PS_OUT_MAIN pOut=(PS_OUT_MAIN) 0;
	// float3  normalColor= txNor.Sample(SamTexture, pIn.texCoor).xyz*2-1; 
	 pOut.G1.xyz=pIn.norView;//mul(normalize(normalColor),(float3x3)m44_V);
	 //pOut.G1.xyz=normalColor; 
	 pOut.G1.w=0;
	 
 // float3 worldNor=normalize(tex2Dlod(SamNor, float4(vIn.texCoor, 0,0))*2-1);
   ///纹理混合
   //(x>=y true)
   float detailUVRepeat=10.0f;
   float3 texD_0=txDetail_Water.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_1=txDetail_Stone.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_2=txDetail_Sand.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*100).rgb;
   float3 texD_3=txDetail_Grass.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_4=txDetail_Bush.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_5=txDetail_Tree.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_6=txDetail_Rock.Sample(SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_7=txDetail_City.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_8=txDetail_Snow.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;

   float surfValue=txSuface.Sample(SamTexture, pIn.texCoor).r;
   //确定surface纹理定义的种类和混合
   float tempV=surfValue/0.125f;

   int intPart=(int)floor(tempV);
   float floatPart=frac(tempV);
   float3 detailMix;
   switch(intPart)
   {
	case 0:
		{
			detailMix=lerp(texD_0,texD_1,floatPart);
			float3 wN_0=WaterNormal.Sample(SamTexture,(pIn.texCoor+WaveUVOffset0)*WaveUVRepeat).xyz*2-1;
			float3 wN_1=WaterNormal.Sample(SamTexture,(pIn.texCoor-WaveUVOffset1)*WaveUVRepeat).xyz*2-1;
			float3 wN=normalize(wN_0+wN_1);
			wN=mul(wN,M33_TBN_To_M);
			wN=mul(wN,(float3x3)m44_V);
			float3 OkN=normalize(pIn.norView*0.7+wN*0.3);
			pOut.G1.xyz=OkN;
		}
		break;
	case 1:
	     detailMix=lerp(texD_1,texD_2,floatPart);
		break;
	case 2:
		detailMix=lerp(texD_2,texD_3,floatPart);
		break;
	case 3:
	    detailMix=lerp(texD_3,texD_4,floatPart);
		break;
	case 4:
		detailMix=lerp(texD_4,texD_5,floatPart);
		break;
	case 5:
		detailMix=lerp(texD_5,texD_6,floatPart);
		break;
	case 6:
		detailMix=lerp(texD_6,texD_7,floatPart);
		break;
	case 7:
		detailMix=lerp(texD_7,texD_8,floatPart);
		break;
	case 8:
		detailMix=texD_8;
		break;
   }
   float3 diffTex=txDiff.Sample(SamTexture,pIn.texCoor).rgb;
   float rateOfDiffTex=saturate((pIn.posView.z-detailMinMax.x)/(detailMinMax.y-detailMinMax.x));
   pOut.G0.xyz=lerp(detailMix,diffTex,rateOfDiffTex);//
   pOut.G0.w=1.0;//alpha
   pOut.G2=pIn.posView;
   pOut.G2.w=intPart;//材质ID
   return pOut;
}
///////////////////////////////////////////////////


BlendState NoBlend
{
    BlendEnable[0] = FALSE;
};

DepthStencilState dsState
{
   DepthFunc = LESS_EQUAL;
   DepthEnable = TRUE;
   DepthWriteMask =1;
};

//RasterizerState rsSolid
//{
//FillMode = Solid;
//CullMode = 3;
//};

technique10 main
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Main() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Main() ) );
       SetDepthStencilState(dsState, 0);
	  // SetRasterizerState(rsSolid);
	   SetBlendState( NoBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
   } 
}
technique10 Lighting
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Light() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Light() ) );
   } 
}

#1LPVOID

Posted 08 September 2013 - 09:44 AM

for more information

I past the Terrain.fx

///////////////////////////纹理/////////////////////////////////////////////////
Texture2D  txDetail_Water:DETAIL_WATER;
Texture2D  txDetail_Stone:DETAIL_STONE;
Texture2D  txDetail_Sand:DETAIL_SAND;
Texture2D  txDetail_Grass:DETAIL_GRASS;
Texture2D  txDetail_Bush:DETAIL_BUSH;
Texture2D  txDetail_Tree:DETAIL_TREE;
Texture2D  txDetail_Rock:DETAIL_ROCK;
Texture2D  txDetail_City:DETAIL_CITY;
Texture2D  txDetail_Snow:DETAIL_SNOW;

Texture2D txSuface:SURFACETEX;
Texture2D txDiff:DIFFUSETEX;
Texture2D txHeight:HEIGHTTEX;

Texture2D WaterNormal:TEX_WATER_N;

//water normal ,tbn space to model space空间转换到模型空间
static  float3x3 M33_TBN_To_M = { 1.0f,0.0f,0.0f,0.0f,0.0f,-1.0f,0.0f,1.0f,0.0f };
//地表材质
//water  0   0.000   0
//stone  1   0.125   32
//sand   2   0.250   64
//Grass  3   0.375   96
//bush   4   0.500   128
//Tree   5   0.625   160
//Rock   6   0.750   192
//sity   7   0.875   224
//snow   8   1.000   255
//细节贴图的近处和远处差值
static float2 detailMinMax=float2(200.0f,1000.0f);

SamplerState SamHeight
{
	Texture = (txHeight);
     Filter = ANISOTROPIC;
    //Filter = MIN_MAG_MIP_POINT;
    AddressU = Mirror;
    AddressV = Mirror;
    MaxAnisotropy=4;
};
SamplerState SamTexture
{
    Filter = ANISOTROPIC;
    AddressU = Wrap;
    AddressV = Wrap;
    MaxAnisotropy=8;
};
SamplerState PointSample
{
	Filter = MIN_MAG_MIP_POINT;
	AddressU = Wrap;
    AddressV = Wrap;
};
//SamplerState SamTexture:		register(s0);
//SamplerState SamHeight:	register(s1);


///////////////////////////参数//////////////////////////////////////////////////
cbuffer transformDate
{
	float4x4 m44_V:VIEW;//
	float4x4 m44_WV_NOSCALE:WV_NOSCALE;
	float4x4 m44_WV:WORLDVIEW;
	float4x4 m44_WVP:WORLDVIEWPROJ;
	float3    v3_CamPos:CAMPOS;
	float4	 exPP:EXTEND_PP;//DetailRepeat  HeigntMapWidth areaWidth,segs
	float4	exPP_1:EXTEND_PP_1;//scaleFnK,scaleFnB,fTime,mMTimeLoop(Mtime在0-2Pi变化)
	float4x4 m44_WVP_L:WORLDVIEWPROJ_L;
	float2 WaveUVOffset0:W_UV_0;
	float2 WaveUVOffset1:W_UV_1;
	float   WaveUVRepeat:W_UV_REPEAT;
}

struct VS_IN
{
   float4 posL: POSITION;
   float3 nor: NORMAL;
   float2 texCoor:TEXCOORD;
};

struct PS_IN_MAIN
{
   float4 pos:SV_POSITION;
	  float3 norView:NORMAL;
   float2 texCoor:TEXCOORD0;
   float4 posView:TEXCOORD1;
};

struct PS_OUT_MAIN
{
	float4 G0 :		SV_Target0;//texture
	float4 G1 :		SV_Target1;//nor
	float4 G2:		SV_Target2;//pos
};
struct PS_IN_L
{
	float4 pos:SV_POSITION;
};

float3 GetModelPos(in float2 UV)
{
	float x=(UV.x-0.5)*exPP.z;
	float z=(0.5-UV.y)*exPP.z;
	float y=tex2Dlod(SamHeight, float4(UV, 0,0)).r*exPP_1.x+exPP_1.y;
	return float3(x,y,z);
}
/////////////光照 VS//////////////
PS_IN_L VS_Light(VS_IN vIn)
{
	PS_IN_L vOut=(PS_IN_L) 0;
    float4 tranformedPos=vIn.posL;
    tranformedPos.y=tex2Dlod(SamHeight, float4(vIn.texCoor, 0,0)).r*exPP_1.x+exPP_1.y;
   vOut.pos=mul(tranformedPos,m44_WVP_L);
	return vOut;
}
///////////光照PS////////////////
//只写深度缓冲,因此不输出
void PS_Light(PS_IN_L pIn)
{
	
}
///////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//9种细节贴图则将1/8=0.125,得到9个颜色分布,靠近
static float surfaceTypeDistance=0.125f;
/////////主体VS////////////
PS_IN_MAIN		VS_Main(VS_IN vIn)
{
	PS_IN_MAIN vOut=(PS_IN_MAIN) 0;
	float UVOffset=1/(exPP.w-1);
   float4 tranformedPos=vIn.posL;
   	float3 PosC=GetModelPos(vIn.texCoor);
    tranformedPos.y=PosC.y;
   	float3 PosL=GetModelPos(vIn.texCoor+float2(-UVOffset,0));
	float3 PosR=GetModelPos(vIn.texCoor+float2(UVOffset,0));
	float3 PosUp=GetModelPos(vIn.texCoor+float2(0,-UVOffset));
	float3 PosDown=GetModelPos(vIn.texCoor+float2(0,UVOffset));
	float3 dirUp=PosUp-PosC;
	float3 dirRight=PosR-PosC;
	float3 dirDown=PosDown-PosC;
	float3 dirLeft=PosL-PosC;
	float3 nor=normalize(cross(dirUp,dirRight));
	 nor+=normalize(cross(dirRight,dirDown));
	nor+=normalize(cross(dirDown,dirLeft));
	nor+=normalize(cross(dirLeft,dirUp)); 
	nor=normalize(nor);
	vOut.norView=normalize(mul(nor,(float3x3)m44_V));

   vOut.texCoor=vIn.texCoor;
   vOut.pos=mul(tranformedPos,m44_WVP);
   vOut.posView=mul(tranformedPos,m44_WV);
   return vOut;
}
////////主体PS////////////
PS_OUT_MAIN PS_Main(PS_IN_MAIN pIn)
{
	PS_OUT_MAIN pOut=(PS_OUT_MAIN) 0;
	// float3  normalColor= txNor.Sample(SamTexture, pIn.texCoor).xyz*2-1; 
	 pOut.G1.xyz=pIn.norView;//mul(normalize(normalColor),(float3x3)m44_V);
	 //pOut.G1.xyz=normalColor; 
	 pOut.G1.w=0;
	 
 // float3 worldNor=normalize(tex2Dlod(SamNor, float4(vIn.texCoor, 0,0))*2-1);
   ///纹理混合
   //(x>=y true)
   float detailUVRepeat=10.0f;
   float3 texD_0=txDetail_Water.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_1=txDetail_Stone.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*5).rgb;
   float3 texD_2=txDetail_Sand.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*100).rgb;
   float3 texD_3=txDetail_Grass.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_4=txDetail_Bush.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat*10).rgb;
   float3 texD_5=txDetail_Tree.Sample(	SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_6=txDetail_Rock.Sample(SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_7=txDetail_City.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;
   float3 texD_8=txDetail_Snow.Sample(		SamTexture,		pIn.texCoor*detailUVRepeat).rgb;

   float surfValue=txSuface.Sample(SamTexture, pIn.texCoor).r;
   //确定surface纹理定义的种类和混合
   float tempV=surfValue/0.125f;

   int intPart=(int)floor(tempV);
   float floatPart=frac(tempV);
   float3 detailMix;
   switch(intPart)
   {
	case 0:
		{
			detailMix=lerp(texD_0,texD_1,floatPart);
			float3 wN_0=WaterNormal.Sample(SamTexture,(pIn.texCoor+WaveUVOffset0)*WaveUVRepeat).xyz*2-1;
			float3 wN_1=WaterNormal.Sample(SamTexture,(pIn.texCoor-WaveUVOffset1)*WaveUVRepeat).xyz*2-1;
			float3 wN=normalize(wN_0+wN_1);
			wN=mul(wN,M33_TBN_To_M);
			wN=mul(wN,(float3x3)m44_V);
			float3 OkN=normalize(pIn.norView*0.7+wN*0.3);
			pOut.G1.xyz=OkN;
		}
		break;
	case 1:
	     detailMix=lerp(texD_1,texD_2,floatPart);
		break;
	case 2:
		detailMix=lerp(texD_2,texD_3,floatPart);
		break;
	case 3:
	    detailMix=lerp(texD_3,texD_4,floatPart);
		break;
	case 4:
		detailMix=lerp(texD_4,texD_5,floatPart);
		break;
	case 5:
		detailMix=lerp(texD_5,texD_6,floatPart);
		break;
	case 6:
		detailMix=lerp(texD_6,texD_7,floatPart);
		break;
	case 7:
		detailMix=lerp(texD_7,texD_8,floatPart);
		break;
	case 8:
		detailMix=texD_8;
		break;
   }
   float3 diffTex=txDiff.Sample(SamTexture,pIn.texCoor).rgb;
   float rateOfDiffTex=saturate((pIn.posView.z-detailMinMax.x)/(detailMinMax.y-detailMinMax.x));
   pOut.G0.xyz=lerp(detailMix,diffTex,rateOfDiffTex);//
   pOut.G0.w=1.0;//alpha
   pOut.G2=pIn.posView;
   pOut.G2.w=intPart;//材质ID
   return pOut;
}
///////////////////////////////////////////////////


BlendState NoBlend
{
    BlendEnable[0] = FALSE;
};

DepthStencilState dsState
{
   DepthFunc = LESS_EQUAL;
   DepthEnable = TRUE;
   DepthWriteMask =1;
};

//RasterizerState rsSolid
//{
//FillMode = Solid;
//CullMode = 3;
//};

technique10 main
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Main() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Main() ) );
       SetDepthStencilState(dsState, 0);
	  // SetRasterizerState(rsSolid);
	   SetBlendState( NoBlend, float4( 0.0f, 0.0f, 0.0f, 0.0f ), 0xFFFFFFFF );
   } 
}
technique10 Lighting
{
  pass P0
  {
       SetVertexShader( CompileShader( vs_4_0, VS_Light() ) );
       SetGeometryShader( NULL );
       SetPixelShader( CompileShader( ps_4_0, PS_Light() ) );
   } 
}

PARTNERS