|
1 | 1 | import { mat2x2f, mat3x3f, mat4x4f } from './matrix.ts';
|
| 2 | +import { clamp, divInteger, smoothstepScalar } from './numberOps.ts'; |
2 | 3 | import {
|
3 | 4 | vec2b,
|
4 | 5 | vec2f,
|
@@ -38,9 +39,6 @@ const dotVec3 = (lhs: v3, rhs: v3) =>
|
38 | 39 | const dotVec4 = (lhs: v4, rhs: v4) =>
|
39 | 40 | lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
|
40 | 41 |
|
41 |
| -const clamp = (value: number, low: number, high: number) => |
42 |
| - Math.min(Math.max(low, value), high); |
43 |
| - |
44 | 42 | type UnaryOp = (a: number) => number;
|
45 | 43 | type BinaryOp = (a: number, b: number) => number;
|
46 | 44 |
|
@@ -162,14 +160,41 @@ const binaryComponentWise4x4f =
|
162 | 160 | );
|
163 | 161 | };
|
164 | 162 |
|
165 |
| -export const NumberOps = { |
166 |
| - divInteger: (lhs: number, rhs: number) => { |
167 |
| - if (rhs === 0) { |
168 |
| - return lhs; |
169 |
| - } |
170 |
| - return Math.trunc(lhs / rhs); |
171 |
| - }, |
172 |
| -}; |
| 163 | +type TernaryOp = (a: number, b: number, c: number) => number; |
| 164 | + |
| 165 | +const ternaryComponentWise2f = |
| 166 | + (op: TernaryOp) => (a: wgsl.v2f, b: wgsl.v2f, c: wgsl.v2f) => |
| 167 | + vec2f(op(a.x, b.x, c.x), op(a.y, b.y, c.y)); |
| 168 | + |
| 169 | +const ternaryComponentWise2h = |
| 170 | + (op: TernaryOp) => (a: wgsl.v2h, b: wgsl.v2h, c: wgsl.v2h) => |
| 171 | + vec2h(op(a.x, b.x, c.x), op(a.y, b.y, c.y)); |
| 172 | + |
| 173 | +const ternaryComponentWise3f = |
| 174 | + (op: TernaryOp) => (a: wgsl.v3f, b: wgsl.v3f, c: wgsl.v3f) => |
| 175 | + vec3f(op(a.x, b.x, c.x), op(a.y, b.y, c.y), op(a.z, b.z, c.z)); |
| 176 | + |
| 177 | +const ternaryComponentWise3h = |
| 178 | + (op: TernaryOp) => (a: wgsl.v3h, b: wgsl.v3h, c: wgsl.v3h) => |
| 179 | + vec3h(op(a.x, b.x, c.x), op(a.y, b.y, c.y), op(a.z, b.z, c.z)); |
| 180 | + |
| 181 | +const ternaryComponentWise4f = |
| 182 | + (op: TernaryOp) => (a: wgsl.v4f, b: wgsl.v4f, c: wgsl.v4f) => |
| 183 | + vec4f( |
| 184 | + op(a.x, b.x, c.x), |
| 185 | + op(a.y, b.y, c.y), |
| 186 | + op(a.z, b.z, c.z), |
| 187 | + op(a.w, b.w, c.w), |
| 188 | + ); |
| 189 | + |
| 190 | +const ternaryComponentWise4h = |
| 191 | + (op: TernaryOp) => (a: wgsl.v4h, b: wgsl.v4h, c: wgsl.v4h) => |
| 192 | + vec4h( |
| 193 | + op(a.x, b.x, c.x), |
| 194 | + op(a.y, b.y, c.y), |
| 195 | + op(a.z, b.z, c.z), |
| 196 | + op(a.w, b.w, c.w), |
| 197 | + ); |
173 | 198 |
|
174 | 199 | export const VectorOps = {
|
175 | 200 | eq: {
|
@@ -446,6 +471,25 @@ export const VectorOps = {
|
446 | 471 | <T extends vBase | mBase>(lhs: T, rhs: T) => T
|
447 | 472 | >,
|
448 | 473 |
|
| 474 | + smoothstep: { |
| 475 | + vec2f: ternaryComponentWise2f(smoothstepScalar), |
| 476 | + vec2h: ternaryComponentWise2h(smoothstepScalar), |
| 477 | + vec3f: ternaryComponentWise3f(smoothstepScalar), |
| 478 | + vec3h: ternaryComponentWise3h(smoothstepScalar), |
| 479 | + vec4f: ternaryComponentWise4f(smoothstepScalar), |
| 480 | + vec4h: ternaryComponentWise4h(smoothstepScalar), |
| 481 | + } as Record< |
| 482 | + VecKind, |
| 483 | + <T extends vBase>( |
| 484 | + edge0: T, |
| 485 | + edge1: T, |
| 486 | + x: T, |
| 487 | + ) => T extends wgsl.AnyVec2Instance ? wgsl.v2f |
| 488 | + : T extends wgsl.AnyVec3Instance ? wgsl.v3f |
| 489 | + : T extends wgsl.AnyVec4Instance ? wgsl.v4f |
| 490 | + : wgsl.AnyVecInstance |
| 491 | + >, |
| 492 | + |
449 | 493 | addMixed: {
|
450 | 494 | vec2f: (a: wgsl.v2f, b: number) => unary2f((e) => e + b)(a),
|
451 | 495 | vec2h: (a: wgsl.v2h, b: number) => unary2h((e) => e + b)(a),
|
@@ -688,41 +732,35 @@ export const VectorOps = {
|
688 | 732 | div: {
|
689 | 733 | vec2f: binaryComponentWise2f((a, b) => a / b),
|
690 | 734 | vec2h: binaryComponentWise2h((a, b) => a / b),
|
691 |
| - vec2i: binaryComponentWise2i(NumberOps.divInteger), |
692 |
| - vec2u: binaryComponentWise2u(NumberOps.divInteger), |
| 735 | + vec2i: binaryComponentWise2i(divInteger), |
| 736 | + vec2u: binaryComponentWise2u(divInteger), |
693 | 737 |
|
694 | 738 | vec3f: binaryComponentWise3f((a, b) => a / b),
|
695 | 739 | vec3h: binaryComponentWise3h((a, b) => a / b),
|
696 |
| - vec3i: binaryComponentWise3i(NumberOps.divInteger), |
697 |
| - vec3u: binaryComponentWise3u(NumberOps.divInteger), |
| 740 | + vec3i: binaryComponentWise3i(divInteger), |
| 741 | + vec3u: binaryComponentWise3u(divInteger), |
698 | 742 |
|
699 | 743 | vec4f: binaryComponentWise4f((a, b) => a / b),
|
700 | 744 | vec4h: binaryComponentWise4h((a, b) => a / b),
|
701 |
| - vec4i: binaryComponentWise4i(NumberOps.divInteger), |
702 |
| - vec4u: binaryComponentWise4u(NumberOps.divInteger), |
| 745 | + vec4i: binaryComponentWise4i(divInteger), |
| 746 | + vec4u: binaryComponentWise4u(divInteger), |
703 | 747 | } as Record<VecKind, <T extends vBase>(a: T, b: T) => T>,
|
704 | 748 |
|
705 | 749 | divMixed: {
|
706 | 750 | vec2f: (a: wgsl.v2f, b: number) => unary2f((e) => e / b)(a),
|
707 | 751 | vec2h: (a: wgsl.v2h, b: number) => unary2h((e) => e / b)(a),
|
708 |
| - vec2i: (a: wgsl.v2i, b: number) => |
709 |
| - unary2i((e) => NumberOps.divInteger(e, b))(a), |
710 |
| - vec2u: (a: wgsl.v2u, b: number) => |
711 |
| - unary2u((e) => NumberOps.divInteger(e, b))(a), |
| 752 | + vec2i: (a: wgsl.v2i, b: number) => unary2i((e) => divInteger(e, b))(a), |
| 753 | + vec2u: (a: wgsl.v2u, b: number) => unary2u((e) => divInteger(e, b))(a), |
712 | 754 |
|
713 | 755 | vec3f: (a: wgsl.v3f, b: number) => unary3f((e) => e / b)(a),
|
714 | 756 | vec3h: (a: wgsl.v3h, b: number) => unary3h((e) => e / b)(a),
|
715 |
| - vec3i: (a: wgsl.v3i, b: number) => |
716 |
| - unary3i((e) => NumberOps.divInteger(e, b))(a), |
717 |
| - vec3u: (a: wgsl.v3u, b: number) => |
718 |
| - unary3u((e) => NumberOps.divInteger(e, b))(a), |
| 757 | + vec3i: (a: wgsl.v3i, b: number) => unary3i((e) => divInteger(e, b))(a), |
| 758 | + vec3u: (a: wgsl.v3u, b: number) => unary3u((e) => divInteger(e, b))(a), |
719 | 759 |
|
720 | 760 | vec4f: (a: wgsl.v4f, b: number) => unary4f((e) => e / b)(a),
|
721 | 761 | vec4h: (a: wgsl.v4h, b: number) => unary4h((e) => e / b)(a),
|
722 |
| - vec4i: (a: wgsl.v4i, b: number) => |
723 |
| - unary4i((e) => NumberOps.divInteger(e, b))(a), |
724 |
| - vec4u: (a: wgsl.v4u, b: number) => |
725 |
| - unary4u((e) => NumberOps.divInteger(e, b))(a), |
| 762 | + vec4i: (a: wgsl.v4i, b: number) => unary4i((e) => divInteger(e, b))(a), |
| 763 | + vec4u: (a: wgsl.v4u, b: number) => unary4u((e) => divInteger(e, b))(a), |
726 | 764 | } as Record<VecKind, <T extends vBase>(lhs: T, rhs: number) => T>,
|
727 | 765 |
|
728 | 766 | dot: {
|
|
0 commit comments