Question

Je suis en train de mettre en œuvre plusieurs lumières dans mon shaders mais j'ai du mal à remplir l'uniforme avec mes données de lumière.

Mon 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);
        }
    }
}

Le code qui ne montre que la dernière lumière:

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

Comme le uPointLightingLocation uniforme est un tableau vec3 avec la taille de 16, je pensais qu'il serait possible de passer le tableau complet à l'uniforme, mais je n'ai pas de solution de travail.

Lorsque je tente de passer le this.lightsColor tableau complet (sans l'indice) Je ne vois pas de lumière du tout.

Était-ce utile?

La solution

gl.uniform3fv attend un tableau aplaties de flotteurs. En outre, vous appelez cela plusieurs fois avec le même emplacement uniforme. Ainsi, le dernier gagne. Imaginez que uniform3fv est une commande de copie de bas niveau, et vous donner (destination, source). Il copie juste un tampon d'un endroit à l'autre.

En supposant que votre exemple a seulement 3 lumières, vous pouvez assigner l'emplacement uniforme de cette façon:

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

Je recommande également d'utiliser un plus simple, shaders lors du débogage des problèmes comme celui-ci. Avec quelque chose comme ce qui suit dans votre vertex shader:

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

shaders fragment:

gl_FragColor = vec4(vColor, 1);

Alors, si vos polygones sont jaunes, vous savez qu'il fonctionne.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top