【问题标题】:Programmatically setting texture in Scene generated by Reality Composer以编程方式在 Reality Composer 生成的场景中设置纹理
【发布时间】:2019-11-27 21:23:44
【问题描述】:

我正在使用 Reality Composer 创建一个圆柱体对象。 我的要求是用自定义图像包裹圆柱体。 图片由应用动态创建。

我尝试了以下方法,但到目前为止它不起作用。

    1. 从体验加载锚点后。
    1. 从锚点获取模型实体。
    1. 从模型实体中获取模型组件。
    1. 添加或编辑材料。

代码:

// Load the "anchor" scene from the "Experience" Reality File
let anchor = try! Experience.loadAnchor()

// Add the anchor to the scene
arView.scene.anchors.append(anchor)

let cylinderEntity : Entity = anchor.cylinder!

let cylinderModelEntity = cylinderEntity.children[0]

var cylinderModelComponent : ModelComponent = cylinderModelEntity.components[ModelComponent.self]!

let paths : NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) as NSArray
let path : NSString = paths.object(at: 0) as! NSString
let filePath : NSString = path.strings(byAppendingPaths: ["Image.png"])[0] as NSString
let url = URL.init(fileURLWithPath: filePath as String)

// Save image.
let image : UIImage = createImage()
try! image.pngData()?.write(to: url)
let data = NSData.init(contentsOf: url)
print(data!)

var material = SimpleMaterial()
material.tintColor = UIColor.yellow
material.baseColor = try! MaterialColorParameter.texture(TextureResource.load(contentsOf: url))
material.roughness = MaterialScalarParameter(floatLiteral: 0.1)
material.metallic = MaterialScalarParameter(floatLiteral: 1.0)

cylinderModelComponent.materials[0] = material

非常感谢任何帮助。

【问题讨论】:

    标签: swift augmented-reality arkit realitykit reality-composer


    【解决方案1】:

    您忘记设置需要存储在实体上的boxComponent。

    为此使用set()实例方法。

    在您的代码中应该如下所示:

    anchor.cylinder!.components.set(cylinderModelComponent)
    

    在我的代码中是这样的:

    anchor.steelBox!.components.set(boxComponent)
    

    这是我的完整代码版本(我在 macOS 应用程序中测试过):

    import AppKit
    import RealityKit
    
    class GameViewController: NSViewController {
    
        @IBOutlet var arView: ARView!
    
        override func awakeFromNib() {
    
            let anchor = try! Experience.loadBox()
            anchor.steelBox?.scale = SIMD3(x: 9, y: 9, z: 9)
            anchor.steelBox?.orientation = simd_quatf(angle: -Float.pi/4,
                                                       axis: SIMD3(x: 1, y: 1, z: 0))      
    
            let boxEntity: Entity = anchor.steelBox!.children[0]
            var boxComponent: ModelComponent = boxEntity.components[ModelComponent].self!
    
            let paths: NSArray = NSSearchPathForDirectoriesInDomains(.documentDirectory, 
                                                                     .userDomainMask, 
                                                                      true) as NSArray
    
            // If you're testing it in macOS app – place your Image.png here:
            // /Users/<UserName>/Library/Containers/<ApplicationName>/Data/Documents/
    
            let path: NSString = paths.object(at: 0) as! NSString
            let filePath: NSString = path.strings(byAppendingPaths: ["Image.png"])[0] as NSString
            let url = URL(fileURLWithPath: filePath as String)
    
            var material = SimpleMaterial()
            material.tintColor = NSColor.yellow
            material.baseColor = try! MaterialColorParameter.texture(TextureResource.load(contentsOf: url))
            material.roughness = MaterialScalarParameter(floatLiteral: 0.1)
            material.metallic = MaterialScalarParameter(floatLiteral: 0.1)    
            boxComponent.materials = [material]
    
            anchor.steelBox!.components.set(boxComponent)
            arView.scene.anchors.append(anchor)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2019-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多