|

楼主 |
发表于 2012-4-1 13:00:00
|
显示全部楼层
Re: Re:请教一下,我的地形和水都用了shader,分别渲染没
Kevin_Qing: Re:请教一下,我的地形和水都用了shader,分别渲染没问题,放在一起就出现问题了……
如果你shader里面有默认值的话,应该是在set的时候初始化的
你可以尝试下修改顺序看看是否效果就对了。
把set放在最前面做了..还是不行..
这些是我shader的代码....
water本身是fx文件..我用纯hlsl的方法载入的...这样行么?
----------water.fx--------
matrix ViewProj;
matrix WorldViewProj;
texture ReflectMap;
texture WaveTexture;
float Time;
sampler rs = sampler_state
{
Texture = <ReflectMap>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = CLAMP;
AddressV = CLAMP;
};
sampler ws = sampler_state
{
Texture = <WaveTexture>;
MinFilter = ANISOTROPIC;
MaxAnisotropy = 8;
MagFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
struct VS_INPUT
{
float4 position : POSITION;
float2 texcoord : TEXCOORD0;
};
struct VS_OUTPUT
{
float4 position : POSITION;
float4 viewcoord : TEXCOORD0;
float2 wavecoord : TEXCOORD1;
};
VS_OUTPUT WaterVS(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT)0;
output.position = mul(input.position, WorldViewProj);
output.viewcoord = mul(input.position, ViewProj);
float translation = (fmod(Time, 20) - 10) * 0.1;
output.wavecoord.xy = input.texcoord.xy * 10 + float2(translation, 0);
return output;
}
struct PS_INPUT
{
float4 viewcoord : TEXCOORD0;
float2 wavecoord : TEXCOORD1;
};
struct PS_OUTPUT
{
float4 color : COLOR;
};
PS_OUTPUT WaterPS(PS_INPUT input)
{
PS_OUTPUT output = (PS_OUTPUT)0;
half3 bumptex = tex2D(ws, input.wavecoord.xy).xyz;
bumptex = normalize(bumptex - 0.5);
half3 reflbump = bumptex.xyz * half3(0.02, 0.02, 1.0);
float4 projcoord = input.viewcoord / input.viewcoord.w;
projcoord.x = 0.5 * projcoord.x + 0.5;
projcoord.y = -0.5 * projcoord.y + 0.5;
output.color = tex2D(rs, projcoord.xy + reflbump.xy);
output.color.a = 0.8;
return output;
}
technique WaterTech
{
pass P0
{
AlphaBlendEnable = TRUE;
DestBlend = INVSRCALPHA;
SrcBlend = SRCALPHA;
VertexShader = compile vs_2_0 WaterVS();
PixelShader = compile ps_2_0 WaterPS();
}
}
------------Terrain.shader------------------
uniform extern float4x4 gWVP;
uniform extern float2 gTexScale;
uniform extern float3 gDirToSunW; // Assumed to be unit length.
struct OutputVS
{
float4 posH : POSITION0;
float shade : TEXCOORD0;
float2 tiledTexC : TEXCOORD1;
float2 nonTiledTexC : TEXCOORD2;
};
OutputVS Terrain_MultiTexVS(float4 posL : POSITION0,
float3 normalL : NORMAL0, // Assumed to be unit length
float2 tiledTexC : TEXCOORD0,
float2 nonTiledTexC : TEXCOORD1)
{
OutputVS outVS = (OutputVS)0;
// Project position homogeneous clip space.
outVS.posH = mul(posL, gWVP);
// Do basic diffuse lighting calculating to compute vertex shade.
outVS.shade = max(0.0f, dot(normalL, gDirToSunW));
// Scale tiled tex-coords as specified by the application.
outVS.tiledTexC.x = tiledTexC.x * gTexScale.x;
outVS.tiledTexC.y = tiledTexC.y * gTexScale.y;
// Forward non-tiled tex-coords to pixel shader.
outVS.nonTiledTexC = nonTiledTexC;
return outVS;
}
sampler gLayerMap0;
sampler gLayerMap1;
sampler gLayerMap2;
sampler gBlendMap;
float4 Terrain_MultiTexPS(float shade : TEXCOORD0,
float2 tiledTexC : TEXCOORD1,
float2 nonTiledTexC : TEXCOORD2) : COLOR
{
// Layer maps are tiled
float3 c0 = tex2D(gLayerMap0, tiledTexC);
float3 c1 = tex2D(gLayerMap1, tiledTexC);
float3 c2 = tex2D(gLayerMap2, tiledTexC);
// Blendmap is not tiled.
float3 B = tex2D(gBlendMap, nonTiledTexC);
// Find the inverse of all the blend weights so that we can
// scale the total color to the range [0, 1].
float totalInverse = 1.0f / (B.r + B.g + B.b);
c0 *= B.r * totalInverse;
c1 *= B.g * totalInverse;
c2 *= B.b * totalInverse;
float3 final = (c0 + c1 + c2) * shade;
return float4(final, 1.0f);
} |
|