xcode 将背景图像添加到 ScnScene

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31994834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:40:33  来源:igfitidea点击:

Adding background image to ScnScene

xcodeswiftsprite-kitscenekitscnnode

提问by snksnk

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.

我有一个使用 SpriteKit 和发射器粒子 (sks) 的应用程序,我发现它不如 Scene kit 的粒子 (scnp) 逼真。所以我希望将它合并到 SKScene 中,但我不确定这是否可能。所以我决定创建一个 SCNScene 和粒子(scnp)。但是我想添加一个背景图像,唯一的 3D 效果就是粒子。谁能帮我为我的 SCNScene 设置背景图像。传统上,我会像这样将它添加到 SKScene,但我无法在线找到 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

先感谢您

回答by Kusal Shrestha

let scnScene = SCNScene()
scnScene.background.contents = UIImage(named: "earth.jpg")

回答by Jonny

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.

与@Kusal Shrestha 的答案基本相同,但我能够使用一系列不同的技术设置渐变背景。仍然非常简单和有用。

    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.

其中“self”是 SCNScene 的子类。

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

此示例需要来自此处的纹理类别:https: //gist.github.com/Tantas/7fc01803d6b559da48d6

回答by Amal T S

I tried using @Kusal Shresthas answer. However the image was not loading when I put the image file in scnassets. But when I put the file in xcassets, it worked.

我尝试使用@Kusal Shresthas 的答案。但是,当我将图像文件放入scnassets. 但是当我把文件放进去时xcassets,它起作用了。

scnScene.background.contents = UIImage(named: "earth")