Passing an array of vectors to a uniform

后端 未结 1 973
长发绾君心
长发绾君心 2021-02-06 07:47

I am trying to implement multiple lights in my shader but I have trouble to fill the uniform with my light data.

My vertex shader:

attribute vec3 aVertex         


        
1条回答
  •  無奈伤痛
    2021-02-06 08:32

    gl.uniform3fv expects a flattened array of floats. Also, you are calling it multiple times with the same uniform location. So, the last one wins. Imagine that uniform3fv is a low-level copy command, and you give it (destination, source). It just copies a buffer from one place to another.

    Assuming your example has only 3 lights, you could assign the location uniform this way:

    var locations = [
      1.0, 0, 0,
      0, 1.0, 0,
      0, 0, 0
    ];
    gl.uniform3fv(shaderProgram.pointLightingLocationUniform, locations);
    

    I would also recommend using a more simple shader when you are debugging problems like this. With something like the following in your vertex shader:

    for (int i = 0; i < 3; i++) {
      vColor += uPointLightingLocation[i];
    }
    

    And fragment shader:

    gl_FragColor = vec4(vColor, 1);
    

    Then, if your polygons are yellow you know it is working.

    0 讨论(0)
提交回复
热议问题