6 struct PbrMaterialParameters
14 layout(set=1) uniform PbrMaterial
16 PbrMaterialParameters pbr_material;
20 layout(set=1) uniform sampler2D base_color_map;
21 layout(set=1) uniform sampler2D metalness_map;
22 layout(set=1) uniform sampler2D roughness_map;
23 layout(set=1) uniform sampler2D occlusion_map;
24 layout(set=1) uniform sampler2D emission_map;
25 layout(set=1) uniform sampler2D fresnel_lookup;
27 layout(constant_id=auto) const bool use_base_color_map = false;
28 layout(constant_id=auto) const bool use_metalness_map = false;
29 layout(constant_id=auto) const bool use_roughness_map = false;
30 layout(constant_id=auto) const bool use_occlusion_map = false;
31 layout(constant_id=auto) const bool use_emission = false;
32 layout(constant_id=auto) const bool use_emission_map = false;
33 layout(constant_id=auto) const bool use_image_based_lighting = false;
34 layout(constant_id=auto) const bool use_alpha_cutoff = false;
36 #pragma MSP stage(fragment)
37 virtual vec4 get_base_color()
39 if(use_base_color_map)
40 return texture(base_color_map, texcoord.xy);
42 return pbr_material.base_color;
45 virtual float get_metalness_value()
48 return texture(metalness_map, texcoord.xy).r;
50 return pbr_material.metalness;
53 virtual float get_roughness_value()
56 return texture(roughness_map, texcoord.xy).r;
58 return pbr_material.roughness;
61 virtual float get_occlusion_value()
64 return texture(occlusion_map, texcoord.xy).r;
69 virtual vec3 get_emission_color()
72 return texture(emission_map, texcoord.xy).rgb;
74 return pbr_material.emission.rgb;
77 /* Computes the diffuse reflection of the macrosurface */
78 vec3 lambert_diffuse(vec3 base_color)
80 /* Scale by pi (cosine-weighted area of a hemisphere) because the light
81 scatters in every direction */
85 /* Computes the fraction of microfacets aligned at the halfway vector
86 (Trowbridge-Reitz GGX) */
87 float normal_distribution_ggxtr(vec3 normal, vec3 halfway, float roughness)
89 float n_dot_h = max(dot(normal, halfway), 0.0);
90 float rough_q = roughness * roughness;
92 float denom = n_dot_h*n_dot_h*(rough_q-1)+1;
93 /* Scale by pi to normalize the total area of the microfacets as projected
94 to the macrosurface */
95 return rough_q/(PI*denom*denom);
98 /* Computes shadowing and masking of a microfacet surface from a given
100 float geometry_schlick_ggx(vec3 normal, vec3 view, float k)
102 float n_dot_v = max(dot(normal, view), 0.0);
103 return n_dot_v/(n_dot_v*(1.0-k)+k);
106 /* Computes shadowing and masking of a microfacet surface for a combination of
107 look and light directions */
108 float geometry_smith(vec3 normal, vec3 look, vec3 light, float roughness)
110 float k = (roughness+1.0)*(roughness+1.0)/8.0;
111 float ggx_look = geometry_schlick_ggx(normal, look, k);
112 float ggx_light = geometry_schlick_ggx(normal, light, k);
113 return ggx_look*ggx_light;
116 /* Computes the reflectance of the material at a given reflection angle */
117 vec3 fresnel_schlick(vec3 halfway, vec3 look, vec3 base_color, float metalness)
119 // 0.04 is a decent approximation for dielectric base reflectivity
120 vec3 f0 = mix(vec3(0.04), base_color, metalness);
121 return mix(f0, vec3(1.0), pow(max(1.0-dot(halfway, look), 0.0), 5.0));
124 /* Computes the full contribution of a single light */
125 vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness)
127 vec3 halfway = normalize(light-look);
128 float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
129 float geom = geometry_smith(normal, -look, light, roughness);
131 vec3 k_spec = fresnel_schlick(halfway, light, base_color, metalness);
132 vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
134 float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
135 return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
138 vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
140 vec3 f0 = mix(vec3(0.04), base_color, metalness);
141 vec2 scale_bias = texture(fresnel_lookup, vec2(roughness, max(dot(normal, -look), 0.0))).rg;
142 vec3 k_spec = f0*scale_bias.x+scale_bias.y;
143 vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
145 if(use_image_based_lighting)
147 vec3 irradiance = get_irradiance_sample(normal);
148 vec3 reflection = get_environment_sample(reflect(look, normal), roughness).rgb;
150 return k_diff*irradiance*base_color+k_spec*reflection;
153 return (k_diff*base_color+k_spec)*ambient_color.rgb;
156 vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
158 vec3 color = vec3(0.0);
159 for(int i=0; i<max_lights; ++i)
160 if(light_sources[i].type!=0)
162 IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
163 float shadow = get_shadow_factor(i, world_vertex);
164 color += cooktorrance_one_light_direct(normal, look, incoming.direction, base_color, metalness, roughness)*incoming.color*shadow;
167 color += cooktorrance_environment(normal, look, base_color, metalness, roughness);
169 color *= get_occlusion_value();
172 color += get_emission_color();
179 vec4 base_color = get_base_color();
180 if(use_alpha_cutoff && base_color.a<alpha_cutoff)
183 vec3 normal = get_fragment_normal();
184 vec3 look = normalize(world_look_dir);
186 float metalness = get_metalness_value();
187 float roughness = get_roughness_value();
189 vec3 lit_color = cooktorrance_lighting(normal, look, base_color.rgb, metalness, roughness);
191 frag_color = vec4(lit_color, base_color.a);