【问题标题】:Image texture in SCNMaterial is always grey. How to apply color?SCNMaterial 中的图像纹理始终为灰色。如何应用颜色?
【发布时间】:2019-09-08 21:25:02
【问题描述】:

我正在按照 Apple 文档中的以下示例编写图像识别应用程序:https://developer.apple.com/documentation/arkit/detecting_images_in_an_ar_experience

到目前为止一切正常,现在我想将透明平面覆盖更改为图像。我是这样设置飞机的材质的。

let image = "overlay"
let material = SCNMaterial()
material.locksAmbientWithDiffuse = true;
material.isDoubleSided = false
material.diffuse.contents = image
material.ambient.contents = UIColor.white

let planeNode = SCNNode(geometry: plane)
planeNode.geometry?.materials = [material]

图像被加载和渲染,只是它总是缺少颜色。原始图像为红色,叠加层为黑白。 我已经尝试了许多不同的环境和漫反射设置,但都没有成功。我在这里想念什么?

谢谢

【问题讨论】:

    标签: ios swift scenekit augmented-reality arkit


    【解决方案1】:

    在您的代码中,以下行是 String 类型,而不是 UIImage 类型:

    let image: String = "overlay"
    

    试试下面的代码:

    let plane = SCNPlane(width: 10, height: 10)
    
    let image = UIImage(named: "art.scnassets/texture")
    // let image = UIColor.red
    // let image = UIColor(hue: 0.25, saturation: 0.5, brightness: 0.75, alpha: 1)
    
    let material = SCNMaterial()
    material.locksAmbientWithDiffuse = true
    material.isDoubleSided = false
    material.diffuse.contents = image
    material.ambient.contents = UIColor.white
    
    let planeNode = SCNNode(geometry: plane)
    planeNode.geometry?.materials = [material]
    
    scene.rootNode.addChildNode(planeNode)
    

    另外,将diffuse 材质槽的图像保存为PNG 格式。

    【讨论】:

    • 感谢您的回答。不幸的是,这并没有什么不同。根据此处的 Apple 文档 (developer.apple.com/documentation/scenekit/scnmaterialproperty/…),content 属性还采用带有图像位置的 NSStrings,与 UIImage、UIColor 等相同。但是,我观察到一个奇怪的行为。当我将图像位置硬编码到代码中时,它工作正常。如果我从即 referenceImages 名称属性构建位置,我会遇到同样的老问题。
    【解决方案2】:

    为了将来参考,这是我发现的: 图像不应具有相同的名称,即使保存在不同的组或文件夹中。我使用图像的名称作为参考来加载叠加层。当我更改名称并手动(通过开关/大小写)获取参考时,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-06
      • 2017-02-18
      相关资源
      最近更新 更多