Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- var __imul = Math.imul ? Math.imul : function(a, b) {
- return (a * (b >>> 16) << 16) + a * (b & 65535) | 0;
- };
- function assert(truth) {
- if (!truth) {
- throw Error('Assertion failed');
- }
- }
- function main() {
- var noiser = new OSimplexNoise();
- noiser.setSeed1();
- tileset.src = 'tileset.png';
- tileset.onload = function() {
- var can = document.getElementById('app');
- var ctx = can.getContext('2d');
- can.width = 200;
- can.height = 200;
- for (var x = 0, count1 = can.width; x < count1; x = x + 1 | 0) {
- for (var y = 0, count = can.height; y < count; y = y + 1 | 0) {
- var a = noiser.noise2D(x / 100 | 0, y / 100 | 0);
- a = (a + 1) * 128;
- var b = a | 0;
- ctx.fillStyle = 'rgb(' + b.toString() + ', ' + b.toString() + ', ' + b.toString() + ')';
- ctx.fillRect(x, y, 1, 1);
- }
- }
- return;
- for (var y1 = 0, count3 = in_List.count(map); y1 < count3; y1 = y1 + 1 | 0) {
- for (var x1 = 0, count2 = in_List.count(in_List.get(map, y1)); x1 < count2; x1 = x1 + 1 | 0) {
- ctx.drawImage(tileset, __imul(in_List.get(in_List.get(map, y1), x1), CELL_SIZE), 0, CELL_SIZE, CELL_SIZE, __imul(x1, CELL_SIZE), __imul(y1, CELL_SIZE), CELL_SIZE, CELL_SIZE);
- }
- }
- };
- }
- function OSimplexNoise() {
- this.seed = 0;
- this.randArray = null;
- this.seed = 0;
- this.randArray = new Int8Array(256);
- }
- OSimplexNoise.prototype.setSeed1 = function() {
- for (var i = 0; i < 256; i = i + 1 | 0) {
- this.randArray[i] = Math.random() * 256;
- }
- };
- OSimplexNoise.prototype.extrapolate2D = function(xsb, ysb, dx, dy) {
- var gradients = new Int8Array([5, 2, 2, 5, -5, 2, -2, 5, 5, -2, 2, -5, -5, -2, -2, -5]);
- var idx = this.randArray[this.randArray[xsb & 255] + ysb & 255] & 14;
- return gradients[idx] * dx + gradients[idx + 1] * dy;
- };
- OSimplexNoise.prototype.noise2D = function(x, y) {
- x = +x;
- y = +y;
- var stretchOffset = (x + y) * OSimplexNoise.STRETCH_CONSTANT_2D;
- var xs = x + stretchOffset;
- var ys = y + stretchOffset;
- var xsb = Math.floor(xs) | 0;
- var ysb = Math.floor(ys) | 0;
- var squishOffset = (xsb + ysb | 0) * OSimplexNoise.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 = 0;
- var dyExt = 0;
- var xsvExt = 0;
- var ysvExt = 0;
- var value = 0;
- var dx1 = dx0 - 1 * OSimplexNoise.SQUISH_CONSTANT_2D;
- var dy1 = dy0 - 0 - OSimplexNoise.SQUISH_CONSTANT_2D;
- var attn1 = 2 - dx1 * dx1 - dy1 * dy1;
- if (attn1 > 0) {
- attn1 *= attn1;
- value += attn1 * attn1 * this.extrapolate2D(xsb + 1 | 0, ysb + 0 | 0, dx1, dy1);
- }
- var dx2 = dx0 - 0 * OSimplexNoise.SQUISH_CONSTANT_2D;
- var dy2 = dy0 - 1 - OSimplexNoise.SQUISH_CONSTANT_2D;
- var attn2 = 2 - dx2 * dx2 - dy2 * dy2;
- if (attn2 > 0) {
- attn2 *= attn2;
- value += attn2 * attn2 * this.extrapolate2D(xsb + 0 | 0, ysb + 1 | 0, dx2, dy2);
- }
- if (inSum <= 1) {
- var zins = 1 - inSum;
- if (zins > xins || zins > yins) {
- if (xins > yins) {
- xsvExt = xsb + 1 | 0;
- ysvExt = ysb - 1 | 0;
- dxExt = dx0 - 1;
- dyExt = dy0 + 1;
- }
- else {
- xsvExt = xsb - 1 | 0;
- ysvExt = ysb + 1 | 0;
- dxExt = dx0 + 1;
- dyExt = dy0 - 1;
- }
- }
- else {
- xsvExt = xsb + 1 | 0;
- ysvExt = ysb + 1 | 0;
- dxExt = dx0 - 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- dyExt = dy0 - 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- }
- }
- else {
- var zins1 = 2 - inSum;
- if (zins1 < xins || zins1 < yins) {
- if (xins > yins) {
- xsvExt = xsb + 2 | 0;
- ysvExt = ysb + 0 | 0;
- dxExt = dx0 - 2 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- dyExt = dy0 + 0 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- }
- else {
- xsvExt = xsb + 0 | 0;
- ysvExt = ysb + 2 | 0;
- dxExt = dx0 + 0 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- dyExt = dy0 - 2 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- }
- }
- else {
- dxExt = dx0;
- dyExt = dy0;
- xsvExt = xsb;
- ysvExt = ysb;
- }
- xsb = xsb + 1 | 0;
- ysb = ysb + 1 | 0;
- dx0 -= 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- dy0 -= 1 - 2 * OSimplexNoise.SQUISH_CONSTANT_2D;
- }
- var attn0 = 2 - dx0 * dx0 - dy0 * dy0;
- if (attn0 > 0) {
- attn0 *= attn0;
- value += attn0 * attn0 * this.extrapolate2D(xsb, ysb, dx0, dy0);
- }
- var attnExt = 2 - dxExt * dxExt - dyExt * dyExt;
- if (attnExt > 0) {
- attnExt *= attnExt;
- value += attnExt * attnExt * this.extrapolate2D(xsvExt, ysvExt, dxExt, dyExt);
- }
- return value / OSimplexNoise.NORM_CONSTANT_2D;
- };
- var in_List = {};
- in_List.get = function(self, index) {
- assert(0 <= index && index < in_List.count(self));
- return self[index];
- };
- in_List.count = function(self) {
- return self.length;
- };
- var map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 7, 3, 6, 1, 1, 1, 1, 1, 1, 1, 7, 3, 6, 1], [1, 2, 0, 4, 1, 1, 1, 1, 1, 1, 7, 16, 0, 4, 1], [1, 2, 0, 4, 1, 1, 1, 1, 1, 7, 16, 0, 0, 4, 1], [1, 9, 15, 17, 3, 6, 1, 1, 7, 16, 0, 0, 0, 4, 1], [1, 1, 9, 15, 0, 4, 1, 7, 20, 5, 5, 5, 5, 8, 1], [1, 1, 1, 2, 0, 4, 1, 2, 4, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 0, 4, 1, 2, 4, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 0, 0, 0, 0, 8, 1, 1, 1, 1, 1, 1], [1, 1, 1, 2, 0, 4, 1, 1, 1, 1, 1, 7, 6, 1, 1], [1, 1, 1, 2, 0, 17, 3, 3, 3, 3, 3, 16, 4, 1, 1], [1, 7, 3, 5, 15, 0, 0, 18, 5, 5, 5, 5, 19, 6, 1], [1, 9, 15, 1, 2, 5, 15, 4, 1, 1, 1, 1, 2, 4, 1], [1, 1, 10, 1, 10, 1, 9, 8, 1, 1, 1, 1, 9, 8, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];
- var CELL_SIZE = 16;
- var tileset = new Image();
- // (1/sqrt(2+1)-1)/2
- OSimplexNoise.STRETCH_CONSTANT_2D = -0.2113248654051871;
- // (sqrt(2+1)-1)/2
- OSimplexNoise.SQUISH_CONSTANT_2D = 0.36602540378443865;
- OSimplexNoise.NORM_CONSTANT_2D = 47;
- main();
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement