Skip to content

Commit

Permalink
feat(shader-ast-stdlib): add rotationAroundAxis3/4, matrix conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 9, 2019
1 parent 16823b2 commit 8a473c1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/shader-ast-stdlib/src/matrix/convert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
defn,
indexMat,
mat3,
mat4,
ret,
vec3,
vec4
} from "@thi.ng/shader-ast";

export const m22ToM33 = defn("mat3", "m22ToM33", ["mat2"], (m) => {
return [
ret(mat3(vec3(indexMat(m, 0), 0), vec3(indexMat(m, 1), 0), vec3()))
];
});

export const m33ToM44 = defn("mat4", "m33ToM44", ["mat3"], (m) => {
return [
ret(
mat4(
vec4(indexMat(m, 0), 0),
vec4(indexMat(m, 1), 0),
vec4(indexMat(m, 2), 0),
vec4()
)
)
];
});
51 changes: 50 additions & 1 deletion packages/shader-ast-stdlib/src/matrix/rotation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import {
$x,
$y,
$z,
add,
cos,
defn,
FloatSym,
mat2,
mat3,
mat4,
mul,
neg,
NumericF,
ret,
sin,
sub,
sym,
Vec2Term
Vec2Term,
vec3
} from "@thi.ng/shader-ast";
import { perpendicularCCW } from "../math/orthogonal";
import { cossin } from "../math/sincos";
import { m33ToM44 } from "./convert";

export const rotation2 = defn("mat2", "rotation2", ["float"], (theta) => {
let cs: Vec2Term;
Expand Down Expand Up @@ -122,3 +132,42 @@ export const rotationZ4 = defn("mat4", "rotationZ4", ["float"], (theta) => {
)
];
});

export const rotationAroundAxis3 = defn(
"mat3",
"rotationAroundAxis3",
["vec3", "float"],
(axis, theta) => {
let s: FloatSym;
let c: FloatSym;
let t: FloatSym;
const $$ = (
a: NumericF,
b: NumericF,
c: NumericF,
d: NumericF,
e: NumericF,
f: NumericF,
g: NumericF
) => add(mul(mul(axis, a), t), mul(vec3(b, c, d), vec3(e, f, g)));
return [
(s = sym(sin(theta))),
(c = sym(cos(theta))),
(t = sym(sub(1, c))),
ret(
mat3(
$$($x(axis), 1, $z(axis), neg($y(axis)), c, s, s),
$$($y(axis), neg($z(axis)), 1, $x(axis), s, c, s),
$$($z(axis), $y(axis), neg($x(axis)), 1, s, s, c)
)
)
];
}
);

export const rotationAroundAxis4 = defn(
"mat4",
"rotationAroundAxis4",
["vec3", "float"],
(axis, theta) => [ret(m33ToM44(rotationAroundAxis3(axis, theta)))]
);

0 comments on commit 8a473c1

Please sign in to comment.