Skip to content

Instantly share code, notes, and snippets.

@i80and
Created August 22, 2018 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i80and/a3c30d039dc9bcdbff95bf5ab7bcc494 to your computer and use it in GitHub Desktop.
Save i80and/a3c30d039dc9bcdbff95bf5ab7bcc494 to your computer and use it in GitHub Desktop.
(module
(func $vec_getx (param $a i64) (result f32)
(i64.and (get_local $a) (i64.const 0xffffffff))
i32.wrap/i64
f32.reinterpret/i32
)
(func $vec_gety (param $a i64) (result f32)
(i64.shr_u (get_local $a) (i64.const 32))
i32.wrap/i64
f32.reinterpret/i32
)
(func $vec_new (param $x f32) (param $y f32) (result i64)
(i64.extend_u/i32 (i32.reinterpret/f32 (get_local $x)))
(i64.shl (i64.extend_u/i32 (i32.reinterpret/f32 (get_local $y))) (i64.const 32))
i64.or
)
(func $vec_dot (param $a i64) (param $b i64) (result f32)
get_local $a
call $vec_getx
get_local $b
call $vec_getx
f32.mul
get_local $a
call $vec_gety
get_local $b
call $vec_gety
f32.mul
f32.add
)
(func $vec_len (param $a i64) (result f32)
(local $x f32)
(tee_local $x (call $vec_getx (get_local $a)))
get_local $x
f32.mul
(tee_local $x (call $vec_gety (get_local $a)))
get_local $x
f32.mul
f32.add
f32.sqrt
)
(func $vec_normalize (param $a i64) (result i64)
(local $len f32)
(call $vec_getx (get_local $a))
(tee_local $len (call $vec_len (get_local $a)))
f32.div
(f32.div (call $vec_gety (get_local $a)) (get_local $len))
call $vec_new
)
(func $vec_add (param $a i64) (param $b i64) (result i64)
(call $vec_new
(f32.add
(call $vec_getx (get_local $a))
(call $vec_getx (get_local $b)))
(f32.add
(call $vec_gety (get_local $a))
(call $vec_gety (get_local $b))))
)
(func $ease (param $x f32) (result f32)
f32.const 6
get_local $x
get_local $x
get_local $x
get_local $x
get_local $x
f32.mul
f32.mul
f32.mul
f32.mul
f32.mul
f32.const 15
get_local $x
get_local $x
get_local $x
get_local $x
f32.mul
f32.mul
f32.mul
f32.mul
f32.sub
f32.const 10
get_local $x
get_local $x
get_local $x
f32.mul
f32.mul
f32.mul
f32.add
)
(func $xorshift (param $x i32) (result i32)
(set_local $x (i32.xor (get_local $x) (i32.shl (get_local $x) (i32.const 6))))
(set_local $x (i32.xor (get_local $x) (i32.shr_u (get_local $x) (i32.const 13))))
(i32.xor (get_local $x) (i32.shl (get_local $x) (i32.const 5)))
)
(func $get_gradient (param $x f32) (param $y f32) (result i64)
i64.const 0
)
(func $noise (param $x f32) (param $y f32) (result f32)
(local $x0 f32)
(local $x1 f32)
(local $y0 f32)
(local $y1 f32)
(local $s f32)
(local $t f32)
(local $u f32)
(local $v f32)
(local $weight_x f32)
(local $weight_y f32)
(local $a f32)
(local $b f32)
(set_local $x0 (f32.floor (get_local $x)))
(set_local $x1 (f32.ceil (get_local $x)))
(set_local $y0 (f32.floor (get_local $y)))
(set_local $y1 (f32.ceil (get_local $y)))
(set_local $s
(call $vec_dot
(call $get_gradient (get_local $x0) (get_local $y0))
(call $vec_new
(f32.sub (get_local $x) (get_local $x0))
(f32.sub (get_local $y) (get_local $y0)))))
(set_local $t
(call $vec_dot
(call $get_gradient (get_local $x1) (get_local $y0))
(call $vec_new
(f32.sub (get_local $x) (get_local $x1))
(f32.sub (get_local $y) (get_local $y0)))))
(set_local $u
(call $vec_dot
(call $get_gradient (get_local $x0) (get_local $y1))
(call $vec_new
(f32.sub (get_local $x) (get_local $x0))
(f32.sub (get_local $y) (get_local $y1)))))
(set_local $v
(call $vec_dot
(call $get_gradient (get_local $x1) (get_local $y1))
(call $vec_new
(f32.sub (get_local $x) (get_local $x1))
(f32.sub (get_local $y) (get_local $y1)))))
(set_local $weight_x (call $ease (f32.sub (get_local $x) (get_local $x0))))
(set_local $a
(f32.add
(get_local $s)
(f32.mul
(get_local $weight_x)
(f32.sub
(get_local $t)
(get_local $s)))))
(set_local $b
(f32.add
(get_local $u)
(f32.mul
(get_local $weight_x)
(f32.sub
(get_local $v)
(get_local $u)))))
(set_local $weight_y (call $ease (f32.sub (get_local $y) (get_local $y0))))
(f32.add
(get_local $a)
(f32.mul
(get_local $weight_y)
(f32.sub
(get_local $b)
(get_local $a))))
)
(func $fbm (param $x f32) (param $y f32) (result f32)
(local $val f32)
(local $amplitude f32)
(local $k f32)
(set_local $amplitude (f32.const 1))
(set_local $k (f32.const 2))
;; Iteration 1
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $amplitude (f32.div (get_local $amplitude) (f32.const 2)))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 2
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 3
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 4
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $amplitude (f32.div (get_local $amplitude) (f32.const 2)))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 5
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $amplitude (f32.div (get_local $amplitude) (f32.const 2)))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 6
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $amplitude (f32.div (get_local $amplitude) (f32.const 2)))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 7
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $amplitude (f32.div (get_local $amplitude) (f32.const 2)))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
;; Iteration 8
(set_local $val
(f32.add
(get_local $val)
(f32.mul
(get_local $amplitude)
(call $noise
(f32.div (get_local $x) (f32.div (f32.const 512) (get_local $k)))
(f32.div (get_local $y) (f32.div (f32.const 512) (get_local $k)))))))
(set_local $amplitude (f32.div (get_local $amplitude) (f32.const 2)))
(set_local $k (f32.mul (get_local $amplitude) (f32.const 2)))
(get_local $val)
)
;; Implements http://www.iquilezles.org/www/articles/warp/warp.htm
(func $warp (param $x f32) (param $y f32) (param $warp_factor f32) (result f32)
;; p = Vec2(5.2, 1.3)
(call $fbm
(f32.add
(f32.const 5.2)
(f32.mul
(call $fbm (get_local $x) (get_local $y))
(get_local $warp_factor)))
(f32.add
(f32.const 1.3)
(f32.mul
(call $fbm
(f32.add (f32.const 5.2) (get_local $x))
(f32.add (f32.const 1.3) (get_local $y)))
(get_local $warp_factor))))
)
(func $init)
(start $init)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment