[SOLVED]Bilboard shader for mesh based special effects

Users who are viewing this thread

Since particle engine warband posses is rather limited i've come up with idea for "simulating"
particle effects via mesh vertex animations.
I need it for coding some cool spell efects i've made in blender, but those are using mesh based emitters and force fields for particle movement.

For example Blender allows for "exporting" particle system as animated mesh,  those could be imported to warband and with correct shader used as animated special effect.

However i need correct shader for that.
It seems that only change i have to make is to rewrite vertex shader part of "soft_particle_add" warband shader to render each triangle of model as facing to viewer.



Regards,
Peter
 
I may be wrong but I thought that as long as you are using the correct shader and flags then particle polygons are rendered so that they are always facing the screen anyway. I think there is possible a flag which performs this.

When you talk about an "animated mesh" do you mean that it uses vertex animation/Morph target animation?

EDIT: I had a little play around with smoke particles in game and it appears that they do rotate in relation to the camera so that they are always facing it as I thought - plus if you look at most particles meshes M&B uses, they are almost always single sided, flat polygons, in order for these to work and look half decent they must be rotated in relation to the camera. I suspect it is a mesh flag which does this but Im honestly not sure, I could be wrong about the whole lot.
 
Particles are created in a way they always face the camera that's their most important property
- but for regular meshes i don't think its a matter of a  flag.
Since it requires per vertex computations and therefore must be done in shader - or before the mesh data is send to reneder.
(Particle "meshes" are generated in a way the face camera but if you have ready mesh u have to ensure that property by yourself)

animated mesh -> vertex animation.
 
Arsakes said:
Since particle engine warband posses is rather limited i've come up with idea for "simulating"
particle effects via mesh vertex animations.
I need it for coding some cool spell efects i've made in blender, but those are using mesh based emitters and force fields for particle movement.

For example Blender allows for "exporting" particle system as animated mesh,  those could be imported to warband and with correct shader used as animated special effect.

However i need correct shader for that.
It seems that only change i have to make is to rewrite vertex shader part of "soft_particle_add" warband shader to render each triangle of model as facing to viewer.

Here is how am i doing that:

Code:
//////////////////////////////////////CUSTOM SHADER based on vs_dephed_flare///////////////////////////////////////////
VS_DEPTHED_FLARE vs_flare_custom(float4 vPosition : POSITION, float4 vColor : COLOR, float2 tc : TEXCOORD0)
{
	VS_DEPTHED_FLARE Out;

	 // apply world transformation to center of mesh (model-View)
	Out.Pos = mul(matWorldView, float4(0.0, 0.0, 0.0, 1.0) )  ;
        // we are in view space x-y are directions on the screen 
	Out.Pos.xy = vPosition.xy;

        // project
	Out.Pos = mul(matViewProj, Out.Pos);
   

	Out.Tex0 = tc;
	Out.Color = vColor * vMaterialColor;
	
	
	if(use_depth_effects) {
		Out.projCoord.xy = (float2(Out.Pos.x, -Out.Pos.y)+Out.Pos.w)/2;
		Out.projCoord.xy += (vDepthRT_HalfPixel_ViewportSizeInv.xy * Out.Pos.w);
		Out.projCoord.zw = Out.Pos.zw;
		Out.Depth = Out.Pos.z * far_clip_Inv;
	}

	//apply fog
	float3 P = mul(matWorldView, vPosition); //position in view space
	float4 vWorldPos = (float4)mul(matWorld,vPosition);
	float d = length(P);
	Out.Fog = get_fog_amount_new(d, vWorldPos.z);

	return Out;
}

....

// kompilacja shadera
VertexShader vs_particlelike_compiled = compile vs_2_0 vs_flare_custom();
technique particlelike_add
{
	pass P0
	{
		VertexShader = vs_particlelike_compiled;
		PixelShader = compile ps_2_0 ps_main_depthed_flare(false,true); // the same pixel shader as for soft_particle add shader
	}
}

As you can guess its not working.
The object is just invisible.
Could anybody check this shader?

The thing compiles and i've put resulting *.fxo everywhere :grin:

Regards,
Peter

I researched some kind of billboarding technique for Star Wars Conquest back them. Marco Tarini (the creator of OpenBRF) managed to do it for trees, for an early version of The Last Days, by using shader trickery.

