#ifdef DESC [program] type: graphics vertex: main fragment: main [binding] name: mesh rate: vertex [attribute] name: position type: vec3 [attribute] name: normal type: vec3 [attribute] name: tangent type: vec3 [attribute] name: uv type: vec2 [interpolator] name: uv type: vec2 [interpolator] name: position type: vec4 [interpolator] name: tbn type: mat3 [struct] name: MVP [variable] name: model type: mat4 [variable] name: view_projection type: mat4 [struct] name: Material [variable] name: albedo type: vec3 [variable] name: metalness type: float [variable] name: roughness type: float [variable] name: ao type: float [variable] name: camera_pos type: vec3 [cbuffer] name: c_mvp type: MVP stage: vertex [cbuffer] name: material type: Material stage: fragment [texture] name: albedo stage: fragment dimension: 2 [texture] name: ao stage: fragment dimension: 2 [texture] name: metal stage: fragment dimension: 2 [texture] name: rough stage: fragment dimension: 2 [texture] name: normal stage: fragment dimension: 2 [texture] name: env_cube stage: fragment dimension: cube [target] name: colour type: vec4 #endif #ifdef VERTEX_SHADER void main() { vec4 pos = c_mvp.model * vec4(position, 1.0); vec3 t = normalize((c_mvp.model * vec4(tangent, 0.0)).xyz); vec3 n = normalize((c_mvp.model * vec4(normal, 0.0)).xyz); vec3 b = cross(t, n); interpolator.tbn = mat3(t, b, n); interpolator.uv = uv; interpolator.position = pos; interpolator.tbn = mat3(t, b, n); gl_Position = c_mvp.view_projection * pos; } #endif #ifdef FRAGMENT_SHADER #define pi 3.14159265358979323846 vec3 diffuse_brdf(vec2 uv) { vec3 a = material.albedo * texture(albedo, uv).rgb; return a / pi; } float specular_G1(float a, vec3 v, vec3 n) { float ndv = max(dot(n, v), 0.0); float a1 = a + 1.0; float k = (a1 * a1) / 8.0; return ndv / (ndv * (1.0 - k) + k); } float specular_brdf(vec2 uv, vec3 ref, vec3 l, vec3 v, vec3 n) { float ndl = max(dot(n, l), 0.0); float ndv = max(dot(n, v), 0.0); float a = texture(rough, uv).r * material.roughness; float a2 = a * a; float ndr = max(dot(n, ref), 0.0); float b = ((ndr * ndr) * (a2 - 1) + 1); float D = a2 / (pi * b * b); float G = specular_G1(a, l, n) * specular_G1(a, v, n); float F0 = 0.04; float F = F0 + (1.0 - F0) * pow(2, ((-5.55473 * ndr - 6.98316), ndr)); return (D * F * G) / (4.0 * ndl * ndv + 0.001); } void main() { vec2 uv = interpolator.uv; vec3 p = interpolator.position.xyz; vec3 nrmsample = texture(normal, uv).rgb; vec2 nrmxy = nrmsample.xy * 2.0 - 1.0; vec3 nrm = normalize(vec3(nrmxy, 1.0)); if (nrmsample.b == 1.0) /* default texture */ nrm = normalize(interpolator.tbn[2]); else nrm = normalize(interpolator.tbn * nrm); vec3 light_dir = vec3(0, 0, 1); vec3 col = texture(albedo, uv).rgb * material.albedo; float cos_theta_i = max(dot(nrm, light_dir), 0.0); vec3 view_dir = normalize(material.camera_pos - p); float met = texture(metal, uv).r * material.metalness; vec3 ref = reflect(-view_dir, nrm); vec3 spec_col = texture(env_cube, ref).rgb; vec3 amb_col = texture(env_cube, nrm).rgb; vec3 diffuse = diffuse_brdf(uv) * (1.0 - met) * cos_theta_i; vec3 spec = mix(spec_col, col, met) * specular_brdf(uv, ref, light_dir, view_dir, nrm) * cos_theta_i; vec3 ambient = min(amb_col, 0.05) * texture(ao, uv).r; colour = vec4(ambient + diffuse + spec, 1.0); } #endif