因此,我有一个希望简单的问题:

我有一个简单的多维数据集,我正在使用Matrix.ScaleM缩放模型视图并压缩多维数据集(这是有原因的,相信我)。

这项工作,多维数据集缩小。但是,我的片段着色器不再正确地将漫射光源应用于立方体的顶部和底部。阴影代码如下。

precision mediump float;
uniform vec3 u_LightPos;
uniform sampler2D u_Texture;
uniform sampler2D u_Texture2;

varying vec3 v_Position;
varying vec4 v_Color;
varying vec3 v_Normal;          // Interpolated normal for this fragment.
varying vec2 v_TexCoordinate;   // Interpolated texture coordinate per fragment.

// The entry point for our fragment shader.
void main()
{

        float distance = length(u_LightPos - v_Position);

// Get a lighting direction vector from the light to the vertex.
vec3 lightVector = normalize(u_LightPos - v_Position);

    // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are
// pointing in the same direction then it will get max illumination.
float diffuse = max(dot(v_Normal, lightVector), 0.0);
mediump float emptyness = 0.0;
mediump float half_emptyness = 0.1;
// Add attenuation.
diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));

// Add ambient lighting
diffuse = diffuse + 0.3;
vec4 textColor1 = texture2D(u_Texture, v_TexCoordinate);
vec4 textColor2 = texture2D(u_Texture2, v_TexCoordinate);



// Multiply the color by the diffuse illumination level and texture value to get final output color.


if(textColor2.w == emptyness){
    diffuse = diffuse * (1.0 / (1.0 + (0.10 * distance)));
    gl_FragColor = ( diffuse * textColor1 );//v_Color *

    gl_FragColor.a = 1.0;
} else{
    diffuse = diffuse * (1.0 / (1.0 + (0.75 * distance)));
    gl_FragColor = ( diffuse * textColor1 );//v_Color *
    gl_FragColor.a = 0.0;
}



}


那么,有什么想法吗?

而且我知道颜色有点...奇怪。那是完全不同的原因。

编辑:根据要求,顶点着色器:

uniform mat4 u_MVPMatrix;
uniform mat4 u_MVMatrix;
attribute vec4 a_Position;
attribute vec4 a_Color;
attribute vec3 a_Normal;
attribute vec2 a_TexCoordinate;

varying vec3 v_Position;        // This will be passed into the fragment shader.
varying vec4 v_Color;           // This will be passed into the fragment shader.
varying vec3 v_Normal;          // This will be passed into the fragment shader.
varying vec2 v_TexCoordinate;   // This will be passed into the fragment shader.

// The entry point for our vertex shader.
void main()
{
// Transform the vertex into eye space.
v_Position = vec3(u_MVMatrix * a_Position);

// Pass through the color.
v_Color = a_Color;

// Pass through the texture coordinate.
v_TexCoordinate = a_TexCoordinate;

// Transform the normal's orientation into eye space.
v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0));
float halfer = 2.0;

// gl_Position is a special variable used to store the final position.
// Multiply the vertex by the matrix to get the final point in normalized screen coordinates.
gl_Position = u_MVPMatrix * a_Position;

}

最佳答案

您将需要一个倒置的转置矩阵,如下所示:

着色器:

uniform mat4 u_IT_MVMatrix;
...
v_Normal = vec3(u_IT_MVMatrix * vec4(a_Normal, 0.0));


在Java代码中,您可以从常规MV矩阵创建矩阵,如下所示:

invertM(tempMatrix, 0, modelViewMatrix, 0);
transposeM(it_modelViewMatrix, 0, tempMatrix, 0);


然后,您只需将其作为制服传递到着色器中即可。

10-04 17:02