我目前正在尝试习惯 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;
}