I am working on an application using ARKit and SceneKit frameworks. I am able to render my 3D object, create overlay etc. I want to load a GIF animation like a overlay in ARSession on tap of one of my child nodes. Is this possible?
I tried adding SKScene as a overlay to my SCNScene like below, and it looks good.
informationOverlay = InformationOverlayScene(size: self.sceneView.frame.size)
self.sceneView.overlaySKScene = informationOverlay
But how to load this GIF file in SKScene or SCNNode?
The following SO question answers how to display an animation inside an SKNode:
Simple Gif like animation with Spritekit
You could use projectPoint to project the location of the SCNNode (of your 3D model) to a 2D location on screen, and then use that for the location of the SKNode containing the animation. That way the animation will always face the screen.
Alternatively, you could assign a Core Animation layer containing the animation to the .diffuse.contents property of a SCNMaterial and assign that to a SCNPlane which you can add to the scene and position above the 3D model. If you want that plane to face the camera, simply assign the .pointOfView.orientantion property of your SCNView to the SCNNode that holds the SCNPlane.
Related
I have a usdz model with walking animation in the scene. I will have a tapGesture added to ARView, through which i can get the tapped 2D point, and rayCast it to get the rayCastResult from the 3D scene, now I want to first rotate model in the tapped direction, then move it the destination by playing walking animation on it, the model should face tapped direction only everytime, even after walking is completed it should not change the direction.
I'm trying to geive the user the ability to drag a skspritenode over a UIImageView and UIButton, but whenever I create the scene it appears behind the UI elements. How do I fix this?
I am afraid that is just not possible. All the sprites and any other elements are rendered frame by frame on the SKScene node and cannot be "dragged" or placed anywhere else. These are not UIKit elements.
In this case, your view is acting as the SKScene and hence all the sprites are being rendered onto that.
SKView is a UIView/NSView in itself. All the Sprite Kit nodes (the scene is also a node) are constituents of the SKView and contained within it. You can only change the draw order of views but not their contents.
To give an example: what you're trying to do is the equivalent of trying to place a UIImageView on top of a UIButton's background but below the UIButton's label.
While technically you could achieve this for plain UIView elements with some trickery, the same will never work for Sprite Kit nodes. They aren't views to begin with but drawn onto an OpenGL framebuffer represented/managed by the SKView. From the perspective of Cocoa an SKView is a single view with no subviews in it.
Simply put: nodes are not views, and nodes are technically incompatible to views.
The UIImageView and its UIButton and whatever other content you want in there must be in a UIView that's sitting lower than the SKView you have the Sprite Kit contents in.
And you'll need to ensure the SKView is transparent (at least its background) and that it passes touches through to the UIView underneath if you need them down there sometimes.
Essentially, when I have one CCLayer for background and platform sprites and another for player sprite and HUD, how should I go about calling CCFollow if I want my camera to always center at the player with the HUD in the same layer moving accordingly as well?
Right now the background layer is a subclass of CCLayer that has a CCSpriteBatchNode for drawing objects in the environment. It contains an instance variable CCLayer *HudLayer that has a CCMenu and another CCSpriteBatchNode for the player animation sprites. They are both direct children of a CCScene, meaning HudLayer is not a children of BackgroundLayer. I'm now calling the CCFollow as [self runAction:[CCFollow actionWithTarget:playerSprite worldBoundary:worldRect]] where self is the BackgroundLayer.
The camera follows the box2d physics body of the player correctly, and the menu moves with it too, but whenever the camera moves, let's say to the right, the player sprite moves twice as fast to the right, leaving its physics body behind. So after moving to the right for a bit the playerSprite is outside of screen. playerSprite.pos and playerSprite.position seem to have nothing wrong though.
I've tried calling the CCFollow in different places but either the HUD refuses to move or the camera won't move or the player moves weirdly. What's wrong with it?
I FIGURED IT OUT! The key here is to put the player sprite in some layer other than the HUD layer. I added my CCSpriteBatchNode for the player sprites as a child of backgroundLayer instead of the HudLayer without changing anything else and it worked.
So basically everything that needs to always stay on screen as the HUD should be in one layer, while the target node for CCFollow should be in a separate layer (in the background in my case). The caller layer of CCFollow should contain the target node as a direct child. This tutorial helped me a great deal: http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d
I have a sprite in scene1.m and I have a button in HUD layer hud.m. Now a button appears in Scene1.
How can I get control of scene1 sprite from HUD layer. Any ideas?
With cocos2d, you can assign a tag to your sprite (or button, or any class derived from CCNode), and to each of its hierarchy of parents (see CCNode class, setTag() ). You can then use these tags to retrieve the corresponding nodes from anywhere in your code, with CCNode getChildByTag().
Anyone who has gone through the Stanford CS193P class on iTunes will recognize this from Assignment 3 (HelloPoly). For those not familiar:
I have a custom view called polyView, an instance of PolygonView, a subclass of UIView. On this view, I use CGContextDrawLinearGradient to paint a color gradient over the entire rectangular view. Then I use CGContextDrawPath to stroke and fill a polygon within the bounds of polyView. And I have a UILabel called nameLabel in the center of the view (and polygon) that displays the name of the polygon (triangle, quadrilateral, pentagon, etc.). All of this works fine, and the code to do all this is in the -(void)drawRect method of the PolygonView class.
Where I ran into trouble is with an additional requirement to rotate the polygon within the view in response to user gestures. I used CGAffineTransformRotate() in response to touchesBegan() and touchesMoved() events within the PolygonView class, and this basically works, too. But I can only rotate the entire polyView, not just the polygon drawn on it. I'm sure I could go back and recalculate the path of the polygon and redraw/fill the path in response to each touchesMoved() event, but that would be expensive and can't be the best method. How can I use CGAffineTransformRotate to rotate just the polygon, without rotating the gradient-filled view or the label in the center?
Or is there some way to create the polygon on a layer that I can place over the background polyView at the desired rotation angle?
Thanks for any help you can give a beginner here!
Duane
You can do a CGContextSaveGState(...) just before doing the rotation transformation, then drawing the polygon and restoring the drawing state with a CGContextRestoreGState(...) afterwards, so as not to affect any other drawing in the view later.