Billboard shader, Model matrix?

Today I was trying to make a billboard vertex shader (i.e. planes rotated to always face the camera). From my googling it seemed relatively simple to do, but I didn’t see any way of accessing the model matrix from Defold? Is there any way to do that? Or maybe I am misunderstanding what I need to do this?

I know I could just rotate the game object parents of my sprites/quads, I was specifically trying to do it via a shader.

1 Like

The vertex positions are all in world space, not in model space. We have an issue for adding inverse world matrix so you can translate positions to object space if needed: DEF-2518

1 Like

Has anyone managed to make a working billboard vp? I really want to be able to make 3d particles always look at the player in shader code without relying on manually rotating GOs.

2019-04-19%2006_15_29-

https://www.geeks3d.com/20140807/billboarding-vertex-shader-glsl/

Haven’t tried, but I’ll give it a go :slight_smile:

2 Likes

Any luck?

Hm, didn’t try with particles really, mostly did a demo that rotates planes towards the camera but maybe I should look at particles instead. What approaches have you tried so far and what is the current pain point?

1 Like

Can you share what you did with planes? A non-shader solution is not good because rotating models has a lot of overhead.

AFAIK because vertex positions are in world space we can’t use the technique as linked above?

I want to use the particlefx particles in 3d scenes and have individual particles face the camera no matter how the player moves around. I have no clue how to do this.

You can set local space for your models.
12

1 Like

How would you adapt this vertex code to work right?

#version 150
in vec4 gxl3d_Position;
in vec4 gxl3d_TexCoord0;

// GLSL Hacker automatic uniforms:
uniform mat4 gxl3d_ModelViewProjectionMatrix;
uniform mat4 gxl3d_ModelViewMatrix;
uniform mat4 gxl3d_ProjectionMatrix;
uniform mat4 gxl3d_ViewMatrix;
uniform mat4 gxl3d_ModelMatrix;

uniform int spherical; // 1 for spherical; 0 for cylindrical

out vec4 Vertex_UV;
void main()
{
  //mat4 modelView = gxl3d_ViewMatrix*gxl3d_ModelMatrix;
  mat4 modelView = gxl3d_ModelViewMatrix;
  
  // First colunm.
  modelView[0][0] = 1.0; 
  modelView[0][1] = 0.0; 
  modelView[0][2] = 0.0; 

  if (spherical == 1)
  {
    // Second colunm.
    modelView[1][0] = 0.0; 
    modelView[1][1] = 1.0; 
    modelView[1][2] = 0.0; 
  }

  // Thrid colunm.
  modelView[2][0] = 0.0; 
  modelView[2][1] = 0.0; 
  modelView[2][2] = 1.0; 
  
  vec4 P = modelView * gxl3d_Position;
  gl_Position = gxl3d_ProjectionMatrix * P;
  
  Vertex_UV = gxl3d_TexCoord0;
}

Would there be any changes to the material file?

Is this because of batching or am I doing something wrong in the vp?

Since Defold uses GLES 2.0 point sprites should be enabled and usable?

I still need help with billboards, any sample VP would be useful!!

Sorry, forgot about it :frowning: I’ll make a mental note to look at this on sunday.

1 Like

Looking forward to it!

Is there something about sprites vs models which would make this not work with sprites?

:thinking:

Ok, using model as billboard is not that hard

 vec4 p = mtx_worldview * vec4(0.0, 0.0, 0.0, 1.0 /*scale*/) + vec4(position.xyz, .0);

did the trick.

But I stuck with sprites. As sprite shader has view_proj * vec4(position.xyz, 1.0) and I have no idea how defold calculates go rotations here.

In theory we can pass rotation (or reversed rotation) for each billboard object to shader (via sprite.set_constant) but what way we should apply this rotation? Huh.

That would break batching (I think?) which is a no-go, this has to be able to be done entirely in shaders for performance reasons and keep performance. I think point sprites are what are supposed to be used for billboard particles and maybe even stuff like foliage / grass.

I still don’t fully understand your case. Maybe you should make a video with wrong behaviour of current (build-in) particle shader or issue with rendering tree (made of sprites?).

Main thing I want is efficient 3d particles for 3d games where player can move around a world. If you put a particlefx in 3d space the particles will always look in a fixed direction and cannot follow the player. I want things that are possible in other game engines.

1 Like

I agree. This was one of our most requested feature for Blastlands (that wasnt critical) as we used a lot of 3D rotations of objects but still wanted the particles to face camera.

4 Likes

Sorry to resurrect this thread, but I can’t seem to find the answer anywhere else on the forum. Would anyone happen to have a billboard material for models, based on the shader code above?

I’ve trying to turn this into a billboard material, but can’t get it to work.

Update: @Pkeod to the rescue! https://github.com/subsoap/defold-shader-examples

4 Likes

Let’s go back to that question.
The bottom line is this: is it REALLY necessary to pass labels, sprites and particles NOT as billboards?
All the games I’ve seen - they all use particles and labels as billboards.
It would make sense that this would be the default behaviour for sprites and particles in Defold!

How can this be resolved?
Pass in the vertex data the centre of the sprite.
For example, the vertex declaration now has the format position + texcoord.
Pass in each vertex additionally the centre of the sprite/particle (you know it anyway). This will align the sprite with the camera in the custom shader.
This solution is completely compatible with previous versions of the engine and does not break anything.

5 Likes