Skip to content

Commit 3b5c03d

Browse files
David-OrangemoonDangoCatGarboMuffin
authored
obviousAlexC/penPlus: Update to the pen-group version (TurboWarp#2408)
All of these fixes are thanks to @LegoBrainBiker Also I think the credit should be changed to pen-group, but I will leave that up to the maintainers over here. --------- Co-authored-by: DangoCat[bot] <dangocat@users.noreply.github.com> Co-authored-by: Thomas Weber <muffin@muffin.ink>
1 parent 8abbaa1 commit 3b5c03d

File tree

1 file changed

+72
-30
lines changed

1 file changed

+72
-30
lines changed

extensions/obviousAlexC/penPlus.js

Lines changed: 72 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// ID: penP
33
// Description: Advanced rendering capabilities.
44
// By: ObviousAlexC <https://scratch.mit.edu/users/pinksheep2917/>
5+
// By: Pen-Group
56
// License: MIT
67

78
// With permission from Sharkpool-SP to use his pen layer data uri block!
@@ -13,12 +14,11 @@
1314

1415
//if you are looking for extension settings search up /* EXTENSION SETTINGS */
1516

16-
//7.1.8 patch notes
17+
//7.1.9 patch notes
1718

1819
/*
1920
? -- Changes -- ?
20-
? Bug Fixes
21-
? Standardized naming
21+
? Bug Fixes (see https://github.com/Pen-Group/extensions/issues/39)
2222
*/
2323

2424
(function (Scratch) {
@@ -91,13 +91,10 @@
9191
//?Call it to have it consistant
9292
updateCanvasSize();
9393

94-
//?Call every frame because I don't know of a way to detect when the stage is resized through window resizing (2/7/24) thought I should clarify
95-
94+
vm.renderer.on("UseHighQualityRenderChanged", updateCanvasSize);
9695
window.addEventListener("resize", updateCanvasSize);
9796
canvas.addEventListener("resize", updateCanvasSize);
98-
vm.runtime.on("STAGE_SIZE_CHANGED", () => {
99-
updateCanvasSize();
100-
});
97+
vm.runtime.on("STAGE_SIZE_CHANGED", updateCanvasSize);
10198

10299
let lastCanvasSize = [canvas.clientWidth, canvas.clientHeight];
103100
vm.runtime.on("BEFORE_EXECUTE", () => {
@@ -242,7 +239,6 @@
242239
void main()
243240
{
244241
gl_FragColor = texture2D(u_drawTex, v_texCoord);
245-
gl_FragColor.rgb = clamp(gl_FragColor.rgb / (gl_FragColor.a + 1e-3), 0.0, 1.0);
246242
}
247243
`,
248244
},
@@ -431,7 +427,7 @@
431427
},
432428
};
433429

434-
extensionVersion = "7.1.8";
430+
extensionVersion = "7.1.9";
435431

436432
//?Stores our attributes
437433
triangleAttributesOfAllSprites = {};
@@ -3375,8 +3371,8 @@
33753371
Scratch.vm.renderer.penPoint(
33763372
Scratch.vm.renderer._penSkinId,
33773373
attrib,
3378-
x,
3379-
y
3374+
Scratch.Cast.toNumber(x),
3375+
Scratch.Cast.toNumber(y)
33803376
);
33813377
}
33823378
drawLine({ x1, y1, x2, y2 }, util) {
@@ -3387,10 +3383,10 @@
33873383
Scratch.vm.renderer.penLine(
33883384
Scratch.vm.renderer._penSkinId,
33893385
attrib,
3390-
x1,
3391-
y1,
3392-
x2,
3393-
y2
3386+
Scratch.Cast.toNumber(x1),
3387+
Scratch.Cast.toNumber(y1),
3388+
Scratch.Cast.toNumber(x2),
3389+
Scratch.Cast.toNumber(y2)
33943390
);
33953391
}
33963392
stampSprite({ sprite }) {
@@ -4125,7 +4121,11 @@
41254121
x = Math.floor(x - 1);
41264122
y = Math.floor(y - 1);
41274123
const colorIndex = (y * curCostume.width + x) * 4;
4128-
if (textureData[colorIndex] && x < curCostume.width && x >= 0) {
4124+
if (
4125+
textureData[colorIndex] !== undefined &&
4126+
x < curCostume.width &&
4127+
x >= 0
4128+
) {
41294129
return (
41304130
this.colorLib.rgbtoSColor({
41314131
R: textureData[colorIndex] / 2.55,
@@ -4778,7 +4778,7 @@
47784778
item > this.programs[shader].uniformDec[uniformName].arrayLength
47794779
)
47804780
return;
4781-
item -= (item - 1) * 2;
4781+
item = (item - 1) * 2;
47824782
this.programs[shader].uniformDat[uniformName][item] = numberX;
47834783
this.programs[shader].uniformDat[uniformName][item + 1] = numberY;
47844784
}
@@ -5712,13 +5712,16 @@
57125712
curCostume.height
57135713
);
57145714

5715+
// don't assume the image is square
5716+
const maxDimension = Math.max(curCostume.width, curCostume.height);
5717+
57155718
gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.penPlusCubemap[name]);
57165719
gl.texImage2D(
5717-
cubemapSetup[faceID].texture.side,
5720+
cubemapSetup[faceID].side,
57185721
0,
57195722
gl.RGBA,
5720-
curCostume.width,
5721-
curCostume.height,
5723+
maxDimension,
5724+
maxDimension,
57225725
0,
57235726
gl.RGBA,
57245727
gl.UNSIGNED_BYTE,
@@ -5743,17 +5746,42 @@
57435746
//Only used for images we got permission to fetch before. Don't need this.
57445747
// eslint-disable-next-line
57455748
const image = new Image();
5749+
57465750
image.onload = () => {
5747-
gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.penPlusCubemap[name]);
5748-
gl.texImage2D(
5749-
cubemapSetup[faceID].texture.side,
5750-
0,
5751-
gl.RGBA,
5752-
gl.RGBA,
5753-
gl.UNSIGNED_BYTE,
5754-
image
5755-
);
5751+
const maxDimension = Math.max(image.width, image.height);
5752+
if (image.width != image.height) {
5753+
// I don't know if there's a better way to do this.
5754+
const canvas = document.createElement("canvas");
5755+
canvas.width = maxDimension;
5756+
canvas.height = maxDimension;
5757+
5758+
const ctx = canvas.getContext("2d");
5759+
ctx.drawImage(
5760+
image,
5761+
(maxDimension - image.width) / 2,
5762+
(maxDimension - image.height) / 2
5763+
);
57565764

5765+
gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.penPlusCubemap[name]);
5766+
gl.texImage2D(
5767+
cubemapSetup[faceID].side,
5768+
0,
5769+
gl.RGBA,
5770+
gl.RGBA,
5771+
gl.UNSIGNED_BYTE,
5772+
canvas
5773+
);
5774+
} else {
5775+
gl.bindTexture(gl.TEXTURE_CUBE_MAP, this.penPlusCubemap[name]);
5776+
gl.texImage2D(
5777+
cubemapSetup[faceID].side,
5778+
0,
5779+
gl.RGBA,
5780+
gl.RGBA,
5781+
gl.UNSIGNED_BYTE,
5782+
image
5783+
);
5784+
}
57575785
gl.texParameteri(
57585786
gl.TEXTURE_CUBE_MAP,
57595787
gl.TEXTURE_MIN_FILTER,
@@ -6116,13 +6144,20 @@
61166144
//If it is named scratch stage get that stuff out of here
61176145
if (name == "Scratch Stage") return;
61186146

6147+
// preserve GL binding
6148+
if (!this.inDrawRegion) renderer.enterDrawRegion(this.penPlusDrawRegion);
6149+
const prevFB = gl.getParameter(gl.FRAMEBUFFER_BINDING);
6150+
61196151
//if the render texture exists delete it
61206152
if (this.renderTextures[this.prefixes.renderTextures + name]) {
61216153
this._deleteFramebuffer(
61226154
this.renderTextures[this.prefixes.renderTextures + name]
61236155
);
61246156
}
61256157

6158+
// restore GL framebuffer binding
6159+
gl.bindFramebuffer(gl.FRAMEBUFFER, prevFB);
6160+
61266161
//Add it
61276162
this.renderTextures[this.prefixes.renderTextures + name] =
61286163
twgl.createFramebufferInfo(gl, triBufferAttachments);
@@ -6134,13 +6169,20 @@
61346169
//If it is named scratch stage get that stuff out of here
61356170
if (name == "Scratch Stage") return;
61366171

6172+
// preserve GL binding
6173+
if (!this.inDrawRegion) renderer.enterDrawRegion(this.penPlusDrawRegion);
6174+
const prevFB = gl.getParameter(gl.FRAMEBUFFER_BINDING);
6175+
61376176
//if the render texture exists delete it
61386177
if (this.renderTextures[this.prefixes.renderTextures + name]) {
61396178
this._deleteFramebuffer(
61406179
this.renderTextures[this.prefixes.renderTextures + name]
61416180
);
61426181
}
61436182

6183+
// restore GL framebuffer binding
6184+
gl.bindFramebuffer(gl.FRAMEBUFFER, prevFB);
6185+
61446186
//Add it
61456187
this.renderTextures[this.prefixes.renderTextures + name] =
61466188
twgl.createFramebufferInfo(gl, triBufferAttachments);

0 commit comments

Comments
 (0)