【问题标题】:cannot change material不能改变材料
【发布时间】:2015-01-31 02:55:59
【问题描述】:

我添加了一行代码(其中显示 //LINE CHANGED)将材质更改为蓝色。但是,当我加载游戏时,默认船保持为正常纹理。关于为什么会发生这种情况的任何想法?我可以在我制作的立方体上很好地更改材料,但由于某种原因,船的材料没有改变。

override func viewDidLoad() {
    super.viewDidLoad()

    // create a new scene
    let scene = SCNScene(named: "art.scnassets/ship.dae")!

    // create and add a camera to the scene
    let cameraNode = SCNNode()
    cameraNode.camera = SCNCamera()
    scene.rootNode.addChildNode(cameraNode)

    // place the camera
    cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)

    // create and add a light to the scene
    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light!.type = SCNLightTypeOmni
    lightNode.position = SCNVector3(x: 0, y: 10, z: 10)
    scene.rootNode.addChildNode(lightNode)

    // create and add an ambient light to the scene
    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light!.type = SCNLightTypeAmbient
    ambientLightNode.light!.color = UIColor.darkGrayColor()
    scene.rootNode.addChildNode(ambientLightNode)

    // retrieve the ship node
    let ship = scene.rootNode.childNodeWithName("ship", recursively: true)!


    //LINE CHANGED
    ship.geometry?.firstMaterial?.diffuse.contents = UIColor.blueColor()
    //LINE CHANGED

    // animate the 3d object\
    ship.runAction(SCNAction.repeatActionForever(SCNAction.rotateByX(0, y: 2, z: 0, duration: 1)))

    // retrieve the SCNView
    let scnView = self.view as SCNView

    // set the scene to the view
    scnView.scene = scene

    // allows the user to manipulate the camera
    scnView.allowsCameraControl = true

    // show statistics such as fps and timing information
    scnView.showsStatistics = true

    // configure the view
    scnView.backgroundColor = UIColor.blackColor()

    // add a tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
    let gestureRecognizers = NSMutableArray()
    gestureRecognizers.addObject(tapGesture)
    if let existingGestureRecognizers = scnView.gestureRecognizers {
        gestureRecognizers.addObjectsFromArray(existingGestureRecognizers)
    }
    scnView.gestureRecognizers = gestureRecognizers
}

【问题讨论】:

    标签: ios swift scenekit


    【解决方案1】:

    ship.geometry 为零。所以该行的其余部分将无效。

    如果您在 xcode 中检查文件“ship.dae”,您会看到名为“ship”的节点没有附加几何图形,但它有一个名为“shipMesh”的子节点,它拥有几何图形。

    【讨论】:

    • 感谢您的回答
    【解决方案2】:

    Toyos already covered the answer pretty well,但我想补充一些建议,但评论太长了...

    Swift 中的可选链(或者在 ObjC 中可能为 nil 的消息)存在固有的危险——如果它失败了,它会默默地这样做。您可能会说这还不如崩溃那么糟糕,但它更难调试。

    与其编写longChain?.of?.optionalCalls(),不如考虑使用optional binding (if let) 作为其中的一部分,这样您就可以看到由于nil 值而导致链中断的位置。它更多的是代码,但它可以让您检查链的每个步骤并以对您的应用程序最有意义的方式处理故障。这甚至可以是一种调试策略——如果您有一行似乎什么都不做的可选链调用,请将其替换为等效的 if let 语句嵌套,直到找到问题为止。 (如果你愿意,可以在修复后返回可选链接。)

    很多人对隐式展开的选项很苛刻,但在某些时候和地方它们可能会很有用。我想说这就是其中之一——你需要通过的选项链是你可以在应用程序构建时(而不是在源代码编译时)保证的东西,因为它是你的应用程序中提供的 DAE 的配置。 (这与 IBOutlets 通常是 IUO 背后的基本原理基本相同。)编写 ship.geometry!.firstMaterial!.contents 会提醒您 DAE 所拥有的与您认为它所拥有的不匹配,因为您会崩溃取消引用 geometry .

    【讨论】:

      猜你喜欢
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 2020-10-13
      • 1970-01-01
      • 2020-08-30
      相关资源
      最近更新 更多