-
Notifications
You must be signed in to change notification settings - Fork 0
/
clippingblending.js
455 lines (428 loc) · 14.3 KB
/
clippingblending.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
(function(Scratch) {
'use strict';
if (!Scratch.extensions.unsandboxed) {
throw new Error("Effects extension must be run unsandboxed");
}
// Simplified remake of an icon by True-Fantom
const icon = 'data:image/svg+xml,' + encodeURIComponent(`
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0,0,200,200">
<circle r="100" cx="100" cy="100" fill="#9966ff"/>
<path d="M122,61v-4a12,12 0,0,0 -12,-12h-4m-17,0h-16m-17,0h-4a12,12 0,0,0 -12,12v4m0,17v16m0,17v4a12,12 0,0,0 12,12h4" stroke="#ffffff" stroke-width="11" stroke-linecap="round" fill="none"/>
<g fill="#ffffff" stroke="#9966ff" stroke-width="7.5">
<circle r="32" cx="118" cy="102"/>
<circle r="32" cx="96" cy="137"/>
<circle r="32" cx="140" cy="137"/>
</g>
</svg>`);
let toCorrectThing = null;
let active = false;
let flipY = false;
const vm = Scratch.vm;
const runtime = vm.runtime;
const renderer = vm.renderer;
const _drawThese = renderer._drawThese;
const gl = renderer._gl;
const canvas = renderer.canvas;
let width = 0;
let height = 0;
let scratchUnitWidth = 480;
let scratchUnitHeight = 360;
let penDirty = false;
renderer._drawThese = function (drawables, drawMode, projection, opts) {
active = true;
[scratchUnitWidth, scratchUnitHeight] = renderer.getNativeSize();
_drawThese.call(this, drawables, drawMode, projection, opts);
gl.disable(gl.SCISSOR_TEST);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
active = false;
};
const bfb = gl.bindFramebuffer;
gl.bindFramebuffer = function (target, framebuffer) {
if (target == gl.FRAMEBUFFER) {
if (framebuffer == null) {
toCorrectThing = true;
flipY = false;
width = canvas.width;
height = canvas.height;
} else if (renderer._penSkinId) {
const fbInfo = renderer._allSkins[renderer._penSkinId]._framebuffer;
if (framebuffer == fbInfo.framebuffer) {
toCorrectThing = true;
flipY = true;
width = fbInfo.width;
height = fbInfo.height;
} else {
toCorrectThing = false;
}
} else {
toCorrectThing = false;
}
}
bfb.call(this, target, framebuffer);
};
// Getting Drawable
const dr = renderer.createDrawable('background');
const DrawableProto = renderer._allDrawables[dr].__proto__;
renderer.destroyDrawable(dr, 'background');
function setupModes(clipbox, blendMode, flipY) {
if (clipbox) {
gl.enable(gl.SCISSOR_TEST);
let x = (clipbox.x_min / scratchUnitWidth + 0.5) * width | 0;
let y = (clipbox.y_min / scratchUnitHeight + 0.5) * height | 0;
let x2 = (clipbox.x_max / scratchUnitWidth + 0.5) * width | 0;
let y2 = (clipbox.y_max / scratchUnitHeight + 0.5) * height | 0;
let w = x2 - x;
let h = y2 - y;
if (flipY) {
y = (-(clipbox.y_max) / scratchUnitHeight + 0.5) * height | 0;
}
gl.scissor(x, y, w, h);
} else {
gl.disable(gl.SCISSOR_TEST);
}
switch (blendMode) {
case 'additive':
gl.blendEquation(gl.FUNC_ADD);
gl.blendFunc(gl.ONE, gl.ONE);
break;
case 'subtract':
gl.blendEquation(gl.FUNC_REVERSE_SUBTRACT);
gl.blendFunc(gl.ONE, gl.ONE);
break;
case 'multiply':
gl.blendEquation(gl.FUNC_ADD);
gl.blendFunc(gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA);
break;
case 'invert':
gl.blendEquation(gl.FUNC_ADD);
gl.blendFunc(gl.ONE_MINUS_DST_COLOR, gl.ONE_MINUS_SRC_COLOR);
break;
default:
gl.blendEquation(gl.FUNC_ADD);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
}
}
// Modifying and expanding Drawable
const gu = DrawableProto.getUniforms;
DrawableProto.getUniforms = function () {
if (active && toCorrectThing) {
setupModes(this.clipbox, this.blendMode, flipY);
}
return gu.call(this);
};
DrawableProto.updateClipBox = function (clipbox) {
this.clipbox = clipbox;
};
DrawableProto.updateBlendMode = function (blendMode) {
this.blendMode = blendMode;
};
// Expanding renderer
renderer.updateDrawableClipBox = function (drawableID, clipbox) {
const drawable = this._allDrawables[drawableID];
if (!drawable) return;
drawable.updateClipBox(clipbox);
};
renderer.updateDrawableBlendMode = function (drawableID, blendMode) {
const drawable = this._allDrawables[drawableID];
if (!drawable) return;
drawable.updateBlendMode(blendMode);
};
// Reset on stop & clones inherit effects
const regTargetStuff = function (args) {
if (args.editingTarget) {
vm.removeListener('targetsUpdate', regTargetStuff);
const proto = vm.runtime.targets[0].__proto__;
const osa = proto.onStopAll;
proto.onStopAll = function () {
this.renderer.updateDrawableClipBox.call(renderer, this.drawableID, null);
this.renderer.updateDrawableBlendMode.call(renderer, this.drawableID, null);
osa.call(this);
};
const mc = proto.makeClone;
proto.makeClone = function () {
const newTarget = mc.call(this);
if (this.clipbox || this.blendMode) {
newTarget.clipbox = Object.assign({}, this.clipbox);
newTarget.blendMode = this.blendMode;
renderer.updateDrawableClipBox.call(renderer, newTarget.drawableID, this.clipbox);
renderer.updateDrawableBlendMode.call(renderer, newTarget.drawableID, this.blendMode);
}
return newTarget;
};
}
};
vm.on('targetsUpdate', regTargetStuff);
// Pen lines support
let emptyObject = {};
let lastTarget = emptyObject;
let lastClipbox = {};
let lastBlendMode = "default";
function patchPen(skin) {
const ext_pen = runtime.ext_pen;
skin._lineOnBufferDrawRegionId.exit = () => {
skin._exitDrawLineOnBuffer();
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
gl.disable(gl.SCISSOR_TEST);
lastTarget = emptyObject;
lastClipbox = null;
lastBlendMode = "default";
};
const willDrawPenWithTarget = function(target) {
if (!penDirty && target == lastTarget) return;
penDirty = false;
const clipbox = target.clipbox;
if ((!lastClipbox ^ !clipbox) ||
(lastBlendMode != target.blendMode) ||
(clipbox && (clipbox.x_min != lastClipbox.x_min || clipbox.y_min != lastClipbox.y_min || clipbox.x_max != lastClipbox.x_max || clipbox.y_max != lastClipbox.y_max))) {
if (skin.a_lineColorIndex) {
skin._flushLines();
}
lastTarget = target;
if (clipbox) {
lastClipbox = {
x_min: clipbox.x_min,
y_min: clipbox.y_min,
x_max: clipbox.x_max,
y_max: clipbox.y_max
};
} else {
lastClipbox = null;
}
lastBlendMode = target.blendMode;
}
};
// onTargetMoved function of pen draws a line.
// When drawing a line it is important to know the target.
// This saves target.
const onTargetMoved = ext_pen._onTargetMoved;
ext_pen._onTargetMoved = function(target, oldX, oldY, isForce) {
willDrawPenWithTarget(target);
onTargetMoved.call(this, target, oldX, oldY, isForce);
};
// Existing tragets may still have old onTargetMoved
for (let target in runtime.tragets) {
if (target.onTargetMoved == onTargetMoved) {
target.onTargetMoved = ext_pen._onTargetMoved;
}
}
// When drawing a dot it is important to know the target.
// This saves target.
const penDown = ext_pen._penDown;
ext_pen._penDown = function(target) {
willDrawPenWithTarget(target);
penDown.call(this, target);
};
// Set up correct clipping/blending before drawing
const flushLines = skin.__proto__._flushLines;
skin.__proto__._flushLines = function() {
setupModes(lastClipbox, lastBlendMode, true);
flushLines.call(this);
};
}
if (renderer._allSkins[renderer._penSkinId]) {
// If pen skin already exists, things can be patched
patchPen(renderer._allSkins[renderer._penSkinId]);
} else {
// If pen skin does not exist, wait until it will,
// trigger code once, and return everything as it was
const createPenSkin = renderer.createPenSkin;
renderer.createPenSkin = function() {
let skinId = createPenSkin.call(this);
patchPen(renderer._allSkins[skinId]);
renderer.createPenSkin = createPenSkin;
return skinId;
};
}
class Extension {
getInfo() {
return {
id: 'xeltallivclipblend',
name: 'Clipping & Blending',
color1: '#9966FF',
color2: '#855CD6',
color3: '#774DCB',
menuIconURI: icon,
blocks: [
{
opcode: 'setClipbox',
blockType: Scratch.BlockType.COMMAND,
text: 'set clipping box x1:[X1] y1:[Y1] x2:[X2] y2:[Y2]',
arguments: {
X1: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0'
},
Y1: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0'
},
X2: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '100'
},
Y2: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '100'
}
},
filter: [Scratch.TargetType.SPRITE]
},
{
opcode: 'clearClipbox',
blockType: Scratch.BlockType.COMMAND,
text: 'clear clipping box',
filter: [Scratch.TargetType.SPRITE]
},
{
opcode: 'getClipbox',
blockType: Scratch.BlockType.REPORTER,
text: 'clipping box [PROP]',
arguments: {
PROP: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'width',
menu: 'props'
}
},
filter: [Scratch.TargetType.SPRITE]
},
'---',
{
opcode: 'setBlend',
blockType: Scratch.BlockType.COMMAND,
text: 'use [BLENDMODE] blending ',
arguments: {
BLENDMODE: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'default',
menu: 'blends'
}
},
filter: [Scratch.TargetType.SPRITE]
},
{
opcode: 'getBlend',
blockType: Scratch.BlockType.REPORTER,
text: 'blending',
filter: [Scratch.TargetType.SPRITE],
disableMonitor: true
},
'---',
{
opcode: 'setAdditiveBlend',
blockType: Scratch.BlockType.COMMAND,
text: 'turn additive blending [STATE]',
arguments: {
STATE: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'on',
menu: 'states'
}
},
filter: [Scratch.TargetType.SPRITE],
hideFromPalette: true
},
{
opcode: 'getAdditiveBlend',
blockType: Scratch.BlockType.BOOLEAN,
text: 'is additive blending on?',
filter: [Scratch.TargetType.SPRITE],
hideFromPalette: true
},
],
menus: {
states: {
acceptReporters: true,
items: ['on', 'off']
},
blends: {
acceptReporters: true,
items: ['default', 'additive', 'subtract', 'multiply', 'invert']
},
props: {
acceptReporters: true,
items: ['width', 'height', 'min x', 'min y', 'max x', 'max y']
},
}
};
}
setClipbox ({X1, Y1, X2, Y2}, {target}) {
if (target.isStage) return;
const newClipbox = {
x_min: Math.min(X1, X2),
y_min: Math.min(Y1, Y2),
x_max: Math.max(X1, X2),
y_max: Math.max(Y1, Y2)
};
penDirty = true;
target.clipbox = newClipbox;
renderer.updateDrawableClipBox.call(renderer, target.drawableID, newClipbox);
if (target.visible) {
renderer.dirty = true;
target.emitVisualChange();
target.runtime.requestRedraw();
target.runtime.requestTargetsUpdate(target);
}
}
clearClipbox (args, {target}) {
if (target.isStage) return;
target.clipbox = null;
penDirty = true;
renderer.updateDrawableClipBox.call(renderer, target.drawableID, null);
if (target.visible) {
renderer.dirty = true;
target.emitVisualChange();
target.runtime.requestRedraw();
target.runtime.requestTargetsUpdate(target);
}
}
getClipbox ({PROP}, {target}) {
const clipbox = target.clipbox;
if (!clipbox) return '';
switch (PROP) {
case 'width': return clipbox.x_max - clipbox.x_min;
case 'height': return clipbox.y_max - clipbox.y_min;
case 'min x': return clipbox.x_min;
case 'min y': return clipbox.y_min;
case 'max x': return clipbox.x_max;
case 'max y': return clipbox.y_max;
default: return '';
}
}
setBlend ({BLENDMODE}, {target}) {
let newValue = null;
switch (BLENDMODE) {
case 'default':
case 'additive':
case 'subtract':
case 'multiply':
case 'invert':
newValue = BLENDMODE;
break;
default:
return;
}
if (target.isStage) return;
penDirty = true;
target.blendMode = newValue;
renderer.updateDrawableBlendMode.call(renderer, target.drawableID, newValue);
if (target.visible) {
renderer.dirty = true;
target.emitVisualChange();
target.runtime.requestRedraw();
target.runtime.requestTargetsUpdate(target);
}
}
getBlend (args, {target}) {
return target.blendMode ?? 'default';
}
setAdditiveBlend ({STATE}, util) {
if (STATE === 'on') this.setBlend ({BLENDMODE: 'additive'}, util);
if (STATE === 'off') this.setBlend ({BLENDMODE: 'default'}, util);
}
getAdditiveBlend (args, {target}) {
return target.blendMode === 'additive';
}
}
Scratch.extensions.register(new Extension());
})(Scratch);