SCNSceneRenderer 在 12 帧后停止
SCNSceneRenderer stop after 12 frame
我创建了一个简单的场景,其中包含一个立方体和一个地板供学习使用。
我将委托设置为自己,但我不明白为什么我的渲染在 12 帧后停止打印我的消息。我知道它为什么停止吗?既然我的场景在视野中,不应该永远 运行ning 吗?
这是我的自定义场景:
class GameSceneMain: SCNScene, SCNSceneRendererDelegate, SCNPhysicsContactDelegate {
var camera : SCNCamera?
var floor : SCNFloor?
var delegate : SCNSceneRendererDelegate?
override init() {
super.init()
setupScene()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupScene(){ // load sky
let skybox = UIImage(named: "skybox")
self.background.contents = skybox
delegate = self
addCamera()
addFloor()
addLight()
addCube()
}
func addCamera(){
camera = SCNCamera()
let cameranode = SCNNode()
cameranode.camera = camera
cameranode.position = SCNVector3(0, 5, 10)
cameranode.eulerAngles = SCNVector3(deg2rad(-20), deg2rad(0), 0)
self.rootNode.addChildNode(cameranode)
}
func addFloor(){
floor = SCNFloor()
guard let floor = floor else {
return
}
floor.reflectivity = 0
let mat = SCNMaterial()
mat.diffuse.contents = UIImage(named: "grass")
mat.diffuse.wrapS = SCNWrapMode.repeat
mat.diffuse.wrapT = SCNWrapMode.repeat
floor.materials = [mat]
let nodeFloor = SCNNode(geometry: floor)
self.rootNode.addChildNode(nodeFloor)
}
func addLight(){
let light = SCNLight()
light.type = .ambient
let nodeLight = SCNNode()
nodeLight.light = light
self.rootNode.addChildNode(nodeLight)
}
func addCube(){
let box = SCNBox(width: 1.5, height: 2, length: 3, chamferRadius: 0.3)
let mat = SCNMaterial()
mat.diffuse.contents = UIColor.blue
box.materials = [mat]
let node = SCNNode(geometry: box)
node.position = SCNVector3(0, 0, 0)
node.name = "box"
self.rootNode.addChildNode(node)
}
var counterFrame = 0
// render STOP why??
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
counterFrame += 1
print("run \(counterFrame)")
}
}
另一个class用于AppManager和加载场景
class AppManager:NSObject, ObservableObject {
var sceneView = SCNView()
var scene : GameSceneMain?
override init(){
super.init()
loadScene()
}
func setupView(){
sceneView.backgroundColor = .black
sceneView.allowsCameraControl = true
sceneView.autoenablesDefaultLighting = true
sceneView.showsStatistics = true
sceneView.scene = scene
sceneView.scene?.isPaused = false
sceneView.delegate = scene?.delegate
}
func loadScene(){
scene = GameSceneMain()
guard let scene = scene else {
return
}
sceneView.scene = scene
}
}
我注意到当场景加载帧的计数器 运行 直到 12 时它停止..如果我在模拟器中移动相机而不是渲染器中的计数器继续。
但为什么?不应该运行一直都在看风景吗?
非常感谢您的帮助...
SceneKit
相当新
这是出于性能和能源效率方面的考虑。如果场景中没有任何变化,那么再次渲染相同的内容是一种浪费。
你可以看看 rendersContinuously
属性.
我创建了一个简单的场景,其中包含一个立方体和一个地板供学习使用。
我将委托设置为自己,但我不明白为什么我的渲染在 12 帧后停止打印我的消息。我知道它为什么停止吗?既然我的场景在视野中,不应该永远 运行ning 吗?
这是我的自定义场景:
class GameSceneMain: SCNScene, SCNSceneRendererDelegate, SCNPhysicsContactDelegate {
var camera : SCNCamera?
var floor : SCNFloor?
var delegate : SCNSceneRendererDelegate?
override init() {
super.init()
setupScene()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupScene(){ // load sky
let skybox = UIImage(named: "skybox")
self.background.contents = skybox
delegate = self
addCamera()
addFloor()
addLight()
addCube()
}
func addCamera(){
camera = SCNCamera()
let cameranode = SCNNode()
cameranode.camera = camera
cameranode.position = SCNVector3(0, 5, 10)
cameranode.eulerAngles = SCNVector3(deg2rad(-20), deg2rad(0), 0)
self.rootNode.addChildNode(cameranode)
}
func addFloor(){
floor = SCNFloor()
guard let floor = floor else {
return
}
floor.reflectivity = 0
let mat = SCNMaterial()
mat.diffuse.contents = UIImage(named: "grass")
mat.diffuse.wrapS = SCNWrapMode.repeat
mat.diffuse.wrapT = SCNWrapMode.repeat
floor.materials = [mat]
let nodeFloor = SCNNode(geometry: floor)
self.rootNode.addChildNode(nodeFloor)
}
func addLight(){
let light = SCNLight()
light.type = .ambient
let nodeLight = SCNNode()
nodeLight.light = light
self.rootNode.addChildNode(nodeLight)
}
func addCube(){
let box = SCNBox(width: 1.5, height: 2, length: 3, chamferRadius: 0.3)
let mat = SCNMaterial()
mat.diffuse.contents = UIColor.blue
box.materials = [mat]
let node = SCNNode(geometry: box)
node.position = SCNVector3(0, 0, 0)
node.name = "box"
self.rootNode.addChildNode(node)
}
var counterFrame = 0
// render STOP why??
func renderer(_ renderer: SCNSceneRenderer, didRenderScene scene: SCNScene, atTime time: TimeInterval) {
counterFrame += 1
print("run \(counterFrame)")
}
}
另一个class用于AppManager和加载场景
class AppManager:NSObject, ObservableObject {
var sceneView = SCNView()
var scene : GameSceneMain?
override init(){
super.init()
loadScene()
}
func setupView(){
sceneView.backgroundColor = .black
sceneView.allowsCameraControl = true
sceneView.autoenablesDefaultLighting = true
sceneView.showsStatistics = true
sceneView.scene = scene
sceneView.scene?.isPaused = false
sceneView.delegate = scene?.delegate
}
func loadScene(){
scene = GameSceneMain()
guard let scene = scene else {
return
}
sceneView.scene = scene
}
}
我注意到当场景加载帧的计数器 运行 直到 12 时它停止..如果我在模拟器中移动相机而不是渲染器中的计数器继续。 但为什么?不应该运行一直都在看风景吗?
非常感谢您的帮助... SceneKit
相当新这是出于性能和能源效率方面的考虑。如果场景中没有任何变化,那么再次渲染相同的内容是一种浪费。
你可以看看 rendersContinuously
属性.