Sprite Kit Particle Emitter Clear Background & Storyboard - ios

I have placed some images and buttons on a storyboard connected to my view with a Particle Emitter on a SKView. The problem is that no matter how many places I set the background color to clearColor the background appears to be black and I cannot see the elements on my storyboard. With Particle Emitters can you use the storyboard?
SKView *skView = [[SKView alloc] initWithFrame:self.view.bounds];
skView.backgroundColor = [SKColor clearColor];
SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];
scene.backgroundColor = [SKColor clearColor];
// Set the scale mode to scale to fit the window
scene.scaleMode = SKSceneScaleModeAspectFit;
[skView presentScene:scene];
NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:#"snowParticle" ofType:#"sks"];
SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
snowParticle.particlePosition = CGPointMake(0, 568);
[scene addChild:snowParticle];
[self.view addSubview:skView];
Thanks,

Related

SpriteKit background not fitting to screen on iPhone 6 plus

I am using a scale mode fill to load a scene with a background with size of 640x1136 as follows:
SKView * skView = (SKView *)self.view;
skView.ignoresSiblingOrder = YES;
MainMenu *scene = [MainMenu sceneWithSize: self.view.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
I am then loading the background inside the screen:
SKSpriteNode *menuBg = [SKSpriteNode spriteNodeWithImageNamed:#"mainBg"];
menuBg.anchorPoint = CGPointZero;
menuBg.position = CGPointZero;
[self addChild:menuBg];
And I expected the backgorund to fill the screen, however it won't resize to fill the screen. I am only holding a #2x background image - what am I doing wrong?
You are not positioning the node in the center of the scene and therefore it won't be scaled correctly with the Aspect Fill scalemode.
Try this instead:
menuBg.anchorPoint = CGPointMake(0.5, 0.5);
menuBg.position = CGPointMake(self.size.width/2, self.size.height/2)
It will set the anchor to the center of your node, and position the node in the center of the scene.

scenekit model from sketchup disappears

I am playing around with scene kit.
I do not know a lot about modeling but i created a very basic Sketchup model with no materials or textures.
I exported the model from ketchup and imported it into the scene.
As i move the camera around towards one end of the model the rear disappears and vice versa. As the camera rotates towards the front the rear gets shadowed in black.
What is the reason for this?
Lighting? Here is the code for the scene. Its a basic Xcode game project
// create a new scene
SCNScene *scene = [SCNScene sceneNamed:#"ship_transport.dae"];
// create and add a camera to the scene
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = [SCNCamera camera];
[scene.rootNode addChildNode:cameraNode];
// place the camera
cameraNode.position = SCNVector3Make(0, 0, 100);
// create and add an ambient light to the scene
SCNNode *ambientLightNode = [SCNNode node];
ambientLightNode.light = [SCNLight light];
ambientLightNode.light.type = SCNLightTypeAmbient
ambientLightNode.light.color = [UIColor whiteColor];
[scene.rootNode addChildNode:ambientLightNode];
// retrieve the ship node
SCNNode *ship = [scene.rootNode childNodeWithName:#"ship" recursively:YES];
ship.geometry.firstMaterial.diffuse.contents = [UIColor redColor];
ship.geometry.firstMaterial.specular.contents = [UIColor whiteColor];
SCNVector3 vector = SCNVector3Make(.1, .1, .1);
[ship setScale:vector];
//[scene.rootNode addChildNode:lightNode];
// animate the 3d object
[ship runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:0 z:0 duration:1]]];
// retrieve the SCNView
SCNView *scnView = (SCNView *)self.view;
scnView.autoenablesDefaultLighting = true;
// set the scene to the view
scnView.scene = scene;
// allows the user to manipulate the camera
scnView.allowsCameraControl = YES;
// show statistics such as fps and timing information
scnView.showsStatistics = YES;
// configure the view
scnView.backgroundColor = [UIColor darkGrayColor];
Try setting the zFar property on the camera. Other related properties to try changing would be the xFov and yFov.

adding a SpriteKit particle to existing app

Ive added a SpriteKit particle to my app - The skView has been added to an existing image view (myImageView). All works well but the skScenne cannot be set to clear colour to show myImage behind the emitter. You can change the colour but not to clear colour ?? `
SKView *skView = [[SKView alloc] initWithFrame:CGRectMake(0.0, 0.0, 768, 1024)];
[myImageView addSubview:skView];
SKScene *skScene = [SKScene sceneWithSize:skView.frame.size];
skScene.scaleMode = SKSceneScaleModeAspectFill;
skScene.backgroundColor = [UIColor clearColor];
SKEmitterNode *emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"MyParticle" ofType:#"sks"]];
emitter.position = CGPointMake(400,0);
[skScene addChild:emitter];
[skView presentScene:skScene];
[self.view addSubview:myImageView];
Unfortunately you can't make an SKView transparent in iOS 7. In iOS 8 it works by using:
skView.allowsTransparency = YES;
skScene.backgroundColor = [UIColor clearColor];
In iOS 7 you can add a large spriteView that is as big as your skView as background and set your myImage as texture.
I suggest you avoid setting the color of your SKScene to [UIColor clearColor] for performance reasons (use opaque views whenever possible). Instead, you can create an SKSpriteNode and add it to your scene as the background...
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:#"image"];
background.size = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
// Bottom/left of your image
background.anchorPoint = CGPointZero;
background.position = CGPointZero;
// Make sure the background is behind other nodes in the scene
background.zPosition = -100;
[self addChild:background];
Since the background is added to the SKScene, it is automatically removed/released when you transition to a new scene.

Change background colours of a SKscene after it has been presented using a timer

So I have setup a skscene within a Skview on an app I'm testing. I setup the background color etc like so.
SKView *skBG = [[SKView alloc] initWithFrame:self.view.bounds];
SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];
// Set the scale mode to scale to fit the window
scene.backgroundColor = [SKColor colorWithRed:0.12f green:0.2f blue:0.27f alpha:1.0f];
scene.scaleMode = SKSceneScaleModeAspectFit;
[skBG presentScene:scene];
NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:#"MyParticle" ofType:#"sks"];
SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
snowParticle.particlePosition = CGPointMake(160, 284);
scene addChild:snowParticle];
[self.view addSubview:skBG];
Then I want to change the background background color randomly on the scene thats already presented.
I've attached a standard timer being called after 5 seconds and in it does something like this:
self.scene.backgroundColor = [SKColor colorWithRed:0.5f green:0.5f blue:0.5f alpha:1.0f];
But it doesn't update? I tried representing the scene with '[skView presentScene:scene];' but doesn't work.
Hm, not all that much to go on, but from what you've actually shared it might be that you're not actually displaying the self.scene property. In the code where you setup the SKView you have:
SKScene *scene = [SKScene sceneWithSize:self.view.bounds.size];
but this is not the same as the .scene property (unless you're doing a self.scene = scene; somewhere inside the method). From what little information we have my guess is that you're either not displaying the self.scene property or that you're covering it up with the new instant. If this is in deed the case then it should be as straightforward as changing what you got to:
SKView *skBG = [[SKView alloc] initWithFrame:self.view.bounds];
_scene = [SKScene sceneWithSize:self.view.bounds.size];
// Set the scale mode to scale to fit the window
self.scene.backgroundColor = [SKColor colorWithRed:0.12f green:0.2f blue:0.27f alpha:1.0f];
self.scene.scaleMode = SKSceneScaleModeAspectFit;
[skBG presentScene:self.scene];
NSString *myParticlePath = [[NSBundle mainBundle] pathForResource:#"MyParticle" ofType:#"sks"];
SKEmitterNode *snowParticle = [NSKeyedUnarchiver unarchiveObjectWithFile:myParticlePath];
snowParticle.particlePosition = CGPointMake(160, 284);
self.scene addChild:snowParticle];
[self.view addSubview:skBG];

Is it possible to apply a filter on a SKEmitterNode?

I have a scene with an SKEmitterNode which works fine when directly added to the scene.
However, I want to add a SKEffectNode on the scene to blur the emitter particles.
This is how it looks.
#implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
SKEffectNode *blurNode = [[SKEffectNode alloc] init];
CIFilter *blurFilter = [CIFilter filterWithName:#"CIGaussianBlur"];
[blurFilter setValue:#10.0 forKey:kCIInputRadiusKey];
blurNode.filter = blurFilter;
blurNode.shouldEnableEffects = YES;
NSString *bokehPath = [[NSBundle mainBundle] pathForResource:#"Bokeh" ofType:#"sks"];
SKEmitterNode *bokeh = [NSKeyedUnarchiver unarchiveObjectWithFile:bokehPath];
[blurNode addChild:bokeh];
[self addChild:blurNode];
}
return self;
}
This results in a blank screen.
Since a SKScene is a subclass of SKEffectNode as well, I tried adding a CIFilter to the scene directly as well.
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
CIFilter *blurFilter = [CIFilter filterWithName:#"CIGaussianBlur"];
[blurFilter setValue:#10.0 forKey:kCIInputRadiusKey];
NSString *bokehPath = [[NSBundle mainBundle] pathForResource:#"Bokeh" ofType:#"sks"];
SKEmitterNode *bokeh = [NSKeyedUnarchiver unarchiveObjectWithFile:bokehPath];
[self addChild:bokeh];
self.filter = blurFilter;
}
return self;
}
Same result.
It's possible to achieve this effect if you indeed apply the filter to the scene directly. Add the following to your second code:
self.shouldEnableEffects = YES;
If you still see a blank screen, try using bright-coloured particles on a black background instead of your white one for this effect. But expect the framerate to drop considerably. You might be better off using a blurred texture for the particles themselves in the Particle Editor.

Resources