【问题标题】:How to I turn off the ambient light in Scene Kit (with Swift)?如何关闭 Scene Kit(使用 Swift)中的环境光?
【发布时间】:2014-09-02 01:40:35
【问题描述】:

当我使用 Swift 语言创建一个新的场景工具包游戏时,已经有一些来给出这个结果:
我想关闭照亮立方体的环境光,但我不知道该怎么做,因为没有任何光明确附加到任何节点。

她的游戏视图控制器代码:

import SceneKit
import QuartzCore

class GameViewController: NSViewController {

    @IBOutlet var gameView: GameView

    override func awakeFromNib(){
        // create a new scene
        let scene = SCNScene()

        // 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: 2)

        // create and add a 3d box to the scene
        let boxNode = SCNNode()
        boxNode.geometry = SCNBox(width: 1, height: 1, length: 1, chamferRadius: 0.02)
        scene.rootNode.addChildNode(boxNode)

        // create and configure a material
        let material = SCNMaterial()
        material.diffuse.contents = NSImage(named: "texture")
        material.specular.contents = NSColor.whiteColor()
        material.specular.intensity = 0.2
        material.locksAmbientWithDiffuse = true

        // set the material to the 3d object geometry
        boxNode.geometry.firstMaterial = material

        // animate the 3d object
        let animation: CABasicAnimation = CABasicAnimation(keyPath: "rotation")
        animation.toValue = NSValue(SCNVector4: SCNVector4(x: 1, y: 1, z: 0, w: M_PI*2))
        animation.duration = 5
        animation.repeatCount = MAXFLOAT //repeat forever
        boxNode.addAnimation(animation, forKey: "")

        // set the scene to the view
        self.gameView!.scene = scene

        // allows the user to manipulate the camera
        self.gameView!.allowsCameraControl = true

        // show statistics such as fps and timing information
        self.gameView!.showsStatistics = true

        // configure the view
        self.gameView!.backgroundColor = NSColor.blackColor()
    }

}

【问题讨论】:

  • 我已经将你的代码移植到ObjC并替换在iOS SceneKit模板中,我只是将NSImage和NSColor更改为UIImage和UIColor,并且渲染了立方体,但是没有纹理也没有灯光,做你有一个想法可能是什么问题?我猜代码是在模板中加载默认纹理。我需要在您的代码中添加灯光吗?

标签: swift scenekit


【解决方案1】:

您似乎看到的是“默认”照明。

您可以通过调用显式禁用它

gameView.autoenablesDefaultLighting = false

一旦您将自己的灯光添加到场景中,它也会被禁用。

【讨论】:

  • 这也可以从 StoryBoard 中的 SCNView 检查器中关闭
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-06
  • 2015-10-08
  • 1970-01-01
  • 2020-03-04
  • 2012-12-15
  • 1970-01-01
相关资源
最近更新 更多