| import Vec2 from "../../game/vector/vec2.js"; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class Vec2Gradient { |
| |
| |
| |
| |
| |
| |
| constructor(values = {}, smooth = false) { |
| const keys = Object.keys(values).map(Number).sort((a, b) => a - b); |
| |
| this._points = []; |
| this._values = []; |
| |
| for (const key of keys) { |
| this._points.push(key); |
| this._values.push(Vec2.from(values[key.toString()])); |
| } |
| |
| this.smooth = smooth; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| get(progress) { |
| if (this._points.length === 0) |
| return new Vec2(); |
| if (this._points.length === 1 || progress <= this._points[0]) |
| return this._values[0].copy(); |
| if (progress >= this._points[this._points.length - 1]) |
| return this._values[this._points.length - 1].copy(); |
| |
| let i = 0; |
| while (progress > this._points[i]) i++; |
| |
| const pointA = this._points[i - 1]; |
| const pointB = this._points[i]; |
| |
| const posA = this._values[i - 1]; |
| const posB = this._values[i]; |
| |
| const alpha = (progress - pointA) / (pointB - pointA); |
| |
| if (this.smooth) |
| return posA.smoothLerp(posB, alpha); |
| else |
| return posA.lerp(posB, alpha); |
| } |
| } |
| |
| export default Vec2Gradient; |