2

我需要在我的 Metal 应用程序中呈现一些文本。与其手动进行,我更愿意使用 Apple 的文本渲染 API。

在一个简单的空窗口中调用此代码将drawRect打印字符串:

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica" size:26], NSFontAttributeName,[NSColor blackColor], NSForegroundColorAttributeName, nil];

NSAttributedString * s = [[NSAttributedString alloc] initWithString:@"Hello!"
attributes: attributes];


[s drawAtPoint:NSMakePoint(300, 300)];

但这在具有金属视图的窗口中不起作用。我想一切都会被冲洗掉?

是否可以?在 Metal 视图上显示按钮 (NSButton) 也是如此。

谢谢。

4

1 回答 1

3

这是可能的,但并不简单。您将需要使用drawViewHierarchyInRectwith UILabelor UIButton,并传递从 a 创建的上下文CVPixelBufferRef。从那里,您需要使用金属 api 将其链接CVPixelBufferRef到金属纹理,然后您可以正常绘制。我不建议每帧都这样做。

如果您需要一些用金属渲染的精美 UI,可能有更好的选择,或者您可以在金属视图顶部的 UIView 内渲染标签、按钮等。

您的文本需要在 3D 世界中还是在 2D 中叠加在顶部?

于 2020-12-24T23:03:41.600 回答