SCNSceneRender在12帧后停止



我创建了一个简单的场景,其中有一个立方体和一个地板,用于学习。

我将代理设置为self,但我不明白为什么我的渲染停止在12帧后打印出我的消息。。我知道它为什么停了?既然我的场景在眼前,就不应该永远奔跑吗?

这里是我的自定义场景:

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)")
}
}

另一个类用于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属性。

相关内容

  • 没有找到相关文章

最新更新