Team Ai
Apppublic

OpenMotionLab/MotionGPT

sourceHugging Facemitupdated 1y agoView on Hugging Face
118likes
mesh.frag457 linesDownload Raw Back to shaders
1#version 330 core2///////////////////////////////////////////////////////////////////////////////3// Structs4///////////////////////////////////////////////////////////////////////////////5 6struct SpotLight {7    vec3 color;8    float intensity;9    float range;10    vec3 position;11    vec3 direction;12    float light_angle_scale;13    float light_angle_offset;14 15    #ifdef SPOT_LIGHT_SHADOWS16    sampler2D shadow_map;17    mat4 light_matrix;18    #endif19};20 21struct DirectionalLight {22    vec3 color;23    float intensity;24    vec3 direction;25 26    #ifdef DIRECTIONAL_LIGHT_SHADOWS27    sampler2D shadow_map;28    mat4 light_matrix;29    #endif30};31 32struct PointLight {33    vec3 color;34    float intensity;35    float range;36    vec3 position;37 38    #ifdef POINT_LIGHT_SHADOWS39    samplerCube shadow_map;40    #endif41};42 43struct Material {44    vec3 emissive_factor;45 46#ifdef USE_METALLIC_MATERIAL47    vec4 base_color_factor;48    float metallic_factor;49    float roughness_factor;50#endif51 52#ifdef USE_GLOSSY_MATERIAL53    vec4 diffuse_factor;54    vec3 specular_factor;55    float glossiness_factor;56#endif57 58#ifdef HAS_NORMAL_TEX59    sampler2D normal_texture;60#endif61#ifdef HAS_OCCLUSION_TEX62    sampler2D occlusion_texture;63#endif64#ifdef HAS_EMISSIVE_TEX65    sampler2D emissive_texture;66#endif67#ifdef HAS_BASE_COLOR_TEX68    sampler2D base_color_texture;69#endif70#ifdef HAS_METALLIC_ROUGHNESS_TEX71    sampler2D metallic_roughness_texture;72#endif73#ifdef HAS_DIFFUSE_TEX74    sampler2D diffuse_texture;75#endif76#ifdef HAS_SPECULAR_GLOSSINESS_TEX77    sampler2D specular_glossiness;78#endif79};80 81struct PBRInfo {82    float nl;83    float nv;84    float nh;85    float lh;86    float vh;87    float roughness;88    float metallic;89    vec3 f0;90    vec3 c_diff;91};92 93///////////////////////////////////////////////////////////////////////////////94// Uniforms95///////////////////////////////////////////////////////////////////////////////96uniform Material material;97uniform PointLight point_lights[MAX_POINT_LIGHTS];98uniform int n_point_lights;99uniform DirectionalLight directional_lights[MAX_DIRECTIONAL_LIGHTS];100uniform int n_directional_lights;101uniform SpotLight spot_lights[MAX_SPOT_LIGHTS];102uniform int n_spot_lights;103uniform vec3 cam_pos;104uniform vec3 ambient_light;105 106#ifdef USE_IBL107uniform samplerCube diffuse_env;108uniform samplerCube specular_env;109#endif110 111///////////////////////////////////////////////////////////////////////////////112// Inputs113///////////////////////////////////////////////////////////////////////////////114 115in vec3 frag_position;116#ifdef NORMAL_LOC117in vec3 frag_normal;118#endif119#ifdef HAS_NORMAL_TEX120#ifdef TANGENT_LOC121#ifdef NORMAL_LOC122in mat3 tbn;123#endif124#endif125#endif126#ifdef TEXCOORD_0_LOC127in vec2 uv_0;128#endif129#ifdef TEXCOORD_1_LOC130in vec2 uv_1;131#endif132#ifdef COLOR_0_LOC133in vec4 color_multiplier;134#endif135 136///////////////////////////////////////////////////////////////////////////////137// OUTPUTS138///////////////////////////////////////////////////////////////////////////////139 140out vec4 frag_color;141 142///////////////////////////////////////////////////////////////////////////////143// Constants144///////////////////////////////////////////////////////////////////////////////145const float PI = 3.141592653589793;146const float min_roughness = 0.04;147 148///////////////////////////////////////////////////////////////////////////////149// Utility Functions150///////////////////////////////////////////////////////////////////////////////151vec4 srgb_to_linear(vec4 srgb)152{153#ifndef SRGB_CORRECTED154    // Fast Approximation155    //vec3 linOut = pow(srgbIn.xyz,vec3(2.2));156    //157    vec3 b_less = step(vec3(0.04045),srgb.xyz);158    vec3 lin_out = mix( srgb.xyz/vec3(12.92), pow((srgb.xyz+vec3(0.055))/vec3(1.055),vec3(2.4)), b_less );159    return vec4(lin_out, srgb.w);160#else161    return srgb;162#endif163}164 165// Normal computation166vec3 get_normal()167{168#ifdef HAS_NORMAL_TEX169 170#ifndef HAS_TANGENTS171    vec3 pos_dx = dFdx(frag_position);172    vec3 pos_dy = dFdy(frag_position);173    vec3 tex_dx = dFdx(vec3(uv_0, 0.0));174    vec3 tex_dy = dFdy(vec3(uv_0, 0.0));175    vec3 t = (tex_dy.t * pos_dx - tex_dx.t * pos_dy) / (tex_dx.s * tex_dy.t - tex_dy.s * tex_dx.t);176 177#ifdef NORMAL_LOC178    vec3 ng = normalize(frag_normal);179#else180    vec3 = cross(pos_dx, pos_dy);181#endif182 183    t = normalize(t - ng * dot(ng, t));184    vec3 b = normalize(cross(ng, t));185    mat3 tbn_n = mat3(t, b, ng);186 187#else188 189    mat3 tbn_n = tbn;190 191#endif192 193    vec3 n = texture(material.normal_texture, uv_0).rgb;194    n = normalize(tbn_n * ((2.0 * n - 1.0) * vec3(1.0, 1.0, 1.0)));195    return n; // TODO NORMAL MAPPING196 197#else198 199#ifdef NORMAL_LOC200    return frag_normal;201#else202    return normalize(cam_pos - frag_position);203#endif204 205#endif206}207 208// Fresnel209vec3 specular_reflection(PBRInfo info)210{211     vec3 res = info.f0 + (1.0 - info.f0) * pow(clamp(1.0 - info.vh, 0.0, 1.0), 5.0);212     return res;213}214 215// Smith216float geometric_occlusion(PBRInfo info)217{218    float r = info.roughness + 1.0;219    float k = r * r  / 8.0;220    float g1 = info.nv / (info.nv * (1.0 - k) + k);221    float g2 = info.nl / (info.nl * (1.0 - k) + k);222    //float k = info.roughness * sqrt(2.0 / PI);223    //float g1 = info.lh / (info.lh * (1.0 - k) + k);224    //float g2 = info.nh / (info.nh * (1.0 - k) + k);225    return g1 * g2;226}227 228float microfacet_distribution(PBRInfo info)229{230    float a = info.roughness * info.roughness;231    float a2 = a * a;232    float nh2 = info.nh * info.nh;233 234    float denom = (nh2 * (a2 - 1.0) + 1.0);235    return a2 / (PI * denom * denom);236}237 238vec3 compute_brdf(vec3 n, vec3 v, vec3 l,239                  float roughness, float metalness,240                  vec3 f0, vec3 c_diff, vec3 albedo,241                  vec3 radiance)242{243        vec3 h = normalize(l+v);244        float nl = clamp(dot(n, l), 0.001, 1.0);245        float nv = clamp(abs(dot(n, v)), 0.001, 1.0);246        float nh = clamp(dot(n, h), 0.0, 1.0);247        float lh = clamp(dot(l, h), 0.0, 1.0);248        float vh = clamp(dot(v, h), 0.0, 1.0);249 250        PBRInfo info = PBRInfo(nl, nv, nh, lh, vh, roughness, metalness, f0, c_diff);251 252        // Compute PBR terms253        vec3 F = specular_reflection(info);254        float G = geometric_occlusion(info);255        float D = microfacet_distribution(info);256 257        // Compute BRDF258        vec3 diffuse_contrib = (1.0 - F) * c_diff / PI;259        vec3 spec_contrib = F * G * D / (4.0 * nl * nv + 0.001);260 261        vec3 color = nl * radiance * (diffuse_contrib + spec_contrib);262        return color;263}264 265float texture2DCompare(sampler2D depths, vec2 uv, float compare) {266    return compare > texture(depths, uv.xy).r ? 1.0 : 0.0;267}268 269float texture2DShadowLerp(sampler2D depths, vec2 size, vec2 uv, float compare) {270    vec2 texelSize = vec2(1.0)/size;271    vec2 f = fract(uv*size+0.5);272    vec2 centroidUV = floor(uv*size+0.5)/size;273 274    float lb = texture2DCompare(depths, centroidUV+texelSize*vec2(0.0, 0.0), compare);275    float lt = texture2DCompare(depths, centroidUV+texelSize*vec2(0.0, 1.0), compare);276    float rb = texture2DCompare(depths, centroidUV+texelSize*vec2(1.0, 0.0), compare);277    float rt = texture2DCompare(depths, centroidUV+texelSize*vec2(1.0, 1.0), compare);278    float a = mix(lb, lt, f.y);279    float b = mix(rb, rt, f.y);280    float c = mix(a, b, f.x);281    return c;282}283 284float PCF(sampler2D depths, vec2 size, vec2 uv, float compare){285    float result = 0.0;286    for(int x=-1; x<=1; x++){287        for(int y=-1; y<=1; y++){288            vec2 off = vec2(x,y)/size;289            result += texture2DShadowLerp(depths, size, uv+off, compare);290        }291    }292    return result/9.0;293}294 295float shadow_calc(mat4 light_matrix, sampler2D shadow_map, float nl)296{297    // Compute light texture UV coords298    vec4 proj_coords = vec4(light_matrix * vec4(frag_position.xyz, 1.0));299    vec3 light_coords = proj_coords.xyz / proj_coords.w;300    light_coords = light_coords * 0.5 + 0.5;301    float current_depth = light_coords.z;302    float bias = max(0.001 * (1.0 - nl), 0.0001) / proj_coords.w;303    float compare = (current_depth - bias);304    float shadow = PCF(shadow_map, textureSize(shadow_map, 0), light_coords.xy, compare);305    if (light_coords.z > 1.0) {306        shadow = 0.0;307    }308    return shadow;309}310 311///////////////////////////////////////////////////////////////////////////////312// MAIN313///////////////////////////////////////////////////////////////////////////////314void main()315{316 317    vec4 color = vec4(vec3(0.0), 1.0);318///////////////////////////////////////////////////////////////////////////////319// Handle Metallic Materials320///////////////////////////////////////////////////////////////////////////////321#ifdef USE_METALLIC_MATERIAL322 323    // Compute metallic/roughness factors324    float roughness = material.roughness_factor;325    float metallic = material.metallic_factor;326#ifdef HAS_METALLIC_ROUGHNESS_TEX327    vec2 mr = texture(material.metallic_roughness_texture, uv_0).rg;328    roughness = roughness * mr.r;329    metallic = metallic * mr.g;330#endif331    roughness = clamp(roughness, min_roughness, 1.0);332    metallic = clamp(metallic, 0.0, 1.0);333    // In convention, material roughness is perceputal roughness ^ 2334    float alpha_roughness = roughness * roughness;335 336    // Compute albedo337    vec4 base_color = material.base_color_factor;338#ifdef HAS_BASE_COLOR_TEX339    base_color = base_color * srgb_to_linear(texture(material.base_color_texture, uv_0));340#endif341 342    // Compute specular and diffuse colors343    vec3 dialectric_spec = vec3(min_roughness);344    vec3 c_diff = mix(vec3(0.0), base_color.rgb * (1 - min_roughness), 1.0 - metallic);345    vec3 f0 = mix(dialectric_spec, base_color.rgb, metallic);346 347    // Compute normal348    vec3 n = normalize(get_normal());349 350    // Loop over lights351    for (int i = 0; i < n_directional_lights; i++) {352        vec3 direction = directional_lights[i].direction;353        vec3 v = normalize(cam_pos - frag_position); // Vector towards camera354        vec3 l = normalize(-1.0 * direction);   // Vector towards light355 356        // Compute attenuation and radiance357        float attenuation = directional_lights[i].intensity;358        vec3 radiance = attenuation * directional_lights[i].color;359 360        // Compute outbound color361        vec3 res = compute_brdf(n, v, l, roughness, metallic,362                                f0, c_diff, base_color.rgb, radiance);363 364        // Compute shadow365#ifdef DIRECTIONAL_LIGHT_SHADOWS366        float nl = clamp(dot(n,l), 0.0, 1.0);367        float shadow = shadow_calc(368            directional_lights[i].light_matrix,369            directional_lights[i].shadow_map,370            nl371        );372        res = res * (1.0 - shadow);373#endif374        color.xyz += res;375    }376 377    for (int i = 0; i < n_point_lights; i++) {378        vec3 position = point_lights[i].position;379        vec3 v = normalize(cam_pos - frag_position); // Vector towards camera380        vec3 l = normalize(position - frag_position); // Vector towards light381 382        // Compute attenuation and radiance383        float dist = length(position - frag_position);384        float attenuation = point_lights[i].intensity / (dist * dist);385        vec3 radiance = attenuation * point_lights[i].color;386 387        // Compute outbound color388        vec3 res = compute_brdf(n, v, l, roughness, metallic,389                                f0, c_diff, base_color.rgb, radiance);390        color.xyz += res;391    }392    for (int i = 0; i < n_spot_lights; i++) {393        vec3 position = spot_lights[i].position;394        vec3 v = normalize(cam_pos - frag_position); // Vector towards camera395        vec3 l = normalize(position - frag_position); // Vector towards light396 397        // Compute attenuation and radiance398        vec3 direction = spot_lights[i].direction;399        float las = spot_lights[i].light_angle_scale;400        float lao = spot_lights[i].light_angle_offset;401        float dist = length(position - frag_position);402        float cd = clamp(dot(direction, -l), 0.0, 1.0);403        float attenuation = clamp(cd * las + lao, 0.0, 1.0);404        attenuation = attenuation * attenuation * spot_lights[i].intensity;405        attenuation = attenuation / (dist * dist);406        vec3 radiance = attenuation * spot_lights[i].color;407 408        // Compute outbound color409        vec3 res = compute_brdf(n, v, l, roughness, metallic,410                                f0, c_diff, base_color.rgb, radiance);411#ifdef SPOT_LIGHT_SHADOWS412        float nl = clamp(dot(n,l), 0.0, 1.0);413        float shadow = shadow_calc(414            spot_lights[i].light_matrix,415            spot_lights[i].shadow_map,416            nl417        );418        res = res * (1.0 - shadow);419#endif420        color.xyz += res;421    }422    color.xyz += base_color.xyz * ambient_light;423 424    // Calculate lighting from environment425#ifdef USE_IBL426    // TODO427#endif428 429    // Apply occlusion430#ifdef HAS_OCCLUSION_TEX431    float ao = texture(material.occlusion_texture, uv_0).r;432    color.xyz *= ao;433#endif434 435    // Apply emissive map436    vec3 emissive = material.emissive_factor;437#ifdef HAS_EMISSIVE_TEX438    emissive *= srgb_to_linear(texture(material.emissive_texture, uv_0)).rgb;439#endif440    color.xyz += emissive * material.emissive_factor;441 442#ifdef COLOR_0_LOC443    color *= color_multiplier;444#endif445 446    frag_color = clamp(vec4(pow(color.xyz, vec3(1.0/2.2)), color.a * base_color.a), 0.0, 1.0);447 448#else449    // TODO GLOSSY MATERIAL BRDF450#endif451 452///////////////////////////////////////////////////////////////////////////////453// Handle Glossy Materials454///////////////////////////////////////////////////////////////////////////////455 456}457