BUG: uniform vec4/ array of floats is broken?

How to use Material.setParameter to pass an array of floats, vec3, vec4, etc

Can I use setParameter to assign value to uniform vec4 vertexParams[4]; or like uniform float vertexParams2[16] . I tried them by assigning an array of numbers, but they don’t seem to work.

I did an audit. It seems it’s impossible to pass parameters to data types like: vec4 or vec4 array[] or float array[] to Material shader via setParameter. Values don’t get sent at all.

In material script:

this.material.setParameter("testPosition", [1,1,1]);
this.material.setParameter("testvec4", [1,1,1,1]);

In vertex shader:

attribute vec3 position;   
uniform vec4 testvec4;
uniform vec3 testPosition;

gl_Position = matrix_viewProjection * matrix_model * testvec4;
vs
gl_Position = matrix_viewProjection * matrix_model * vec4(testPosition,1);

2nd case works with vec3 parameter being used instead (testPosition). But 1st case with vec4 testVec4!!!
doesn’t work!.

I did an audit of the commit functions. It appears certain commit functions aren’t called for. UNIFORMTYPE_VEC4 and UNIFORMTYPE_FLOATARRAY!!) But UNIFORMTYPE_VEC3 does get called.

this.app.graphicsDevice.commitFunction[pc.UNIFORMTYPE_VEC4] = function(u) {
    
  console.log("vec4 is called:"+u.locationId);  
};
this.app.graphicsDevice.commitFunction[pc.UNIFORMTYPE_FLOATARRAY] = function(u) {
    
  console.log("float f is called:"+u.scopeId.name);  
};
this.app.graphicsDevice.commitFunction[pc.UNIFORMTYPE_VEC3] = function(u) {
    
  console.log("vec3 f is called:"+u.scopeId.name);  
};

Did you guys miss out something?

1 Like

I’m setting a vec4 this way:

var v = new pc.Vec4(1, 2, 3, 4);
this.material.setParameter('myVect', v.data);

In the shader is defined

uniform vec4 myVect;

pc.Vec4.data is a Float32Array which looks like the key difference here: https://stackoverflow.com/questions/15823021/when-to-use-float32array-instead-of-array-in-javascript/15935873

The spec here helps explain this: https://www.khronos.org/registry/webgl/specs/1.0/#5.14.10

Perhaps the docs should be updated to make this more obvious?

1 Like

Vec3.data also uses Float32Array as well. But the vec3 uniform works and is applied (1,1,1) but not vec4 uniform (1,1,1,1). Anyone tried passing parameter to uniform float varArr[]?.

Is there a reproducible for this issue?

https://playcanvas.com/project/546744
Hmm…above single vec4 variable case seems to be working now, but not several other secondary cases . But i’m trying to narrow down on how the extract trigger.

Did you guys recently pushed a fix or something?

It seems the calls to the following UNIFORMTYPE_VEC4 commit functions are occuring now.

this.app.graphicsDevice.commitFunction[pc.UNIFORMTYPE_VEC4] = function(u) {
    
  console.log("vec4 is called:"+u.scopeId.name);  
};

but not functions like: UNIFORMTYPE_FLOATARRAY :

  this.app.graphicsDevice.commitFunction[pc.UNIFORMTYPE_FLOATARRAY] = function(u) {
    
  console.log("float array is called:"+u.scopeId.name);  
};

The following composition cases don’t seem to work.

  • this.material.setParameter(“vertexParams”, new pc.Mat4().data); //this doesn’t seem to work // Float32 array of length 16 to -> uniform vec4 vertexParams[4]; ??
  • this.material.setParameter(“vertexParams2”, new pc.Mat4().data); // this doesn’t seem to work // Float32 array of length 16 to -> uniform float vertexParams2[16]; ?

Only the basic uniform mat4 data type representation can resolve properly. UNIFORMTYPE_MAT4:

  • this.material.setParameter(“vParamsMatrix”, new pc.Mat4().data); // this works fine // Float32 array of length 16 to -> uniform mat4 vParamsMatrix;