Specify a random initial particle color without animated changes?

Is there a way for particles to appear with a random particle color based on the current “color ramp”? Particles do not change color throughout their lives, they are simply assigned a color from somewhere along the “Color ramp” at birth and retain that color until they die.

The result will be a combination of particles at birth with mixing colors from RED to BLUE.

Color blend

In my tests, it seems to me that I can get behavior when particles appear as RED, and then gradually turn into BLUE, as an approach to the bottom of the screen.

enter image description hereFall leaves

+6
source share
1 answer

Well, it looks like you cannot use the predefined sks file for SKEmitterNode ... I was able to figure this out programmatically by creating SKEmitterNode . The reason is that it does not look like when starting SKEmitterNode with sks it does not respond to setParticleColor: but programmatically initiates it.

Today, this hour has passed, I first ever ruined SKEmitterNode, so you have to chase me because I could not figure out how to achieve the snow effect, but I'm sure you can just mess around with the values ​​that you can change.

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

First you need to make SKEmitterNode global / property / etc, because you will need to access it later.


In 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 did here is a programmatically created particle effect, here you change the animation / speed / etc variables to get the best particle effect you are looking for. I would suggest reading this for more details.


Now remember, as I said, that it should be presented on SKScene? Good, because we will use the update:(CFTimeInterval)currentTime function, which comes with SKScene .

Inside update:(CFTimeInterval)currentTime is the place where we will change the color of SKEmitterNode . Since this refresh function is called each frame, it makes it easy to change colors without any fancy timers or such. Not sure if this is a good idea, but it is an idea that matters.

 -(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 */ } 

In this case, we change the color to a random RGB value, but I will let you choose the colors yourself.


In turn, this is my code:

enter image description here


All of this says that it doesn't seem like you can get the effect you want to use using the particle interface, unfortunately.

+8
source

Source: https://habr.com/ru/post/957506/


All Articles