Search code examples
three.jsvertex-shader

Can't get separation of modelViewMatrix into modelMatrix and viewMatrix to work


I am trying to do a vertexShader where I want to use the modelMatrix on the vertex position to do some calculations, and afterwards apply the viewMatrix.

I didn't manage to get this working, while I'm convinced that this code is supposed to be true: modelViewMatrix = modelMatrix * viewMatrix

But it doesn't apply correctly even in the most basic vertexShader:

void main() {
    gl_Position = projectionMatrix * modelMatrix * viewMatrix * vec4(position, 1.0);
}

While this works fine:

void main() {
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}

Three.JS seems to pass the matrices, though I have not found a way to verify the values of these variables:

uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;

Solution

  • It should be the other way around:

    void main() {
        gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
    }