gpt4 book ai didi

ios - NSAttributedString 不适用于 SCNText

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:59:53 26 4
gpt4 key购买 nike

我想在 SceneKit 场景中显示一些文本,其中字符具有不同的颜色。 documentation of SCNText状态:

When you create a text geometry from an attributed string, SceneKit styles the text according to the attributes in the string, and the properties of the SCNText object determine the default style for portions of the string that have no style attributes.

我试过了,但没用。 SCNText 似乎忽略了我的字符串的属性。我检查了我的 NSAttributedString 的内容是否正确并将其添加到 UILabel,它按预期显示。

为什么 3D 文本没有颜色?

这是一个小型测试应用程序(来自单 View 应用程序模板)和屏幕截图:

Screenshot of test application

#import "ViewController.h"

#import <SceneKit/SceneKit.h>

@interface ViewController ()

@end

@implementation ViewController

- (void) viewDidLoad
{
[super viewDidLoad];

NSAttributedString* str = [ViewController makeSomeText];

[self addSceneViewWithText: str];
[self addLabelWithText: str];
}

+ (NSMutableAttributedString*) makeSomeText
{
NSDictionary* redAttr = @{NSForegroundColorAttributeName : [UIColor redColor]};
NSDictionary* greenAttr = @{NSForegroundColorAttributeName : [UIColor greenColor]};
NSDictionary* blueAttr = @{NSForegroundColorAttributeName : [UIColor blueColor]};

NSAttributedString* redStr = [[NSAttributedString alloc] initWithString: @"red" attributes: redAttr];
NSAttributedString* greenStr = [[NSAttributedString alloc] initWithString: @"green" attributes: greenAttr];
NSAttributedString* blueStr = [[NSAttributedString alloc] initWithString: @"blue" attributes: blueAttr];

NSMutableAttributedString* str = [[NSMutableAttributedString alloc] init];
[str appendAttributedString: redStr];
[str appendAttributedString: greenStr];
[str appendAttributedString: blueStr];

return str;
}

- (void) addSceneViewWithText: (NSAttributedString*) string
{
SCNView* view = [[SCNView alloc] initWithFrame: self.view.bounds];
view.scene = [SCNScene scene];
view.backgroundColor = [UIColor grayColor];
view.autoenablesDefaultLighting = true;
view.allowsCameraControl = true;
SCNNode* root = view.scene.rootNode;
[self.view addSubview: view];

SCNNode* cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
cameraNode.camera.automaticallyAdjustsZRange = true;
[root addChildNode: cameraNode];

SCNText* text = [SCNText textWithString: string extrusionDepth: 3];
// text.firstMaterial.diffuse.contents = [UIColor yellowColor];
SCNNode* textNode = [SCNNode nodeWithGeometry: text];
textNode.rotation = SCNVector4Make (1, 0, 0, 0.5f);
[root addChildNode: textNode];

SCNVector3 text_center;
float dummy;
[text getBoundingSphereCenter: &text_center radius: &dummy];
textNode.position = SCNVector3Make (-text_center.x, -text_center.y, -150);
}

- (void) addLabelWithText: (NSAttributedString*) string
{
CGRect bounds = self.view.bounds;
UILabel* label = [[UILabel alloc] initWithFrame: CGRectMake (0, 50, bounds.size.width, 50)];
label.attributedText = string;
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview: label];
}

- (void) didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

@end

最佳答案

直接回答您的问题:“为什么 3D 文本没有着色?”

颜色可能不被视为“样式”的一部分,因为他们在文档中使用了这个词。这也可能是使用错误的词,因为它是对文本质量的描述,在它曾经使用过的每个地方都会弯曲和扭曲。

他们的意思很可能是粗细、对齐、下划线、删除线、字距调整和行距之类的东西……这种性质的东西由 SceneKit 解释。但只有反复试验才能确定什么行得通,什么行不通。

Apple 应该提供一个表格,其中列出了 SceneKit 识别和使用的文本的属性/质量。

我能在 Core Text 和 NSAttributedString 文档中找到的唯一使用“样式”的地方表明,这些框架考虑用这个词来对每个段落的这些品质进行分组,这在考虑 SceneKit 文本时也几乎没有帮助。


编辑:关于 3D 和 Material 的其他内容,以防万一这对您来说是新闻:

3D 中的“ Material ”会在 3D 对象上创建颜色印象、对光的响应、反射率和 Material 的其他品质,并且需要创建和应用。这远不像在 2D 思维中说“让 objectABC 变红”那么简单。

单个文本对象(在 3D 中,不是您在 Cocoa 中对这些东西的看法)可能无法在 SceneKit 中使用不同的 Material 和/或 Material ID 自动制作。 Material ID 是在大多数人创建几何体和 Material 的 3ds Max 中如何将不同 Material 应用于单个对象的不同部分。

不幸的是,SceneKit 不遵循此约定,而是使用“元素”作为几何对象的一部分,每个元素都是索引的一部分,该索引对应于您提供该几何的任何 Material 。

您可以在这里阅读更多相关信息:

https://developer.apple.com/library/ios/documentation/SceneKit/Reference/SCNGeometry_Class/index.html#//apple_ref/doc/uid/TP40012000-CLSCHSCNGeometry-SW15

因此,要获得您正在寻找的内容,您可能需要制作 3 个文本对象并为每个对象提供您想要的颜色的独特 Material ,或者将 3 个单词对象分解为 3 个元素,然后有趣地猜测如何将正确的 Material 放到每个元素上。

编辑:忽略上一段的最后一部分。您需要制作 3 个文本对象以获得您想要的外观。 Scene Kit 中素材分布的元素特性保留在文本对象上,因此您不能将单词级别的文本分解为不同的元素。

来自文档:

A text geometry may contain one, three, or five geometry elements:

  • If its extrusionDepth property is 0.0, the text geometry has one element corresponding to its one visible side.

  • If its extrusion depth is greater than zero and its chamferRadius property is 0.0, the text geometry has three elements, corresponding to its front, back, and extruded sides.

  • If both extrusion depth and chamfer radius are greater than zero, the text geometry has five elements, corresponding to its front, back, extruded sides, front chamfer, and back chamfer.

关于ios - NSAttributedString 不适用于 SCNText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32610914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com