5 struct PbrMaterialParameters
15 PbrMaterialParameters pbr_material;
18 uniform sampler2D base_color_map;
19 uniform sampler2D metalness_map;
20 uniform sampler2D roughness_map;
21 uniform sampler2D occlusion_map;
22 uniform sampler2D emission_map;
23 uniform sampler2D fresnel_lookup;
25 layout(constant_id=auto) const bool use_base_color_map = false;
26 layout(constant_id=auto) const bool use_metalness_map = false;
27 layout(constant_id=auto) const bool use_roughness_map = false;
28 layout(constant_id=auto) const bool use_occlusion_map = false;
29 layout(constant_id=auto) const bool use_emission = false;
30 layout(constant_id=auto) const bool use_emission_map = false;
31 layout(constant_id=auto) const bool use_image_based_lighting = false;
33 #pragma MSP stage(fragment)
34 virtual vec4 get_base_color()
36 if(use_base_color_map)
37 return texture(base_color_map, texcoord.xy);
39 return pbr_material.base_color;
42 virtual float get_metalness_value()
45 return texture(metalness_map, texcoord.xy).r;
47 return pbr_material.metalness;
50 virtual float get_roughness_value()
53 return texture(roughness_map, texcoord.xy).r;
55 return pbr_material.roughness;
58 virtual float get_occlusion_value()
61 return texture(occlusion_map, texcoord.xy).r;
66 virtual vec3 get_emission_color()
69 return texture(emission_map, texcoord.xy).rgb;
71 return pbr_material.emission.rgb;
74 /* Computes the diffuse reflection of the macrosurface */
75 vec3 lambert_diffuse(vec3 base_color)
77 // Scale by pi to get a result per steradian, suitable for integration
81 /* Computes the fraction of microfacets aligned at the halfway vector
82 (Trowbridge-Reitz GGX) */
83 float normal_distribution_ggxtr(vec3 normal, vec3 halfway, float roughness)
85 float n_dot_h = max(dot(normal, halfway), 0.0);
86 float rough_q = roughness * roughness;
88 float denom = n_dot_h*n_dot_h*(rough_q-1)+1;
89 /* Scale by pi to normalize the total area of the microfacets as projected
90 to the macrosurface */
91 return rough_q/(PI*denom*denom);
94 /* Computes shadowing and masking of a microfacet surface from a given
96 float geometry_schlick_ggx(vec3 normal, vec3 view, float k)
98 float n_dot_v = max(dot(normal, view), 0.0);
99 return n_dot_v/(n_dot_v*(1.0-k)+k);
102 /* Computes shadowing and masking of a microfacet surface for a combination of
103 look and light directions */
104 float geometry_smith(vec3 normal, vec3 look, vec3 light, float roughness)
106 float k = (roughness+1.0)*(roughness+1.0)/8.0;
107 float ggx_look = geometry_schlick_ggx(normal, look, k);
108 float ggx_light = geometry_schlick_ggx(normal, light, k);
109 return ggx_look*ggx_light;
112 /* Computes the reflectance of the material at a given reflection angle */
113 vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
115 // 0.04 is a decent approximation for dielectric base reflectivity
116 vec3 f0 = mix(vec3(0.04), base_color, metalness);
117 return mix(f0, vec3(1.0), pow(max(1.0-dot(halfway, look), 0.0), 5.0));
120 /* Computes the full contribution of a single light */
121 vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness)
123 vec3 halfway = normalize(light-look);
124 float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
125 float geom = geometry_smith(normal, -look, light, roughness);
127 vec3 k_spec = fresnel_schlick(halfway, light, base_color, metalness);
128 vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
130 float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
131 return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
134 vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
136 vec3 f0 = mix(vec3(0.04), base_color, metalness);
137 vec2 scale_bias = texture(fresnel_lookup, vec2(roughness, max(dot(normal, -look), 0.0))).rg;
138 vec3 k_spec = f0*scale_bias.x+scale_bias.y;
139 vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
141 if(use_image_based_lighting)
143 vec3 irradiance = get_irradiance_sample(normal);
144 vec3 reflection = get_environment_sample(reflect(look, normal), roughness).rgb;
146 return k_diff*irradiance*base_color+k_spec*reflection;
149 return (k_diff*base_color+k_spec)*ambient_color.rgb;
152 vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
154 vec3 color = vec3(0.0);
155 for(int i=0; i<max_lights; ++i)
156 if(light_sources[i].type!=0)
158 IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
159 float shadow = get_shadow_factor(i, world_vertex);
160 color += cooktorrance_one_light_direct(normal, look, incoming.direction, base_color, metalness, roughness)*incoming.color*shadow;
163 color += cooktorrance_environment(normal, look, base_color, metalness, roughness);
165 color *= get_occlusion_value();
168 color += get_emission_color();
175 vec3 normal = get_fragment_normal();
176 vec3 look = normalize(world_look_dir);
178 vec4 base_color = get_base_color();
179 float metalness = get_metalness_value();
180 float roughness = get_roughness_value();
182 vec3 lit_color = cooktorrance_lighting(normal, look, base_color.rgb, metalness, roughness);
184 frag_color = vec4(lit_color, base_color.a);