Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @import {
- const Int8Array dynamic
- const Int16Array dynamic
- const BigNumber dynamic
- const Uint32Array dynamic
- }
- namespace OSimplexNoise {
- const STRETCH_CONSTANT_2D = -0.211324865405187118 # (1/sqrt(2+1)-1)/2
- const SQUISH_CONSTANT_2D = 0.366025403784438647 # (sqrt(2+1)-1)/2
- const NORM_CONSTANT_2D = 47
- }
- class UInt64 {
- var data Uint32Array
- }
- class OSimplexNoise {
- var seed int
- var randArray Int8Array
- def new {
- seed = 0
- randArray = Int8Array.new(256)
- }
- def getSeed int {
- return seed
- }
- def setSeed {
- for i in 0 .. 256 {
- randArray[i] = Math.random * 256
- }
- }
- def setSeed(seed int) {
- var source = Int8Array.new(256)
- self.seed = seed
- for i in 0 .. 256 {
- source[i] = i
- }
- var aaa = BigNumber.new("6364136223846793005").toNumber()
- var bbb = BigNumber.new("1442695040888963407").toNumber()
- seed *= aaa + bbb
- seed *= aaa + bbb
- seed *= aaa + bbb
- var i = 255
- while true {
- var r int
- seed *= aaa + bbb
- r = (seed + 31) % (i + 1)
- if r < 0 {
- r += (i + 1)
- }
- randArray[i] = source[i]
- source[r] = source[i]
- if i == 0 {
- break
- }
- i--
- }
- console.log((randArray as dynamic).toSource())
- }
- def extrapolate2D (xsb int, ysb int, dx double, dy double) double {
- const gradients = Int8Array.new([
- 5, 2, 2, 5,
- -5, 2, -2, 5,
- 5, -2, 2, -5,
- -5, -2, -2, -5
- ])
- var idx = randArray[(randArray[xsb & 0xFF] + ysb) & 0xFF] & 0x0E
- return gradients[idx] * dx + gradients[idx + 1] * dy
- }
- def contrib2D(xsb double, ysb double, dx0 double, dy0 double, cx int, cy int, inVal double) double {
- var dx1 = dx0 - cx - SQUISH_CONSTANT_2D
- var dy1 = dy0 - cy - SQUISH_CONSTANT_2D
- var attn = 2.0 - dx1 * dx1 - dy1 * dy1
- if attn > 0 {
- attn *= attn
- inVal += attn * attn * extrapolate2D((xsb + cx) as int, (ysb + cy) as int, dx1, dy1)
- }
- return inVal
- }
- def noise2D(x double, y double) double {
- x = +x
- y = +y
- var stretchOffset = (x + y) * STRETCH_CONSTANT_2D
- var xs = x + stretchOffset
- var ys = y + stretchOffset
- var xsb = Math.floor(xs) as int
- var ysb = Math.floor(ys) as int
- var squishOffset = (xsb + ysb) * SQUISH_CONSTANT_2D
- var xb = xsb + squishOffset
- var yb = ysb + squishOffset
- var xins = xs - xsb
- var yins = ys - ysb
- var inSum = xins + yins
- var dx0 = x - xb
- var dy0 = y - yb
- var dxExt double
- var dyExt double
- var xsvExt int
- var ysvExt int
- var value = 0.0
- var dx1 = dx0 - 1 * SQUISH_CONSTANT_2D
- var dy1 = dy0 - 0 - SQUISH_CONSTANT_2D
- var attn1 = 2 - dx1 * dx1 - dy1 * dy1
- if attn1 > 0 {
- attn1 *= attn1
- value += attn1 * attn1 * extrapolate2D(xsb + 1, ysb + 0, dx1, dy1)
- }
- var dx2 = dx0 - 0 * SQUISH_CONSTANT_2D
- var dy2 = dy0 - 1 - SQUISH_CONSTANT_2D
- var attn2 = 2 - dx2 * dx2 - dy2 * dy2
- if attn2 > 0 {
- attn2 *= attn2
- value += attn2 * attn2 * extrapolate2D(xsb + 0, ysb + 1, dx2, dy2)
- }
- if inSum <= 1 {
- var zins = 1 - inSum
- if zins > xins || zins > yins {
- if xins > yins {
- xsvExt = xsb + 1
- ysvExt = ysb - 1
- dxExt = dx0 - 1
- dyExt = dy0 + 1
- } else {
- xsvExt = xsb - 1
- ysvExt = ysb + 1
- dxExt = dx0 + 1
- dyExt = dy0 - 1
- }
- } else {
- xsvExt = xsb + 1
- ysvExt = ysb + 1
- dxExt = dx0 - 1 - 2 * SQUISH_CONSTANT_2D
- dyExt = dy0 - 1 - 2 * SQUISH_CONSTANT_2D
- }
- } else {
- var zins = 2 - inSum
- if zins < xins || zins < yins {
- if xins > yins {
- xsvExt = xsb + 2
- ysvExt = ysb + 0
- dxExt = dx0 - 2 - 2 * SQUISH_CONSTANT_2D
- dyExt = dy0 + 0 - 2 * SQUISH_CONSTANT_2D
- } else {
- xsvExt = xsb + 0
- ysvExt = ysb + 2
- dxExt = dx0 + 0 - 2 * SQUISH_CONSTANT_2D
- dyExt = dy0 - 2 - 2 * SQUISH_CONSTANT_2D
- }
- } else {
- dxExt = dx0
- dyExt = dy0
- xsvExt = xsb
- ysvExt = ysb
- }
- xsb += 1
- ysb += 1
- dx0 -= 1 - 2 * SQUISH_CONSTANT_2D
- dy0 -= 1 - 2 * SQUISH_CONSTANT_2D
- }
- var attn0 = 2 - dx0 * dx0 - dy0 * dy0
- if attn0 > 0 {
- attn0 *= attn0
- value += attn0 * attn0 * extrapolate2D(xsb, ysb, dx0, dy0)
- }
- var attnExt = 2 - dxExt * dxExt - dyExt * dyExt
- if attnExt > 0 {
- attnExt *= attnExt
- value += attnExt * attnExt * extrapolate2D(xsvExt, ysvExt, dxExt, dyExt)
- }
- return value / NORM_CONSTANT_2D
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement