Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scratch-render",
"version": "0.1.1",
"version": "0.1.2",
"description": "WebGL Renderer for Scratch 3.0",
"author": "Massachusetts Institute of Technology",
"license": "MPL-2.0",
Expand Down
19 changes: 17 additions & 2 deletions src/Drawable.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 FREE_MOVES_RANGE = 1e4;
const SHAPE_EFFECTS_MASK = ShaderManager.EFFECT_INFO.fisheye.mask |
ShaderManager.EFFECT_INFO.whirl.mask |
ShaderManager.EFFECT_INFO.pixelate.mask |
Expand Down Expand Up @@ -130,10 +131,17 @@ class Drawable {
this._rotationCenterDirty = true;
this._skinScale = twgl.v3.create(0, 0, 0);
this._skinScaleDirty = true;
this._skinSlot = -1;
this._inverseMatrix = twgl.m4.identity();
this._inverseTransformDirty = true;
this._snappedFastBounds = new Rectangle();
this._snappedFastBoundsDirty = true;
this._freeMovesLeft = NaN;
this._freeMovesRight = 0;
this._freeMovesBottom = 0;
this._freeMovesTop = 0;
this._freeMovesStageRight = 0;
this._freeMovesStageTop = 0;
this._visible = true;

/** A bitmask identifying which effects are currently in use.
Expand Down Expand Up @@ -185,6 +193,7 @@ class Drawable {
this._inverseTransformDirty = true;
this._transformedHullDirty = true;
this._snappedFastBoundsDirty = true;
this._freeMovesLeft = NaN;
}

/**
Expand All @@ -207,11 +216,11 @@ class Drawable {
set skin (newSkin) {
if (this._skin !== newSkin) {
if (this._skin) {
this._skin.attachedDrawables.delete(this);
this._skin.detachDrawable(this);
}
this._skin = newSkin;
if (newSkin) {
newSkin.attachedDrawables.add(this);
newSkin.attachDrawable(this);
}
this._skinWasAltered();
}
Expand Down Expand Up @@ -256,7 +265,11 @@ class Drawable {
this._position[1] = Math.round(position[1]);
}
this._renderer.dirty = true;
const freeMovesLeft = this._freeMovesLeft;
this.setTransformDirty();
if (Math.abs(this._position[0]) <= FREE_MOVES_RANGE && Math.abs(this._position[1]) <= FREE_MOVES_RANGE) {
this._freeMovesLeft = freeMovesLeft;
}
}
}

Expand Down Expand Up @@ -940,4 +953,6 @@ class Drawable {
}
}

Drawable.FREE_MOVES_RANGE = FREE_MOVES_RANGE;

module.exports = Drawable;
26 changes: 18 additions & 8 deletions src/PenSkin.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,17 @@ class PenSkin extends Skin {
}

_markSilhouetteDirty (left, right, bottom, top) {
if (!(left <= right && bottom <= top)) {
left = 0;
right = this._size[0];
bottom = 0;
top = this._size[1];
}
if (this._silhouetteDirty) {
this._dirtyLeft = Math.min(this._dirtyLeft, left);
this._dirtyRight = Math.max(this._dirtyRight, right);
this._dirtyBottom = Math.min(this._dirtyBottom, bottom);
this._dirtyTop = Math.max(this._dirtyTop, top);
if (left < this._dirtyLeft) this._dirtyLeft = left;
if (right > this._dirtyRight) this._dirtyRight = right;
if (bottom < this._dirtyBottom) this._dirtyBottom = bottom;
if (top > this._dirtyTop) this._dirtyTop = top;
} else {
this._silhouetteDirty = true;
this._dirtyLeft = left;
Expand Down Expand Up @@ -463,11 +469,15 @@ class PenSkin extends Skin {
const dirtyRadius = (lineThickness * 0.5) + 2;
const halfWidth = this._size[0] * 0.5;
const halfHeight = this._size[1] * 0.5;
const lowX = x0 < x1 ? x0 : x1;
const highX = x0 < x1 ? x1 : x0;
const lowY = y0 < y1 ? y0 : y1;
const highY = y0 < y1 ? y1 : y0;
this._markSilhouetteDirty(
halfWidth + Math.min(x0, x1) - dirtyRadius,
halfWidth + Math.max(x0, x1) + dirtyRadius,
halfHeight - Math.max(y0, y1) - dirtyRadius,
halfHeight - Math.min(y0, y1) + dirtyRadius
halfWidth + lowX - dirtyRadius,
halfWidth + highX + dirtyRadius,
halfHeight - highY - dirtyRadius,
halfHeight - lowY + dirtyRadius
);

for (let i = 0; i < iters; i++) {
Expand Down
56 changes: 54 additions & 2 deletions src/RenderWebGL.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const colorMatches = (a, b, offset) => (
* @type {number}
*/
const FENCE_WIDTH = 15;
const FENCE_SLACK = 0.01;
const FENCE_ROUNDING = 1e-6;
const FREE_MOVES_RANGE = Drawable.FREE_MOVES_RANGE;

// Loading text wrapper takes a while because of some of its dependencies, so only do so when needed.
let _TextWrapper;
Expand Down Expand Up @@ -959,8 +962,9 @@ class RenderWebGL extends EventEmitter {
}

skinWasAltered(skin) {
for (const drawable of skin.attachedDrawables) {
drawable._skinWasAltered();
const attached = skin.attachedDrawables;
for (let i = 0; i < attached.length; i++) {
attached[i]._skinWasAltered();
}
}

Expand Down Expand Up @@ -1989,9 +1993,19 @@ class RenderWebGL extends EventEmitter {
return [x, y];
}

if (x >= drawable._freeMovesLeft && x <= drawable._freeMovesRight &&
y >= drawable._freeMovesBottom && y <= drawable._freeMovesTop &&
drawable._freeMovesStageRight === this._xRight && drawable._freeMovesStageTop === this._yTop) {
return [x, y];
}

const dx = x - drawable._position[0];
const dy = y - drawable._position[1];
const aabb = skin.getFenceBounds(drawable, __fenceBounds);
if (skin.getFenceBounds === Skin.prototype.getFenceBounds &&
drawable.getAABB === Drawable.prototype.getAABB) {
this._rememberFreeMoves(drawable, aabb);
}
const inset = Math.floor(Math.min(aabb.width, aabb.height) / 2);

const sx = this._xRight - Math.min(FENCE_WIDTH, inset);
Expand All @@ -2009,6 +2023,44 @@ class RenderWebGL extends EventEmitter {
return [x, y];
}

_rememberFreeMoves(drawable, aabb) {
drawable._freeMovesStageRight = this._xRight;
drawable._freeMovesStageTop = this._yTop;
const positionX = drawable._position[0];
const positionY = drawable._position[1];
if (!(Math.abs(positionX) <= FREE_MOVES_RANGE && Math.abs(positionY) <= FREE_MOVES_RANGE)) {
drawable._freeMovesLeft = NaN;
return;
}
const halfShortSide = Math.min(aabb.width, aabb.height) / 2;
if (Number.isNaN(halfShortSide)) {
drawable._freeMovesLeft = -Infinity;
drawable._freeMovesRight = Infinity;
drawable._freeMovesBottom = -Infinity;
drawable._freeMovesTop = Infinity;
return;
}
const inset = Math.min(FENCE_WIDTH, Math.floor(halfShortSide + FENCE_SLACK));
const reachX = this._xRight - inset;
const reachY = this._yTop - inset;
const marginX = FENCE_SLACK +
((FREE_MOVES_RANGE + Math.abs(aabb.left) + Math.abs(aabb.right)) * FENCE_ROUNDING);
const marginY = FENCE_SLACK +
((FREE_MOVES_RANGE + Math.abs(aabb.bottom) + Math.abs(aabb.top)) * FENCE_ROUNDING);
let left = positionX - reachX - aabb.right;
if (left > -Infinity) left += marginX;
let right = positionX + reachX - aabb.left;
if (right < Infinity) right -= marginX;
let bottom = positionY - reachY - aabb.top;
if (bottom > -Infinity) bottom += marginY;
let top = positionY + reachY - aabb.bottom;
if (top < Infinity) top -= marginY;
drawable._freeMovesLeft = left;
drawable._freeMovesRight = right;
drawable._freeMovesBottom = bottom;
drawable._freeMovesTop = top;
}

/**
* Clear a pen layer.
* @param {int} penSkinID - the unique ID of a Pen Skin.
Expand Down
23 changes: 18 additions & 5 deletions src/Skin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,24 @@ class Skin {
*/
this.private = false;

/**
* Drawables currently using this skin, maintained by the Drawable skin setter.
* @type {Set<Drawable>}
*/
this.attachedDrawables = new Set();
this.attachedDrawables = [];
}

attachDrawable (drawable) {
drawable._skinSlot = this.attachedDrawables.length;
this.attachedDrawables.push(drawable);
}

detachDrawable (drawable) {
const attached = this.attachedDrawables;
const slot = drawable._skinSlot;
if (attached[slot] !== drawable) return;
const last = attached.pop();
if (last !== drawable) {
attached[slot] = last;
last._skinSlot = slot;
}
drawable._skinSlot = -1;
}

/**
Expand Down
Loading
Loading