I have an scene with a few nodes. When there is some animation then it works with 60FPS. But when objects are not moving, it's dropping to 30FPS. I'm rotating camera using coremotion, so this is visible. Is there anything I can do to stop this behavior and keep 60FPS?
If you want to force a continuous refresh of the SCNView:
view.playing=YES
view.loops=YES
Related
I guess someone must have asked this before, but I can't find it right now so in the meanwhile here goes.
Animations of SpriteKit overlay on SceneKit stops when SceneKit animations stop. This is unexpected. It's like the whole SCNView and its children goes into sleep as soon as the 3D contents don't move. How can I make the spritekit scene animate independently of what the scenekit scene does?
Update: I have confirmed that if I add a simple 3D box that rotates indefinitely and keep it in the view somewhere, the 3D scene does not "sleep" and neither does the overlay. Is there a setting somewhere to stop the SceneKit part from sleeping?
Background:
I use SCNView.overlaySKScene. I have a indefinitely repeating animation of one of the subnodes of that overlay. As soon as an action and/or animation goes on in the SceneKit scene, the overlay 2D stuff also animates, but stops as soon as the 3D animations stop.
iOS 9.2.1.
A SceneKit view runs its update/render loop only when it knows there's animation going on. (Because if there's no change in what's to be rendered, rendering just wastes CPU/GPU time and eats up battery power.)
SceneKit is rather conservative in guessing when it should run the render loop. It runs if there are CAAnimations or SCNActions in the scene or if the physics engine is doing something, but I think that's about it.
If you've got other stuff that wants to run the render loop (like an overlay SKScene that needs to run its own game loop), just set the view's playing property to true.
This is something I've been struggling with for a while now, and I'm really hoping someone can shed some light on the issue. Basically, I have a scene with a bunch of colliding SKPhysicsBodies. If I put the camera in the middle of the action on the very first frame of animation, the entire simulation runs at about 17fps indefinitely. However, if on the very first frame of animation I start the camera off-screen such that nothing (or very little) is drawn that first frame, and then move the camera to the middle of the action on the 2nd frame, then the simulation runs at 60fps.
The simulation is exactly the same in both cases. Nothing is different other than what sprites are culled on the very first frame, yet that seems to make all the difference for the rest of the simulation's performance.
This has proven 100% reproducible in every case I come up with, so in order to make sure that my game runs at 60fps, I have to "prime" the camera by starting it way, way off-screen, render a frame, and then move the camera to where I really want it to be. If I don't do that, the game runs at a cripplingly low frame rate and will never speed up.
I have a SpriteKit Scene in which I want to have the effect as if a camera zoom and scale. Does anyone know of any libraries or some easy methods of doing this?
It was very easy to do in other 2D engines but does not seem simple.
I was thinking of doing it from the app delegate, and using the window to zoom since my character does stay around the same position.
The desired effect I would like to accomplish is like that of the start of an Angry Bird level when the camera pans into the level and then the launch doc.
http://www.youtube.com/watch?v=_iQbZ3KNGWQ This is an example of the camera zoom and pans I am talking about.
Thanks for the help.
If you add an SKNode to the SKScene, and make your scene content children of that node instead of direct children of the scene, then you can zoom and pan all of the contained content just by adjusting the xScale, yScale and position properties of the added node. (Any content you did not want scrolled e.g. scores or whatever could then be added to a different SKNode or added directly to the scene).
The adjustment could be done by overriding one of update:, didEvaluateActions, or didSimulatePhysics in your SKScene subclass. The choice would depend on if you are just moving your character around by yourself in update:, or if it also gets moved around by running SKActions or by simulated physics.
We are developing a game that has 2d elements displayed with UIViews over an OpenGL ES view (specifically, we're using GLKit's GLKView) and are having problems keeping the positions perfectly in sync.
In the parent view's layoutSubviews, we're projecting 3d positions in the world onto the screen, and using those as locations for several UIView "markers" in the game. The whole game only updates in response to the user moving the camera, and the camera tells the view setNeedsLayout each time it moves.
Everthing's working fine, except that the markers seem to be roughly 1 frame out of sync with the 3d rendering. I say roughly because (1) it's an estimate! and (2) I'm wondering whether there's potentially a multithreading issue: doesn't GLKView sync to a special screen refresh callback or something?
Is there some way of hooking a view's layoutSubviews so that it sync's to the 3d view update?
Update: Weirdly, calling layoutIfNeeded immediately after setNeedsLayout makes the problem worse! Possibly 2 or more frames out. Really don't understand that!
What's triggering your call to LayoutSubviews?
It all depends where in the RunLoop your call is triggered vs. where your GLK update call is triggered.
In general, for what you're doing, I'd aim to do your layout as a side-effect of the GLK update - i.e. don't wait for layoutSubviews to change your position.
(if you're using OpenGL, then the whole "layout" system isn't much use to you: GLK is running in its own little world of variable frame rate, and you want to make that your reference point)
This is impossible to do correctly without drawing the frames of the video using OpenGL (drawing in the same context so that you are always sure that one frame contains the same time of video and animation). Everyting else you do, framerate compensation, lag prediction, only depends on chance, and will always be a little bit unsynchronized.
I'm not familiar with UIView but if there is any way to let it play audio and copy the frames to a texture, do that. The lag in the audio is much easier to compensate and much less noticeable by the humans than in the video.
I'm tring to synchronize cocos2D layer objects with the map, I managed to get it working by adjusting the glView to the visibleMapRect of the MKMapView. I can zoom, move, my objects are following the map. But, there is a small and annoying lag between the MKMapView and the cocos2D Layer.
I'm synchronizing it at each display loop.
Method:
1) Retrieve the MKMapView.visibleMapRect
2) Set the glViewPort
3) Do an orthographic projection to adjust my layer to the MapView.
I already tried others methods, like moving the cocos2D layer with touch and then move the coordinates of my map according to the touch, still laggy.
Even disabling acceleration and deceleration of the MapView doesn't remove the lag.
Thanks.
Shot in the dark: we know that iOS devices use optimizations to speed up rendering while scaling. This is true for Safari browser, when you zoom in you actually only zoom in on the image that is currently being displayed as the browser window's contents. Only after you stop the pinch motion does the device update the view.
You'll see this specifically with text on older devices. When the device re-renders the contents with the new scale factor, the text suddenly becomes sharp and crisp again. I believe the same optimization is done in MKMapView.
You might want to check if the visibleMapRect values are actually updated during the zoom, and whether they accurately reflect the current zoom level or not.
The other issue I can imagine is that the framerate with MKMapView + Cocos2D is simply low. And specifically zooming might consume a lot of CPU power. You might want to enable the cocos2d FPS display to see what the framerate is.
Another trick that's necessary to allow smooth scrolling of views in cocos2d (particularly complex views like UITableView) is to reduce cocos2d's max framerate (animationInterval) and/or to run the rendering of the gl view on a separate thread. Your issue may simply be a variation of this issue: UIScrollView pauses NSTimer until scrolling finishes
Note that this also occurs with DisplayLink director. The info in this question did the trick for me.