uniform sampler2D blockPBRProperties;
const int NUM_BLOCK_TYPES = 256;
const float GLOSS_SCALE = 0.7; // Global glossiness reduction
const float MIN_ROUGHNESS = 0.1; // Minimum roughness to prevent mirror-like surfaces

// Block ID ranges (after subtracting 10000 from block.properties IDs)
const int STONE_BLOCK_START = 20300; // Stone-like blocks (sandstone, deepslate, etc.)
const int STONE_BLOCK_END = 20309;
const int DIRT_BLOCK_START = 30000; // Dirt-like blocks (dirt, clay, mud, etc.)
const int DIRT_BLOCK_END = 30002;
const int WOOD_BLOCK_START = 20314; // Wood-like blocks (planks, logs, etc.)
const int WOOD_BLOCK_END = 20314;
const int PLANT_BLOCK_START = 10002; // Plant-like blocks (leaves, grass, etc.)
const int PLANT_BLOCK_END = 10059;
const int MISC_BLOCK_START = 20151; // Miscellaneous blocks (shulker boxes, chests, etc.)
const int MISC_BLOCK_END = 20257;
const int FOLIAGE_BLOCK_START = 10304; // Foliage blocks (grass, leaves, etc.)
const int FOLIAGE_BLOCK_END = 10319;
const int TINTED_GLASS_START = 10201; // Tinted glass blocks
const int TINTED_GLASS_END = 10216;
const int EMISSIVE_BLOCK_START = 10003; // Emissive blocks (glowstone, torches, etc.)
const int EMISSIVE_BLOCK_END = 10071;
const int METALLIC_BLOCK_START = 20301; // Metallic blocks (iron, gold, copper, etc.)
const int METALLIC_BLOCK_END = 20301;
const int TRANSLUCENT_BLOCK_START = 10000; // Translucent blocks (glass, ice, etc.)
const int TRANSLUCENT_BLOCK_END = 10002;
const int CORAL_BLOCK_START = 10072; // Coral blocks
const int CORAL_BLOCK_END = 10072;
const int CORAL_FAN_START = 10074; // Coral fans
const int CORAL_FAN_END = 10074;
const int WOOL_BLOCK_START = 10073; // Wool blocks
const int WOOL_BLOCK_END = 10073;
const int ORE_BLOCK_START = 10080; // Ore blocks (emerald, diamond, etc.)
const int ORE_BLOCK_END = 10080;
const int DEFAULT_BLOCK_START = 20000; // Default non-emissive blocks
const int DEFAULT_BLOCK_END = 20000;

