Skip to content

Commit 9bec1f4

Browse files
authored
Looks-Expanded.js -- Fix Poor Performance
1 parent aa731f9 commit 9bec1f4

File tree

1 file changed

+20
-50
lines changed

1 file changed

+20
-50
lines changed

extension-code/Looks-Expanded.js

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// By: CST1229 <https://scratch.mit.edu/users/CST1229/>
66
// Licence: MIT
77

8-
// Version V.1.0.32
8+
// Version V.1.0.4
99

1010
(function (Scratch) {
1111
"use strict";
@@ -25,12 +25,6 @@
2525

2626
const drawableKey = Symbol("SPlooksKey");
2727
const MAX_REPLACERS = 15;
28-
const newUniforms = [
29-
"u_replaceColorFromSP", "u_replaceColorToSP", "u_replaceThresholdSP", "u_numReplacersSP",
30-
"u_warpSP", "u_maskTextureSP", "u_shouldMaskSP",
31-
"u_tintColorSP", "u_saturateSP", "u_opaqueSP", "u_contrastSP",
32-
"u_posterizeSP", "u_sepiaSP", "u_bloomSP"
33-
];
3428

3529
const newSingleEffects = {
3630
saturation: 1, opaque: 0, contrast: 1,
@@ -53,29 +47,11 @@
5347
const replaceTo = new Float32Array(MAX_REPLACERS * 4).fill(0);
5448
const replaceThresh = new Float32Array(MAX_REPLACERS).fill(1);
5549

56-
let currentShader;
57-
5850
/* patch for new effects */
5951
function initDrawable(drawable) {
6052
if (!drawable[drawableKey]) drawable[drawableKey] = genEffectFactory();
6153
}
6254

63-
const ogGetShader = render._shaderManager.getShader;
64-
render._shaderManager.getShader = function (drawMode, effectBits) {
65-
const shader = ogGetShader.call(this, drawMode, effectBits);
66-
if (!shader._patched) {
67-
shader._patched = true;
68-
const gl = render._gl;
69-
70-
// add uniforms to the existing shader
71-
for (const name of newUniforms) {
72-
shader.uniformSetters[name] = gl.getUniformLocation(shader.program, name);
73-
}
74-
}
75-
currentShader = shader;
76-
return shader;
77-
};
78-
7955
// Clear the renderer's shader cache since we're patching shaders
8056
for (const cache of Object.values(render._shaderManager._shaderCache)) {
8157
for (const programInfo of cache) {
@@ -94,8 +70,7 @@
9470
.replaceAll("vec4(a_position", "vec4(positionSP")
9571
.replace("v_texCoord = a_texCoord;", "")
9672
.replace("#if !(defined(DRAW_MODE_line) || defined(DRAW_MODE_background))", "#if 1")
97-
.replace(
98-
`void main() {`,
73+
.replace(`void main() {`,
9974
`uniform vec2 u_warpSP[4];
10075
10176
void main() {
@@ -131,7 +106,6 @@ void main() {
131106
);
132107
}
133108

134-
135109
args[1][1] = args[1][1].replace(
136110
`uniform sampler2D u_skin;`,
137111
`uniform sampler2D u_skin;
@@ -252,10 +226,9 @@ gl_FragColor.a = baseAlpha;`
252226
};
253227

254228
const ogGetUniforms = render.exports.Drawable.prototype.getUniforms;
255-
render.exports.Drawable.prototype.getUniforms = function() {
229+
render.exports.Drawable.prototype.getUniforms = function () {
256230
const gl = render.gl;
257231
const uniforms = ogGetUniforms.call(this);
258-
if (!currentShader) return uniforms;
259232

260233
initDrawable(this);
261234
const effectData = this[drawableKey];
@@ -269,34 +242,31 @@ gl_FragColor.a = baseAlpha;`
269242
}
270243
}
271244

245+
const newEffects = effectData.newEffects;
246+
uniforms.u_replaceColorFromSP = replaceFrom;
247+
uniforms.u_replaceColorToSP = replaceTo;
248+
uniforms.u_replaceThresholdSP = replaceThresh;
249+
uniforms.u_numReplacersSP = replacers ? Math.min(replacers.length, MAX_REPLACERS) : 0;
250+
uniforms.u_tintColorSP = effectData.tint;
251+
uniforms.u_warpSP = effectData.warp;
252+
uniforms.u_shouldMaskSP = effectData.shouldMask;
253+
uniforms.u_saturateSP = newEffects.saturation;
254+
uniforms.u_opaqueSP = newEffects.opaque;
255+
uniforms.u_contrastSP = newEffects.contrast;
256+
uniforms.u_posterizeSP = newEffects.posterize;
257+
uniforms.u_sepiaSP = newEffects.sepia;
258+
uniforms.u_bloomSP = newEffects.bloom;
259+
272260
if (effectData.shouldMask) {
273-
gl.activeTexture(gl.TEXTURE30);
274-
gl.bindTexture(gl.TEXTURE_2D, effectData._maskTexture);
275-
gl.uniform1i(currentShader.uniformSetters["u_maskTextureSP"], 30);
276-
gl.activeTexture(gl.TEXTURE0);
261+
uniforms["u_maskTextureSP"] = effectData._maskTexture;
277262
}
278263

279-
const newEffects = effectData.newEffects;
280-
gl.uniform3fv(currentShader.uniformSetters.u_replaceColorFromSP, replaceFrom);
281-
gl.uniform4fv(currentShader.uniformSetters.u_replaceColorToSP, replaceTo);
282-
gl.uniform1fv(currentShader.uniformSetters.u_replaceThresholdSP, replaceThresh);
283-
gl.uniform1i(currentShader.uniformSetters.u_numReplacersSP, replacers ? Math.min(replacers.length, MAX_REPLACERS) : 0);
284-
gl.uniform4fv(currentShader.uniformSetters.u_tintColorSP, effectData.tint);
285-
gl.uniform2fv(currentShader.uniformSetters.u_warpSP, effectData.warp);
286-
gl.uniform1f(currentShader.uniformSetters.u_shouldMaskSP, effectData.shouldMask);
287-
gl.uniform1f(currentShader.uniformSetters.u_saturateSP, newEffects.saturation);
288-
gl.uniform1f(currentShader.uniformSetters.u_opaqueSP, newEffects.opaque);
289-
gl.uniform1f(currentShader.uniformSetters.u_contrastSP, newEffects.contrast);
290-
gl.uniform1f(currentShader.uniformSetters.u_posterizeSP, newEffects.posterize);
291-
gl.uniform1f(currentShader.uniformSetters.u_sepiaSP, newEffects.sepia);
292-
gl.uniform1f(currentShader.uniformSetters.u_bloomSP, newEffects.bloom);
293-
294264
return uniforms;
295265
}
296266

297267
// reset on stop/start/clear
298268
const ogClearEffects = vm.exports.RenderedTarget.prototype.clearEffects;
299-
vm.exports.RenderedTarget.prototype.clearEffects = function() {
269+
vm.exports.RenderedTarget.prototype.clearEffects = function () {
300270
const drawable = render._allDrawables[this.drawableID];
301271
drawable[drawableKey] = genEffectFactory();
302272
ogClearEffects.call(this);

0 commit comments

Comments
 (0)