简体   繁体   English

纹理中的粒子系统不会加载

[英]Particle system from texture doesn't load

I am trying to use a particle system loading the particles for a texture. 我正在尝试使用粒子系统加载粒子作为纹理。 Since it's just an exercise, it's fine to me to generate the texture "manually". 因为它只是一个练习,所以我可以“手动”生成纹理。 So in this case I just allocate a buffer of data and fill it with red color (RGBA 32 bit format). 所以在这种情况下,我只是分配一个数据缓冲区并用红色填充(RGBA 32位格式)。

But the problem is that when the program starts running, I just see the black screen and there isn't what I am expecting to see (some red particles moving on). 但问题是,当程序开始运行时,我只看到黑屏,并没有我期待看到的东西(一些红色粒子继续前进)。

I explain what I am trying to do in the comments: 我在评论中解释我想要做的事情:

if( (self=[super init]))
{
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];

    uint32_t mask= 255+pow(255, 4); // This is a 32 bit red pixel
    NSUInteger size=256; // The texture is 256x256
    void* buffer= malloc(size*size*4); // Here I hold the data, it will be all red
    for(NSUInteger i=0; i<size; i++)
    {
        for(NSUInteger j=0; j<size; j++)
        {
            memcpy(buffer+j+i*32, &mask, 4);
        }
    }
    NSData* data=[[[NSData alloc]initWithBytesNoCopy: buffer length: size*size*4 freeWhenDone: YES]autorelease];  // I wrap it into NSData to automatically release it later. 
    CCTexture2D* texture=[[[CCTexture2D alloc]initWithData: data.bytes pixelFormat: kCCTexture2DPixelFormat_RGBA8888 pixelsWide: size pixelsHigh: size contentSize: CGSizeMake(size, size)]autorelease];
    CCParticleSystemQuad* system=[[[CCParticleSystemQuad alloc]initWithTotalParticles: 100]autorelease];
    system.texture= texture;  // I set the texture in a way that it should be load to draw particles
    system.position= ccp(200, 200);
    system.startSize= 10;
    system.endSize= 5;
    system.duration= kCCParticleDurationInfinity;
    system.life= 1;
    [self addChild: system]; 

    [pool drain];
}

Using an image 使用图像

I tried to modify the code in a way that I use an UIImage instead of the manually created texture: 我尝试以我使用UIImage而不是手动创建的纹理的方式修改代码:

if( (self=[super init]))
{
    // Even with glClearColor() it doesn't work
    //glClearColor(1, 0, 1, 1);
    NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];

    NSBundle* bundle=[NSBundle mainBundle];
    NSString* path=[bundle pathForResource: @"fire" ofType: @"png"];
    UIImage* image=[UIImage imageWithContentsOfFile: path];
    CCTexture2D* texture=[[[CCTexture2D alloc]initWithImage: image]autorelease];
    CCParticleSystemQuad* system=[[[CCParticleSystemQuad alloc]initWithTotalParticles: 100]autorelease];
    system.texture= texture;
    system.startSize= 10;
    system.endSize= 10;
    system.life= 1;
    system.duration= kCCParticleDurationInfinity;
    system.position= ccp(200, 200);
    // Even changing the start/end color it doesn't work
    //system.startColor= (ccColor4F){1.0,0.0,0.0,1.0};
    //system.endColor= (ccColor4F){1.0,0.0,0.0,1.0};
    system.emitterMode= kCCParticleModeGravity;
    system.gravity= ccp(200, 200);
    system.speed=1;
    [self addChild: system];

    [pool drain];
}

The image "fire.png" in the app bundle is: 应用包中的图片“fire.png”是:

在此输入图像描述

I also set some other properties but it still doesn't work: black screen. 我还设置了一些其他属性,但它仍然不起作用:黑屏。

Couple things come to my mind (besides the code improvements, see my comments): 我想到了几件事情(除了代码改进,请参阅我的评论):

Change the clear color, just to see if the particle system may be running but only spawning black particles: 更改清晰颜色,只是为了查看粒子系统是否正在运行但只生成黑色粒子:

glClearColor(1, 0, 1, 1);

The particle system itself may not be properly configured (incomplete data, properties using default values which usually are just 0s). 粒子系统本身可能未正确配置(不完整的数据,使用默认值的属性,通常只有0)。 It's possible that the properties you haven't modified cause the particle system to render incorrectly. 您未修改的属性可能会导致粒子系统无法正确渲染。 For example the particle system has startColor and endColor properties. 例如,粒子系统具有startColor和endColor属性。 If they are both initialized to a black or transparent color because you haven't set them to a brighter (white) color, it doesn't matter what color your texture has, the particles will be black. 如果它们都被初始化为黑色或透明颜色,因为您没有将它们设置为更亮(白色)的颜色,那么纹理的颜色无关紧要,颗粒将是黑色的。 I believe this is most likely what's happening here. 我相信这很有可能发生在这里。

Try setting up the particle system with a regular texture from an image file first. 首先尝试使用图像文件中的常规纹理设置粒子系统。 If you have confirmed the particle system creates visible, non-black particles you can then switch to using your custom texture. 如果您已确认粒子系统会创建可见的非黑色粒子,则可以切换到使用自定义纹理。

我最终使用粒子mint创建了一个粒子系统,然后使用工厂方法:

CCParticleSystemQuad* system=[[[CCParticleSystemQuad alloc]initWithDictionary: [ParticleFactory makeEffect]]autorelease];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM