![](/img/trans.png)
[英]SceneKit. How to create box with rounded edges and with different radiuses?
[英]SceneKit. How to achieve default scene light behavior without autoenablesDefaultLighting?
我嘗試為我的 SCNView 設置 autoenablesDefaultLighting autoenablesDefaultLighting=true
,它看起來不錯。 但是我想在沒有autoenablesDefaultLighting
的情況下通過設置燈光並稍微調整一下來實現相同的行為。
我用這段代碼嘗試了全光燈:
let lightNode = SCNNode()
lightNode.light = SCNLight()
lightNode.light?.castsShadow = true
lightNode.light?.type = .omni
lightNode.light?.intensity = 10000
lightNode.position = SCNVector3(x: 0, y: 0, z: 100)
scene.rootNode.addChildNode(lightNode)
得到這個:
使用autoenablesDefaultLighting=true
我得到了這個:
我相信,在 SceneKit 中,默認場景照明是沒有任何陰影的定向光,直接附加到默認相機節點(即pointOfView
節點)。 要模擬與.autoenablesDefaultLighting
屬性為true
時相同的照明條件,請使用以下代碼:
委托的渲染器方法——光的 position 方向將每秒更新 60 次:
import SceneKit
extension GameViewController: SCNSceneRendererDelegate {
func renderer(_ renderer: SCNSceneRenderer,
updateAtTime time: TimeInterval) {
sunNode.transform = (sceneView?.pointOfView?.worldTransform)!
let cameraAngles = (self.sceneView?.pointOfView?.eulerAngles)!
let lightAngles = self.sunNode.eulerAngles
print("Camera: " + String(format: "%.2f, %.2f, %.2f", cameraAngles.x,
cameraAngles.y,
cameraAngles.z))
print("Light: " + String(format: "%.2f, %.2f, %.2f", lightAngles.x,
lightAngles.y,
lightAngles.z))
}
}
這是 GameViewController class:
class GameViewController: NSViewController {
var sceneView: SCNView? = nil
let sunNode = SCNNode()
override func viewDidLoad() {
super.viewDidLoad()
sceneView = self.view as? SCNView
sceneView?.delegate = self
let scene = SCNScene(named: "ship.scn")!
sceneView?.scene = scene
sceneView?.scene?.lightingEnvironment.contents = .none
sceneView?.scene?.background.contents = .none
sceneView?.backgroundColor = .black
sceneView?.allowsCameraControl = true
// sceneView?.autoenablesDefaultLighting = true
sunNode.light = SCNLight()
sunNode.light?.type = .directional
sceneView?.scene?.rootNode.addChildNode(sunNode)
}
}
我想補充一點,如果場景中根本沒有光(包括autoenablesDefaultLighting
參數),那么場景中唯一無法控制的光源將是不可切換的環境光。
除了上述之外,基於物理的着色器始終需要額外的環境光燈具(否則基於物理的表面將是黑色的)。 該光源的位置和方向無關緊要。
如果 Directional Light 垂直照亮表面,則表面以 100% 強度照亮(默認強度為 1000 流明),但如果光源的光線與表面平行,則表面不會被此光源照亮。
如您所見,第一張和最后一張圖像具有相同的照明環境。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.