Directx 使用D2D渲染文本

Directx 使用D2D渲染文本,directx,Directx,我已经创建了一个UI文本组件,它将在我的游戏引擎(UWP)的屏幕上绘制文本。但是,由于分辨率比例的原因,我在定位方面遇到了问题。在我的笔记本电脑屏幕(分辨率刻度2)上看起来不错,但在我的外部显示器(分辨率刻度1)上,文本略有偏移 这是UI文本组件呈现代码: void TextComponent::Render(shared_ptr<AmberDevice> device, ShaderConfiguration& configuration) { auto context

我已经创建了一个UI文本组件,它将在我的游戏引擎(UWP)的屏幕上绘制文本。但是,由于分辨率比例的原因,我在定位方面遇到了问题。在我的笔记本电脑屏幕(分辨率刻度2)上看起来不错,但在我的外部显示器(分辨率刻度1)上,文本略有偏移

这是UI文本组件呈现代码:

void TextComponent::Render(shared_ptr<AmberDevice> device, ShaderConfiguration& configuration)
{
  auto context = m_device->GetProperties().GetD2DDeviceContext();
  context->SaveDrawingState(m_stateBlock.Get());
  context->BeginDraw();

  device->ApplyTextTransform(GetTransform());

  context->DrawTextLayout(
    Point2(0.0f, 0.0f),
    m_layout.Get(),
    m_brush.Get());

  context->EndDraw();
  context->RestoreDrawingState(m_stateBlock.Get());
}

我将屏幕大小和组件位置除以分辨率比例,以使其正常化,并使其与系统的逻辑分辨率相同(即非比例)。我将逻辑屏幕宽度和高度的一半添加到最终的平移中,这样我可以在屏幕中心获得0,0,使其与在D3D正交网格中绘制精灵的方式相似。

不应进行缩放。文本的呈现方式应该与物理分辨率相匹配。问题在于,这样做与精灵的正交呈现方式不同,因此无法将文本组件作为其他UI组件的子组件放置(例如,创建带有文本的按钮)。
void AmberDevice::ApplyTextTransform(Amber::Scene::Transform2D transform)
{
  auto size = transform.GetSize();
  auto scaling = transform.GetScale();
  auto rotation = transform.GetRotation();
  auto translation = transform.GetPosition();
  auto pivot = transform.GetPivot();
  auto screenSize = m_properties.GetRenderOutputSize();
  auto scaleFactor = m_properties.GetResolutionScale();

  Matrix3x2F translate = Matrix3x2F::Translation(
    (translation.x / scaleFactor) + (screenSize.x / (scaleFactor * 2.f)),
   -(translation.y / scaleFactor) + (screenSize.y / (scaleFactor * 2.f)));

  Matrix3x2F scale = Matrix3x2F::Scale(Size(scaling.x, scaling.y), center);
  Matrix3x2F rotate = Matrix3x2F::Rotation(rotation, center);

  m_properties.GetD2DDeviceContext()->SetTransform(
    scale * rotate * translate * m_properties.GetOrientationTransform2D());
}