-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattern.js
74 lines (63 loc) · 1.69 KB
/
pattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { color, add, multiply, subtract, point } from "./tuple.js"
import * as matrix from "./matrix.js"
import { shape } from "./shape.js"
export const BLACK = color()
export const WHITE = color(1, 1, 1)
export default function pattern(a = WHITE, b = BLACK) {
return { a, b, transform: matrix.id4(), at: ([x, y, z] = point()) => color(x, y, z) }
}
export function patternat(p = pattern(), pp = point()) {
return p?.at(pp)
}
// TODO: Merge with above?
export function patternatshape(p = pattern(), o = shape(), wp = point()) {
// TODO: Pass through `shape.worldToObject()` somehow?
const op = matrix.multiply(matrix.inverse(o.transform), wp)
const pp = matrix.multiply(matrix.inverse(p.transform), op)
return p?.at(pp)
}
export function stripe(a = WHITE, b = BLACK) {
return {
a,
b,
transform: matrix.id4(),
at([x] = point()) {
return Math.floor(x) % 2 === 0 ? this.a : this.b
},
}
}
export function stripeatshape(...args) {
return patternatshape(...args)
}
export function gradient(a = WHITE, b = BLACK) {
return {
a,
b,
transform: matrix.id4(),
at([x] = point()) {
const distance = subtract(this.b, this.a)
const fraction = x - Math.floor(x)
return add(this.a, multiply(distance, fraction))
}
}
}
export function ring(a = WHITE, b = BLACK) {
return {
a,
b,
transform: matrix.id4(),
at([x, _, z] = point()) {
return Math.floor(Math.sqrt((x * x) + (z * z))) % 2 === 0 ? this.a : this.b
}
}
}
export function checkers(a = WHITE, b = BLACK) {
return {
a,
b,
transform: matrix.id4(),
at([x, y, z] = point()) {
return Math.floor(x + y + z) % 2 === 0 ? this.a : this.b
}
}
}