I know that normally one should do the matrix directly before sending them to the graphics API to avoid redundancies, but it can be done in-shader too, albeit under a performance penalty.

Code:
http://mbx.streetofeyes.com/index.php/topic,812.msg20380.html#msg20380

There are a bunch of other resources if you know what to look for, in that thread there are a bunch of posts from Marco about it.
Apart from that, and the HLSL code released, there are many articles on the web about the needed computations.

The camera vector is provided to the shader author as an uniform, even on early M&B versions.
Have fun with all the projection transformation matrices!

f403b13744b797e0b7eab5ab6f32f56d6f6c1f02.png

beam.png

(By Bytehazard, linked down there)


PS: I was mostly interested in axially aligned billboards (which are much much harder to do in shader and require extra attributes for it to compute the final position, Marco did it very ingeniously by providing the vertex count (1,2,3,4) of a quad in the vertex colors so the shader did know to what side to pull the vertex, I didn't manage to do it back them), for laser beams (think of Portal 2, or yeah, lightsabers).

Code:
http://www.bytehazard.com/code/beam.html (Mirrored here: https://archive.today/EjdoX)
http://www.flipcode.com/archives/Billboarding-Excerpt_From_iReal-Time_Renderingi_2E.shtml
 
Hey,
could you provide some extra info regarding this beam-like particles (axial blilboarding) ?
This is precisely i need to do now. Is there any ready shader for this available?

Can I apply new shader to particle system just as "easy" as to props? Or there is some special warband **** that makes it impssible.
 
Arsakes said:
Hey,
could you provide some extra info regarding this beam-like particles (axial blilboarding) ?
This is precisely i need to do now. Is there any ready shader for this available?

Can I apply new shader to particle system just as "easy" as to props? Or there is some special warband **** that makes it impssible.

There's no shader code, but there's an OpenGL code snippet in the original article. Looks like ByteHazard has updated his site structure and the old links are dead:

Code:
http://www.bytehazard.com/articles/beam.html
(Official site)
Code:
http://archive.is/bFz8U
(Backup)
 
Hmm, so how did you do it in your starwars mod? The laser beams axis aligned particels?
I've seen the movies it seems you used such technique.
 
Arsakes said:
Hmm, so how did you do it in your starwars mod? The laser beams axis aligned particels?
I've seen the movies it seems you used such technique.

In Star Wars Conquest we just used six to eight crossed planes in X shape and set the materials to additive.
If you take a vertical look to our lightsabers -or an horizontal one to blaster beams- you'll see the trick.

Basically our way is a much more cruder way of doing it, instead of having a single plane that always looks in the camera direction we just added planes in almost every direction, as there's no shadows and the blending is additive (think of the overlay method of blending layers in Photoshop) it still looks kind of a poor's man version of the real deal... No shaders required.

blendmodes.png


Tricks like this were used in our mod for almost everything. Pretty ingenious solutions for sundry problems, restrictions and limitations.
 
Ok i've did the shader, didn't test it yet in MB though it work in ShaderMaker (the beam is oriented the best way possible as in this opengl tut).
Its in GLSL, will get you noticed after its teested in MB.

Thx.

I'm assuming that particle model is oriented along y direction (that is beam direction is in y in model coorinates), z is normal to bilboard mode.
Fragment shader is easy to write so I'm not showing it.

Code:
        
uniform mat4 viewMatrix;
uniform mat4 gl_ModelViewMatrix;
uniform mat4 gl_ProjectionMatrix;


const vec4 ex = vec4(1.0,0.0,0.0,0.0);
const vec4 ey = vec4(0.0,1.0,0.0,0.0);
const vec4 ez = vec4(0.0,0.0,1.0,0.0);

void main()
{

  // for rotation matrices this is inverse
  mat4 tvm = transpose(gl_ModelViewMatrix);
  vec3 up = gl_ModelViewMatrix[1].xyz; // ey na to przechodzi

  // question is what the direction in model space is transformed 
  // onto x-y view plane and is orthogonal to beam direction
  vec4 w = vec4(normalize(cross(ez.xyz, up.xyz)), 0.0);
  // cool now invert it to get direction in model space
  vec3 invW = (tvm*w).xyz;

  vec3 pos = gl_Vertex.x * invW + gl_Vertex.y * ey.xyz +          gl_Vertex.z*ez.xyz;

  gl_Position    = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
  gl_FrontColor  = gl_Color;
  gl_TexCoord[0] = gl_MultiTexCoord0;
}
 
Arsakes said:
Ok i've did the shader, didn't test it yet in MB though it work in ShaderMaker (the beam is oriented the best way possible as in this opengl tut).
Its in GLSL, will get you noticed after its teested in MB.

Thx.

I'm assuming that particle model is oriented along y direction (that is beam direction is in y in model coorinates), z is normal to bilboard mode.
Fragment shader is easy to write so I'm not showing it.

Code:
        
uniform mat4 viewMatrix;
uniform mat4 gl_ModelViewMatrix;
uniform mat4 gl_ProjectionMatrix;


const vec4 ex = vec4(1.0,0.0,0.0,0.0);
const vec4 ey = vec4(0.0,1.0,0.0,0.0);
const vec4 ez = vec4(0.0,0.0,1.0,0.0);

void main()
{

  // for rotation matrices this is inverse
  mat4 tvm = transpose(gl_ModelViewMatrix);
  vec3 up = gl_ModelViewMatrix[1].xyz; // ey na to przechodzi

  // question is what the direction in model space is transformed 
  // onto x-y view plane and is orthogonal to beam direction
  vec4 w = vec4(normalize(cross(ez.xyz, up.xyz)), 0.0);
  // cool now invert it to get direction in model space
  vec3 invW = (tvm*w).xyz;

  vec3 pos = gl_Vertex.x * invW + gl_Vertex.y * ey.xyz +          gl_Vertex.z*ez.xyz;

  gl_Position    = gl_ModelViewProjectionMatrix * vec4(pos, 1.0);
  gl_FrontColor  = gl_Color;
  gl_TexCoord[0] = gl_MultiTexCoord0;
}

There's a camera uniform in the HLSL effects file of the M&B engine.

I guess you could use that to get a 3D vector pointing back from the objects center to the camera position. And from that one could offset the four vertices pivoting around their common center.

The Last Days of the Third Age had map trees that worked like this, Marco Tarini created a degenerated quad (composed by two triangles), stored their individual relative positions in their per-vertex color slots, brought them (their actual positions) together at 0,0,0 and made a shader that reconstructed the position of every of them in the vertex shader.

Sounds quite complex, but there are probably easier ways of doing this. Not that I'm good with math.
 
Hmm aren't rain particles axis aligned ones? Maybe the correct transformation is already there i've tested it getting strange results.
Did you test it?
 
Arsakes said:
Hmm aren't rain particles axis aligned ones? Maybe the correct transformation is already there i've tested it getting strange results.
Did you test it?

Yes, they are. Sort of.

But I think that particles from the engine's particle subsystem are computed and pre-aligned in the CPU and fed to the GPU as ready-to-use meshes.

Take a look to the
Code:
game_rain
particle system, which uses
Code:
psf_billboard_2d
, while
Code:
game_snow
makes use of the
Code:
psf_billboard_3d
flag instead:
Code:
https://github.com/tldmod/tldmod/blob/master/ModuleSystem/module_particle_systems.py#L46

And if you see the defines at the top:
Code:
#psf_billboard_2d        = 0x0000000100 # up_vec = dir, front rotated towards camera
#psf_billboard_3d        = 0x0000000200 # front_vec point to camera.

So, a 2D billboard only rotates along the up vector, i.e: the drops are always vertical respect to the ground no matter the camera angle, but the camera almost never sees the rain drop sprites flat, as they rotate around the ground position looking at the camera to minimize flatness. Looking a game raindrop from above makes it look completely flat due to this axial limitation.


A 3D billboard like a snow flake always points directly to the camera, even if the player is looking it from above. Never flat, always crispy.
 
I've DID IT! SHADER for axis aligned particle effects that works with scene props, missiles etc.
Welcome laser beams!!

Screeenies in spoiler!!
33w7c02.jpg

1zp2ssx.jpg

:grin: :grin: :grin:

THanks man you've helped me.
 
Arsakes said:
I've DID IT! SHADER for axis aligned particle effects that works with scene props, missiles etc.
Welcome laser beams!!

Screeenies in spoiler!!
33w7c02.jpg

1zp2ssx.jpg

:grin: :grin: :grin:

THanks man you've helped me.

Neat! Glad that it worked out in the end after so much fiddling!
 
Back
Top Bottom