关于一树木 的纹理透明遮挡问题

无路不荆棘 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文件?谢谢
...全文
182 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingzhe2001 2009-08-20
  • 打赏
  • 举报
回复
比如 pDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true);就对应 AlphaTestEnable = TRUE;

用alphatest渲染比alphablend快。
xingzhe2001 2009-08-20
  • 打赏
  • 举报
回复
不用fx也可以,你只要设置渲染状态对了就可以。
你的x文件里的那个fx那样渲染状态就是对的。

ZEnable = TRUE;
ZWriteEnable = TRUE;
CullMode = NONE;
AlphaBlendEnable = FALSE;
AlphaTestEnable = TRUE;
AlphaRef = 156;
AlphaFunc = GREATER;
你自己用 pDevice->SetRenderState()也可以设置这些状态,SDK里很详细
无路不荆棘 2009-08-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhangci226 的回复:]
刚才不是给你说了吗
呵呵

从远到近的顺序
[/Quote]
可是一棵树自身的树叶也有遮挡啊,这个怎么实现呢?看了一楼推荐的文章知道使用fx来解决 但fx以前根本没接触过。。。
张赐 2009-08-20
  • 打赏
  • 举报
回复
刚才不是给你说了吗
呵呵

从远到近的顺序
xingzhe2001 2009-08-20
  • 打赏
  • 举报
回复
xingzhe2001 2009-08-20
  • 打赏
  • 举报
回复
如果你有部分区域透明的纹理(如树叶), 并且图案边缘包含了一些半透明的像素用于反走样, 那你可以使用双pass渲染技术:

Pass 1: 绘制不透明部分: alpha混合关闭, alpha测试只接受100%不透明的区域, 深度缓冲开启
Pass 2: 绘制边缘: alpha混合开启, alpha测试设置只接受alpha <1的, 深度缓冲开启, 深度写入关闭
以每个物体渲染两次的代价, 为纹理中间完全不透明的部分提供了100%正确的深度缓冲排序, 和相对精确的半透明边缘排序. 这个方法为纹理裁剪的边缘进行了一些反走样, 同时也保证了不用对每一棵树或者草叶进行额外的排序

http://data.gameres.com/document.asp?TopicID=123630这篇文章有更详细地说明

8,305

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