import { applyMaterialMixins } from '../../meshmaterial.js';
import { mixinLight } from '../lit.js';
import { LIGHT_TYPE_POINT } from '../../../values.js';

/**
 * Lambert lighting model mixin
 * @param BaseCls - Class to mix in
 * @returns Mixed class
 * @public
 */ function mixinLambert(BaseCls) {
    if (BaseCls.lambertMixed) {
        return BaseCls;
    }
    const S = applyMaterialMixins(BaseCls, mixinLight);
    return class extends S {
        static lambertMixed = true;
        constructor(){
            super();
        }
        lambertLight(scope, worldPos, normal, albedo) {
            const pb = scope.$builder;
            const funcName = 'Z_lambertLight';
            const that = this;
            pb.func(funcName, [
                pb.vec3('worldPos'),
                pb.vec3('normal'),
                pb.vec4('albedo')
            ], function() {
                if (!that.needFragmentColor()) {
                    this.$return(this.albedo.rgb);
                } else {
                    if (that.needCalculateEnvLight()) {
                        this.$l.diffuseColor = that.getEnvLightIrradiance(this, this.normal);
                    } else {
                        this.$l.diffuseColor = pb.vec3(0);
                    }
                    that.forEachLight(this, function(type, posRange, dirCutoff, colorIntensity, extra, shadow) {
                        this.$l.diffuseScale = pb.float(1);
                        this.$if(pb.equal(type, LIGHT_TYPE_POINT), function() {
                            this.diffuseScale = extra.x;
                        });
                        this.$l.lightAtten = that.calculateLightAttenuation(this, type, this.worldPos, posRange, dirCutoff);
                        this.$l.lightDir = that.calculateLightDirection(this, type, this.worldPos, posRange, dirCutoff);
                        this.$l.NoL = pb.clamp(pb.dot(this.normal, this.lightDir), 0, 1);
                        this.$l.lightColor = pb.mul(colorIntensity.rgb, colorIntensity.a, this.lightAtten, this.diffuseScale);
                        this.$l.diffuse = pb.mul(this.lightColor, 1 / Math.PI, this.NoL);
                        if (shadow) {
                            this.$l.shadow = pb.vec3(that.calculateShadow(this, this.worldPos, this.NoL));
                            this.diffuse = pb.mul(this.diffuse, this.shadow);
                        }
                        this.diffuseColor = pb.add(this.diffuseColor, this.diffuse);
                    });
                    this.$l.litColor = pb.mul(this.albedo.rgb, this.diffuseColor);
                    this.$return(this.litColor);
                }
            });
            return pb.getGlobalScope()[funcName](worldPos, normal, albedo);
        }
    };
}

export { mixinLambert };
//# sourceMappingURL=lambert.js.map
