UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

303 lines (295 loc) 6.77 kB
export class b2Color { constructor(t = .5, s = .5, i = .5, h = 1) { this.r = t; this.g = s; this.b = i; this.a = h; } Clone() { return (new b2Color).Copy(this); } Copy(t) { this.r = t.r; this.g = t.g; this.b = t.b; this.a = t.a; return this; } IsEqual(t) { return this.r === t.r && this.g === t.g && this.b === t.b && this.a === t.a; } IsZero() { return this.r === 0 && this.g === 0 && this.b === 0 && this.a === 0; } Set(t, s, i, h = this.a) { this.SetRGBA(t, s, i, h); } SetByteRGB(t, s, i) { this.r = t / 255; this.g = s / 255; this.b = i / 255; return this; } SetByteRGBA(t, s, i, h) { this.r = t / 255; this.g = s / 255; this.b = i / 255; this.a = h / 255; return this; } SetRGB(t, s, i) { this.r = t; this.g = s; this.b = i; return this; } SetRGBA(t, s, i, h) { this.r = t; this.g = s; this.b = i; this.a = h; return this; } SelfAdd(t) { this.r += t.r; this.g += t.g; this.b += t.b; this.a += t.a; return this; } Add(t, s) { s.r = this.r + t.r; s.g = this.g + t.g; s.b = this.b + t.b; s.a = this.a + t.a; return s; } SelfSub(t) { this.r -= t.r; this.g -= t.g; this.b -= t.b; this.a -= t.a; return this; } Sub(t, s) { s.r = this.r - t.r; s.g = this.g - t.g; s.b = this.b - t.b; s.a = this.a - t.a; return s; } SelfMul(t) { this.r *= t; this.g *= t; this.b *= t; this.a *= t; return this; } Mul(t, s) { s.r = this.r * t; s.g = this.g * t; s.b = this.b * t; s.a = this.a * t; return s; } Mix(t, s) { b2Color.MixColors(this, t, s); } static MixColors(t, s, i) { const h = i * (s.r - t.r); const r = i * (s.g - t.g); const e = i * (s.b - t.b); const n = i * (s.a - t.a); t.r += h; t.g += r; t.b += e; t.a += n; s.r -= h; s.g -= r; s.b -= e; s.a -= n; } MakeStyleString(t = this.a) { return b2Color.MakeStyleString(this.r, this.g, this.b, t); } static MakeStyleString(t, s, i, h = 1) { t *= 255; s *= 255; i *= 255; if (h < 1) { return `rgba(${t},${s},${i},${h})`; } else { return `rgb(${t},${s},${i})`; } } } b2Color.ZERO = new b2Color(0, 0, 0, 0); b2Color.RED = new b2Color(1, 0, 0); b2Color.GREEN = new b2Color(0, 1, 0); b2Color.BLUE = new b2Color(0, 0, 1); export class b2TypedColor { constructor(...t) { if (t[0] instanceof Float32Array) { if (t[0].length !== 4) { throw new Error; } this.data = t[0]; } else { const s = typeof t[0] === "number" ? t[0] : .5; const i = typeof t[1] === "number" ? t[1] : .5; const h = typeof t[2] === "number" ? t[2] : .5; const r = typeof t[3] === "number" ? t[3] : 1; this.data = new Float32Array([ s, i, h, r ]); } } get r() { return this.data[0]; } set r(t) { this.data[0] = t; } get g() { return this.data[1]; } set g(t) { this.data[1] = t; } get b() { return this.data[2]; } set b(t) { this.data[2] = t; } get a() { return this.data[3]; } set a(t) { this.data[3] = t; } Clone() { return new b2TypedColor(new Float32Array(this.data)); } Copy(t) { if (t instanceof b2TypedColor) { this.data.set(t.data); } else { this.r = t.r; this.g = t.g; this.b = t.b; this.a = t.a; } return this; } IsEqual(t) { return this.r === t.r && this.g === t.g && this.b === t.b && this.a === t.a; } IsZero() { return this.r === 0 && this.g === 0 && this.b === 0 && this.a === 0; } Set(t, s, i, h = this.a) { this.SetRGBA(t, s, i, h); } SetByteRGB(t, s, i) { this.r = t / 255; this.g = s / 255; this.b = i / 255; return this; } SetByteRGBA(t, s, i, h) { this.r = t / 255; this.g = s / 255; this.b = i / 255; this.a = h / 255; return this; } SetRGB(t, s, i) { this.r = t; this.g = s; this.b = i; return this; } SetRGBA(t, s, i, h) { this.r = t; this.g = s; this.b = i; this.a = h; return this; } SelfAdd(t) { this.r += t.r; this.g += t.g; this.b += t.b; this.a += t.a; return this; } Add(t, s) { s.r = this.r + t.r; s.g = this.g + t.g; s.b = this.b + t.b; s.a = this.a + t.a; return s; } SelfSub(t) { this.r -= t.r; this.g -= t.g; this.b -= t.b; this.a -= t.a; return this; } Sub(t, s) { s.r = this.r - t.r; s.g = this.g - t.g; s.b = this.b - t.b; s.a = this.a - t.a; return s; } SelfMul(t) { this.r *= t; this.g *= t; this.b *= t; this.a *= t; return this; } Mul(t, s) { s.r = this.r * t; s.g = this.g * t; s.b = this.b * t; s.a = this.a * t; return s; } Mix(t, s) { b2Color.MixColors(this, t, s); } MakeStyleString(t = this.a) { return b2Color.MakeStyleString(this.r, this.g, this.b, t); } } export var b2DrawFlags; (function(t) { t[t["e_none"] = 0] = "e_none"; t[t["e_shapeBit"] = 1] = "e_shapeBit"; t[t["e_jointBit"] = 2] = "e_jointBit"; t[t["e_aabbBit"] = 4] = "e_aabbBit"; t[t["e_pairBit"] = 8] = "e_pairBit"; t[t["e_centerOfMassBit"] = 16] = "e_centerOfMassBit"; t[t["e_particleBit"] = 32] = "e_particleBit"; t[t["e_controllerBit"] = 64] = "e_controllerBit"; t[t["e_all"] = 63] = "e_all"; })(b2DrawFlags || (b2DrawFlags = {})); export class b2Draw { constructor() { this.m_drawFlags = 0; } SetFlags(t) { this.m_drawFlags = t; } GetFlags() { return this.m_drawFlags; } AppendFlags(t) { this.m_drawFlags |= t; } ClearFlags(t) { this.m_drawFlags &= ~t; } }