Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding background image to ScnScene

I have an app which uses SpriteKit and emitter particles (sks) which I find to be not as realistic as Scene kit's particles (scnp). So I wish to incorporate that into SKScene but I'm not sure if that is even possible. So I decided to create an SCNScene and particles(scnp). However I want to add a background image and the only 3D effect would be the particles. Can anyone help me set a background image to my SCNScene. Traditionally I would add it like this to an SKScene but I could not find any help online for the SceneKit.

 let backgroundNode = SKSpriteNode(imageNamed: "backgroundImage")
    backgroundNode.size = view.frame.size
    backgroundNode.position = CGPointMake(view.frame.size.width/2, view.frame.height/2)

    self.addChild(backgroundNode)

Thank you in advance

like image 850
snksnk Avatar asked Aug 13 '15 17:08

snksnk


2 Answers

let scnScene = SCNScene()
scnScene.background.contents = UIImage(named: "earth.jpg")
like image 137
Kusal Shrestha Avatar answered Oct 28 '22 21:10

Kusal Shrestha


Basically the same answer as @Kusal Shrestha, but I was able to put a gradient background using a bunch of different technologies. Still very simple and useful.

    const CGFloat DIVISOR = 255.0f;
    CIColor *bottomColor = [CIColor colorWithRed:(CGFloat)0xee / DIVISOR green:(CGFloat)0x78 / DIVISOR blue:(CGFloat)0x0f / DIVISOR alpha:1];
    CIColor *topColor = [CIColor colorWithRed:0xff / DIVISOR green:0xfb / DIVISOR blue:0xcf / DIVISOR alpha:1];
    SKTexture* textureGradient = [SKTexture textureWithVerticalGradientofSize:scnview.frame.size topColor:topColor bottomColor:bottomColor];
    UIImage* uiimageGradient = [UIImage imageWithCGImage:textureGradient.CGImage];
    self.background.contents = uiimageGradient;

Where "self" is a subclass of SCNScene.

This example requires the category on texture from here: https://gist.github.com/Tantas/7fc01803d6b559da48d6

like image 32
Jonny Avatar answered Oct 28 '22 22:10

Jonny