XNA Shader编程教程3.3-多纹理
3D图形的一个表面可以贴上多个图片,一般称为多层纹理或多重纹理。例如一个正方形有两个纹理,纹理1是墙壁图案:
纹理2是光影图案:
两个纹理混合后的效果如图所示:
主要的shader代码如下:
float4x4 WorldViewProj : WORLDVIEWPROJECTION; Texture Texture1; Texture Texture2; sampler TextureSampler1 = sampler_state { texture = <Texture1>; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; }; sampler TextureSampler2 = sampler_state { texture = <Texture2>; MipFilter = LINEAR; MinFilter = LINEAR; MagFilter = LINEAR; }; void TransformV1_1(in float4 inputPosition:POSITION,in float2 inputTexCoord:TEXCOORD0, out float4 outputPosition:POSITION,out float2 outputTexCoord:TEXCOORD0, out float2 outputSecondTexCoord:TEXCOORD1) { outputPosition = mul(inputPosition,WorldViewProj); outputTexCoord = inputTexCoord; outputSecondTexCoord = inputTexCoord; } void TextureColorV1_1(in float4 P:POSITION,in float2 textureCoords : TEXCOORD0, in float2 textureCoords2 : TEXCOORD1,out float4 diffuseColor : COLOR0) { float4 diffuseColor1 = tex2D(TextureSampler1, textureCoords); float4 diffuseColor2 = tex2D(TextureSampler2, textureCoords2); diffuseColor=lerp(diffuseColor1,diffuseColor2,0.6f); } technique RenderScene { pass P0 { VertexShader = compile vs_1_1 TransformV1_1(); PixelShader = compile ps_1_1 TextureColorV1_1(); } }
基本思路就是先用对应的采样器获取两纹理的颜色,接着使用lerp函数在两个颜色之间(前两个参数)进行线性插值,而第3个参数决定前2个参数在插值时的比例,你可以在源代码中试着调整这个参数看看效果。
发布时间:2009/4/10 下午2:19:28 阅读次数:10443