ios - 无法对 SCNPlane 阵列使用多个图像

标签 ios swift calayer scenekit

我有一组 SCNPlane 对象,我想向每个对象添加不同的图像。我的问题是,每当我在 for 循环中初始化 SCNPlanes 时,每个平面最终都会得到完全相同的图像。我的循环基本上如下所示:

var layer = [CALayer](count: 8, repeatedValue: CALayer())
var tmpPhoto = [CGImage]()

for var i = 0; i < 8; ++i{
    tmpPhoto.append("image data")
    layer[i].contents = tmpPhoto[i]
    layer[i].frame = CGRectMake(0, 0, "image width", "image height")

    //initialize the SCNPlane and add it to an array of SCNNodes

    plane[i].geometry?.firstMaterial?.locksAmbientWithDiffuse = true
    plane[i].geometry?.firstMaterial?.diffuse.contents = layer[i]

    //add SCNPlane constraints
}

我注意到显示的图像将是最后添加/更改的图像。我知道这一点是因为在这个循环之后,我尝试仅修改平面数组中的第一个条目。在运行时,第一个数组条目的图像将显示在所有其他 SCNPlanes 上!请记住,我根本没有使用 displayLayer() 或 setNeedsDisplay()。

这是我尝试过的:

  • 使用类似的层变量而不是数组,并在每个循环开始时对其进行修改
  • 在循环外操作层变量
  • 直接添加 UIImage,而不先转换为 CGImage(我知道每个图像都被加载到数组中)
  • 尝试直接修改 SCNPlane 的图层
  • 使用 SCNMaterial 变量(遵循 this ,但为每个 SCNPlane 的 Material 变量添加一层)
  • 使用 addSublayer()(不起作用)或layoutSublayersOfLayer()(由于未捕获的异常而导致应用崩溃)将图层添加到现有 View 结构

我会错过一些重要的事情吗?

编辑:忘记一行。

最佳答案

为每个SCNNode创建不同的SCNPlane。如果您为多个节点创建一个 SCNPlane,那么它们将共享几何图形。在几何体上设置 Material 将更改使用该几何体的所有节点。

关于ios - 无法对 SCNPlane 阵列使用多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34595179/

相关文章:

macos - 如何为 NSView 显示阴影?

iphone - 我可以从带有 alpha channel 的图像中获取 CGPath 吗?

iOS 根据字符数动态标签宽度

iphone - NSURLConnection启动+自动释放

ios - 如何删除Xcode项目文件?

ios - Swift 搜索栏( Controller )内存泄漏

ios - 模拟一个 "picture taken"的屏幕闪

cocoa-touch - 如何在 Xcode 4 中将数据模型转换为版本化数据模型?

iphone - 使用 NSUserDefaults 的正确方法?

ios - 如何在wkwebview视频中禁用自动启动AVPlayer