/** /*:
* @author TheUnpropro
* @plugindesc A thing for a dude on the inter webs
*
* @target MZ
*
* @command Chrom
* @text Chroma Level
* @desc Sets the intensity of the chroma effect
*
* @arg value
* @text Intensity
* @type number
* @decimals 2
* @default 2
* @desc Adjusts the intensity
*/
(function () {
const pi60 = Math.PI / 60;
PluginManager.registerCommand('chroma_color', "Chrom", args => {
args.value = parseFloat(args.value);
window.__ChromaIntensConfig(args.value);
});
window.__ChromaIntensConfig = function (value) {
try {
const oldData = JSON.parse(localStorage.getItem('chroma_data'));
oldData.intensity = value;
oldData.view[0] = Graphics._width;
oldData.view[1] = Graphics._height;
window._globalChromaFilter.uniforms.view[0] = Graphics._width;
window._globalChromaFilter.uniforms.view[1] = Graphics._height;
window._globalChromaFilter.uniforms.intensity = value;
SceneManager._scene._chromaFilter.uniforms.view = window._globalChromaFilter.uniforms.view;
SceneManager._scene._chromaFilter.uniforms.intensity = window._globalChromaFilter.uniforms.intensity;
console.log(oldData);
SceneManager._scene._writeChromaData(oldData);
} catch (e) {
throw e;
}
}
async function FilterFragShader(frag, src) {
try {
let ret = await Promise.all(frag.match(/\/\*\@file:(.*?)\*\//gm).map(async (match) => {
try {
const filename = match.replace('/*@file:', '').replace('*/', '').trim();
const fileContents = await fetch(`./data/filters/${src}/${filename}`).then(response => response.text());
return { match, replacement: fileContents };
} catch (error) {
console.error(`Error loading file ${filename}:`, error);
return { match, replacement: "" }; // Return empty string if file loading fails
}
}));
ret.forEach(({ match, replacement }) => {
frag = frag.replace(match, replacement);
});
} catch (e) {
}
return frag;
}
async function checkCompile(src, vert, frag) {
if (buildMode) {
const path = require('path');
const fs = require('fs');
const corePath = `./data/filters/${src}/`;
try {
const flags = await fetch(`${corePath}/flags.json`).then(f => f.json());
if (flags.compile.output_dir) {
try {
const mainPath = path.resolve(`${flags.compile.output_dir}/${src}`);
fs.mkdirSync(mainPath, { recursive: true });
if (flags.compile.vert) fs.writeFileSync(`${mainPath}/main.vert`, vert);
if (flags.compile.frag) fs.writeFileSync(`${mainPath}/main.frag`, frag);
} catch (e) {
throw e;
}
}
} catch (e) {
console.log(`No flags.json file set`)
}
}
}
const buildMode = false;
const __filterCache = {};
async function loadFilter(src) {
PIXI.settings.ROUND_PIXELS = true;
return new Promise((resolve, reject) => {
if (__filterCache[src]) resolve({ vert: __filterCache[src].vert, frag: __filterCache[src].frag })
var frag = "";
var vert = "";
fetch(`/js/plugins/filters/${src}/main.vert`)
.then(a => a.text())
.then(_vert => {
vert = _vert;
return fetch(`/js/plugins/filters/${src}/main.frag`);
})
.then(a => a.text())
.then(async _frag => {
frag = await FilterFragShader(_frag, src);
vert = `#version 300 es \nprecision highp float;\n${vert}`;
frag = `#version 300 es \nprecision highp float;\n${frag}`;
checkCompile(src, vert, frag);
resolve({ vert, frag });
__filterCache[src] = { vert, frag }
})
.catch(error => reject(error));
});
}
const osb = Scene_Boot.prototype.initialize;
Scene_Boot.prototype.initialize = function () {
osb.call(this, arguments);
(async () => {
try {
const data = await loadFilter('chroma');
if (!data) {
throw new Error("Failed to load filter data");
}
const { vert, frag } = data;
const _uniformData = this._getChromaData()
const uniformBase = {
delta: 0,
intensity: 2.25,
view: [Graphics._width, Graphics._height],
state: 1
};
const uniforms = _uniformData ?? uniformBase;
for(let key in uniformBase) {
if(!uniforms[key]) uniforms[key] = uniformBase[key];
}
window._globalChromaFilter = new PIXI.Filter(vert, frag, uniforms);
if (!_uniformData) this._writeChromaData(uniforms);
} catch (error) {
console.error("Error loading filter:", error);
}
})();
};
Scene_Base.prototype._getChromaData = function () {
const data = localStorage.getItem('chroma_data');
if (data) try {
return JSON.parse(data);
} catch (e) {
return data;
}
return data;
}
Scene_Base.prototype._writeChromaData = function (data) {
localStorage.setItem('chroma_data', JSON.stringify(data));
}
const osc = Scene_Base.prototype.create;
Scene_Base.prototype.create = function () {
osc.call(this, arguments);
this._chromaFilter = window._globalChromaFilter;
if (this._chromaFilter) {
if (!this.filters) this.filters = [];
this.filters.push(this._chromaFilter);
}
}
const osu = Scene_Base.prototype.update;
Scene_Base.prototype.update = function () {
osu.call(this, arguments);
if (this._chromaFilter) {
this._chromaFilter.uniforms.delta += pi60;
this._chromaFilter.uniforms.view[0] = Graphics._width;
this._chromaFilter.uniforms.view[1] = Graphics._height;
if(this.constructor.name==`Scene_Map`) {
this._chromaFilter.uniforms.state = 1;
this._chromaFilter.uniforms.intensity = window._globalChromaFilter.uniforms.intensity*0.15;
} else {
this._chromaFilter.uniforms.state = 0;
this._chromaFilter.uniforms.intensity = window._globalChromaFilter.uniforms.intensity;
}
}
}
})();