指定没有动画变化的随机粒子起始颜色? [英] Specify random particle start colour with no animated change?

查看:51
本文介绍了指定没有动画变化的随机粒子起始颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让粒子根据当前的颜色渐变"产生随机的每个粒子颜色?粒子在其生命周期内不会改变颜色,它们只是在出生时沿着颜色斜坡"的某个地方被分配一种颜色,并保持该颜色直到它们死亡.

Is there a way to have particles spawn with a random per particle colour based on the current "Color Ramp"? The particles do not change colour over their lifespan, they are simply assigned a colour from somewhere along the "Color Ramp" at birth and keep that colour until they die.

这样的结果将是粒子在出生时与从红色到蓝色的混合颜色的混合.

The result of this would be a mix of particles at birth with blend colours from RED through to BLUE.

在我的测试中,我似乎只能得到这样的行为:粒子产生为红色,然后随着接近屏幕底部逐渐变成蓝色.

In my tests I only seem to be able to get the behaviour where particles spawn as RED and then gradually turn to BLUE as the approach the bottom of the screen.

推荐答案

好吧,看来您不能将预定义的 sks 文件用于 SKEmitterNode... 我能够通过以编程方式创建 SKEmitterNode 来解决这个问题.原因是因为当您使用 sks 启动 SKEmitterNode 时,它看起来不像,它不会响应 setParticleColor: 但是以编程方式启动一个.

Well, It looks like you can't use a predefined sks file for the SKEmitterNode... I was able to figure this out by programmatically creating the SKEmitterNode. The reason why is because it doesn't look like when you initiate an SKEmitterNode with an sks, it doesn't respond to setParticleColor: but programmatically initiating one does.

今天,过去的这个小时,是我第一次弄乱 SKEmitterNode,所以你必须忍受我,因为我不知道如何让雪效果完美,但我敢肯定您可以随意更改 SKEmitterNode 允许您更改的值.

Now today, this past hour, was the first time I ever messed with an SKEmitterNode, so you'll have to bear with me because I couldn't figure out how to get the snow effect perfect, but I'm sure you can just mess with the values an SKEmitterNode allows you to change.

无论如何,我将假设 SKEmitterNode 出现在 SKScene 上(这是我知道如何获得所需效果的唯一方法).

In any case, I'm going to assume that the SKEmitterNode is presented on the SKScene (that's the only way I know how to get your desired effect).

首先,您需要将 SKEmitterNode 设为全局/属性/等,因为您稍后需要访问它.

First you'll need to make you're SKEmitterNode a global/property/etc because you'll need access to it later.

MyScene.m 中:

@implementation MyScene {
    SKEmitterNode* leafEmitter;
}

-(id)initWithSize:(CGSize)size {    
        leafEmitter = [[SKEmitterNode alloc] init];
        [leafEmitter setParticleTexture:[SKTexture textureWithImageNamed:@"saFMB.png"]];
        [leafEmitter setParticleBirthRate:10];
        [leafEmitter setScale:0.5];
        [leafEmitter setYAcceleration:-10.0];
        [leafEmitter setParticleSpeedRange:100];
        [leafEmitter setParticleLifetimeRange:100.0];
        [leafEmitter setParticlePositionRange:CGVectorMake(self.size.width, self.size.height)];
        [leafEmitter setPosition:CGPointMake(100, 400)];
        [leafEmitter setParticleBlendMode:SKBlendModeAlpha];
        [self addChild:leafEmitter];
}

所以我在这里所做的是以编程方式创建粒子效果,在这里您将更改动画/速度/等变量以获得您正在寻找的最佳粒子效果.我建议 阅读这个了解更多详情.

So what i've done here is programatically created the particle effect, this is where you'll change the animation/speed/etc variables to get the best particle effect you are looking for. I would suggest reading this for more details.

现在还记得我说过它需要在 SKScene 上呈现吗?那是因为我们将利用 SKScene 附带的 update:(CFTimeInterval)currentTime 函数.

Now remember how I said that it needs to be presented on the SKScene? Well that's because we are going to be taking advantage of the update:(CFTimeInterval)currentTime function that comes along with an SKScene.

update:(CFTimeInterval)currentTime 内是我们将更改SKEmitterNode 颜色的位置.由于此更新函数每帧都会调用,因此无需任何花哨的计时器等即可轻松更改颜色.不确定这是否是个好主意,但重要的是这个主意.

Inside the update:(CFTimeInterval)currentTime is the location where we will be changing the SKEmitterNode's color. Since this update function is called every frame it makes it easy to change the color without any fancy timers or such. Not sure if this is a good idea, but it's the idea that counts.

-(void)update:(CFTimeInterval)currentTime {
[leafEmitter setParticleColor:[UIColor colorWithHue:drand48() saturation:1.0 brightness:1.0 alpha:1.0]];
[leafEmitter setParticleColorBlendFactor:1.0];
    /* Called before each frame is rendered */
}

在这种情况下,我们将颜色更改为随机 RGB 值,但我会让您自己选择颜色.

In this case, we are changing the color to a random RGB value but i'll let you select the colors yourself.

作为回报,这是我的代码产生的:

In return this is what my code has produced:

综上所述,不幸的是,您似乎无法仅使用粒子界面来获得想要的效果.

All this said, it doesn't look like you can get the effect you want solely using the particle interface unfortunately.

这篇关于指定没有动画变化的随机粒子起始颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