cables_dev/cables/src/core/cg/cg_shader.js
import { EventTarget } from "../eventtarget.js";
import { simpleId } from "../utils.js";
class CgShader extends EventTarget
{
constructor()
{
super();
this.id = simpleId();
this._isValid = true;
this._defines = [];
}
/**
* easily enable/disable a define without a value
* @function toggleDefine
* @memberof Shader
* @instance
* @param {name} name
* @param {any} enabled value or port
*/
toggleDefine(name, enabled)
{
if (enabled && typeof (enabled) == "object" && enabled.addEventListener) // port
{
if (enabled.changeListener)enabled.removeEventListener(enabled.changeListener);
enabled.onToggleDefine = (v) =>
{
this.toggleDefine(name, v);
};
enabled.changeListener = enabled.on("change", enabled.onToggleDefine);
enabled = enabled.get();
}
if (enabled) this.define(name);
else this.removeDefine(name);
}
/**
* add a define to a shader, e.g. #define DO_THIS_THAT 1
* @function define
* @memberof Shader
* @instance
* @param {String} name
* @param {Any} value (can be empty)
*/
define(name, value)
{
if (value === null || value === undefined) value = "";
if (typeof (value) == "object") // port
{
value.removeEventListener("change", value.onDefineChange);
value.onDefineChange = (v) =>
{
this.define(name, v);
};
value.on("change", value.onDefineChange);
value = value.get();
}
for (let i = 0; i < this._defines.length; i++)
{
if (this._defines[i][0] == name && this._defines[i][1] == value) return;
if (this._defines[i][0] == name)
{
this._defines[i][1] = value;
this.setWhyCompile("define " + name + " " + value);
this._needsRecompile = true;
return;
}
}
this.setWhyCompile("define " + name + " " + value);
this._needsRecompile = true;
this._defines.push([name, value]);
}
getDefines()
{
return this._defines;
}
getDefine(name)
{
for (let i = 0; i < this._defines.length; i++)
if (this._defines[i][0] == name) return this._defines[i][1];
return null;
}
/**
* return true if shader has define
* @function hasDefine
* @memberof Shader
* @instance
* @param {String} name
* @return {Boolean}
*/
hasDefine(name)
{
for (let i = 0; i < this._defines.length; i++)
if (this._defines[i][0] == name) return true;
}
/**
* remove a define from a shader
* @param {name} name
* @function removeDefine
* @memberof Shader
* @instance
*/
removeDefine(name)
{
for (let i = 0; i < this._defines.length; i++)
{
if (this._defines[i][0] == name)
{
this._defines.splice(i, 1);
this._needsRecompile = true;
this.setWhyCompile("define removed:" + name);
return;
}
}
}
}
export { CgShader };