kinetic-slider
Version:
A WebGL-powered kinetic slider component using PIXI.js
81 lines (78 loc) • 2.48 kB
JavaScript
import { SimpleLightmapFilter } from 'pixi-filters';
import { Texture, Assets } from 'pixi.js';
import { ShaderResourceManager } from '../managers/ShaderResourceManager.js';
function createSimpleLightmapFilter(config) {
const shaderManager = ShaderResourceManager.getInstance();
let lightMapTexture = config.lightMap;
if (typeof config.lightMap === "string") {
lightMapTexture = Texture.EMPTY;
Assets.load(config.lightMap).then((texture) => {
if (filter) {
filter.lightMap = texture;
}
}).catch((error) => {
console.error(`Failed to load lightMap texture: ${config.lightMap}`, error);
});
}
let colorValue;
if (config.color !== void 0) {
if (typeof config.color === "number") {
colorValue = config.color;
}
}
const colorHex = (colorValue || 0).toString(16);
const shaderKey = `simple-lightmap-filter-${colorHex}`;
const filter = new SimpleLightmapFilter(
lightMapTexture,
// lightMap texture
colorValue || 0,
// ambient color as number (default black)
config.alpha
// alpha value
);
try {
shaderManager.registerFilter(filter, shaderKey);
} catch (error) {
console.warn("Error registering simple lightmap filter with shader manager:", error);
}
const updateIntensity = (intensity) => {
const normalizedIntensity = Math.max(0, Math.min(10, intensity));
if (config.primaryProperty) {
switch (config.primaryProperty) {
case "alpha":
filter.alpha = normalizedIntensity / 10;
break;
case "brightness":
filter.alpha = normalizedIntensity / 10;
break;
case "colorIntensity":
filter.alpha = normalizedIntensity / 10;
break;
default:
filter.alpha = normalizedIntensity / 10;
}
} else {
filter.alpha = normalizedIntensity / 10;
}
};
updateIntensity(config.intensity);
const reset = () => {
filter.alpha = config.alpha !== void 0 ? config.alpha : 1;
if (colorValue !== void 0) {
filter.color = colorValue;
} else {
filter.color = 0;
}
};
const dispose = () => {
try {
shaderManager.releaseFilter(filter, shaderKey);
} catch (error) {
console.warn("Error releasing simple lightmap filter shader:", error);
}
filter.destroy();
};
return { filter, updateIntensity, reset, dispose };
}
export { createSimpleLightmapFilter };
//# sourceMappingURL=simpleLightmapFilter.js.map