环境贴图:Environment Mapping - MartinRGB/GLES30_ProgrammingGuide_NDK GitHub Wiki

环境贴图

简单纹理环境贴图

通过使用 reflect 内置函数,输入 眼睛|摄像机的向量法向量,生成 反射向量,然后使用这个向量在2D纹理中进行索引。

球面纹理坐标:Sphere Mapping

varying vec2 sphere_mapping
vec2 sphere_map(vec3 position,vec3 normal){
    vec3 reflection = reflect(position,normal);
    float m = 2.0 * sqrt(reflection.x * reflection.x + reflection.y * reflection.y+
                   (reflection.z + 1.0) *  (reflection.z + 1.0));
    return vec2((reflection.x/m+0.5),(reflection.y/m+0.5));
}
void main() {

    sphere_mapping = sphere_map((u_mvMatrix * a_position).xyz,normalize(u_mvMatrix * vec4(a_normal,1.0)).xyz);
    gl_Position = u_mvpMatrix * a_position;
} 

立方体图纹理坐标: Cube Mapping

varying vec2 cube_mapping:
vec2 cube_map(vec3 position,vec3 normal){
    vec3 reflection = reflect(position,normal);
    return reflection.xy;
}

void main() {
    sphere_mapping = sphere_map((u_mvMatrix * a_position).xyz,normalize(u_mvMatrix * vec4(a_normal,1.0)).xyz);
    gl_Position = u_mvpMatrix * a_position;
} 

片段中:

uniform sampler2D reflectionSampler
varying vec2 sphere_mapping

void main() {
    vec3 base = texture2D( reflectionSampler, vec2(sphere_mapping.x+time/10.,sphere_mapping.y+time/10.)).rgb;
    gl_FragColor = vec4( base, 1. );

}