这几天我在网上到处找,没找到。不幸的是,有关此特定问题的苹果文档含糊不清,并且没有可用的示例代码(至少那是我发现的)。您可能会问什么问题...
我正在尝试将 uiview 的层设置为用于渲染 iPhone 模型屏幕的 Material 的内容(是的,trippy :P )。 iPhone 屏幕的 UV 映射设置为 0 到 1,因此将纹理/层映射到纹素上不会出现问题。
因此,我没有像左图那样在 iPhone 上渲染这个层,而是像右图那样在 iPhone 上渲染它
正确的呈现 不正确的呈现
另请注意,当我设置断点并调试实际的 iPhone 节点并在 Xcode 中查看它时,会显示完全不同的渲染,并且当我继续执行时该层会半固定:
那么……我该如何解决这个问题???我试过使用漫反射的内容变换矩阵,但没有任何问题得到解决。我还尝试将 UIView 的大小调整为 256x256(因为 UV 似乎是 256x256,如 blender - 3d 建模包中所示),但这并没有解决任何问题。
这是层的代码:
UIView *screen = [[UIView alloc] initWithFrame:self.view.bounds];
screen.backgroundColor = [UIColor redColor];
UIView *temp = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen.bounds.size.width, 60)];
temp.backgroundColor = [UIColor colorWithRed:0 green:(112.f/255.f) blue:(235.f/255.f) alpha:1];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectInset(temp.bounds, 40, 0)];
label.frame = CGRectOffset(label.frame, 40, 0);
label.textColor = [UIColor colorWithRed:0 green:(48.f/255.f) blue:(84.f/255.f) alpha:1];
label.text = @"Select Track";
label.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:30];
label.minimumScaleFactor = 0.001;
label.adjustsFontSizeToFitWidth = YES;
label.lineBreakMode = NSLineBreakByClipping;
[temp addSubview:label];
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, temp.bounds.size.height - 2, temp.bounds.size.width, 2)];
separator.backgroundColor = [UIColor colorWithRed:0 green:(48.f/255.f) blue:(84.f/255.f) alpha:1];
[temp addSubview:separator];
[screen addSubview:temp];
screen.layer.contentsGravity = kCAGravityCenter;
编辑
更奇怪的是,如果我使用以下方法捕获 View 的 UIImage:
- (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
并将其用作漫反射的内容...一切都完美无缺?!这真的很奇怪和令人沮丧,因为图像的大小与 uiview 的大小完全相同......
编辑2
我最终只使用 View 的图像作为纹理,这使得事情比我需要的静态得多。我不会将此设置为答案,因为我仍然会等待这个问题的正确修复,即使它需要很长时间。所以,如果你有答案,而且这个话题已经开了很久,可以的话请顶一下。这部分的文档太差了。
最佳答案
旧线程上的新帖子,但在这个时代,可以将 UIView 本身设置为 SCNMaterialProperty (diffuse) contents
。 Apple 的 SceneKit 工程师直接传达了支持此功能的意图,但文档尚未更新以反射(reflect)它。
回到原来的帖子,不要将 UIView.layer 设置为 Material 属性内容;而是将内容设置为 UIView 本身。
[更新:根据下面 Lance 的评论,对观点的支持可能会越来越差,而不是越来越好。]
关于ios - 将 CALayer 设置为 SCNMaterial 的漫反射内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36190789/