关于一树木 的纹理透明遮挡问题
无路不荆棘 2009-08-20 01:53:51 关于一棵树 里面的树叶使用透明纹理,结果不但几棵树之间有相互遮挡问题,树自身的树叶也有遮挡问题。树与树之间可以动态调整渲染顺序,但树自身的三角形顺序也要调整吗,效率会很低的吧。。。看了一下sdk的StateManager例子,发现每一颗树的.x文件内都包含一个.fx文件,.x部分代码如下:
...
Material {
1.000000;1.000000;1.000000;1.000000;;
0.000000;
1.000000;1.000000;1.000000;;
0.000000;0.000000;0.000000;;
TextureFilename {
"pine04.dds";
}
EffectInstance {
"AlphaTest.fx";
EffectParamString{ "Texture0@Name"; "pine04.dds"; }
EffectParamFloats{ "Diffuse"; 4; 1.0, 1.0, 1.0, 1.0; }
EffectParamFloats{ "Ambient"; 4; 1.0, 1.0, 1.0, 1.0; }
EffectParamFloats{ "Power"; 1; 0.0; }
EffectParamFloats{ "Specular"; 4; 1.0, 1.0, 1.0, 1.0; }
EffectParamFloats{ "Emissive"; 4; 0.0, 0.0, 0.0, 0.0; }
...
AlphaTest.fx代码如下:
//--------------------------------------------------------------------------------------
//
// Alpha-Tested Pine Lighting Model
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// Effect Edit defaults
//--------------------------------------------------------------------------------------
string XFile = "pine04.x"; // model
int BCLR = 0xff202080; // background
string BIMG = "lake.bmp"; // Background image
//--------------------------------------------------------------------------------------
// Scene Setup
//--------------------------------------------------------------------------------------
// light direction (world space)
float3 lightDir = {0.577, -0.577, 0.577};
// light intensity
float4 I_a = { 0.5f, 0.5f, 0.5f, 1.0f }; // ambient
float4 I_d = { 0.5f, 0.5f, 0.5f, 1.0f }; // diffuse
float4 I_s = { 1.0f, 1.0f, 1.0f, 1.0f }; // specular
// Transformation Matrices
matrix matWorld : WORLD;
matrix matViewProj : VIEWPROJECTION;
//--------------------------------------------------------------------------------------
// Material Properties
//--------------------------------------------------------------------------------------
// Set by EffectInstance when mesh is loaded
// (Default values provided for Effect Edit)
float4 Diffuse = float4( 1.f, 1.f, 1.f, 1.f );
float4 Ambient = float4( 1.f, 1.f, 1.f, 1.f );
// Texture Parameter, annotation specifies default texture for EffectEdit
texture Texture0 < string name = "pine04.dds"; >;
// Sampler, for sampling the pine texture
sampler s0 = sampler_state
{
texture = <Texture0>;
minfilter = LINEAR;
mipfilter = LINEAR;
magfilter = LINEAR;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
void VS( in float3 pos : POSITION,
in float3 norm : NORMAL,
in float2 texcrds : TEXCOORD,
out float4 opos : POSITION,
out float4 ocolor : COLOR0,
out float2 otexcrds : TEXCOORD )
{
// Transform the vertex to clip space
opos = mul( float4(pos, 1.f), mul( matWorld, matViewProj ) );
// Calculate the normal (in world-space)
// Assumes inverse( transpose( matWorld ) ) == matWorld
float3 norm_w = normalize( mul( norm, (float3x3)matWorld ) );
// Calculate the diffuse lighting coefficient
// The Pine Primitives are 2-Sided, and may be illuminated from either direction
float dot = abs( dot( -lightDir, norm_w ) );
// Simple Diffuse/Ambient Lighting
ocolor = saturate( dot * Diffuse * I_d + Ambient * I_a );
// Pass the texture coordinates to the pixel shader
otexcrds = texcrds;
}
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
void PS( in float4 color : COLOR,
in float2 texcord : TEXCOORD,
out float4 ocolor : COLOR )
{
// Sample the Texture
float4 tex_color = tex2D( s0, texcord );
// Modulate the texture color by the vertex lighting
ocolor = tex_color * color;
}
//--------------------------------------------------------------------------------------
// Default Technique
// Establishes Vertex and Pixel Shader
// Ensures base states are set to required values
// (Other techniques within the scene perturb these states)
//--------------------------------------------------------------------------------------
technique tec0
{
pass p0
{
VertexShader = compile vs_1_1 VS();
PixelShader = compile ps_1_4 PS();
ZEnable = TRUE;
ZWriteEnable = TRUE;
CullMode = NONE;
AlphaBlendEnable = FALSE;
AlphaTestEnable = TRUE;
AlphaRef = 156;
AlphaFunc = GREATER;
}
}
个位高手给讲解一下吧~~ 还有就是怎么把.fx信息添加到.x文件内,是在使用3ds建模导出时使用插件吗,我使用的panda插件,里面有个include fx选项,但勾上没什么用,搜索.x文件没发现fx字样,怎么把fx并入.x文件?谢谢