├── LICENSE ├── README.md └── Simplex └── Simplex.gd /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 OvermindDL1 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Godot-Helpers 2 | A listing of generic GDScripts 3 | -------------------------------------------------------------------------------- /Simplex/Simplex.gd: -------------------------------------------------------------------------------- 1 | #Copyright (c) 2015 OvermindDL1 2 | # 3 | #Permission is hereby granted, free of charge, to any person obtaining a copy 4 | #of this software and associated documentation files (the "Software"), to deal 5 | #in the Software without restriction, including without limitation the rights 6 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | #copies of the Software, and to permit persons to whom the Software is 8 | #furnished to do so, subject to the following conditions: 9 | # 10 | #The above copyright notice and this permission notice shall be included in all 11 | #copies or substantial portions of the Software. 12 | # 13 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | #SOFTWARE. 20 | # 21 | #OvermindDL1 would love to receive updates, fixes, and more to this code, 22 | #though it is not required: https://github.com/OvermindDL1/Godot-Helpers 23 | 24 | ## Testing only 25 | #extends SceneTree 26 | 27 | #const F2 = 0.5 * (sqrt(3.0) - 1.0) 28 | #const G2 = (3.0 - sqrt(3.0)) / 6.0 29 | #const G2o = G2*2.0 - 1.0 30 | #const F3 = (1.0 / 3.0) 31 | #const G3 = (1.0 / 6.0) 32 | #const F2 = 0.3660254037844385965883020617184229195117950439453125 33 | #const G2 = 0.2113248654051871344705659794271923601627349853515625 34 | #const G2o = -0.577350269189625731058868041145615279674530029296875 35 | #const F3 = 0.333333333333333314829616256247390992939472198486328125 36 | #const G3 = 0.1666666666666666574148081281236954964697360992431640625 37 | const F2 = 0.36602540378443860 38 | const G2 = 0.21132486540518714 39 | const G2o = -0.577350269189626 40 | const F3 = 0.33333333333333333 41 | const G3 = 0.16666666666666666 42 | 43 | const GRAD3 = [ 44 | 1,1,0, -1,1,0, 1,-1,0, -1,-1,0, 45 | 1,0,1, -1,0,1, 1,0,-1, -1,0,-1, 46 | 0,1,1, 0,-1,1, 0,1,-1, 0,-1,-1, 47 | 1,0,-1, -1,0,-1, 0,-1,1, 0,1,1, 48 | ] 49 | 50 | const GRAD4 = [ 51 | 0,1,1,1, 0,1,1,-1, 0,1,-1,1, 0,1,-1,-1, 52 | 0,-1,1,1, 0,-1,1,-1, 0,-1,-1,1, 0,-1,-1,-1, 53 | 1,0,1,1, 1,0,1,-1, 1,0,-1,1, 1,0,-1,-1, 54 | -1,0,1,1, -1,0,1,-1, -1,0,-1,1, -1,0,-1,-1, 55 | 1,1,0,1, 1,1,0,-1, 1,-1,0,1, 1,-1,0,-1, 56 | -1,1,0,1, -1,1,0,-1, -1,-1,0,1, -1,-1,0,-1, 57 | 1,1,1,0, 1,1,-1,0, 1,-1,1,0, 1,-1,-1,0, 58 | -1,1,1,0, -1,1,-1,0, -1,-1,1,0, -1,-1,-1,0, 59 | ] 60 | 61 | const PERM = [ 62 | 151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 63 | 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 64 | 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 65 | 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 66 | 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 67 | 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 68 | 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 69 | 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 70 | 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 71 | 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 72 | 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 73 | 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 74 | 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 75 | 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 76 | 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 77 | 141, 128, 195, 78, 66, 215, 61, 156, 180, 151, 160, 137, 91, 90, 15, 131, 78 | 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 79 | 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 80 | 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 81 | 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 82 | 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 83 | 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 84 | 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 85 | 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 86 | 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 87 | 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 88 | 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 89 | 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 90 | 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 91 | 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 92 | 05, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 93 | 180, 94 | ] 95 | 96 | static func simplex2(c0, c1): 97 | var s = (c0+c1) * 0.36602540378443860 98 | var a0 = floor(c0+s) 99 | var a1 = floor(c1+s) 100 | var t = (a0+a1) * 0.21132486540518714 101 | 102 | var n0 = 0.0 103 | var n1 = 0.0 104 | var n2 = 0.0 105 | 106 | var p00 = c0-(a0-t) 107 | var p01 = 0.0 108 | var p02 = 0.0 109 | var p10 = c1-(a1-t) 110 | var p11 = 0.0 111 | var p12 = 0.0 112 | 113 | # Current gdscript casts false to 0 and true to 1 114 | var b0 = int(p00 > p10) 115 | var b1 = int(p00 <= p10) 116 | 117 | p01 = p00 - b0 + 0.21132486540518714 118 | p11 = p10 - b1 + 0.21132486540518714 119 | p02 = p00 + -0.577350269189626 120 | p12 = p10 + -0.577350269189626 121 | 122 | var A0 = int(a0)&255 123 | var A1 = int(a1)&255 124 | var g0 = (PERM[A0 + PERM[A1]] % 12) * 3 125 | var g1 = (PERM[A0 + b0 + PERM[A1 + b1]] % 12) * 3 126 | var g2 = (PERM[A0 + 1 + PERM[A1 + 1]] % 12) * 3 127 | 128 | var f0 = 0.5 - p00*p00 - p10*p10 129 | var f1 = 0.5 - p01*p01 - p11*p11 130 | var f2 = 0.5 - p02*p02 - p12*p12 131 | 132 | if f0 > 0: 133 | #n0 = f0*f0*f0*f0 * (GRAD3[g0]*p00 + GRAD3[g0]*p10) 134 | n0 = f0*f0*f0*f0 * (GRAD3[g0]*p00 + GRAD3[g0 + 1]*p10) 135 | if f1 > 0: 136 | #n1 = f1*f1*f1*f1 * (GRAD3[g1 + 1]*p01 + GRAD3[g1 + 1]*p11) 137 | n1 = f1*f1*f1*f1 * (GRAD3[g1]*p01 + GRAD3[g1 + 1]*p11) 138 | if f2 > 0: 139 | #n2 = f2*f2*f2*f2 * (GRAD3[g2 + 2]*p02 + GRAD3[g2 + 2]*p12) 140 | n2 = f2*f2*f2*f2 * (GRAD3[g2]*p02 + GRAD3[g2 + 1]*p12) 141 | 142 | return (n0 + n1 + n2) * 70.0 143 | 144 | 145 | static func simplex3(c0, c1, c2): 146 | var s = (c0+c1+c2) * 0.33333333333333333 147 | var a0 = floor(c0+s) 148 | var a1 = floor(c1+s) 149 | var a2 = floor(c2+s) 150 | var t = (a0+a1+a2) * 0.16666666666666666 151 | 152 | var n00 = 0 153 | var n01 = 0 154 | var n02 = 0 155 | var n10 = 0 156 | var n11 = 0 157 | var n12 = 0 158 | 159 | var p00 = c0-(a0-t) 160 | var p01 = c1-(a1-t) 161 | var p02 = c2-(a2-t) 162 | 163 | if p00 >= p01: 164 | if p01 >= p02: 165 | n00 = 1 166 | n10 = 1 167 | n11 = 1 168 | elif p00 >= p02: 169 | n00 = 1 170 | n10 = 1 171 | n12 = 1 172 | else: 173 | n02 = 1 174 | n10 = 1 175 | n12 = 1 176 | else: 177 | if p01 < p02: 178 | n02 = 1 179 | n11 = 1 180 | n12 = 1 181 | elif p00 < p02: 182 | n01 = 1 183 | n11 = 1 184 | n12 = 1 185 | else: 186 | n01 = 1 187 | n10 = 1 188 | n11 = 1 189 | 190 | var p10 = p00 - 1.0 + 0.5 191 | var p20 = p00 - n10 + 0.33333333333333333 192 | var p30 = p00 - n00 + 0.16666666666666666 193 | var p11 = p01 - 1.0 + 0.5 194 | var p21 = p01 - n11 + 0.33333333333333333 195 | var p31 = p01 - n01 + 0.16666666666666666 196 | var p12 = p02 - 1.0 + 0.5 197 | var p22 = p02 - n12 + 0.33333333333333333 198 | var p32 = p02 - n02 + 0.16666666666666666 199 | 200 | var A0 = int(a0)&255 201 | var A1 = int(a1)&255 202 | var A2 = int(a2)&255 203 | var g0 = (PERM[A0 + PERM[A2]] % 12) * 3 204 | var g1 = (PERM[A0 + n00 + PERM[A1 + n01 + PERM[n02 + A2]]] % 12) * 3 205 | var g2 = (PERM[A0 + n10 + PERM[A1 + n11 + PERM[n12 + A2]]] % 12) * 3 206 | var g3 = (PERM[A0 + 1 + PERM[A1 + 1 + PERM[A2 + 1]]] % 12) * 3 207 | 208 | var f0 = 0.6 - p00*p00 - p01*p01 - p02*p02 209 | var f1 = 0.6 - p10*p10 - p11*p11 - p12*p12 210 | var f2 = 0.6 - p20*p20 - p21*p21 - p22*p22 211 | var f3 = 0.6 - p30*p30 - p31*p31 - p32*p32 212 | 213 | var n0 = 0.0 214 | var n1 = 0.0 215 | var n2 = 0.0 216 | var n3 = 0.0 217 | if f0 > 0: 218 | n0 = f0*f0*f0*f0 * (p00*GRAD3[g0] + p01*GRAD3[g0+1] + p02*GRAD3[g0+2]) 219 | if f1 > 0: 220 | n1 = f1*f1*f1*f1 * (p10*GRAD3[g1] + p11*GRAD3[g1+1] + p12*GRAD3[g1+2]) 221 | if f2 > 0: 222 | n2 = f2*f2*f2*f2 * (p20*GRAD3[g2] + p21*GRAD3[g2+1] + p22*GRAD3[g2+2]) 223 | if f3 > 0: 224 | n3 = f3*f3*f3*f3 * (p30*GRAD3[g3] + p31*GRAD3[g3+1] + p32*GRAD3[g3+2]) 225 | 226 | return (n0 + n1 + n2 + n3) * 32.0 227 | 228 | 229 | ## Testing only 230 | #func _init(): 231 | # var c = 1000000 232 | # print("Preloading data into active memory") 233 | # for a in range(c/100): 234 | # simplex2(c, c) 235 | # 236 | # print("Testing simplex2...") 237 | # var t = OS.get_unix_time() 238 | # for a in range(c): 239 | # simplex2(c, c) 240 | # print ("Testing of simplex2 with "+str(c)+" iterations took "+str(OS.get_unix_time()-t)+" seconds.") 241 | # 242 | # print("Testing simplex3...") 243 | # t = OS.get_unix_time() 244 | # for a in range(c): 245 | # simplex3(c, c, c) 246 | # print ("Testing of simplex3 with "+str(c)+" iterations took "+str(OS.get_unix_time()-t)+" seconds.") 247 | # quit() 248 | --------------------------------------------------------------------------------