效果图
天空盒顶点shader
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
varying vec3 V_Texcoord;
void main()
{
V_Texcoord=pos;
gl_Position=P*V*M*vec4(pos,1.0);
}
天空盒片元shader
varying vec3 V_Texcoord;
uniform samplerCube U_MainTexture;
void main()
{
gl_FragColor=textureCube(U_MainTexture,V_Texcoord);
}
反射顶点shader
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
uniform mat4 NM;
varying vec4 V_WorldPos;
varying vec3 V_Normal;
void main()
{
V_WorldPos=M*vec4(pos,1.0);
V_Normal=mat3(NM)*normal;//将法线坐标转换到世界空间
gl_Position=P*V*M*vec4(pos,1.0);
}
反射片元shader
varying vec4 V_WorldPos;
varying vec3 V_Normal;
uniform samplerCube U_MainTexture;
void main()
{
//视线方向向量
vec3 eyeVec=normalize(V_WorldPos.xyz-vec3(0.0));
vec3 n=normalize(V_Normal);
//求视线方向的反射向量
vec3 r=reflect(eyeVec,n);
vec4 color=textureCube(U_MainTexture,r);
gl_FragColor=color;
}
折射片元shader
varying vec4 V_WorldPos;
varying vec3 V_Normal;
uniform samplerCube U_MainTexture;
void main()
{
vec3 eyeVec=normalize(V_WorldPos.xyz-vec3(0.0));
vec3 n=normalize(V_Normal);
vec3 r=refract(eyeVec,n,1.0/1.52);
vec4 color=textureCube(U_MainTexture,r);
gl_FragColor=colo