在DirectX 11中呈现精灵的最佳实践是什么?

11

我目前正在尝试熟悉DirectX API,我想知道在DirectX 11中呈现精灵的常用方法是什么(例如俄罗斯方块克隆)。

是否有类似于ID3DX10Sprite的界面?如果没有,哪种方法通常用于在DirectX 11中绘制精灵?

编辑:这是对我有用的HLSL代码(投影坐标的计算可以更好地完成):

struct SpriteData
{
    float2 position;
    float2 size;
    float4 color;
};

struct VSOut
{
    float4 position : SV_POSITION;
    float4 color : COLOR;
};

cbuffer ScreenSize : register(b0)
{
    float2 screenSize;
    float2 padding; // cbuffer must have at least 16 bytes
}

StructuredBuffer<SpriteData> spriteData : register(t0);

float2 GetVertexPosition(uint VID)
{
    [branch] switch(VID)
    {
        case 0:
            return float2(0, 0); 
        case 1:
            return float2(1, 0); 
        case 2:
            return float2(0, 1); 
        default:
            return float2(1, 1);
    }
}

float4 ComputePosition(float2 positionInScreenSpace, float2 size, float2 vertexPosition)
{
    float2 origin = float2(-1, 1);
    float2 vertexPositionInScreenSpace = positionInScreenSpace + (size * vertexPosition);

    return float4(origin.x + (vertexPositionInScreenSpace.x / (screenSize.x / 2)), origin.y - (vertexPositionInScreenSpace.y / (screenSize.y / 2)), 1, 1);
}

VSOut VShader(uint VID : SV_VertexID, uint SIID : SV_InstanceID)
{
    VSOut output;

    output.color = spriteData[SIID].color;
    output.position = ComputePosition(spriteData[SIID].position, spriteData[SIID].size, GetVertexPosition(VID));

    return output;
}

float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
    return color;
}
1个回答

6

没有等价物。通常的方法是绘制一个三角形带,形成一个四边形。

您可以利用实例化技术,只需更新包含精灵数据的缓冲区(像素中的x、y屏幕位置,要在纹理数组中获取的纹理id,缩放,旋转,层等),并使用着色器在一个单独的绘制调用中呈现所有精灵。

以下是我能想到的一些HLSL小贴士:

//--------------------------------------------------------------------------------------
// Shader code for Sprite Rendering
//--------------------------------------------------------------------------------------

struct Sprite {
    float2 position; // x, y world position
    float rotation;
    float scaling;

    float layer; // if you have multiple layers of sprites (e.g. background sprites)
    uint textureId;
};

StructuredBuffer<Sprite> SpritesRO : register( t0 );
Texture2DArray<float4> TextureSlices : register (t1);

cbuffer cbRenderConstants : register( b0 )
{
    matrix g_mViewProjection;
    // other constants
};

struct VSSpriteOut
{
    float3 position : SV_Position;
    uint textureId;
};

//-------------------------------------------------------------------------------------            
// Sprite Vertex Shader
//-------------------------------------------------------------------------------------

VSSpriteOut SpriteVS(uint VID : SV_VertexID, uint SIID : SV_InstanceID)
{
    VSSpriteOut Out = (VSSpriteOut)0;

    // VID is either 0, 1, 2 or 3
    // We can map 0 to position (0,0), 1 to (0,1), 2 to (1,0), 3 to (1,1)

    // We fetch the sprite instance data accord SIID
    Sprite sdata = SpritesRO[SIID];

    // function f computes screen space vertex position
    float3 pos = f (g_mViewProjection, VID, position, rotation, scaling, layer etc)

    Out.position = pos;
    Out.textureId = sdata.textureId;

    return Out;
}

//-------------------------------------------------------------------------------------
// Sprite Pixel Shader
//-------------------------------------------------------------------------------------

float4 SpritePS(VSSpriteOut In) : SV_Target
{
    // use In.textureId to fetch the right texture slice in texture array
    return color;
}

你能给我一个简短的代码示例吗?我在使用缓冲区方面遇到了困难。 - Mario Pistrich
查看DXSDK中的所有D3D11示例,并仔细了解它们如何创建缓冲区、绑定缓冲区、更新缓冲区等。 - elmattic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接