diff --git a/examples/models/resources/shaders/pbr.fs b/examples/models/resources/shaders/pbr.fs index bb2c5f30b..ee044f0b3 100644 --- a/examples/models/resources/shaders/pbr.fs +++ b/examples/models/resources/shaders/pbr.fs @@ -16,10 +16,9 @@ #define LIGHT_POINT 1 struct MaterialProperty { - int enabled; - sampler2D sampler; - float value; vec3 color; + int useSampler; + sampler2D sampler; }; struct Light { @@ -31,8 +30,8 @@ struct Light { }; // Input vertex attributes (from vertex shader) -in vec3 fragPosition; in vec2 fragTexCoord; +in vec3 fragPos; in vec3 fragNormal; in vec3 fragTangent; in vec3 fragBinormal; @@ -46,15 +45,14 @@ 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; -// Input lighting values -uniform Light lights[MAX_LIGHTS]; - - // Other uniform values uniform int renderMode; uniform vec3 viewPos; @@ -66,6 +64,7 @@ const float PI = 3.14159265359; // Output fragment color out vec4 finalColor; +vec3 ComputeMaterialProperty(MaterialProperty property); float DistributionGGX(vec3 N, vec3 H, float roughness); float GeometrySchlickGGX(float NdotV, float roughness); float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness); @@ -73,6 +72,12 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0); vec3 fresnelSchlickRoughness(float cosTheta, vec3 F0, float roughness); vec2 ParallaxMapping(vec2 texCoords, vec3 viewDir); +vec3 ComputeMaterialProperty(MaterialProperty property) +{ + if (property.useSampler == 1) return texture(property.sampler, texCoord).rgb; + else return property.color; +} + float DistributionGGX(vec3 N, vec3 H, float roughness) { float a = roughness*roughness; @@ -97,7 +102,6 @@ float GeometrySchlickGGX(float NdotV, float roughness) return nom/denom; } - float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) { float NdotV = max(dot(N, V), 0.0); @@ -169,25 +173,25 @@ void main() // Calculate lighting required attributes vec3 normal = normalize(fragNormal); - vec3 view = normalize(viewPos - fragPosition); + vec3 view = normalize(viewPos - fragPos); vec3 refl = reflect(-view, normal); // Check if parallax mapping is enabled and calculate texture coordinates to use based on height map - if (height.enabled == 1) texCoord = ParallaxMapping(fragTexCoord, view); + if (height.useSampler == 1) texCoord = ParallaxMapping(fragTexCoord, view); else texCoord = fragTexCoord; // Use default texture coordinates // Fetch material values from texture sampler or color attributes - vec3 color = pow(texture(albedo.sampler, texCoord).rgb, vec3(2.2)); - vec3 metal = texture(metalness.sampler, texCoord).rgb; - vec3 rough = texture(roughness.sampler, texCoord).rgb; - vec3 emiss = vec3(0);//texture(emission.sampler, texCoord).rgb; - vec3 ao = texture(occlusion.sampler, texCoord).rgb; - + vec3 color = ComputeMaterialProperty(albedo); + vec3 metal = ComputeMaterialProperty(metalness); + vec3 rough = ComputeMaterialProperty(roughness); + vec3 emiss = ComputeMaterialProperty(emission); + vec3 ao = ComputeMaterialProperty(occlusion); + // Check if normal mapping is enabled - if (normals.enabled == 1) + if (normals.useSampler == 1) { // Fetch normal map color and transform lighting values to tangent space - normal = texture(normals.sampler, texCoord).rgb; //ComputeMaterialProperty(normals); + normal = ComputeMaterialProperty(normals); normal = normalize(normal*2.0 - 1.0); normal = normalize(normal*TBN); @@ -203,7 +207,6 @@ 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) @@ -214,8 +217,8 @@ void main() if (lights[i].type == LIGHT_DIRECTIONAL) light = -normalize(lights[i].target - lights[i].position); else if (lights[i].type == LIGHT_POINT) { - light = normalize(lights[i].position - fragPosition); - float distance = length(lights[i].position - fragPosition); + light = normalize(lights[i].position - fragPos); + float distance = length(lights[i].position - fragPos); float attenuation = 1.0/(distance*distance); radiance *= attenuation; } @@ -245,7 +248,6 @@ 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); @@ -267,7 +269,6 @@ void main() // Calculate fragment color based on render mode vec3 fragmentColor = ambient + Lo + emiss; // Physically Based Rendering - if (renderMode == 1) fragmentColor = color; // Albedo else if (renderMode == 2) fragmentColor = normal; // Normals else if (renderMode == 3) fragmentColor = metal; // Metalness @@ -286,5 +287,5 @@ void main() fragmentColor = pow(fragmentColor, vec3(1.0/2.2)); // Calculate final fragment color - finalColor = vec4(fragmentColor, 1.0); // It works, so texture is correctly binded! + finalColor = vec4(fragmentColor, 1.0); }