[READ-ONLY] Mirror of https://github.com/flo-bit/every-noise. javascript noise class with lots of features flo-bit.github.io/every-noise/
javascript noise procedural-generation
0

Configure Feed

Select the types of activity you want to include in your feed.

adding 4D noise

flo-bit (Dec 31, 2022, 1:00 AM +0100) ace85513 fae5fdd2

+174 -104
+174 -104
noise.js
··· 47 47 } 48 48 this.x = x; 49 49 this.y = y; 50 - this.z = z; 51 - this.w = w; 50 + if (z != undefined && z != NaN) this.z = z; 51 + if (w != undefined && w != NaN) this.w = w; 52 52 return this; 53 53 } 54 54 add(x, y, z, w) { ··· 271 271 class Noise { 272 272 static defaultUp = new Vector(0, 1, 0); 273 273 274 - /** 274 + /* 275 275 * simplex-noise.js by Jonas Wagner 276 276 * 277 277 * version 4.0.1 slightly modified ··· 823 823 824 824 this.shift = Noise.firstDefined( 825 825 opts.shift, 826 - new Vector(opts.x || 0.357, opts.y || 0.579, opts.z || 0.739) 826 + new Vector( 827 + opts.x || 0.357, 828 + opts.y || 0.579, 829 + opts.z || 0.739, 830 + opts.w || 0.123 831 + ) 827 832 ); 828 833 829 834 // fbm stuff ··· 875 880 this.layers.push(n); 876 881 } 877 882 } else { 883 + this.noise2D = Noise.Simplex.createNoise2D(); //this.seed); 884 + this.noise3D = Noise.Simplex.createNoise3D(); //this.seed); 885 + this.noise4D = Noise.Simplex.createNoise4D(); //this.seed); 886 + 878 887 this.simplex = Noise.Simplex.createNoise3D(); //this.seed); 879 888 } 880 889 ··· 1123 1132 } 1124 1133 } 1125 1134 1126 - getFBM(x, y, z, noErosion) { 1135 + getFBM(x, y, z, w, noErosion) { 1127 1136 let scale = this.scale; 1128 1137 1129 1138 // if no layers exit early 1130 1139 if (this.layers == undefined) { 1131 1140 // if object has simplex noise return result of that 1132 - if (this.simplex != undefined) 1141 + /* 1142 + if (this.simplex != undefined) { 1133 1143 return this.simplex(x * scale, y * scale, z * scale); 1144 + }*/ 1145 + console.log(x, y, z, w); 1146 + if (this.noise4D != undefined && w != undefined) 1147 + return this.noise4D(x * scale, y * scale, z * scale, w * scale); 1148 + if (this.noise3D != undefined && z != undefined) 1149 + return this.noise3D(x * scale, y * scale, z * scale); 1150 + if (this.noise2D != undefined) return this.noise2D(x * scale, y * scale); 1134 1151 // no data 1135 1152 return 0; 1136 1153 } ··· 1176 1193 return n / maxAmp; 1177 1194 } 1178 1195 1179 - warpPosition(x, y, z) { 1196 + warpPosition() { 1180 1197 let warp = this.warp; 1181 - if (warp) { 1182 - if (this.warpNoise) { 1183 - this.warpNoise.pos.copy(this.pos); 1184 - let scl = this.warpNoise.scale; 1185 - x += 1198 + if (warp == undefined || warp == 0) return; 1199 + let x = this.pos.x, 1200 + y = this.pos.y, 1201 + z = this.pos.z, 1202 + w = this.pos.w; 1203 + if (this.warpNoise) { 1204 + this.warpNoise.pos.copy(this.pos); 1205 + let scl = this.warpNoise.scale; 1206 + x += this.warpNoise.get(x - 74.98 * scl, y + 49.33 * scl) * warp; 1207 + y += this.warpNoise.get(x + 13.23 * scl, y + 56.79 * scl) * warp; 1208 + if (z != undefined) { 1209 + z += 1186 1210 this.warpNoise.get( 1187 - x - 74.98 * scl, 1188 - y + 49.33 * scl, 1189 - z + 11.11 * scl 1211 + x + 11.47 * scl, 1212 + y + 17.98 * scl, 1213 + z + 23.56 * scl 1190 1214 ) * warp; 1191 - y += 1215 + } 1216 + if (w != undefined) { 1217 + w += 1192 1218 this.warpNoise.get( 1193 - x + 13.23 * scl, 1194 - y + 56.79 * scl, 1195 - z + 93.31 * scl 1219 + x + 65.47 * scl, 1220 + y + 43.98 * scl, 1221 + z + 96.56 * scl, 1222 + w + 23.56 * scl 1196 1223 ) * warp; 1224 + } 1225 + } else { 1226 + let scl = this.scale; 1227 + x += 1228 + this.getFBM( 1229 + x - 74.98 * scl, 1230 + y + 41.33 * scl, 1231 + undefined, 1232 + undefined, 1233 + true 1234 + ) * warp; 1235 + y += 1236 + this.getFBM( 1237 + x + 1.23 * scl, 1238 + y + 5.79 * scl, 1239 + undefined, 1240 + undefined, 1241 + true 1242 + ) * warp; 1243 + if (z != undefined) { 1197 1244 z += 1198 - this.warpNoise.get( 1245 + this.getFBM( 1199 1246 x + 11.47 * scl, 1200 1247 y + 17.98 * scl, 1201 - z + 23.56 * scl 1248 + z + 23.56 * scl, 1249 + undefined, 1250 + true 1251 + ) * warp; 1252 + } 1253 + if (w != undefined) { 1254 + w += 1255 + this.getFBM( 1256 + x + 54.47 * scl, 1257 + y + 34.98 * scl, 1258 + z + 76.56 * scl, 1259 + w + 45.98 * scl, 1260 + true 1202 1261 ) * warp; 1203 - } else { 1204 - let scl = this.scale; 1205 - x += 1206 - this.getFBM(x - 74.98 * scl, y + 41.33 * scl, z + 18.1 * scl, true) * 1207 - warp; 1208 - y += 1209 - this.getFBM(x + 1.23 * scl, y + 5.79 * scl, z + 9.31 * scl, true) * 1210 - warp; 1211 - z += 1212 - this.getFBM(x + 11.47 * scl, y + 17.98 * scl, z + 23.56 * scl, true) * 1213 - warp; 1214 1262 } 1215 1263 } 1216 1264 1217 1265 let warp2 = this.warp2; 1218 - if (warp2) { 1219 - if (this.warpNoise2) { 1220 - this.warpNoise2.pos.copy(this.pos); 1221 - let scl = this.warpNoise2.scale; 1222 - x += 1223 - this.warpNoise2.get(x + 1.23 * scl, y + 5.79 * scl, z + 9.31 * scl) * 1224 - warp2; 1225 - y += 1226 - this.warpNoise2.get( 1227 - x + 11.47 * scl, 1228 - y + 17.98 * scl, 1229 - z + 23.56 * scl 1230 - ) * warp2; 1266 + if (warp2 == undefined || warp2 == 0) return; 1267 + 1268 + if (this.warpNoise2) { 1269 + this.warpNoise2.pos.copy(this.pos); 1270 + let scl = this.warpNoise2.scale; 1271 + x += 1272 + this.warpNoise2.get(x + 1.23 * scl, y + 5.79 * scl, z + 9.31 * scl) * 1273 + warp2; 1274 + y += 1275 + this.warpNoise2.get(x + 11.47 * scl, y + 17.98 * scl, z + 23.56 * scl) * 1276 + warp2; 1277 + if (z != undefined) { 1231 1278 z += 1232 1279 this.warpNoise2.get( 1233 1280 x - 71.98 * scl, 1234 1281 y + 43.33 * scl, 1235 1282 z + 93.1 * scl 1236 1283 ) * warp2; 1237 - } else { 1238 - let scl = this.scale; 1239 - x += 1240 - this.getFBM(x + 11.47 * scl, y + 17.98 * scl, z + 23.56 * scl, true) * 1241 - warp2; 1242 - y += 1243 - this.getFBM(x - 73.98 * scl, y + 44.33 * scl, z + 15.13 * scl, true) * 1244 - warp2; 1284 + } 1285 + if (w != undefined) { 1286 + w += 1287 + this.warpNoise2.get( 1288 + x + 11.23 * scl, 1289 + y + 53.79 * scl, 1290 + z + 96.31 * scl, 1291 + w + 23.56 * scl 1292 + ) * warp2; 1293 + } 1294 + } else { 1295 + let scl = this.scale; 1296 + x += 1297 + this.getFBM( 1298 + x + 11.47 * scl, 1299 + y + 17.98 * scl, 1300 + undefined, 1301 + undefined, 1302 + true 1303 + ) * warp2; 1304 + y += 1305 + this.getFBM( 1306 + x - 73.98 * scl, 1307 + y + 44.33 * scl, 1308 + undefined, 1309 + undefined, 1310 + true 1311 + ) * warp2; 1312 + if (w != undefined) { 1245 1313 z += 1246 - this.getFBM(x + 11.23 * scl, y + 53.79 * scl, z + 96.31 * scl, true) * 1247 - warp2; 1314 + this.getFBM( 1315 + x + 11.23 * scl, 1316 + y + 53.79 * scl, 1317 + z + 96.31 * scl, 1318 + undefined, 1319 + true 1320 + ) * warp2; 1321 + } 1322 + if (w != undefined) { 1323 + w += 1324 + this.getFBM( 1325 + x + 11.23 * scl, 1326 + y + 53.79 * scl, 1327 + z + 96.31 * scl, 1328 + w + 23.56 * scl, 1329 + true 1330 + ) * warp2; 1248 1331 } 1249 1332 } 1250 1333 1251 - this.pos.set(x, y, z); 1334 + this.pos.set(x, y, z, w); 1252 1335 } 1253 1336 1254 - tilePosition(x, y, z) { 1337 + tilePosition() { 1338 + if (!this.tileX && !this.tileY) return; 1339 + 1340 + let x = this.x; 1341 + let y = this.y; 1255 1342 let newX = 0, 1256 1343 newY = 0, 1257 - newZ = 0; 1344 + newZ = 0, 1345 + newW = 0; 1258 1346 if (this.tileX) { 1259 1347 newX = Math.sin(x * Math.PI * 2); 1260 1348 newY = Math.cos(x * Math.PI * 2); 1261 1349 } 1262 1350 if (this.tileY) { 1263 - newY += Math.sin(y * Math.PI * 2); 1264 - newZ = Math.cos(y * Math.PI * 2); 1351 + newZ = Math.sin(y * Math.PI * 2); 1352 + newW = Math.cos(y * Math.PI * 2); 1353 + } 1354 + if (this.tileX && !this.tileY) { 1355 + this.pos.set(newX, newY + y); 1356 + } else if (this.tileY && !this.tileX) { 1357 + this.pos.set(newX + x, newY); 1358 + } else if (this.tileX && this.tileY) { 1359 + this.pos.set(newX, newY, newZ + this.z, newW + this.w); 1265 1360 } 1266 - this.pos.set( 1267 - (this.tileX ? 0 : x) + newX, 1268 - (this.tileY ? 0 : y) + newY, 1269 - z + newZ 1270 - ); 1271 1361 } 1272 1362 1273 1363 // main method, returns value between -1 and +1 1274 - getNoise(x, y, z) { 1364 + getNoise(x, y, z, w) { 1275 1365 x = x || 0; 1276 1366 y = y || 0; 1277 - z = z || 0; 1278 - this.pos.set(x + this.shift.x, y + this.shift.y, z + this.shift.z); 1367 + this.pos.set( 1368 + x + this.shift.x, 1369 + y + this.shift.y, 1370 + z != undefined ? z + this.shift.z : undefined, 1371 + w != undefined ? w + this.shift.w : undefined 1372 + ); 1279 1373 1280 - this.tilePosition(this.x, this.y, this.z); 1374 + this.tilePosition(); 1281 1375 this.warpPosition(this.x, this.y, this.z); 1282 1376 1283 - let norm = this.getFBM(this.x, this.y, this.z); 1377 + let norm = this.getFBM(this.x, this.y, this.z, this.w); 1284 1378 1285 1379 if (this.clamp) { 1286 1380 norm = Math.min(norm, 1); ··· 1361 1455 } 1362 1456 1363 1457 /** 1364 - * same as .getNorm but can only be called with x, y, z 1365 - * 1366 - * @param {number} x 1367 - * @param {number} y 1368 - * @param {number} z 1369 - * @returns {number} 1370 - */ 1371 - getNormXYZ(x, y, z) { 1372 - return this.getNoise(x, y, z); 1373 - } 1374 - /** 1375 1458 * function to get normalized noise value (between -1 and 1) 1376 1459 * can be called either with x, y, z or with a vector 1377 1460 * 1378 - * @param {number, object} vecOrX 1379 - * @param {number} y 1380 - * @param {number} z 1381 - * @returns {number} 1382 - */ 1383 - getNorm(vecOrX, y, z) { 1384 - if (typeof vecOrX == "number") { 1385 - return this.getNormXYZ(vecOrX, y, z); 1386 - } 1387 - return this.getNormXYZ(vecOrX.x, vecOrX.y, vecOrX.z); 1388 - } 1389 - 1390 - /** 1391 - * same as .get but can only be called with x, y, z 1392 - * 1393 1461 * @param {number} x 1394 1462 * @param {number} y 1395 1463 * @param {number} z 1464 + * @param {number} w 1396 1465 * @returns {number} 1397 1466 */ 1398 - getXYZ(x, y, z) { 1399 - return this.normToMinMax(this.getNormXYZ(x, y, z)); 1467 + getNorm(x, y, z, w) { 1468 + if (typeof x == "number") { 1469 + return this.getNoise(x, y, z, w); 1470 + } 1471 + return this.getNoise(x.x, x.y, x.z, x.w); 1400 1472 } 1401 1473 1402 1474 /** ··· 1404 1476 * will return value between min and max 1405 1477 * call either with x, y, z or with a vector 1406 1478 * 1407 - * @param {number, object} vecOrX 1479 + * @param {number} x 1408 1480 * @param {number} y 1409 1481 * @param {number} z 1482 + * @param {number} w 1410 1483 * @returns {number} 1411 1484 */ 1412 - get(vecOrX, y, z) { 1413 - if (typeof vecOrX == "number") { 1414 - return this.getXYZ(vecOrX, y, z); 1415 - } 1416 - return this.getXYZ(vecOrX.x, vecOrX.y, vecOrX.z); 1485 + get(x, y, z, w) { 1486 + return this.normToMinMax(this.getNorm(x, y, z, w)); 1417 1487 } 1418 1488 }