Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCNRender scene with animated objects

I have SCNScene with animated objects (.dae files) and also I have SCNRender initialized with openGLContext. When I set my animated scene to this render I don't see animation, but when I set my animated scene to SCNView, I see animations.

how I set Render:

self.renderer = [SCNRenderer rendererWithContext:_openGLContext options:nil];
self.renderer.autoenablesDefaultLighting = YES;
self.renderer.playing = YES;
self.renderer.scene = myAnimatedScene;

I understand that OpenGL can only draw objects, it doesn't know anything about animation settings in my .dae file

Can somebody explain me how I can draw animated scene using SCNRender initialized with openGLContext?

like image 359
Roman Bobelyuk Avatar asked Nov 02 '16 10:11

Roman Bobelyuk


1 Answers

It appears that when calling the following function:

renderer.render(atTime: time, viewport: viewport,
                commandBuffer: commandBuffer,
                passDescriptor: renderPassDescriptor)

the time parameter is really important. Before I just left it with zero and there were no animations. If you set it with CFAbsoluteTimeGetCurrent(), it seems that you can only get one animation (I've just tested it. My game hero has a shooting action as well as a walking one, and by setting atTime with CFAbsoluteTimeGetCurrent(), only the walking one appears.)

However, our situations are a little bit different. In my case, I also have a SCNView that renders the hero's perspective. So in order to run all the animations, I created a globe variable:

var globeTime:TimeInterval = 0

And saved the current time in the SCNView's renderer function:

func renderer(_ aRenderer: SCNSceneRenderer,
              updateAtTime time: TimeInterval) {
    globeTime = time
    //...
}

And finally passed it to my SCNRender.

//...
renderer.render(atTime: globeTime, viewport: viewport,
                commandBuffer: commandBuffer,
                passDescriptor: renderPassDescriptor)

This solved my problem perfectly.

like image 71
Kelin Sasha Avatar answered Oct 20 '22 23:10

Kelin Sasha