UNPKG

@spearwolf/twopoint5d

Version:

a library to create 2.5d realtime graphics and pixelart with three.js

108 lines 2.95 kB
const isNumber = (x) => typeof x === 'number'; function add(a, b) { if (isNumber(a) && isNumber(b)) { return a + b; } else if (isNumber(a)) { switch (a) { case 0: return b; default: return `${a} + ${b}`; } } else if (isNumber(b)) { switch (b) { case 0: return a; default: return `${a} + ${b}`; } } else { return `${a} + ${b}`; } } function sub(a, b) { if (isNumber(a) && isNumber(b)) { return a - b; } else if (isNumber(a)) { switch (a) { case 0: return `-${b}`; default: return `${a} - ${b}`; } } else if (isNumber(b)) { switch (b) { case 0: return a; default: return `${a} - ${b}`; } } else { return `${a} - ${b}`; } } function mul(a, b) { if (isNumber(b) && isNumber(a)) { return a * b; } else if (isNumber(a)) { switch (a) { case 0: return 0; case 1: return b; default: return `${a} * ${b}`; } } else if (isNumber(b)) { switch (b) { case 0: return 0; case 1: return a; default: return `${a} * ${b}`; } } else { return `${a} * ${b}`; } } function asFloat(number) { const str = `${number}`.trim(); if (str.match(/^[0-9]+$/)) { return `${str}.0`; } return str; } const ret = (res) => `return ${res};`; function mat4(m00 = 0, m01 = 0, m02 = 0, m03 = 0, m10 = 0, m11 = 0, m12 = 0, m13 = 0, m20 = 0, m21 = 0, m22 = 0, m23 = 0, m30 = 0, m31 = 0, m32 = 0, m33 = 1, as = asFloat) { const toStr = as || ((x) => `${x}`); return `mat4(${toStr(m00)}, ${toStr(m01)}, ${toStr(m02)}, ${toStr(m03)}, ${toStr(m10)}, ${toStr(m11)}, ${toStr(m12)}, ${toStr(m13)}, ${toStr(m20)}, ${toStr(m21)}, ${toStr(m22)}, ${toStr(m23)}, ${toStr(m30)}, ${toStr(m31)}, ${toStr(m32)}, ${toStr(m33)})`; } const rotate = (funcName = 'rotate', x = 0.0, y = 0.0, z = 1.0) => ` mat4 ${funcName}(float angle) { float s = sin(angle); float c = cos(angle); float oc = 1.0 - c; ${ret(mat4(add(mul('oc', x * x), 'c'), sub(mul('oc', x * y), mul(z, 's')), add(mul('oc', z * x), mul(y, 's')), 0, add(mul('oc', x * y), mul(z, 's')), add(mul('oc', y * y), 'c'), sub(mul('oc', y * z), mul(x, 's')), 0, sub(mul('oc', z * x), mul(y, 's')), add(mul('oc', y * z), mul(x, 's')), add(mul('oc', z * z), 'c')))} }`; const rotateZ = (funcName = 'rotateZ') => rotate(funcName, 0, 0, 1); export const ShaderTool = { add, asFloat, mat4, mul, ret, rotate, rotateZ, sub, }; //# sourceMappingURL=ShaderTool.js.map