Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

How to convert this logic to a shader


PyroStunts
 Share

Recommended Posts

This week I've been rewriting some code to use pixi.js . In my game I have a smoke grenade. The smoke particles were drawn with circles with PIXI.Graphics(); . On each frame, I could be drawing from up to 1000 of these or other kinds of circles for things like explosions. I read that PIXI.Graphics() should not be used for this purpose as the game becomes very slow if there are too many particles to render. Further reading suggested I write my own shader. I would like some help with writing a shader as I have no experience with this kind of code as yet. Here is what I have so far

My Smoke Particle:
 

var Smoke = function (x,y) {
	this.OPACITY_REDUCTION=0.01;
	this.opacity=getRandom(0.5,1);
	this.size=getRandom(5,10);
	this.x=x;
	this.y=y;
	this.xdir=getRandom(-1,1);
    this.ydir=getRandom(0,-2);
};


Smoke.prototype.updateAndCanKeep = function () {
	this.x+=this.xdir;
	this.y+=this.ydir;
	this.opacity-=this.OPACITY_REDUCTION;
	return (this.opacity>0);
};

One smoke particle is created every frame while the smoke grenade is on the ground (30 seconds). It is removed when the opacity is <0.

This code is inside my render function:

for(var i=smokeTrails.length-1; i>=0; i--){
	if(smokeTrails[i].updateAndCanKeep()){
		particleGraphics.lineStyle(0);
		particleGraphics.beginFill(0x00C800, smokeTrails[i].opacity);
		particleGraphics.drawCircle(smokeTrails[i].x-thisPlayer.viewX,smokeTrails[i].y-thisPlayer.viewY, smokeTrails[i].size);
		particleGraphics.endFill();
	}else{
		smokeTrails.splice(i,1);
	}

}

Is this code easy to port into a shader? If I can have some help with this, I'll be able to do the same for all my other particles. Thanks.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...