最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Passing an array of vectors to a uniform - Stack Overflow

programmeradmin3浏览0评论

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;
attribute vec3 aNormal;
attribute vec2 aTexture;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;

uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation[16];
uniform vec3 uPointLightingColor[16];

varying vec2 vTexture;
varying vec3 vLightWeighting;

void main(void) {
    vec4 mvPosition = uMVMatrix * vec4(aVertex, 1.0);
    gl_Position = uPMatrix * mvPosition;
    vTexture = aTexture;

    int i;
    for (i = 0; i < 16; i++) {
        vec3 lightDirection = normalize(uPointLightingLocation[i] - mvPosition.xyz);
        vec4 transformedNormal = uNMatrix * vec4(aNormal, 1.0);
        float directionalLightWeighting = max(dot(transformedNormal.xyz, lightDirection), 0.0);
        if (i == 0) {
            vLightWeighting = uAmbientColor + uPointLightingColor[i] * directionalLightWeighting;
        } else {
            vLightWeighting = vLightWeighting * (uAmbientColor + uPointLightingColor[i] * directionalLightWeighting);
        }
    }
}

Code that shows only the latest light:

for (var light in this.lightsDirection) {
    gl.uniform3fv(this.shaderProgram.pointLightingLocationUniform, this.lightsDirection[light]);
    gl.uniform3fv(this.shaderProgram.pointLightingColorUniform, this.lightsColor[light]);
}

As the uniform uPointLightingLocation is a vec3 array with the size of 16, I thought that it would be possible to pass the complete array to the uniform, but I have no working solution.

When I try to pass the complete array this.lightsColor (without the index) I see no light at all.

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;
attribute vec3 aNormal;
attribute vec2 aTexture;

uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;

uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation[16];
uniform vec3 uPointLightingColor[16];

varying vec2 vTexture;
varying vec3 vLightWeighting;

void main(void) {
    vec4 mvPosition = uMVMatrix * vec4(aVertex, 1.0);
    gl_Position = uPMatrix * mvPosition;
    vTexture = aTexture;

    int i;
    for (i = 0; i < 16; i++) {
        vec3 lightDirection = normalize(uPointLightingLocation[i] - mvPosition.xyz);
        vec4 transformedNormal = uNMatrix * vec4(aNormal, 1.0);
        float directionalLightWeighting = max(dot(transformedNormal.xyz, lightDirection), 0.0);
        if (i == 0) {
            vLightWeighting = uAmbientColor + uPointLightingColor[i] * directionalLightWeighting;
        } else {
            vLightWeighting = vLightWeighting * (uAmbientColor + uPointLightingColor[i] * directionalLightWeighting);
        }
    }
}

Code that shows only the latest light:

for (var light in this.lightsDirection) {
    gl.uniform3fv(this.shaderProgram.pointLightingLocationUniform, this.lightsDirection[light]);
    gl.uniform3fv(this.shaderProgram.pointLightingColorUniform, this.lightsColor[light]);
}

As the uniform uPointLightingLocation is a vec3 array with the size of 16, I thought that it would be possible to pass the complete array to the uniform, but I have no working solution.

When I try to pass the complete array this.lightsColor (without the index) I see no light at all.

Share Improve this question edited Jan 18, 2011 at 17:18 WarrenFaith asked Jan 18, 2011 at 14:46 WarrenFaithWarrenFaith 57.7k25 gold badges138 silver badges151 bronze badges 1
  • 1 I don't see why this question was voted down. Seems perfectly reasonable. – Jon Bringhurst Commented Mar 6, 2011 at 16:48
Add a comment  | 

1 Answer 1

Reset to default 19

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)

  1. 暂无评论