将CALayer设置为SCNMaterial的漫反射内容

时间:2016-03-23 23:41:27

标签: ios objective-c calayer scenekit material

过去几天我一直在互联网上搜索无济于事。不幸的是,关于这个特定问题的苹果文档是模糊的,没有可用的示例代码(至少这是我发现的)。您可能会问的问题似乎是什么......

我正在尝试将uiview的图层设置为用于渲染iPhone模型屏幕的材质(Yep,trippy:P)。 iPhone的屏幕UV映射设置为0到1,因此在将纹理/图层映射到纹素上时不会出现问题。

所以,不要让这个图层在iPhone上显示,与左图像相同,相反,我将其渲染到iPhone上就像右图一样

更正渲染不正确的渲染

enter image description here

另请注意,当我设置断点并调试实际的iPhone节点并在Xcode中查看时,会显示一个完全不同的渲染,并且当我继续执行时,图层会被修复为半:

Weird bug

现在......我如何解决这个问题?我尝试过使用漫反射内容变换矩阵,但没有任何修复。我也尝试将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;

修改
甚至更奇怪的是,如果我使用以下方法捕获视图的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

我最终只使用视图的图像作为纹理,这使得事物比我需要的更加静态。我不会将此作为答案,因为即使是在很长一段时间内,我仍然会等待正确解决这个问题。所以,如果你有一个答案并且这个主题已经打开了很长时间,请尽可能地碰到它。这部分的文档很差。

2 个答案:

答案 0 :(得分:6)

旧帖子上的新帖子,但是这个日期,可以将UIView本身设置为SCNMaterialProperty(diffuse)contents。直接从Apple的SceneKit工程部门传达支持此功能的意图,尽管文档尚未更新以反映它。

要绑定回原始帖子,请不要将UIView.layer设置为素材属性内容;而是将内容设置为UIView本身。

答案 1 :(得分:4)

SceneKit docs非常强烈地建议,虽然在某些情况下您可以使用动画CALayers作为素材内容,但不包括UIView图层:

  

SceneKit无法使用已在其他位置显示的图层(例如,UIView对象的背衬层。)

这表明如果你想为你的素材制作动画内容,你最好使用完全独立使用的Core Animation或者SpriteKit。