What I want to achieve:
I have a fully functional shader for 3D objects written in GLSL, that was initially designed for Three.js, that I would like to use in libGDX now. The full code of the shader and the link to a live preview is provided at the end.
In the shader, I have the following uniforms, that were initially provided by Three.js:
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
As per Three.js documentation, the values of the uniforms are the following:
// = object.matrixWorld
uniform mat4 modelMatrix;
// = camera.matrixWorldInverse * object.matrixWorld
uniform mat4 modelViewMatrix;
// = camera.projectionMatrix
uniform mat4 projectionMatrix;
// = camera.matrixWorldInverse
uniform mat4 viewMatrix;
// = inverse transpose of modelViewMatrix
uniform mat3 normalMatrix;
// = camera position in world space
uniform vec3 cameraPosition;
My question:
How do I get these values in libGDX?
@Override
public void render(Renderable renderable) {
program.setUniformMatrix(modelMatrix, ???);
program.setUniformMatrix(modelViewMatrix, ???);
program.setUniformMatrix(projectionMatrix, ???);
program.setUniformMatrix(viewMatrix, ???);
renderable.meshPart.render(program);
}
Here is the code of the shader.
Fragment:
precision highp float;
precision highp int;
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
uniform vec3 color;
uniform vec3 lightPosition;
varying vec3 vPosition;
varying vec3 vNormal;
varying vec2 vUv;
varying vec2 vUv2;
void main() {
vec3 worldPosition = ( modelMatrix * vec4( vPosition, 1.0 )).xyz;
vec3 worldNormal = normalize( vec3( modelMatrix * vec4( vNormal, 0.0 ) ) );
vec3 lightVector = normalize( lightPosition - worldPosition );
float brightness = dot( worldNormal, lightVector );
gl_FragColor = vec4( color * brightness, 1.0 );
}
Vertex:
precision highp float;
precision highp int;
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
attribute vec2 uv2;
varying vec3 vPosition;
varying vec3 vNormal;
varying vec2 vUv;
varying vec2 vUv2;
void main() {
vNormal = normal;
vUv = uv;
vUv2 = uv2;
vPosition = position;
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
}
Live preview of the shader:
https://shaderfrog.com/app/view/5365
Any help is highly appreciated!