From 65f0f28289e92cb25d15d54ea52be3ae3a66ada7 Mon Sep 17 00:00:00 2001 From: Will Stott Date: Fri, 24 May 2024 20:17:07 +0100 Subject: [PATCH] Committing some uncommitted stuff --- docs/teshima/teshima.js | 194 ++++++++++++++++++++++++++++------------ 1 file changed, 136 insertions(+), 58 deletions(-) diff --git a/docs/teshima/teshima.js b/docs/teshima/teshima.js index 2c9e52e..73c0cee 100644 --- a/docs/teshima/teshima.js +++ b/docs/teshima/teshima.js @@ -25,7 +25,7 @@ class Matrix3D { if (this.array) { if (this.width !== width) { - array = new Uint8ClampedArray(width * height * this.stride); + array = new Uint16Array(width * height * this.stride); let cropRowLen = Math.min(this.width, width) * this.stride; let srcRowLen = this.width * this.stride; let dstRowLen = width * this.stride; @@ -34,12 +34,12 @@ class Matrix3D { array.set(this.array.subarray(i, i + cropRowLen), dstRowLen * y); } } else if (height > this.height) { - array = new Uint8ClampedArray(width * height * this.stride); + array = new Uint16Array(width * height * this.stride); array.set(this.array); // Grown } else array = this.array.slice(0, width * height * this.stride); // Shrunk } else - array = new Uint8ClampedArray(width * height * this.stride); + array = new Uint16Array(width * height * this.stride); if (this.height < height) rects.push([0, this.height, width, height - this.height]); @@ -69,7 +69,7 @@ class Matrix3D { return (arr[idx] += value) - orig; } - getElement(x, y, i, array) { + get(x, y, i, array) { return (array || this.array)[(y * this.width + x) * this.stride + i]; } @@ -194,37 +194,37 @@ class Concrete extends Matrix3D { var vy = 0; if (xi < this.width - 1) - vx += 255 - this.getElement(xi + 1, yi, 0); + vx += 255 - this.get(xi + 1, yi, 0); if (xi > 0) - vx -= 255 - this.getElement(xi - 1, yi, 0); + vx -= 255 - this.get(xi - 1, yi, 0); if (yi < this.width - 1) - vy += 255 - this.getElement(yi, yi + 1, 0); + vy += 255 - this.get(yi, yi + 1, 0); if (yi > 0) - vy -= 255 - this.getElement(yi, yi - 1, 0); + vy -= 255 - this.get(yi, yi - 1, 0); if (xi < this.width - 1 && yi < this.height - 1) { - let tr = (255 - this.getElement(xi + 1, yi + 1, 0)) * diagonal; + let tr = (255 - this.get(xi + 1, yi + 1, 0)) * diagonal; vx += tr; vy += tr; } if (xi < this.width - 1 && yi > 0) { - let br = (255 - this.getElement(xi + 1, yi - 1, 0)) * diagonal; + let br = (255 - this.get(xi + 1, yi - 1, 0)) * diagonal; vx += br; vy -= br; } if (xi > 0 && yi < this.height - 1) { - let tl = (255 - this.getElement(xi - 1, yi + 1, 0)) * diagonal; + let tl = (255 - this.get(xi - 1, yi + 1, 0)) * diagonal; vx -= tl; vy += tl; } if (xi > 0 && yi > 0) { - let bl = (255 - this.getElement(xi - 1, yi - 1, 0)) * diagonal; + let bl = (255 - this.get(xi - 1, yi - 1, 0)) * diagonal; vx -= bl; vy -= bl; } @@ -348,7 +348,7 @@ class Water extends Matrix3D { // Scale surplus for each to normalize to total surplus? // var adjSum = this.sumAdjacentLevels(x, y, orig)[0]; - // var val = this.getElement(x, y, 0); + // var val = this.get(x, y, 0); // var adjDelta = adjSum - val; // // Is there surplus in this wixel? // if (adjDelta > 0) @@ -358,13 +358,13 @@ class Water extends Matrix3D { // "Game of Life" - inspired algo // Empty wixels on low-ish areas of concrete become full if there's any nearby water. - // var val = this.getElement(x, y, 0, orig); + // var val = this.get(x, y, 0, orig); // var max = this.maxAdjacentLevel(x, y, orig); // if (max > val) // { // // Limit to concrete "height" - // // max = Math.min(concrete.getElement(x, y, 0), max); - // let conc = concrete.getElement(x, y, 0); + // // max = Math.min(concrete.get(x, y, 0), max); + // let conc = concrete.get(x, y, 0); // if (max > conc) // this.setElement(x, y, 0, max); // // else @@ -373,10 +373,10 @@ class Water extends Matrix3D { // Concrete Velocity Algo // Water flows out of wixels at the rate pre-caclucated in Concrete.bakeVelocities - // var val = this.getElement(x, y, 0, orig); + // var val = this.get(x, y, 0, orig); // if (val) // { - // var vx = (concrete.getElement(x, y, 1) - 128) * timeDelta; + // var vx = (concrete.get(x, y, 1) - 128) * timeDelta; // if (vx > 0) // { // if (x < this.width - 1) @@ -386,7 +386,7 @@ class Water extends Matrix3D { // else // vx = 0; - // var vy = (concrete.getElement(x, y, 2) - 128) * timeDelta; + // var vy = (concrete.get(x, y, 2) - 128) * timeDelta; // if (vy > 0) // { // if (y < this.height - 1) @@ -401,7 +401,7 @@ class Water extends Matrix3D { // Fill Lowest Wixel First, till average adjacent height matches // var adjMean; - // var v = this.getElement(x, y, 0/*, orig*/); + // var v = this.get(x, y, 0/*, orig*/); // while((adjMean = this.meanAdjacentLevels(x, y)) < v) // { // let [min, mx, my] = this.minAdjacentWixel(x, y); @@ -414,17 +414,24 @@ class Water extends Matrix3D { // this.setElement(x, y, 0, v); // Mean With Lowest Wixel - var min, mx, my, v = this.getElement(x, y, 0/*, orig*/); - while(([min, mx, my] = this.minAdjacentWixel(x, y))[0] < v) + const cv = concrete.get(x, y, 0); + var min, mx, my, v = this.get(x, y, 0/*, orig*/); + v += cv; + while(([min, mx, my] = this.minAdjacentWixelWithConcrete(x, y))[0] < v) { - let avg = (v + min) / 2; - let v1 = Math.floor(avg); + let sum = v + min; + let avg = sum / 2; + let v1 = Math.ceil(avg); if (v1 === v) break; + if (v1 < cv) + v1 = cv; v = v1; - this.setElement(mx, my, 0, Math.ceil(avg)); + this.setElement(mx, my, 0, sum - v); + if (v <= cv) + break; } - this.setElement(x, y, 0, v); + this.setElement(x, y, 0, v - cv); } sumAdjacentLevels(x, y, array) { @@ -434,39 +441,39 @@ class Water extends Matrix3D { { if (x > 0) { - s += this.getElement(x - 1, y - 1, 0, array); + s += this.get(x - 1, y - 1, 0, array); cnt++; } - s += this.getElement(x, y - 1, 0, array); + s += this.get(x, y - 1, 0, array); cnt++; if (x < this.width - 1) { - s += this.getElement(x + 1, y - 1, 0, array); + s += this.get(x + 1, y - 1, 0, array); cnt++; } } if (x > 0) { - s += this.getElement(x - 1, y, 0, array); + s += this.get(x - 1, y, 0, array); cnt++; } if (x < this.width - 1) { - s += this.getElement(x + 1, y, 0, array); + s += this.get(x + 1, y, 0, array); cnt++; } if (y < this.height - 1) { if (x > 0) { - s += this.getElement(x - 1, y + 1, 0, array); + s += this.get(x - 1, y + 1, 0, array); cnt++; } - s += this.getElement(x, y + 1, 0, array); + s += this.get(x, y + 1, 0, array); cnt++; if (x < this.width - 1) { - s += this.getElement(x + 1, y + 1, 0, array); + s += this.get(x + 1, y + 1, 0, array); cnt++; } } @@ -482,29 +489,29 @@ class Water extends Matrix3D { if (y > 0) { if (x > 0) - if (this.getElement(x - 1, y - 1, 0, array)) + if (this.get(x - 1, y - 1, 0, array)) return true; - if (this.getElement(x, y - 1, 0, array)) + if (this.get(x, y - 1, 0, array)) return true; if (x < this.width - 1) - if (this.getElement(x + 1, y - 1, 0, array)) + if (this.get(x + 1, y - 1, 0, array)) return true; } if (x > 0) - if (this.getElement(x - 1, y, 0, array)) + if (this.get(x - 1, y, 0, array)) return true; if (x < this.width - 1) - if (this.getElement(x + 1, y, 0, array)) + if (this.get(x + 1, y, 0, array)) return true; if (y < this.height - 1) { if (x > 0) - if (this.getElement(x - 1, y + 1, 0, array)) + if (this.get(x - 1, y + 1, 0, array)) return true; - if (this.getElement(x, y + 1, 0, array)) + if (this.get(x, y + 1, 0, array)) return true; if (x < this.width - 1) - if (this.getElement(x + 1, y + 1, 0, array)) + if (this.get(x + 1, y + 1, 0, array)) return true; } return false; @@ -517,22 +524,22 @@ class Water extends Matrix3D { if (y > 0) { if (x > 0) - s = Math.max(this.getElement(x - 1, y - 1, 0, array), s); - s = Math.max(this.getElement(x, y - 1, 0, array), s); + s = Math.max(this.get(x - 1, y - 1, 0, array), s); + s = Math.max(this.get(x, y - 1, 0, array), s); if (x < w2) - s = Math.max(this.getElement(x + 1, y - 1, 0, array), s); + s = Math.max(this.get(x + 1, y - 1, 0, array), s); } if (x > 0) - s = Math.max(this.getElement(x - 1, y, 0, array), s); + s = Math.max(this.get(x - 1, y, 0, array), s); if (x < w2) - s = Math.max(this.getElement(x + 1, y, 0, array), s); + s = Math.max(this.get(x + 1, y, 0, array), s); if (y < h2) { if (x > 0) - s = Math.max(this.getElement(x - 1, y + 1, 0, array), s); - s = Math.max(this.getElement(x, y + 1, 0, array), s); + s = Math.max(this.get(x - 1, y + 1, 0, array), s); + s = Math.max(this.get(x, y + 1, 0, array), s); if (x < w2) - s = Math.max(this.getElement(x + 1, y + 1, 0, array), s); + s = Math.max(this.get(x + 1, y + 1, 0, array), s); } return s; } @@ -547,20 +554,90 @@ class Water extends Matrix3D { if (y > 0) { if (x > 0) - if ((v = this.getElement(x - 1, y - 1, 0, array)) < m) + if ((v = this.get(x - 1, y - 1, 0, array)) < m) + { + m = v; + rx = x - 1; + ry = y - 1; + } + if ((v = this.get(x, y - 1, 0, array)) < m) + { + m = v; + rx = x; + ry = y - 1; + } + if (x < w2) + if ((v = this.get(x + 1, y - 1, 0, array)) < m) + { + m = v; + rx = x + 1; + ry = y - 1; + } + } + if (x > 0) + if ((v = this.get(x - 1, y, 0, array)) < m) + { + m = v; + rx = x - 1; + ry = y; + } + if (x < w2) + if ((v = this.get(x + 1, y, 0, array)) < m) + { + m = v; + rx = x + 1; + ry = y; + } + if (y < h2) + { + if (x > 0) + if ((v = this.get(x - 1, y + 1, 0, array)) < m) + { + m = v; + rx = x - 1; + ry = y + 1; + } + if ((v = this.get(x, y + 1, 0, array)) < m) + { + m = v; + rx = x; + ry = y + 1; + } + if (x < w2) + if ((v = this.get(x + 1, y + 1, 0, array)) < m) + { + m = v; + rx = x + 1; + ry = y + 1; + } + } + return [m, rx, ry]; + } + + minAdjacentWixelWithConcrete(x, y, array) { + var m = 255; + var v; + const w2 = this.width - 1; + const h2 = this.height - 1; + var rx = 0; + var ry = 0; + if (y > 0) + { + if (x > 0) + if ((v = this.get(x - 1, y - 1, 0, array) + concrete.get(x - 1, y - 1, 0)) < m) { m = v; rx = x - 1; ry = y - 1; } - if ((v = this.getElement(x, y - 1, 0, array)) < m) + if ((v = this.get(x, y - 1, 0, array) + concrete.get(x, y - 1, 0)) < m) { m = v; rx = x; ry = y - 1; } if (x < w2) - if ((v = this.getElement(x + 1, y - 1, 0, array)) < m) + if ((v = this.get(x + 1, y - 1, 0, array) + concrete.get(x + 1, y - 1, 0)) < m) { m = v; rx = x + 1; @@ -568,14 +645,14 @@ class Water extends Matrix3D { } } if (x > 0) - if ((v = this.getElement(x - 1, y, 0, array)) < m) + if ((v = this.get(x - 1, y, 0, array) + concrete.get(x - 1, y, 0)) < m) { m = v; rx = x - 1; ry = y; } if (x < w2) - if ((v = this.getElement(x + 1, y, 0, array)) < m) + if ((v = this.get(x + 1, y, 0, array) + concrete.get(x + 1, y, 0)) < m) { m = v; rx = x + 1; @@ -584,20 +661,20 @@ class Water extends Matrix3D { if (y < h2) { if (x > 0) - if ((v = this.getElement(x - 1, y + 1, 0, array)) < m) + if ((v = this.get(x - 1, y + 1, 0, array) + concrete.get(x - 1, y + 1, 0)) < m) { m = v; rx = x - 1; ry = y + 1; } - if ((v = this.getElement(x, y + 1, 0, array)) < m) + if ((v = this.get(x, y + 1, 0, array) + concrete.get(x, y + 1, 0)) < m) { m = v; rx = x; ry = y + 1; } if (x < w2) - if ((v = this.getElement(x + 1, y + 1, 0, array)) < m) + if ((v = this.get(x + 1, y + 1, 0, array) + concrete.get(x + 1, y + 1, 0)) < m) { m = v; rx = x + 1; @@ -756,6 +833,7 @@ ready(function () lastTime = timestamp; waterRenderer.tick(deltaTime / 1000); + // console.debug(water.stats(0).mean); waterRenderer.render(); window.requestAnimationFrame(step);