Skip to content

Commit

Permalink
Add GLSL's mod function
Browse files Browse the repository at this point in the history
  • Loading branch information
baku89 committed Oct 7, 2023
1 parent f61c776 commit c404495
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export function fract(a: number): number {
return a - floor(a)
}

/**
* Compute value of one parameter module another. This is computed as x - y * floor(x/y). Unlike JavaScript's `%` operator, the sign of result always matches to `b`.
* @see https://thebookofshaders.com/glossary/?search=mod
*/
export function mod(a: number, b: number): number {
return a - b * floor(a / b)
}

export function quantize(s: number, step: number, offset = 0): number {
return Math.round((s - offset) / step) * step + offset
}
Expand Down
13 changes: 13 additions & 0 deletions src/vec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,19 @@ export function fract(a: Vec2): Vec2 {
return sub(a, floor(a))
}

/**
* Compute value of one parameter module another. This is computed as x - y * floor(x/y). Unlike JavaScript's `%` operator, the sign of result always matches to `b`.
* @see https://thebookofshaders.com/glossary/?search=mod
*/
export function mod(a: Vec2, b: Vec2 | number): Vec2 {
if (typeof b === 'number') b = [b, b]

return [
a[0] - b[0] * Math.floor(a[0] / b[0]),
a[1] - b[1] * Math.floor(a[1] / b[1]),
]
}

export function quantize(
v: Vec2,
step: Vec2 | number,
Expand Down
14 changes: 14 additions & 0 deletions src/vec3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ export function fract(a: Vec3): Vec3 {
return sub(a, floor(a))
}

/**
* Compute value of one parameter module another. This is computed as x - y * floor(x/y). Unlike JavaScript's `%` operator, the sign of result always matches to `b`.
* @see https://thebookofshaders.com/glossary/?search=mod
*/
export function mod(a: Vec3, b: Vec3 | number): Vec3 {
if (typeof b === 'number') b = [b, b, b]

return [
a[0] - b[0] * Math.floor(a[0] / b[0]),
a[1] - b[1] * Math.floor(a[1] / b[1]),
a[2] - b[2] * Math.floor(a[2] / b[2]),
]
}

export function quantize(
v: Vec3,
step: Vec3 | number,
Expand Down
15 changes: 15 additions & 0 deletions src/vec4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ export function fract(a: Vec4): Vec4 {
return sub(a, floor(a))
}

/**
* Compute value of one parameter module another. This is computed as x - y * floor(x/y). Unlike JavaScript's `%` operator, the sign of result always matches to `b`.
* @see https://thebookofshaders.com/glossary/?search=mod
*/
export function mod(a: Vec4, b: Vec4 | number): Vec4 {
if (typeof b === 'number') b = [b, b, b, b]

return [
a[0] - b[0] * Math.floor(a[0] / b[0]),
a[1] - b[1] * Math.floor(a[1] / b[1]),
a[2] - b[2] * Math.floor(a[2] / b[2]),
a[3] - b[3] * Math.floor(a[3] / b[3]),
]
}

export function quantize(
v: Vec4,
step: Vec4 | number,
Expand Down

0 comments on commit c404495

Please sign in to comment.