Shader: Problems setting a uniform array


#1

Hey hey

I’m trying to pass an array of floats to a shader, but I can’t seem to get it to work. Here is a small example project, showcasing my problem: Shader Problems

Basically, I’m trying to pass an array of floats or vec2 into my shader, but whatever i try, all the values inside the uniform property stay 0.0.

My Fragment Shader:

uniform vec3 u_workingVector;
uniform float u_notWorkingArray[3];

void main(void)
{
    // gl_FragColor = vec4(u_workingVector.x, u_workingVector.y, u_workingVector.z, 1.0);
    
    gl_FragColor = vec4(u_notWorkingArray[0], u_notWorkingArray[1], u_notWorkingArray[2], 1.0);
}

The way I try to pass values to u_testArray:


    // 1. Attempt
    var testColor = [0, 1, 0];
    
    // 2. Attempt
    var testColor = new Array(3);
    testColor[0] = 0;
    testColor[1] = 1;
    testColor[2] = 0;
    
    // 3. Attempt
    var testColor = new Float32Array(3);
    testColor[0] = 0;
    testColor[1] = 1;
    testColor[2] = 0;
    
    material.setParameter('u_notWorkingArray', testColor);
    material.setParameter('u_workingVector', testColor);

Ofcourse I know, that in this simple case, I could just use a vec3 instead of a float array. But for my real project, I need to pass in many more values than just 3. Can you please help me? Thanks

Ali Moe


#2

Related:

A workaround might have you use a series of vec4s instead…(a bit like AGALs Flash days with uniform vec4s…). But that requires in your shader, dividing float index by 4 to get base vec4 index (floored), getting remainder by subtracting floored value, and picking either X /Y/Z/W in shader with float pickResult = 0; // and add 4 dimensions pick-add lines.. pickResult += vector.[[x or y or z or w]]* float(remainder==[[0 or 1 or 2 or 3]]) casts , which is lame…hmm. THis was one of the reasons why i couldn’t get a HXsl shader transpiled code to work in Playcanvas as well, since that code uses float arrays.


#3

Yeah, I saw your post from April last year, but since there were no more answers, I hoped that by now maybe a solution for this problem was available. Guess I will go with the multiple vec4 solution for now…