Billboarding particle system in vertex shader

Started by
3 comments, last by FredrikHolmstr 10 years, 8 months ago

I've been working on a GPU driven particle system for mobile devices (OpenGL ES 2.0). I've gotten stuck on one part of it, namely billboarding the particles in the vertex shader. Since there is no vertex texture fetch in OpenGL ES 2.0 I have packed all the data each particle needs into the vertex buffer, this somewhat limits the effects I can achieve with the system, but it's good enough for what I need.

The problem is that after the position, etc. for each particle has been calculated, it's no longer positioned at the origin in the local space of the particle system, so when I apply my billboarding the entire particle system gets billboarded as one unit - but this is obviously not what I want as I want to billboard each particle individually. And I simply can not come up with a way to do this inside the vertex shader.

Advertisement

I just billboard particles at view space.

1. Simulation calculate particle world space position.

2. Transform position to view space.

3. Expand particle using texture coords.(if uv are range of [0 : 1] corner is located at: viewSpacePosition - (uv - 0.5) * size) Optionally rotated around view space z.

kalle_h: if I'm not mistaken, this solution requires you to do some of the work on the CPU right? I have quite a lot of particles, and i'm doing this on a mobile device (iPhone 3GS), I have literally 0 CPU budget for this.

kalle_h: if I'm not mistaken, this solution requires you to do some of the work on the CPU right? I have quite a lot of particles, and i'm doing this on a mobile device (iPhone 3GS), I have literally 0 CPU budget for this.

0% cpu work.

Code snippet from actual shader code. This requires particles to have angle and world space position from some where.(I get those from stateless vertex shader simulation).


highp vec4  pos      = u_viewMatrix * vec4(position, 1.0);
highp float sinA     = sin(angle);
highp float cosA     = cos(angle);
highp vec2 posOffset = size * (texcoord - vec2(0.5));

pos.xy += vec2(cosA*posOffset.x + sinA*posOffset.y, -cosA*posOffset.y + sinA*posOffset.x);

gl_Position = u_projectionMatrix * pos;

kalle_h: Thanks for all your help, the solution I ended up with is a bit different, because for some reason I couldn't get your example to work (I'm with you on the world position, but not sure where you derive the angle from). Anyway, my solution works like this:

1) I generate a mesh dynamically which contains all my particle quads, the data is packed like this into the vertex buffer:

POSITION: Midpoint of the quad

TEXCOORD0: Offset of this vertex from the midpoint

2) In the vertex shader I do the following operations:

First, I apply my particle simulation, which gives me the middle point of the current quad in local space of the particle system

Second, I transform the quad to view space

Third, I apply the offset of the quad on the X/Y axes of the mid point

Fourth, I transform the resulting vertex with the Projection matrix

The exact shader code I use looks like this:


VEC4 viewSpace = mul(ModelView, VEC4(localSpace, 1));
viewSpace.x += i.uv.x;
viewSpace.y += i.uv.y;

o.pos = mul(Projection, viewSpace);

This seems to be working fine, but is there any drawback to doing it like this in viewspace?

This topic is closed to new replies.

Advertisement