Here is a PBR shader I put together to test once.
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D texture4;
varying highp vec2 uvVarying;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec3 tangentVarying;
varying mediump vec3 binormalVarying;
varying mediump vec3 lightVarying;
uniform mediump vec4 agk_MeshDiffuse;
uniform mediump vec4 agk_MeshEmissive;
uniform vec3 agk_CameraPos;
uniform vec3 LightPosition[2];
uniform vec3 LightColor[2];
uniform float LightSize[2];
const float PI = 3.14159265359;
vec3 GetPSNormal()
{
mediump vec3 normalmap = texture2D(texture1, uvVarying).xyz * 2.0 - 1.0;
// normalmap.xy *= normalSize;
mediump vec3 norm = normalize(normalVarying);
mediump vec3 tangent = normalize(tangentVarying);
mediump vec3 binormal = normalize(binormalVarying);
mediump mat3 TBN = mat3( tangent, binormal, norm );
return normalize(TBN * normalmap);
}
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0);
}
void main()
{
mediump vec3 diffuseColor = texture2D(texture0, uvVarying).rgb;
mediump float metallic = texture2D(texture2, uvVarying).r;
mediump float roughness = texture2D(texture3, uvVarying).r;
mediump float ao = texture2D(texture4, uvVarying).r;
vec3 V = normalize(agk_CameraPos - posVarying);
vec3 N = GetPSNormal();
diffuseColor = vec3(pow(diffuseColor.r, 2.2), pow(diffuseColor.g, 2.2), pow(diffuseColor.b, 2.2));
vec3 F0 = vec3(0.04);
F0 = mix(F0, diffuseColor, metallic);
// reflectance equation
vec3 Lo = vec3(0.0);
for(int i = 0; i < LightPosition.length(); ++i)
{
// calculate per-light radiance
vec3 L = LightPosition[i] - posVarying;
float dist = length(L);
L /= dist;
vec3 H = normalize(V + L);
float attenuation = 1.0 / (dist * dist);
vec3 radiance = (LightColor[i].rgb * attenuation) * LightSize[i];
// cook-torrance brdf
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= 1.0 - metallic;
vec3 nominator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.001;
vec3 specular = nominator / denominator;
// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * diffuseColor / PI + specular) * radiance * NdotL;
}
vec3 ambient = vec3(0.03) * diffuseColor * ao;
vec3 finalColor = ambient + Lo;
finalColor /= finalColor + vec3(1.0);
finalColor = pow(finalColor, vec3(1.0/2.2));
gl_FragColor = vec4(finalColor, 1.0);
}
I don't know what you're trying but you' probably won't get happy if you are try to make big scenes with this. (for now)