在DirectX 11中渲染精灵的最佳做法是什么?

问题描述 投票:10回答:1

我目前正在尝试习惯DirectX API,我想知道在DirectX 11中渲染精灵的常用方法是什么(例如,对于俄罗斯方块克隆)。

是否有与qazxsw poi类似的界面,如果没有,这是在DirectX 11中绘制精灵的常用方法?

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

ID3DX10Sprite
directx 2d directx-11
1个回答
6
投票

不,没有相应的。通常的方法是绘制形成四边形的三角形条带。

您可以使用实例化,因此您只需要使用精灵数据(x,y屏幕位置,以像素为单位,纹理的id在纹理数组中获取,缩放,旋转,图层等)更新缓冲区,并使用着色器在一次绘制调用中渲染所有精灵。

这是我头顶的一些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;
}
© www.soinside.com 2019 - 2024. All rights reserved.