rev: 21c5e6d2f665d0e8b4ff917a51b664c895dab2ed tukan/tukan/normal.sc -rw-r--r-- 1.2 KiB View raw Log this file
21c5e6d2f665 — Leonard Ritter * renamed project from Liminal to Tukan 2 years ago
                                                                                
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
using import glm

# octahedron packing as described in http://jcgt.org/published/0003/02/01/

# vec2 <- vec2
fn oct_wrap (v)
    *
        1.0 - (abs v.yx)
        vec2
            ? (v.x >= 0.0) 1.0 -1.0
            ? (v.y >= 0.0) 1.0 -1.0

# vec2 <- vec3
fn pack_normal_snorm (n)
    let n =
        n /
            +
                abs n.x
                abs n.y
                abs n.z
    ? (n.z >= 0.0)
        n.xy
        oct_wrap n.xy

# vec3 <- vec2
fn unpack_normal_snorm (encN)
    let z = (1.0 - (abs encN.x) - (abs encN.y))
    normalize
        vec3
            ? (z >= 0.0) encN.xy (oct_wrap encN.xy)
            z

# vec2 <- vec3
fn pack_normal_unorm (n)
    (pack_normal_snorm n) * 0.5 + 0.5

# vec3 <- vec2
fn unpack_normal_unorm (encN)
    unpack_normal_snorm (encN * 2.0 - 1.0)

let encode_normal = pack_normal_unorm
let decode_normal = unpack_normal_unorm

if main-module?
    print
        decode_normal (encode_normal (vec3 0 0 1))
        decode_normal (encode_normal (vec3 0 1 0))
        decode_normal (encode_normal (vec3 1 0 0))
        decode_normal (encode_normal (vec3 0 0 -1))
        decode_normal (encode_normal (vec3 0 -1 0))
        decode_normal (encode_normal (vec3 -1 0 0))
    compile
        typify encode_normal vec3

locals;