Working on materials PBR...

Some issues with the shader (GPU dependant)
This commit is contained in:
Ray 2017-07-14 01:16:22 +02:00
commit 28679e3c79
2 changed files with 126 additions and 89 deletions

View file

@ -30,11 +30,9 @@ int main()
Texture2D texHDR = LoadTexture("resources/pinetree.hdr");
model.material = LoadMaterialPBR(texHDR, (Color){ 255, 255, 255, 255 }, 1.0f, 1.0f);
model.material.maps[TEXMAP_ALBEDO].tex = LoadTexture("resources/pbr/trooper_albedo.png");
SetShaderValuei(model.material.shader, GetShaderLocation(model.material.shader, "albedo.sampler"), (int[1]){ TEXMAP_ALBEDO }, 1);
SetShaderValuei(model.material.shader, GetShaderLocation(model.material.shader, "albedo.useSampler"), (int[1]){ 1 }, 1);
//model.material.maps[TEXMAP_ALBEDO].tex = LoadTexture("resources/pbr/trooper_albedo.png");
SetMaterialTexture(&model.material, TEXMAP_ALBEDO, LoadTexture("resources/pbr/trooper_albedo.png"));
/*
SetMaterialTexture(&model.material, TEXMAP_NORMAL, LoadTexture("resources/pbr/trooper_normals.png"));
SetTextureFilter(model.material.maps[TEXMAP_NORMAL].tex, FILTER_BILINEAR);
@ -49,8 +47,9 @@ int main()
for (int i = 0; i < 12; i++)
{
printf("TexMap %i ID: %i\n", i, model.material.maps[i].tex.id);
//printf("TexMap %i ID: %i\n", i, model.material.maps[i].tex.id);
}
*/
SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
@ -73,7 +72,7 @@ int main()
Begin3dMode(camera);
DrawModel(model, VectorZero(), 1.0f, RED);
DrawModel(model, VectorZero(), 1.0f, WHITE);
DrawGrid(10, 1.0f);

View file

@ -41,18 +41,23 @@ uniform MaterialProperty albedo;
uniform MaterialProperty normals;
uniform MaterialProperty metalness;
uniform MaterialProperty roughness;
uniform MaterialProperty ao;
uniform MaterialProperty occlusion;
uniform MaterialProperty emission;
uniform MaterialProperty height;
// Input lighting values
uniform Light lights[MAX_LIGHTS];
// Input uniform values
uniform samplerCube irradianceMap;
uniform samplerCube prefilterMap;
uniform sampler2D brdfLUT;
//uniform sampler2D albedo;
//uniform sampler2D normals;
//uniform sampler2D metalness;
// Input lighting values
uniform Light lights[MAX_LIGHTS];
// Other uniform values
uniform int renderMode;
uniform vec3 viewPos;
@ -184,17 +189,19 @@ void main()
else texCoord = fragTexCoord; // Use default texture coordinates
// Fetch material values from texture sampler or color attributes
vec3 color = pow(ComputeMaterialProperty(albedo), vec3(2.2));
vec3 metal = ComputeMaterialProperty(metalness);
vec3 rough = ComputeMaterialProperty(roughness);
vec3 emiss = ComputeMaterialProperty(emission);
vec3 occlusion = ComputeMaterialProperty(ao);
vec3 color = pow(texture(albedo.sampler, texCoord).rgb, vec3(2.2));//pow(ComputeMaterialProperty(albedo), vec3(2.2));
vec3 metal = texture(metalness.sampler, texCoord).rgb; //ComputeMaterialProperty(metalness);
vec3 rough = texture(roughness.sampler, texCoord).rgb; //ComputeMaterialProperty(roughness);
vec3 emiss = texture(emission.sampler, texCoord).rgb; //ComputeMaterialProperty(emission);
vec3 ao = texture(occlusion.sampler, texCoord).rgb; //ComputeMaterialProperty(occlusion);
vec3 all = color + metal + rough + emiss + ao;
// Check if normal mapping is enabled
if (normals.useSampler == 1)
{
// Fetch normal map color and transform lighting values to tangent space
normal = ComputeMaterialProperty(normals);
normal = texture(normals.sampler, texCoord).rgb; //ComputeMaterialProperty(normals);
normal = normalize(normal*2.0 - 1.0);
normal = normalize(normal*TBN);
@ -210,6 +217,7 @@ void main()
vec3 Lo = vec3(0.0);
vec3 lightDot = vec3(0.0);
/*
for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled == 1)
@ -251,6 +259,7 @@ void main()
lightDot += radiance*NdotL + brdf*lights[i].color.a;
}
}
*/
// Calculate ambient lighting using IBL
vec3 F = fresnelSchlickRoughness(max(dot(normal, view), 0.0), F0, rough.r);
@ -268,7 +277,7 @@ void main()
vec3 reflection = prefilterColor*(F*brdf.x + brdf.y);
// Calculate final lighting
vec3 ambient = (kD*diffuse + reflection)*occlusion;
vec3 ambient = (kD*diffuse + reflection)*ao;
// Calculate fragment color based on render mode
vec3 fragmentColor = ambient + Lo + emiss; // Physically Based Rendering
@ -277,7 +286,7 @@ void main()
else if (renderMode == 2) fragmentColor = normal; // Normals
else if (renderMode == 3) fragmentColor = metal; // Metalness
else if (renderMode == 4) fragmentColor = rough; // Roughness
else if (renderMode == 5) fragmentColor = occlusion; // Ambient Occlusion
else if (renderMode == 5) fragmentColor = ao; // Ambient Occlusion
else if (renderMode == 6) fragmentColor = emiss; // Emission
else if (renderMode == 7) fragmentColor = lightDot; // Lighting
else if (renderMode == 8) fragmentColor = kS; // Fresnel
@ -291,5 +300,5 @@ void main()
fragmentColor = pow(fragmentColor, vec3(1.0/2.2));
// Calculate final fragment color
finalColor = vec4(fragmentColor, 1.0);
finalColor = vec4(all, 1.0);//vec4(fragmentColor, 1.0); // It works, so texture is correctly binded!
}