void generateIPBR(inout vec4 albedo, in vec3 worldPos, in vec3 viewPos, inout vec2 lightmap, inout float emission, inout float smoothness, inout float metalness, inout float subsurface) {
    // Initialize default values
    smoothness = 0.0;
    metalness = 0.0;
    emission = 0.0;
    subsurface = 0.0;

    // Get material ID and texture coordinate
    int material = max(mat - 10000, 0);
    float texCoord = float(material) / float(NUM_BLOCK_TYPES);
    vec4 pbr = texture(blockPBRProperties, vec2(texCoord, 0.5));

    // Extract PBR properties
    smoothness = pbr.r;
    metalness = pbr.g;
    emission = pbr.b;
    subsurface = pbr.a;

    // Convert smoothness to roughness and apply scaling
    float roughness = 1.0 - (smoothness * GLOSS_SCALE);
    roughness = max(roughness, MIN_ROUGHNESS); // Prevent mirror-like surfaces
    smoothness = 1.0 - roughness; // Convert back to smoothness

    // Apply non-linear mapping for more natural appearance
    smoothness = pow(smoothness, 1.5);

    // Material-specific adjustments
    if (material >= 0 && material < NUM_BLOCK_TYPES) {
        // Stone-like blocks (sandstone, deepslate, prismarine, etc.)
        if (material >= STONE_BLOCK_START && material <= STONE_BLOCK_END) {
            metalness *= 0.1; // Minimal metalness for stone
            smoothness *= 0.4; // Low gloss for stone-like surfaces
            subsurface *= 0.1; // Minimal subsurface scattering
            emission = 0.0; // No emission
        }
        // Dirt-like blocks (dirt, clay, mud, podzol, etc.)
        else if (material >= DIRT_BLOCK_START && material <= DIRT_BLOCK_END) {
            metalness *= 0.05; // Almost no metalness for dirt
            smoothness *= 0.3; // Very low gloss for dirt-like surfaces
            subsurface *= 0.2; // Low subsurface scattering
            emission = 0.0; // No emission
        }
        // Wood-like blocks (planks, logs, stripped logs, etc.)
        else if (material >= WOOD_BLOCK_START && material <= WOOD_BLOCK_END) {
            metalness *= 0.1; // Minimal metalness for wood
            smoothness *= 0.5; // Moderate gloss for wood
            subsurface *= 0.15; // Low subsurface scattering
            emission = 0.0; // No emission
        }
        // Plant-like blocks (leaves, grass, kelp, etc.)
        else if (material >= PLANT_BLOCK_START && material <= PLANT_BLOCK_END || material >= FOLIAGE_BLOCK_START && material <= FOLIAGE_BLOCK_END) {
            metalness *= 0.05; // No metalness for plants
            smoothness *= 0.3; // Low gloss for plants
            subsurface = min(subsurface * 0.5, 0.7); // Moderate subsurface for translucency
            emission = 0.0; // No emission unless specified
        }
        // Miscellaneous blocks (shulker boxes, chests, crafting tables, etc.)
        else if (material >= MISC_BLOCK_START && material <= MISC_BLOCK_END) {
            metalness *= 0.1; // Low metalness for misc blocks
            smoothness *= 0.5; // Moderate gloss for crafted blocks
            subsurface *= 0.2; // Low subsurface scattering
            emission = 0.0; // No emission
        }
        // Tinted glass blocks (stained glass, tinted glass)
        else if (material >= TINTED_GLASS_START && material <= TINTED_GLASS_END) {
            metalness = 0.0; // No metalness for glass
            smoothness = min(smoothness * 0.9, 0.95); // High smoothness for glass
            subsurface = min(subsurface * 1.2, 1.0); // High subsurface for translucency
            emission = 0.0; // No emission
        }
        // Emissive blocks (glowstone, torches, froglights, etc.)
        else if (material >= EMISSIVE_BLOCK_START && material <= EMISSIVE_BLOCK_END) {
            metalness *= 0.1; // Low metalness for emissive blocks
            smoothness *= 0.4; // Low to moderate gloss
            emission = clamp(pbr.b * 1.5, 0.0, 1.0); // Allow emission based on texture
            subsurface *= 0.3; // Low subsurface scattering
        }
        // Metallic blocks (iron, gold, copper, etc.)
        else if (material >= METALLIC_BLOCK_START && material <= METALLIC_BLOCK_END) {
            metalness = min(metalness * 1.5, 1.0); // High metalness for metallic blocks
            smoothness = min(smoothness * 0.8, 0.9); // Moderate to high gloss
            subsurface = 0.0; // No subsurface scattering
            emission = 0.0; // No emission
        }
        // Translucent blocks (glass, ice, etc.)
        else if (material >= TRANSLUCENT_BLOCK_START && material <= TRANSLUCENT_BLOCK_END) {
            metalness = 0.0; // No metalness for translucent blocks
            smoothness = min(smoothness * 0.9, 0.95); // High smoothness for glass/ice
            subsurface = min(subsurface * 1.2, 1.0); // High subsurface for translucency
            emission = 0.0; // No emission
        }
        // Coral blocks
        else if (material >= CORAL_BLOCK_START && material <= CORAL_BLOCK_END) {
            metalness *= 0.05; // Minimal metalness for coral
            smoothness *= 0.4; // Low to moderate gloss for coral
            subsurface = min(subsurface * 0.6, 0.8); // Moderate subsurface for organic look
            emission = 0.0; // No emission
        }
        // Coral fans
        else if (material >= CORAL_FAN_START && material <= CORAL_FAN_END) {
            metalness *= 0.05; // Minimal metalness for coral fans
            smoothness *= 0.3; // Low gloss for coral fans
            subsurface = min(subsurface * 0.5, 0.7); // Moderate subsurface for organic look
            emission = 0.0; // No emission
        }
        // Wool blocks
        else if (material >= WOOL_BLOCK_START && material <= WOOL_BLOCK_END) {
            metalness *= 0.0; // No metalness for wool
            smoothness *= 0.2; // Very low gloss for wool
            subsurface *= 0.3; // Low subsurface scattering for fabric
            emission = 0.0; // No emission
        }
        // Ore blocks
        else if (material >= ORE_BLOCK_START && material <= ORE_BLOCK_END) {
            metalness = min(metalness * 0.3, 0.5); // Moderate metalness for ores
            smoothness *= 0.6; // Moderate gloss for ores
            subsurface *= 0.1; // Minimal subsurface scattering
            emission = 0.0; // No emission
        }
        // Default blocks (all other non-emissive blocks)
        else if (material >= DEFAULT_BLOCK_START && material <= DEFAULT_BLOCK_END) {
            metalness *= 0.0; // Low metalness for default blocks
            smoothness *= 0.5; // Moderate gloss for general blocks
            subsurface *= 1.0; // Low subsurface scattering
            emission = 0.0; // No emission
        }
    }

    // Clamp values to prevent invalid results
    smoothness = clamp(smoothness, 0.0, 1.0);
    metalness = clamp(metalness, 0.0, 1.0);
    emission = clamp(emission, 0.0, 0.0); // Allow emission for emissive blocks
    subsurface = clamp(subsurface, 0.0, 1.0);
}
