Spaces:
Running
Running
Download dengtaishan-src/src/video/util.ts from Ethscriptions/mx: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/util.ts
- Command line
-
hf download hf://spaces/Ethscriptions/mx/dengtaishan-src/src/video/util.ts
-
curl -L -o util.ts https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/util.ts
3.75 kB
| // 数学、缓动、确定性随机与噪声工具 —— 所有画面都是时间 t 的确定性函数 | |
| export const clamp = (v: number, a = 0, b = 1) => (v < a ? a : v > b ? b : v) | |
| export const lerp = (a: number, b: number, t: number) => a + (b - a) * t | |
| export const inv = (a: number, b: number, v: number) => clamp((v - a) / (b - a)) | |
| export const smooth = (t: number) => { | |
| t = clamp(t) | |
| return t * t * (3 - 2 * t) | |
| } | |
| export const easeOut = (t: number) => 1 - Math.pow(1 - clamp(t), 3) | |
| export const easeIn = (t: number) => Math.pow(clamp(t), 3) | |
| export const easeInOut = (t: number) => { | |
| t = clamp(t) | |
| return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2 | |
| } | |
| export const easeOutBack = (t: number) => { | |
| t = clamp(t) | |
| const c1 = 1.4 | |
| const c3 = c1 + 1 | |
| return 1 + c3 * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2) | |
| } | |
| /** 在 [a, a+d] 区间内淡入,在 [b-d2, b] 区间内淡出 */ | |
| export const window2 = (t: number, a: number, b: number, d = 0.6, d2 = d) => | |
| Math.min(smooth(inv(a, a + d, t)), 1 - smooth(inv(b - d2, b, t))) | |
| export function mulberry32(seed: number) { | |
| let a = seed >>> 0 | |
| return () => { | |
| a |= 0 | |
| a = (a + 0x6d2b79f5) | 0 | |
| let t = Math.imul(a ^ (a >>> 15), 1 | a) | |
| t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t | |
| return ((t ^ (t >>> 14)) >>> 0) / 4294967296 | |
| } | |
| } | |
| export const hash = (n: number) => { | |
| const s = Math.sin(n * 127.1 + 311.7) * 43758.5453 | |
| return s - Math.floor(s) | |
| } | |
| /** 一维值噪声 */ | |
| export function noise1(x: number, seed = 0) { | |
| const i = Math.floor(x) | |
| const f = x - i | |
| const u = f * f * (3 - 2 * f) | |
| return lerp(hash(i + seed * 101.3), hash(i + 1 + seed * 101.3), u) * 2 - 1 | |
| } | |
| export function fbm1(x: number, seed = 0, oct = 5) { | |
| let v = 0 | |
| let amp = 0.5 | |
| let fr = 1 | |
| for (let o = 0; o < oct; o++) { | |
| v += amp * noise1(x * fr, seed + o * 7) | |
| fr *= 2.03 | |
| amp *= 0.5 | |
| } | |
| return v | |
| } | |
| /** 二维值噪声 */ | |
| function h2(x: number, y: number, s: number) { | |
| const n = Math.sin(x * 127.1 + y * 311.7 + s * 74.7) * 43758.5453 | |
| return n - Math.floor(n) | |
| } | |
| export function noise2(x: number, y: number, seed = 0) { | |
| const xi = Math.floor(x) | |
| const yi = Math.floor(y) | |
| const xf = x - xi | |
| const yf = y - yi | |
| const u = xf * xf * (3 - 2 * xf) | |
| const v = yf * yf * (3 - 2 * yf) | |
| const a = h2(xi, yi, seed) | |
| const b = h2(xi + 1, yi, seed) | |
| const c = h2(xi, yi + 1, seed) | |
| const d = h2(xi + 1, yi + 1, seed) | |
| return lerp(lerp(a, b, u), lerp(c, d, u), v) | |
| } | |
| export function fbm2(x: number, y: number, seed = 0, oct = 4) { | |
| let v = 0 | |
| let amp = 0.5 | |
| let fr = 1 | |
| for (let o = 0; o < oct; o++) { | |
| v += amp * noise2(x * fr, y * fr, seed + o * 13) | |
| fr *= 2.01 | |
| amp *= 0.5 | |
| } | |
| return v | |
| } | |
| export type Canvas = HTMLCanvasElement | OffscreenCanvas | |
| export type Ctx = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D | |
| export function makeCanvas(w: number, h: number): Canvas { | |
| w = Math.max(1, Math.round(w)) | |
| h = Math.max(1, Math.round(h)) | |
| if (typeof OffscreenCanvas !== 'undefined') return new OffscreenCanvas(w, h) | |
| const c = document.createElement('canvas') | |
| c.width = w | |
| c.height = h | |
| return c | |
| } | |
| export function ctx2d(c: Canvas): Ctx { | |
| return c.getContext('2d') as Ctx | |
| } | |
| export function rgba(hex: string, a: number) { | |
| const n = parseInt(hex.slice(1), 16) | |
| return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${a})` | |
| } | |
| export function mixHex(h1: string, h2: string, t: number) { | |
| const a = parseInt(h1.slice(1), 16) | |
| const b = parseInt(h2.slice(1), 16) | |
| const r = Math.round(lerp((a >> 16) & 255, (b >> 16) & 255, t)) | |
| const g = Math.round(lerp((a >> 8) & 255, (b >> 8) & 255, t)) | |
| const bl = Math.round(lerp(a & 255, b & 255, t)) | |
| return `#${((1 << 24) | (r << 16) | (g << 8) | bl).toString(16).slice(1)}` | |
| } | |