// Integrated PBR system adapted from Super Duper Vanilla
// Automatically determines PBR properties based on block ID and albedo color

void applyIntegratedPBR(inout vec4 specularData, inout vec3 albedo, in uint matID, in vec3 albedoRGB) {
    float albedoSum = albedoRGB.r + albedoRGB.g + albedoRGB.b;
    float maxAlbedo = max(max(albedoRGB.r, albedoRGB.g), albedoRGB.b);
    
    // === FLOWERS AND PLANTS ===
    // Glow berries (10001)
    if (matID == 10001u) {
        if (albedoRGB.r + albedoRGB.g > albedoRGB.g * 2.0) {
            float emissive = smoothstep(0.3, 0.9, maxAlbedo);
            specularData.b = emissive; // emissive channel
        }
    }
    
    // Fungus (10002)
    else if (matID == 10002u) {
        if (albedoRGB.r + albedoRGB.g > 1.0) {
            specularData.b = 1.0;
        }
    }
    
    // Flowers - torch flower (10003)
    else if (matID == 10003u) {
        float emissive = albedoRGB.r * albedoRGB.r * 0.5;
        specularData.b = emissive;
    }
    
    // === LIGHT EMITTING BLOCKS ===
    // Frog lights (12000)
    else if (matID == 12000u) {
        float emissive = pow(max(0.0, albedoSum * 1.33333332 - 3.0), 3.0);
        specularData.r = 0.9; // smoothness
        specularData.b = emissive;
    }
    
    // Reflective light emitting blocks and redstone lamps (12001)
    else if (matID == 12001u) {
        float emissive = clamp(albedoSum * 1.33333332 - 2.0, 0.0, 1.0);
        specularData.r = 0.9;
        specularData.b = emissive;
    }
    
    // Creaking heart (12002)
    else if (matID == 12002u) {
        float rg = (albedoRGB.r + albedoRGB.g) * 0.66666667;
        float emissive = clamp(rg * rg - albedoRGB.b, 0.0, 1.0);
        specularData.b = emissive;
    }
    
    // Redstone block (12003)
    else if (matID == 12003u) {
        specularData.r = 0.9 * sqrt(albedoRGB.r) * 0.1999 + 0.4; // smoothness
        specularData.g = 1.0; // metallic
        specularData.b = albedoRGB.r * 0.25; // emissive
    }
    
    // === METAL BLOCKS ===
    // Dark metals (12004)
    else if (matID == 12004u) {
        specularData.r = albedoSum * 0.1999 + 0.4;
        specularData.g = 1.0;
    }
    
    // Metal blocks (12005)
    else if (matID == 12005u) {
        specularData.r = albedoSum * 0.3 + 0.06;
        specularData.g = 1.0;
    }
    
    // Copper blocks (12006-12007)
    else if (matID >= 12006u && matID <= 12007u) {
        specularData.r = max(albedoRGB.r, albedoRGB.g) * 0.96;
        specularData.g = 1.0;
        
        // Copper bulbs (12007) with emission
        if (matID == 12007u) {
            float emission = max(0.0, mix(-0.45, 0.55, albedoRGB.r * albedoRGB.g));
            specularData.b = emission;
        }
    }
    
    // Iron blocks (12008-12009)
    else if (matID >= 12008u && matID <= 12009u) {
        specularData.r = albedoSum * 0.25 + 0.3;
        specularData.g = 1.0;
    }
    
    // Gold blocks (12010-12011)
    else if (matID >= 12010u && matID <= 12011u) {
        specularData.r = max(albedoRGB.r, albedoRGB.g) * 0.9 + 0.1;
        specularData.g = 1.0;
    }
    
    // === SMOOTH BLOCKS ===
    // Packed ice and blue ice (12015)
    else if (matID == 12015u) {
        specularData.r = 0.96;
    }
    
    // Crystal blocks (12016 - quartz, amethyst etc)
    else if (matID == 12016u) {
        specularData.r = sqrt(min(0.8, albedoSum));
        specularData.g = 0.17;
    }
    
    // Polished blocks (12017)
    else if (matID == 12017u) {
        specularData.r = maxAlbedo * 0.9;
    }
    
    // Obsidian (12018)
    else if (matID == 12018u) {
        specularData.r = 0.85;
        specularData.g = 0.12;
    }
    
    // Barrel (12019)
    else if (matID == 12019u) {
        specularData.r = 0.35;
        specularData.g = 0.06;
    }
    
    // Chest (12020)
    else if (matID == 12020u) {
        specularData.r = 0.32;
        specularData.g = 0.05;
    }
    
    // Quartz (12021)
    else if (matID == 12021u) {
        specularData.r = 0.88;
        specularData.g = 0.15;
    }
    
    // === ORES ===
    // Crystals in stone (12022)
    else if (matID == 12022u) {
        // Detect crystal pixels
        if (albedoRGB.r > albedoRGB.g || albedoRGB.r != albedoRGB.b || albedoRGB.g > albedoRGB.b) {
            if (albedoSum > 0.75) {
                specularData.r = min(0.96, albedoSum * 0.5);
                specularData.g = 0.17;
            }
        }
    }
    
    // Crystals in netherrack (12023)
    else if (matID == 12023u) {
        if (albedoRGB.r < albedoRGB.g * 1.6 && albedoRGB.r < albedoRGB.b * 1.6) {
            specularData.r = min(0.93, albedoSum * 0.5);
            specularData.g = 0.17;
        }
    }
    
    // Metal ores in stone (12024)
    else if (matID == 12024u) {
        if (albedoRGB.r > albedoRGB.g || albedoRGB.r != albedoRGB.b || albedoRGB.g > albedoRGB.b) {
            if (albedoSum > 0.75) {
                specularData.r = max(albedoRGB.r, albedoRGB.g) * 0.96;
                specularData.g = 1.0;
            }
        }
    }
    
    // Metal ores in netherrack (12025)
    else if (matID == 12025u) {
        if (max(albedoRGB.r, albedoRGB.g) > 0.6) {
            specularData.r = max(albedoRGB.r, albedoRGB.g) * 0.96;
            specularData.g = 1.0;
        }
    }
    
    // Gilded blackstone (12026)
    else if (matID == 12026u) {
        if (max(albedoRGB.r, albedoRGB.g) > albedoRGB.b * 2.0) {
            specularData.r = max(albedoRGB.r, albedoRGB.g) * 0.96;
            specularData.g = 1.0;
        }
    }
    
    // === PYRO EMISSIVES ===
    // Magma block (12027)
    else if (matID == 12027u) {
        float brightness = max(0.0, maxAlbedo - 0.75) * 4.0;
        brightness = brightness * (2.0 - brightness); // smoothen
        specularData.b = brightness;
    }
    
    // Fire/lava blocks (12028)
    else if (matID == 12028u) {
        float emissive = albedoSum * 0.83333333 - 1.0;
        emissive = clamp(emissive, 0.0, 1.0);
        emissive = emissive * emissive;
        emissive = emissive * emissive;
        specularData.b = emissive;
    }
    
    // === REDSTONE COMPONENTS ===
    // Redstone components (12029)
    else if (matID == 12029u) {
        if (albedoRGB.r > albedoRGB.b * 2.4) {
            specularData.r = 0.9;
            specularData.g = 1.0;
            specularData.b = (albedoRGB.r > 0.5) ? 0.5 : 0.0;
        }
    }
    
    // Rails (12030)
    else if (matID == 12030u) {
        if (albedoRGB.r < albedoRGB.g * 1.6 && albedoRGB.r < albedoRGB.b * 1.6) {
            specularData.r = albedoSum * 0.32;
            specularData.g = 1.0;
        }
    }
    
    // === BIOLUMINESCENT ===
    // Glow lichen (12031)
    else if (matID == 12031u) {
        specularData.b = (albedoRGB.r > albedoRGB.b) ? 1.0 : 0.0;
    }
    
    // Warped stems (12032)
    else if (matID == 12032u) {
        if (albedoRGB.r < 0.1) {
            specularData.b = maxAlbedo * 0.72;
        }
    }
    
    // Crimson stems (12033)
    else if (matID == 12033u) {
        if (albedoRGB.b < 0.16 && albedoRGB.r > 0.4) {
            specularData.b = maxAlbedo * 0.72;
        }
    }
    
    // Chorus plant (12034)
    else if (matID == 12034u) {
        if (albedoRGB.r + albedoRGB.g > 1.1) {
            specularData.b = 1.0;
        }
    }
    
    // Chorus flower (12035)
    else if (matID == 12035u) {
        float sum_gb = albedoRGB.g + albedoRGB.b;
        specularData.b = exp(sum_gb * 8.0 - 16.0);
    }
    
    // Common sculk (12036)
    else if (matID == 12036u) {
        specularData.r = 0.45;
        float blue_cubed = albedoRGB.b * albedoRGB.b * albedoRGB.b;
        specularData.b = blue_cubed;
    }
    
    // Uncommon sculk (12037)
    else if (matID == 12037u) {
        specularData.r = 0.45;
        float blue_cubed = albedoRGB.b * albedoRGB.b * albedoRGB.b;
        specularData.b = blue_cubed;
    }
    
    // === CRYSTAL BLOCKS ===
    // Beacon (12038)
    else if (matID == 12038u) {
        specularData.b = exp(albedoSum * 2.66666664 - 8.0);
    }
    
    // End portal frame (12039)
    else if (matID == 12039u) {
        if (albedoRGB.g + albedoRGB.b > albedoRGB.r * 2.0) {
            float green_blue_diff = (albedoRGB.g - albedoRGB.b) * 4.0;
            green_blue_diff = clamp(green_blue_diff, 0.0, 1.0);
            specularData.b = green_blue_diff * green_blue_diff;
        }
    }
    
    // Crying obsidian (12040)
    else if (matID == 12040u) {
        specularData.r = sqrt(min(0.8, albedoSum));
        specularData.g = 0.17;
        float max_cubed = maxAlbedo * maxAlbedo * maxAlbedo;
        specularData.b = max_cubed;
    }
    
    // Amethyst blocks (12041-12042)
    else if (matID >= 12041u && matID <= 12042u) {
        float amethystAverage = albedoSum * 0.33333333;
        specularData.r = amethystAverage * 0.6 + 0.3;
        specularData.g = 0.17;
        
        float amethystLumaSquared = amethystAverage * amethystAverage;
        float amethystLumaSquaredSquared = amethystLumaSquared * amethystLumaSquared;
        
        if (matID == 12041u) {
            // Budding amethyst - more emissive
            specularData.b = amethystLumaSquaredSquared * amethystLumaSquared * amethystLumaSquaredSquared;
        } else {
            // Regular amethyst cluster
            specularData.b = amethystLumaSquaredSquared * amethystAverage;
        }
    }
    
    // Copper bulbs ON/OFF states (2048-2055)
    else if (matID >= 2048u && matID <= 2055u) {
        // Determine oxidation level (0-3)
        int oxidationLevel = int((matID - 2048u) / 2);
        bool isOn = (matID % 2u) == 0u;
        
        // Base properties by oxidation
        if (oxidationLevel == 0) { // Clean
            specularData.r = 0.85;
            specularData.g = 0.92;
        } else if (oxidationLevel == 1) { // Exposed
            specularData.r = 0.68;
            specularData.g = 0.85;
        } else if (oxidationLevel == 2) { // Weathered
            specularData.r = 0.48;
            specularData.g = 0.70;
        } else { // Oxidized
            specularData.r = 0.28;
            specularData.g = 0.50;
        }
        
        // Apply emission if ON
        if (isOn) {
            float emission = max(0.0, mix(-0.45, 0.55, albedoRGB.r * albedoRGB.g));
            specularData.b = emission * 0.9;
        }
    }
    
    // Lightning rod (12064)
    else if (matID == 12064u) {
        specularData.r = 0.88;
        specularData.g = 1.0;
    }
    
    // === WOOD PLANKS (12068-12075) ===
    else if (matID >= 12068u && matID <= 12075u) {
        // Base wood properties
        specularData.r = 0.28;
        specularData.g = 0.08;
        
        // Adjust by wood type
        if (matID == 12069u) { // Birch
            specularData.r = 0.32;
        } else if (matID == 12074u) { // Cherry
            specularData.r = 0.35;
        } else if (matID == 12075u) { // Pale oak
            specularData.r = 0.38;
        } else if (matID == 12072u) { // Dark oak
            specularData.r = 0.22;
            specularData.g = 0.06;
        }
    }
    
    // Chorus plant detailed (12088)
    else if (matID == 12088u) {
        if (albedoRGB.r + albedoRGB.g > 1.1) {
            specularData.b = 1.0;
        }
    }
    
    // === COPPER LANTERNS (13060-13067) ===
    else if (matID >= 13060u && matID <= 13067u) {
        int oxidationLevel = int(matID - 13060u);
        
        if (oxidationLevel == 0 || oxidationLevel == 1) { // Clean copper
            specularData.r = 0.85;
            specularData.g = 0.92;
        } else if (oxidationLevel == 2 || oxidationLevel == 3) { // Exposed
            specularData.r = 0.68;
            specularData.g = 0.85;
        } else if (oxidationLevel == 4 || oxidationLevel == 5) { // Weathered
            specularData.r = 0.48;
            specularData.g = 0.70;
        } else { // Oxidized
            specularData.r = 0.28;
            specularData.g = 0.50;
        }
        
        // Lanterns emit light
        specularData.b = 0.85;
    }
    
    // === COLORED CONCRETE (13070-13075) ===
    // Yellow concrete (13070)
    else if (matID == 13070u) {
        specularData.r = 0.65;
        specularData.g = 0.12;
    }
    // Red concrete (13071)
    else if (matID == 13071u) {
        specularData.r = 0.62;
        specularData.g = 0.10;
    }
    // Purple concrete (13072)
    else if (matID == 13072u) {
        specularData.r = 0.68;
        specularData.g = 0.11;
    }
    // Blue concrete (13073)
    else if (matID == 13073u) {
        specularData.r = 0.70;
        specularData.g = 0.13;
    }
    // Cyan concrete (13074)
    else if (matID == 13074u) {
        specularData.r = 0.72;
        specularData.g = 0.14;
    }
    // Pink concrete (13075)
    else if (matID == 13075u) {
        specularData.r = 0.66;
        specularData.g = 0.12;
    }
}
