setMatrix() is not working as expected in Babylon.js

I’m having trouble with the setMatrix() function in Babylon.js playground.

Here’s a simplified version of my code:

		`// Attributes
        attribute vec3 position;
        uniform mat4 worldViewProj; // 월드-뷰-투영 행렬
        uniform mat4 world;   // 월드 행렬
        // uniform mat4 view;    // 뷰 행렬
        // uniform mat4 projection;  // 투영 행렬

        void main(void) {
            gl_Position = world*vec4(position, 1.0);
        }`;
const effectWrapper = new BABYLON.EffectWrapper({
        engine: engine,
        name: "redShader",
        useShaderStore: true,
        vertexShader:  "custom",
        fragmentShader: "custom",
        attributeNames: ["position"],
        uniformNames: ["worldViewProj", "world"]
    });
const position = new BABYLON.Vector3(0,0,0);
    const rotation = BABYLON.Quaternion.RotationYawPitchRoll(0, Math.PI/4,0);
    // console.log(rotation);
    const scaling = new BABYLON.Vector3(1,1,2);
    var worldMatrix = BABYLON.Matrix.Compose(scaling, rotation, position);
    effectWrapper.effect.setMatrix("world", worldMatrix);

babylon.playground.j…2161612:sourcemap:2 TypeError: Cannot read properties of undefined (reading ‘world’)
at e.setMatrix (babylon.js?t=1742282…:sourcemap:1:264268)
at e.setMatrix (babylon.js?t=1742282…sourcemap:1:1436337)
at createScene (:77:26)
at window.initFunction (:152:16)
at async R._compileAndRunAsync (babylon.playground.j…sourcemap:2:3001359) ‘Retrying if possible. If this error persists please notify the team.’
This is a error I got

My playground

Any help would be greatly appreciated! Thanks!

1 Like

What is the problem? Do you mind using the PG to repro?

1 Like

Oh Sorry. Here’s a error I got:

I think that the matrix ‘world’ is correctly defined in the shader and effect wrapper.
However, I’m getting the following error message in the setMatrix function.

babylon.playground.j…2161612:sourcemap:2 TypeError: Cannot read properties of undefined (reading ‘world’)
at e.setMatrix (babylon.js?t=1742282…:sourcemap:1:264268)
at e.setMatrix (babylon.js?t=1742282…sourcemap:1:1436337)
at createScene (:77:26)
at window.initFunction (:152:16)
at async R._compileAndRunAsync (babylon.playground.j…sourcemap:2:3001359) ‘Retrying if possible. If this error persists please notify the team.’

and How can I use the PG to repro?

If you are getting an error while working in the playground, save the playground and paste the link to the playground here.

I wonder if “world” can’t be set because it’s also a default uniform. Try renaming it?

(google)

By default Babylon. js passes the following Uniforms to your shader: “world”, “worldView”, “worldViewProjection”, “view”, “projection” .

I see. I just modified the code as you suggested, but the same error is still occurring.

Thank you I have uploaded it as you told.

I wrapped the setMatrix in effectWrapper.onApplyObservable.add().

(also named it “world2” just in case, had to change the name in 3 or 4 places)

Found the solution by searching the playground for effectWrapper and found this example.

Does that solve your problem?

Edit: that’s the value of supplying a repro in the playground!

2 Likes