diff --git a/package-lock.json b/package-lock.json index b87683d29..fafa8ce21 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "scratch-render", - "version": "0.1.0", + "version": "0.1.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "scratch-render", - "version": "0.1.0", + "version": "0.1.1", "license": "MPL-2.0", "dependencies": { "@turbowarp/ancient-hull.js": "^0.2.13", diff --git a/package.json b/package.json index 4e63caf68..98b4288cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scratch-render", - "version": "0.1.0", + "version": "0.1.1", "description": "WebGL Renderer for Scratch 3.0", "author": "Massachusetts Institute of Technology", "license": "MPL-2.0", diff --git a/src/Drawable.js b/src/Drawable.js index 1274c8dea..4f636ee8e 100644 --- a/src/Drawable.js +++ b/src/Drawable.js @@ -4,6 +4,8 @@ const Rectangle = require('./Rectangle'); const RenderConstants = require('./RenderConstants'); const ShaderManager = require('./ShaderManager'); const EffectTransform = require('./EffectTransform'); +const Silhouette = require('./Silhouette'); +const Skin = require('./Skin'); const log = require('./util/log'); /** @@ -15,6 +17,27 @@ const __isTouchingPosition = twgl.v3.create(); const __hullProjection = twgl.m4.ortho(-1, 1, -1, 1, -1, 1); const __hullTransform = twgl.m4.identity(); const FLOATING_POINT_ERROR_ALLOWANCE = 1e-6; +const SHAPE_EFFECTS_MASK = ShaderManager.EFFECT_INFO.fisheye.mask | + ShaderManager.EFFECT_INFO.whirl.mask | + ShaderManager.EFFECT_INFO.pixelate.mask | + ShaderManager.EFFECT_INFO.mosaic.mask; + +const alphaAt = (data, width, height, x, y) => { + if (x >= width || y >= height || x < 0 || y < 0) { + return 0; + } + return data[(((y * width) + x) * 4) + 3]; +}; + +const touchesTexel = (data, width, height, linear, x, y) => { + if (linear) { + return alphaAt(data, width, height, x, y) > 0 || + alphaAt(data, width, height, x + 1, y) > 0 || + alphaAt(data, width, height, x, y + 1) > 0 || + alphaAt(data, width, height, x + 1, y + 1) > 0; + } + return alphaAt(data, width, height, x, y) > 0; +}; /** * Convert a scratch space location into a texture space float. Uses the @@ -109,6 +132,8 @@ class Drawable { this._skinScaleDirty = true; this._inverseMatrix = twgl.m4.identity(); this._inverseTransformDirty = true; + this._snappedFastBounds = new Rectangle(); + this._snappedFastBoundsDirty = true; this._visible = true; /** A bitmask identifying which effects are currently in use. @@ -159,6 +184,7 @@ class Drawable { this._transformDirty = true; this._inverseTransformDirty = true; this._transformedHullDirty = true; + this._snappedFastBoundsDirty = true; } /** @@ -480,6 +506,7 @@ class Drawable { */ setConvexHullDirty () { this._convexHullDirty = true; + this._snappedFastBoundsDirty = true; } /** @@ -497,6 +524,7 @@ class Drawable { this._transformedHullPoints.push(twgl.v3.create()); } this._transformedHullDirty = true; + this._snappedFastBoundsDirty = true; } /** @@ -609,6 +637,14 @@ class Drawable { return this.getAABB(result); } + getSnappedFastBounds () { + if (this._snappedFastBoundsDirty) { + this.getFastBounds(this._snappedFastBounds).snapToInt(); + this._snappedFastBoundsDirty = false; + } + return this._snappedFastBounds; + } + /** * Transform all the convex hull points by the current Drawable's * transform. This allows us to skip recalculating the convex hull @@ -681,6 +717,155 @@ class Drawable { } } + getPlainTouchingSilhouette () { + const skin = this._skin; + if (!skin || (this.enabledEffects & SHAPE_EFFECTS_MASK) !== 0) { + return null; + } + if (this.isTouching !== this._isTouchingNearest && this.isTouching !== this._isTouchingLinear) { + return null; + } + if (skin.isTouchingNearest !== Skin.prototype.isTouchingNearest || + skin.isTouchingLinear !== Skin.prototype.isTouchingLinear) { + return null; + } + const silhouette = skin._silhouette; + if (!silhouette || + silhouette.isTouchingNearest !== Silhouette.prototype.isTouchingNearest || + silhouette.isTouchingLinear !== Silhouette.prototype.isTouchingLinear) { + return null; + } + return silhouette; + } + + getPlainColorSilhouette () { + const skin = this._skin; + if (!skin || (this.enabledEffects & SHAPE_EFFECTS_MASK) !== 0) { + return null; + } + const silhouette = skin._silhouette; + if (!silhouette || silhouette.colorAtNearest !== Silhouette.prototype.colorAtNearest) { + return null; + } + return silhouette; + } + + static isTouchingPlain (matrix, data, width, height, linear, x, y) { + const d = (x * matrix[3]) + (y * matrix[7]) + matrix[15]; + let localX = Math.fround(0.5 - ((((x * matrix[0]) + (y * matrix[4])) + matrix[12]) / d)); + let localY = Math.fround(((((x * matrix[1]) + (y * matrix[5])) + matrix[13]) / d) + 0.5); + if (Math.abs(localX) < FLOATING_POINT_ERROR_ALLOWANCE) localX = 0; + if (Math.abs(localY) < FLOATING_POINT_ERROR_ALLOWANCE) localY = 0; + return touchesTexel(data, width, height, linear, + Math.floor(localX * (width - 1)), Math.floor(localY * (height - 1))); + } + + static samplePlainColor4b (x, y, drawable, silhouette, dst) { + const matrix = drawable._inverseMatrix; + const d = (x * matrix[3]) + (y * matrix[7]) + matrix[15]; + let localX = Math.fround(0.5 - ((((x * matrix[0]) + (y * matrix[4])) + matrix[12]) / d)); + let localY = Math.fround(((((x * matrix[1]) + (y * matrix[5])) + matrix[13]) / d) + 0.5); + if (Math.abs(localX) < FLOATING_POINT_ERROR_ALLOWANCE) localX = 0; + if (Math.abs(localY) < FLOATING_POINT_ERROR_ALLOWANCE) localY = 0; + if (localX < 0 || localY < 0 || localX > 1 || localY > 1) { + dst[0] = 0; + dst[1] = 0; + dst[2] = 0; + dst[3] = 0; + return dst; + } + const color = silhouette._getColor( + silhouette, + Math.floor(localX * (silhouette._width - 1)), + Math.floor(localY * (silhouette._height - 1)), + dst + ); + if (drawable.enabledEffects === 0) return color; + return EffectTransform.transformColor(drawable, color); + } + + static isTouchingPair (first, second, bounds, point) { + const firstSilhouette = first.getPlainTouchingSilhouette(); + const secondSilhouette = second.getPlainTouchingSilhouette(); + if (firstSilhouette === null || secondSilhouette === null) { + for (let x = bounds.left; x <= bounds.right; x++) { + point[0] = x; + for (let y = bounds.bottom; y <= bounds.top; y++) { + point[1] = y; + if (first.isTouching(point) && second.isTouching(point)) { + return true; + } + } + } + return false; + } + + const firstData = firstSilhouette._colorData; + const secondData = secondSilhouette._colorData; + if (!firstData || !secondData) { + return false; + } + const firstWidth = firstSilhouette._width; + const firstHeight = firstSilhouette._height; + const firstLinear = first.isTouching === first._isTouchingLinear; + const secondWidth = secondSilhouette._width; + const secondHeight = secondSilhouette._height; + const secondLinear = second.isTouching === second._isTouchingLinear; + const a = first._inverseMatrix; + const a0 = a[0]; + const a1 = a[1]; + const a3 = a[3]; + const a4 = a[4]; + const a5 = a[5]; + const a7 = a[7]; + const a12 = a[12]; + const a13 = a[13]; + const a15 = a[15]; + const b = second._inverseMatrix; + const b0 = b[0]; + const b1 = b[1]; + const b3 = b[3]; + const b4 = b[4]; + const b5 = b[5]; + const b7 = b[7]; + const b12 = b[12]; + const b13 = b[13]; + const b15 = b[15]; + const left = bounds.left; + const right = bounds.right; + const bottom = bounds.bottom; + const top = bounds.top; + + for (let column = left; column <= right; column++) { + const x = Math.fround(column); + const ax0 = x * a0; + const ax1 = x * a1; + const ax3 = x * a3; + for (let row = bottom; row <= top; row++) { + const y = Math.fround(row); + const ad = ax3 + (y * a7) + a15; + let ax = Math.fround(0.5 - ((ax0 + (y * a4) + a12) / ad)); + let ay = Math.fround(((ax1 + (y * a5) + a13) / ad) + 0.5); + if (Math.abs(ax) < FLOATING_POINT_ERROR_ALLOWANCE) ax = 0; + if (Math.abs(ay) < FLOATING_POINT_ERROR_ALLOWANCE) ay = 0; + if (!touchesTexel(firstData, firstWidth, firstHeight, firstLinear, + Math.floor(ax * (firstWidth - 1)), Math.floor(ay * (firstHeight - 1)))) { + continue; + } + const bd = (x * b3) + (y * b7) + b15; + let bx = Math.fround(0.5 - (((x * b0) + (y * b4) + b12) / bd)); + let by = Math.fround((((x * b1) + (y * b5) + b13) / bd) + 0.5); + if (Math.abs(bx) < FLOATING_POINT_ERROR_ALLOWANCE) bx = 0; + if (Math.abs(by) < FLOATING_POINT_ERROR_ALLOWANCE) by = 0; + if (touchesTexel(secondData, secondWidth, secondHeight, secondLinear, + Math.floor(bx * (secondWidth - 1)), Math.floor(by * (secondHeight - 1)))) { + return true; + } + } + } + return false; + } + /** * Respond to an internal change in the current Skin. */ diff --git a/src/RenderWebGL.js b/src/RenderWebGL.js index acf2b8c7d..96995da76 100644 --- a/src/RenderWebGL.js +++ b/src/RenderWebGL.js @@ -22,11 +22,11 @@ const log = require('./util/log'); const __isTouchingDrawablesPoint = twgl.v3.create(); const __candidatesBounds = new Rectangle(); const __touchingBounds = new Rectangle(); -const __candidateBounds = new Rectangle(); const __fenceBounds = new Rectangle(); const __touchingColor = new Uint8ClampedArray(4); const __blendColor = new Uint8ClampedArray(4); const __drawableScale = [0, 0]; +const __nativeScale = [100, 100]; const __projectionUniforms = {u_projectionMatrix: null}; // More pixels than this and we give up to the GPU and take the cost of readPixels @@ -212,7 +212,8 @@ class RenderWebGL extends EventEmitter { /** @type {Array} */ this._drawList = []; - this._intersectionPool = []; + this._candidatePool = []; + this._candidatePoolUsed = 0; // A list of layer group names in the order they should appear // from furthest back to furthest in front. @@ -723,6 +724,7 @@ class RenderWebGL extends EventEmitter { oldSkin.dispose(); } delete this._allSkins[skinId]; + this._releaseCandidatePool(); } /** @@ -839,6 +841,7 @@ class RenderWebGL extends EventEmitter { const drawable = this._allDrawables[drawableID]; drawable.dispose(); delete this._allDrawables[drawableID]; + this._releaseCandidatePool(); const currentLayerGroup = this._layerGroups[group]; const endIndex = this._endIndexForKnownLayerGroup(currentLayerGroup); @@ -1189,25 +1192,48 @@ class RenderWebGL extends EventEmitter { drawable.updateCPURenderAttributes(); + for (let index = 0; index < candidates.length; index++) { + const candidate = candidates[index]; + candidate.colorSilhouette = candidate.drawable.getPlainColorSilhouette(); + } + const plainSilhouette = hasMask ? null : drawable.getPlainTouchingSilhouette(); + const plainData = plainSilhouette === null ? null : plainSilhouette._colorData; + const plainMatrix = drawable._inverseMatrix; + const plainWidth = plainData ? plainSilhouette._width : 0; + const plainHeight = plainData ? plainSilhouette._height : 0; + const plainLinear = drawable.isTouching === drawable._isTouchingLinear; + // Masked drawable ignores ghost effect const effectMask = ~ShaderManager.EFFECT_INFO.ghost.mask; + const left = bounds.left; + const right = bounds.right; + const bottom = bounds.bottom; + const top = bounds.top; + const pixelsPerRow = bounds.width * (candidates.length + 1); + // Scratch Space - +y is top - for (let y = bounds.bottom; y <= bounds.top; y++) { - if (bounds.width * (y - bounds.bottom) * (candidates.length + 1) >= maxPixelsForCPU) { - return this._isTouchingColorGpuFin(bounds, color3b, y - bounds.bottom); + for (let y = bottom; y <= top; y++) { + if (pixelsPerRow * (y - bottom) >= maxPixelsForCPU) { + return this._isTouchingColorGpuFin(bounds, color3b, y - bottom); } - for (let x = bounds.left; x <= bounds.right; x++) { + for (let x = left; x <= right; x++) { point[1] = y; point[0] = x; - // if we use a mask, check our sample color... - if (hasMask ? - maskMatches(Drawable.sampleColor4b(point, drawable, color, effectMask), mask3b) : - drawable.isTouching(point)) { - this.sampleColor4b(point, candidates, color); + let touching; + if (plainData) { + touching = Drawable.isTouchingPlain( + plainMatrix, plainData, plainWidth, plainHeight, plainLinear, point[0], point[1]); + } else if (hasMask) { + touching = maskMatches(Drawable.sampleColor4b(point, drawable, color, effectMask), mask3b); + } else { + touching = drawable.isTouching(point); + } + if (touching) { + this._sampleCandidatesColor4b(point, candidates, color); if (debugCanvasContext) { debugCanvasContext.fillStyle = `rgb(${color[0]},${color[1]},${color[2]})`; - debugCanvasContext.fillRect(x - bounds.left, bounds.bottom - y, 1, 1); + debugCanvasContext.fillRect(x - left, bottom - y, 1, 1); } // ...and the target color is drawn at this pixel if (colorMatches(color, color3b, 0)) { @@ -1362,17 +1388,9 @@ class RenderWebGL extends EventEmitter { // This is an EXTREMELY brute force collision detector, but it is // still faster than asking the GPU to give us the pixels. for (let index = 0; index < candidates.length; index++) { - const candidate = candidates[index].drawable; - const intersection = candidates[index].intersection; - for (let x = intersection.left; x <= intersection.right; x++) { - // Scratch Space - +y is top - point[0] = x; - for (let y = intersection.bottom; y <= intersection.top; y++) { - point[1] = y; - if (drawable.isTouching(point) && candidate.isTouching(point)) { - return true; - } - } + const candidate = candidates[index]; + if (Drawable.isTouchingPair(drawable, candidate.drawable, candidate.intersection, point)) { + return true; } } @@ -1729,7 +1747,7 @@ class RenderWebGL extends EventEmitter { const drawable = this._allDrawables[drawableID]; /** @todo remove this once URL-based skin setting is removed. */ - if (!drawable.skin || !drawable.skin.getTexture([100, 100])) return null; + if (!drawable.skin || !drawable.skin.getTexture(__nativeScale)) return null; const bounds = drawable.getFastBounds(__touchingBounds); @@ -1762,46 +1780,67 @@ class RenderWebGL extends EventEmitter { if (bounds === null) { return result; } - const pool = this._intersectionPool; + const pool = this._candidatePool; + const allDrawables = this._allDrawables; + const allowPrivateSkinAccess = this.allowPrivateSkinAccess; + const left = bounds.left; + const right = bounds.right; + const bottom = bounds.bottom; + const top = bounds.top; // iterate through the drawables list BACKWARDS - we want the top most item to be the first we check for (let index = candidateIDs.length - 1; index >= 0; index--) { const id = candidateIDs[index]; if (id !== drawableID) { - const drawable = this._allDrawables[id]; + const drawable = allDrawables[id]; if (!drawable) continue; - // Text bubbles aren't considered in "touching" queries - if (drawable.skin instanceof TextBubbleSkin) continue; - if (drawable.skin && drawable._visible) { + const skin = drawable._skin; + if (!skin || !drawable._visible) continue; + + // Push bounds out to integers. If a drawable extends out into half a pixel, that half-pixel still + // needs to be tested. Plus, in some areas we construct another rectangle from the union of these, + // and iterate over its pixels (width * height). Turns out that doesn't work so well when the + // width/height aren't integers. + const candidateBounds = drawable._snappedFastBoundsDirty ? + drawable.getSnappedFastBounds() : drawable._snappedFastBounds; + + if (left <= candidateBounds.right && + candidateBounds.left <= right && + top >= candidateBounds.bottom && + candidateBounds.top >= bottom) { + // Text bubbles aren't considered in "touching" queries + if (skin instanceof TextBubbleSkin) continue; // If private skin access is disabled, do not allow projects to use touching blocks to guess the // contents of a private skin. - if (!this.allowPrivateSkinAccess && drawable.skin.private) continue; - - const candidateBounds = drawable.getFastBounds(__candidateBounds); - - // Push bounds out to integers. If a drawable extends out into half a pixel, that half-pixel still - // needs to be tested. Plus, in some areas we construct another rectangle from the union of these, - // and iterate over its pixels (width * height). Turns out that doesn't work so well when the - // width/height aren't integers. - candidateBounds.snapToInt(); - - if (bounds.intersects(candidateBounds)) { - // Update the CPU position data - drawable.updateCPURenderAttributes(); - if (result.length >= pool.length) { - pool.push(new Rectangle()); - } - result.push({ - id, - drawable, - intersection: Rectangle.intersect(bounds, candidateBounds, pool[result.length]) - }); + if (!allowPrivateSkinAccess && skin.private) continue; + // Update the CPU position data + drawable.updateCPURenderAttributes(); + if (result.length >= pool.length) { + pool.push({id: 0, drawable: null, intersection: new Rectangle(), colorSilhouette: null}); } + const candidate = pool[result.length]; + candidate.id = id; + candidate.drawable = drawable; + candidate.colorSilhouette = null; + Rectangle.intersect(bounds, candidateBounds, candidate.intersection); + result.push(candidate); } } } + if (result.length > this._candidatePoolUsed) { + this._candidatePoolUsed = result.length; + } return result; } + _releaseCandidatePool() { + const pool = this._candidatePool; + for (let index = 0; index < this._candidatePoolUsed; index++) { + pool[index].drawable = null; + pool[index].colorSilhouette = null; + } + this._candidatePoolUsed = 0; + } + /** * Helper to get the union bounds from a set of candidates returned from the above method * @private @@ -1809,13 +1848,13 @@ class RenderWebGL extends EventEmitter { * @return {Rectangle} the outer bounding box union */ _candidatesBounds(candidates) { - return candidates.reduce((memo, { intersection }) => { - if (!memo) { - return intersection; - } + let bounds = null; + for (let index = 0; index < candidates.length; index++) { + const intersection = candidates[index].intersection; // store the union of the two rectangles in our static rectangle instance - return Rectangle.union(memo, intersection, __candidatesBounds); - }, null); + bounds = bounds ? Rectangle.union(bounds, intersection, __candidatesBounds) : intersection; + } + return bounds; } /** @@ -2484,6 +2523,39 @@ class RenderWebGL extends EventEmitter { return dst; } + _sampleCandidatesColor4b(vec, candidates, dst) { + const x = vec[0]; + const y = vec[1]; + dst[0] = 0; + dst[1] = 0; + dst[2] = 0; + + let blendAlpha = 1; + for (let index = 0; blendAlpha !== 0 && index < candidates.length; index++) { + const candidate = candidates[index]; + const intersection = candidate.intersection; + if (intersection.left > x || intersection.right < x || + intersection.bottom > y || intersection.top < y) { + continue; + } + if (candidate.colorSilhouette === null) { + Drawable.sampleColor4b(vec, candidate.drawable, __blendColor); + } else { + Drawable.samplePlainColor4b(x, y, candidate.drawable, candidate.colorSilhouette, __blendColor); + } + dst[0] += __blendColor[0] * blendAlpha; + dst[1] += __blendColor[1] * blendAlpha; + dst[2] += __blendColor[2] * blendAlpha; + blendAlpha *= (1 - (__blendColor[3] / 255)); + } + dst[0] += 255 * this._backgroundColor4f[0] * blendAlpha; + dst[1] += 255 * this._backgroundColor4f[1] * blendAlpha; + dst[2] += 255 * this._backgroundColor4f[2] * blendAlpha; + blendAlpha *= 1 - this._backgroundColor4f[3]; + dst[3] = 255 * (1 - blendAlpha); + return dst; + } + /** * @callback RenderWebGL#snapshotCallback * @param {string} dataURI Data URI of the snapshot of the renderer diff --git a/src/SVGSkin.js b/src/SVGSkin.js index a3dcfb580..68ed29ae6 100644 --- a/src/SVGSkin.js +++ b/src/SVGSkin.js @@ -12,6 +12,9 @@ const ShaderManager = require('./ShaderManager'); */ const INDEX_OFFSET = 8; +let __smallerMIPCanvas = null; +let __smallerMIPContext = null; + class SVGSkin extends Skin { /** * Create a new SVG skin. @@ -104,21 +107,23 @@ class SVGSkin extends Skin { */ createMIP (scale) { const isLargestMIP = this._largestMIPScale < scale; - // TW: Silhouette will lazily read image data from our . However, this canvas is shared - // between the Skin and Silhouette so changing it here can mess up Silhouette. To prevent that, - // we will force the silhouette to synchronously read the image data before we mutate the - // canvas, unless the new MIP is the largest MIP, in which case doing so is unnecessary as we - // will update the silhouette later anyways. + let canvas = this._canvas; + let context = this._context; if (!isLargestMIP) { - this._silhouette.unlazy(); + if (__smallerMIPCanvas === null) { + __smallerMIPCanvas = document.createElement('canvas'); + __smallerMIPContext = __smallerMIPCanvas.getContext('2d'); + } + canvas = __smallerMIPCanvas; + context = __smallerMIPContext; } const [width, height] = this._size; - this._canvas.width = width * scale; - this._canvas.height = height * scale; + canvas.width = width * scale; + canvas.height = height * scale; if ( - this._canvas.width <= 0 || - this._canvas.height <= 0 || + canvas.width <= 0 || + canvas.height <= 0 || // Even if the canvas at the current scale has a nonzero size, the image's dimensions are floored // pre-scaling; e.g. if an image has a width of 0.4 and is being rendered at 3x scale, the canvas will have // a width of 1, but the image's width will be rounded down to 0 on some browsers (Firefox) prior to being @@ -126,13 +131,13 @@ class SVGSkin extends Skin { this._svgImage.naturalWidth <= 0 || this._svgImage.naturalHeight <= 0 ) return super.getTexture(); - this._context.clearRect(0, 0, this._canvas.width, this._canvas.height); - this._context.setTransform(scale, 0, 0, scale, 0, 0); - this._context.drawImage(this._svgImage, 0, 0); + context.clearRect(0, 0, canvas.width, canvas.height); + context.setTransform(scale, 0, 0, scale, 0, 0); + context.drawImage(this._svgImage, 0, 0); // TW: Reading image data from is very slow and causes animations to stutter, // so we just use the canvas directly instead. - const textureData = this._canvas; + const textureData = canvas; const textureOptions = { auto: false, @@ -147,6 +152,9 @@ class SVGSkin extends Skin { if (isLargestMIP) { this._silhouette.update(textureData); this._largestMIPScale = scale; + } else { + canvas.width = 1; + canvas.height = 1; } return mip; diff --git a/test/unit/TouchingFastPathTests.js b/test/unit/TouchingFastPathTests.js new file mode 100644 index 000000000..53e246c44 --- /dev/null +++ b/test/unit/TouchingFastPathTests.js @@ -0,0 +1,247 @@ +const test = require('tap').test; + +global.window = {}; +global.document = { + createElement: () => ({getContext: () => {}}) +}; +global.ImageData = class { + constructor (data, width, height) { + this.data = data; + this.width = width; + this.height = height; + } +}; + +const twgl = require('twgl.js'); +const Drawable = require('../../src/Drawable'); +const MockSkin = require('../fixtures/MockSkin'); +const Rectangle = require('../../src/Rectangle'); + +const makeRandom = seed => { + let state = seed >>> 0; + return () => { + state = (Math.imul(state, 1664525) + 1013904223) >>> 0; + return state / 4294967296; + }; +}; + +const mockRenderer = drawable => ({ + skinWasAltered: () => { + drawable._skinWasAltered(); + } +}); + +const makeDrawable = (random, width, height) => { + const drawable = new Drawable(null, {}); + const skin = new MockSkin(0, mockRenderer(drawable)); + drawable.skin = skin; + skin.size = [width, height]; + skin.rotationCenter = [width / 2, height / 2]; + const data = new Uint8ClampedArray(width * height * 4); + for (let index = 0; index < data.length; index += 4) { + data[index] = Math.floor(random() * 256); + data[index + 1] = Math.floor(random() * 256); + data[index + 2] = Math.floor(random() * 256); + const roll = random(); + data[index + 3] = roll < 0.5 ? 0 : (roll < 0.75 ? 255 : Math.floor(random() * 256)); + } + skin._silhouette.update(new global.ImageData(data, width, height)); + drawable.updateCPURenderAttributes(); + return drawable; +}; + +const directions = [90, 0, 45, -90, 180, 137.5, 33.3]; +const scales = [100, 50, 200, 37, 310]; + +const place = (random, drawable) => { + drawable.updateProperties({ + position: [Math.floor(random() * 60) - 30, Math.floor(random() * 60) - 30], + direction: directions[Math.floor(random() * directions.length)], + scale: [scales[Math.floor(random() * scales.length)], scales[Math.floor(random() * scales.length)]] + }); + drawable.updateCPURenderAttributes(); +}; + +const snappedBounds = drawable => { + const bounds = drawable.getFastBounds(new Rectangle()); + bounds.snapToInt(); + return bounds; +}; + +const slowTouchingPair = (first, second, bounds) => { + const point = twgl.v3.create(); + for (let x = bounds.left; x <= bounds.right; x++) { + point[0] = x; + for (let y = bounds.bottom; y <= bounds.top; y++) { + point[1] = y; + if (first.isTouching(point) && second.isTouching(point)) { + return true; + } + } + } + return false; +}; + +const comparePairs = (t, random, first, second, rounds) => { + const point = twgl.v3.create(); + let touching = 0; + let mismatches = 0; + for (let round = 0; round < rounds; round++) { + place(random, first); + place(random, second); + const bounds = Rectangle.intersect(snappedBounds(first), snappedBounds(second), new Rectangle()); + const expected = slowTouchingPair(first, second, bounds); + const actual = Drawable.isTouchingPair(first, second, bounds, point); + if (expected) touching++; + if (expected !== actual) mismatches++; + } + t.equal(mismatches, 0); + return touching; +}; + +test('touching pair matches the point by point test', t => { + const random = makeRandom(1); + const first = makeDrawable(random, 23, 17); + const second = makeDrawable(random, 9, 31); + t.ok(first.getPlainTouchingSilhouette()); + const touching = comparePairs(t, random, first, second, 400); + t.ok(touching > 20); + t.ok(touching < 400); + t.end(); +}); + +test('touching pair ignores effects that do not move pixels', t => { + const random = makeRandom(2); + const first = makeDrawable(random, 23, 17); + const second = makeDrawable(random, 9, 31); + first.updateProperties({ghost: 50, color: 25}); + second.updateProperties({brightness: -30}); + first.updateCPURenderAttributes(); + second.updateCPURenderAttributes(); + t.ok(first.getPlainTouchingSilhouette()); + t.ok(second.getPlainTouchingSilhouette()); + comparePairs(t, random, first, second, 200); + t.end(); +}); + +test('touching pair falls back for effects that move pixels', t => { + const random = makeRandom(3); + for (const effect of ['fisheye', 'whirl', 'pixelate', 'mosaic']) { + const first = makeDrawable(random, 23, 17); + const second = makeDrawable(random, 9, 31); + first.updateProperties({[effect]: 50}); + first.updateCPURenderAttributes(); + t.equal(first.getPlainTouchingSilhouette(), null); + t.equal(first.getPlainColorSilhouette(), null); + comparePairs(t, random, first, second, 60); + comparePairs(t, random, second, first, 60); + } + t.end(); +}); + +test('touching pair falls back for a skin with its own touching test', t => { + const random = makeRandom(4); + const first = makeDrawable(random, 23, 17); + const second = makeDrawable(random, 9, 31); + second.skin.isTouchingNearest = () => true; + t.equal(second.getPlainTouchingSilhouette(), null); + const touching = comparePairs(t, random, first, second, 100); + t.ok(touching > 0); + t.end(); +}); + +test('touching pair is false while a silhouette has no pixel data', t => { + const random = makeRandom(5); + const first = makeDrawable(random, 23, 17); + const second = makeDrawable(random, 9, 31); + second.skin._silhouette._colorData = null; + const bounds = Rectangle.intersect(snappedBounds(first), snappedBounds(second), new Rectangle()); + t.equal(slowTouchingPair(first, second, bounds), false); + t.equal(Drawable.isTouchingPair(first, second, bounds, twgl.v3.create()), false); + t.end(); +}); + +test('plain point test matches isTouching', t => { + const random = makeRandom(6); + const drawable = makeDrawable(random, 23, 17); + const point = twgl.v3.create(); + let mismatches = 0; + let touching = 0; + for (let round = 0; round < 60; round++) { + place(random, drawable); + const silhouette = drawable.getPlainTouchingSilhouette(); + const bounds = snappedBounds(drawable); + for (let x = bounds.left - 2; x <= bounds.right + 2; x++) { + for (let y = bounds.bottom - 2; y <= bounds.top + 2; y++) { + point[0] = x; + point[1] = y; + const expected = Boolean(drawable.isTouching(point)); + const actual = Drawable.isTouchingPlain( + drawable._inverseMatrix, silhouette._colorData, silhouette._width, silhouette._height, + drawable.isTouching === drawable._isTouchingLinear, point[0], point[1]); + if (expected) touching++; + if (expected !== actual) mismatches++; + } + } + } + t.equal(mismatches, 0); + t.ok(touching > 0); + t.end(); +}); + +test('plain color sample matches sampleColor4b', t => { + const random = makeRandom(7); + const point = twgl.v3.create(); + const expected = new Uint8ClampedArray(4); + const actual = new Uint8ClampedArray(4); + let mismatches = 0; + let opaque = 0; + for (const effects of [{}, {ghost: 50}, {color: 25, brightness: -30}, {brightness: 40, ghost: 10}]) { + const drawable = makeDrawable(random, 23, 17); + drawable.updateProperties(effects); + for (let round = 0; round < 40; round++) { + place(random, drawable); + const silhouette = drawable.getPlainColorSilhouette(); + const bounds = snappedBounds(drawable); + for (let x = bounds.left - 2; x <= bounds.right + 2; x++) { + for (let y = bounds.bottom - 2; y <= bounds.top + 2; y++) { + point[0] = x; + point[1] = y; + Drawable.sampleColor4b(point, drawable, expected); + Drawable.samplePlainColor4b(point[0], point[1], drawable, silhouette, actual); + if (expected[3] > 0) opaque++; + if (expected[0] !== actual[0] || expected[1] !== actual[1] || + expected[2] !== actual[2] || expected[3] !== actual[3]) { + mismatches++; + } + } + } + } + } + t.equal(mismatches, 0); + t.ok(opaque > 0); + t.end(); +}); + +test('snapped fast bounds follow the drawable', t => { + const random = makeRandom(8); + const drawable = makeDrawable(random, 23, 17); + for (let round = 0; round < 50; round++) { + place(random, drawable); + const expected = snappedBounds(drawable); + const actual = drawable.getSnappedFastBounds(); + t.same( + [actual.left, actual.right, actual.bottom, actual.top], + [expected.left, expected.right, expected.bottom, expected.top] + ); + } + drawable.skin.size = [40, 12]; + drawable.skin.rotationCenter = [20, 6]; + const resized = snappedBounds(drawable); + const cached = drawable.getSnappedFastBounds(); + t.same( + [cached.left, cached.right, cached.bottom, cached.top], + [resized.left, resized.right, resized.bottom, resized.top] + ); + t.end(); +});