I need an instanced mesh to have all instances face the camera.
uniform vec2 size;
uniform vec3 center;
vec3 billboard(vec3 v, mat4 view) {
vec3 up = vec3(view[0][1], view[1][1], view[2][1]);
vec3 right = vec3(view[0][0], view[1][0], view[2][0]);
vec3 pos = center + right * v.x * size.x + up * v.y * size.y;
return pos;
}
void main() {
vec3 worldPos = billboard(position, viewMatrix);
gl_Position = projectionMatrix * modelViewMatrix * vec4(worldPos, 1.0);
}
This is my code currently to render one billboard. This doesn't display anything. Passing 'position' in the vec4 constructor on the last line renders the shape as expected, so I know there is something wrong with the calculation.
I have been trying to follow this tutorial: http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/billboards/ 1, specifically solution #2, however, I haven’t had much luck figuring this out.
The answer here also looks promising, but I can’t get anything working. THREE.JS GLSL sprite always front to camera 1
The solution for this question: THREE.js - Billboard Vertex Shader is on the right track, but not accurate for my use case.
My understanding is that the center position needs to be sent after any translations to the geometry or mesh to have the correct calculation in world space.
Three.js passes the camera position in the vertex shader. I was thinking some transformation could be applied to each vertex based on the subtraction between the camera position and center position of the billboard. Would that have a hit on performance?
Finally, being that this will be instanced, using a Sprite object or a mesh and setting LookAt to the camera position every frame isn't an option.
Thanks for any help!