更改同一帧内的着色器变量

时间:2013-04-05 16:41:10

标签: directx shader hlsl

这可能是一个新手问题,因为我刚刚继承了我需要维护的大量3D代码。

从广义上讲,我的问题是,可以使用单个着色器技术在同一帧内渲染多个对象,同时为每个渲染对象使用不同的纹理。因此,简而言之,着色器变量将在一个startscene和endscene之间重置多次。

以下是我的着色器代码

texture g_MeshTexture;
texture g_MeshTextureTextLeft;
texture g_MeshTextureTextRight;

sampler MeshTextureSampler = sampler_state
{
    Texture = <g_MeshTexture>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler MeshTextureSamplerTextLeft = sampler_state
{
    Texture = <g_MeshTextureTextLeft>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

sampler MeshTextureSamplerTextRight = sampler_state
{
    Texture = <g_MeshTextureTextRight>;
    MipFilter = LINEAR;
    MinFilter = LINEAR;
    MagFilter = LINEAR;
};

VSTEXTURE_OUTPUT_2 VSTextureV1_1
    (
    float4 Position : POSITION, 
    float2 TexCoord : TEXCOORD0
   )
{

    VSTEXTURE_OUTPUT_2 Out = (VSTEXTURE_OUTPUT_2)0;

    float3 P = mul(Position, g_mWorldView);            //position (view space)

    Out.Position = mul(float4(P, 1), g_mProjection);    //projected position
    Out.TexCoord = TexCoord;                        //texture coordinates
    Out.TexCoord2 = TexCoord;
    Out.TexCoord3 = TexCoord;

    return Out;    
}

PS_OUTPUT GenericTexturePS( VSTEXTURE_OUTPUT_2 In ) 
{ 
    PS_OUTPUT Output;

    float4 main = tex2D(MeshTextureSampler, In.TexCoord);
    float4 left = tex2D(MeshTextureSamplerTextLeft, In.TexCoord2);
    float4 right = tex2D(MeshTextureSamplerTextRight, In.TexCoord3);
    float4 useColor; 

    if (left.a > right.a )
        useColor = left;
    else 
        useColor = right;

    if (useColor.a == 0)
        Output.RGBColor = main;
    else 
        Output.RGBColor = useColor;
    return Output;
}

technique GenericTexture
{
    pass P0
    {
        VertexShader = compile vs_1_1 VSTextureV1_1();
        PixelShader  = compile ps_1_1 GenericTexturePS();    
    }
}

现在在主渲染循环中,根据对象,我设置了三个输入纹理变量

PseudoCode 
Render()
{
BeginScene()
    SetEffect("g_MeshTexture", firstTexture); 
     RenderShader();
    SetEffect("g_MeshTexture", secondTexture); 
     RenderShader();
    ....
EndScene()
}

我原本以为这会根据我看到的例子而有效,但我的应用程序在第一次绘制框架后挂在BeginScene上。

我非常感谢指针,因为我一直在努力解决这个问题已有一段时间了。

非常感谢!

0 个答案:

没有答案