绘画纹理图层 - 像素着色器

时间:2013-03-04 14:36:29

标签: directx textures game-engine hlsl pixel-shader

我正在为我的地形创建一个小编辑器,所以我可以“绘制”地形,但是从现在开始,我使用8个纹理和2个alphamaps得到大约20 FPS的东西。

像素着色器的HLSL代码:

////////////////////////////////////////////////////////////////////////////////
// Filename: terrain.ps
////////////////////////////////////////////////////////////////////////////////


/////////////
// GLOBALS //
/////////////

Texture2D textures[8];
Texture2D alphaTextures[2];


//////////////
// SAMPLERS //
//////////////
SamplerState SampleType;


//////////////
// TYPEDEFS //
//////////////
struct PixelInputType
{
    float4 position : SV_POSITION;
    float2 tex : TEXCOORD0;
    float2 alpha : TEXCOORD1;
    float3 normal : NORMAL;
};


////////////////////////////////////////////////////////////////////////////////
// Pixel Shader
////////////////////////////////////////////////////////////////////////////////
float4 TerrainPixelShader(PixelInputType input) : SV_TARGET
{
    int i;
    float4 color;
    float4 baseColor;
    float4 textureColor[8];
    float4 alphaMap[2];

    // Sample all textures
    for(i=0; i<8; i++)
    {
        textureColor[i] = textures[i].Sample(SampleType, input.tex, 0);
    }

    // Sample the alphamaps
    for(i=0; i<2; i++)
    {
        alphaMap[i] = alphaTextures[i].Sample(SampleType, input.alpha, 0);
    }

    // Set the base color
    color.r = 0;
    color.g = 0;
    color.b = 0;
    color.a = 255;

    // Add the second layer using the red channel of the alpha map.
    color = lerp(color, textureColor[0], alphaMap[0].r);

    // Add the third layer using the green channel of the alpha map.
    color = lerp(color, textureColor[1], alphaMap[0].g);

    // Add the forth layer using the blue channel of the alpha map.
    color = lerp(color, textureColor[2], alphaMap[0].b);

    // Add the fifth layer using the alpha channel of the alpha map.
    color = lerp(color, textureColor[3], alphaMap[0].a);

    // Add the sixth layer using the red channel of the alpha map.
    color = lerp(color, textureColor[4], alphaMap[1].r);

    // Add the seventh layer using the green channel of the alpha map.
    color = lerp(color, textureColor[5], alphaMap[1].g);

    // Add the eigth layer using the blue channel of the alpha map.
    color = lerp(color, textureColor[6], alphaMap[1].b);

    // Add the eigth layer using the alpha channel of the alpha map.
    color = lerp(color, textureColor[7], alphaMap[1].a);

    // Return the final color
    return color;
}

是的我知道我可以检查是否真的需要使用8个纹理(并跳过代码的某些部分)但我想要想象一下我需要使用所有纹理的情况。

当我绘制更多图层时,FPS开始下降,一切都开始变黑,当我绘制第一层时(只需要一个小像素)我的FPS从55变为45,第二层使我的fps从45变为到37等等......

有更好的方法来实现这个或这是唯一的方法吗? 我对directX有点新意,所以我相信我的代码(或我的理论)有问题 谢谢!


请注意,我的第一个纹理坐标重复地形大约4倍,而alpha只填充一次,这也会减慢速度,因为如果我使用纹理的alpha坐标,我的FPS也会增长+ -20。 (两者的坐标从0.0到1.0)。

0 个答案:

没有答案