Skip to content

Commit 7a0f2a9

Browse files
authored
bg opacity thing
1 parent 824b4ad commit 7a0f2a9

1 file changed

Lines changed: 72 additions & 7 deletions

File tree

static/extensions/Gen1x/lighting.js

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ self.onmessage = ({ data: msg }) => {
365365

366366
const RENDER_DEFAULTS = {
367367
shadowOpacity: 0.85,
368+
bgOpacity: 1.0,
368369
ambient: '#333333',
369370
bloomAmount: 1.0,
370371
bloomRadius: 0.32,
@@ -385,6 +386,7 @@ self.onmessage = ({ data: msg }) => {
385386
this.ambient = RENDER_DEFAULTS.ambient;
386387
this.visible = true;
387388
this.shadowOpacity = RENDER_DEFAULTS.shadowOpacity;
389+
this.bgOpacity = RENDER_DEFAULTS.bgOpacity;
388390

389391
this.bloomAmount = RENDER_DEFAULTS.bloomAmount;
390392
this.bloomRadius = RENDER_DEFAULTS.bloomRadius;
@@ -452,7 +454,7 @@ self.onmessage = ({ data: msg }) => {
452454
async openRenderSettings() {
453455
const ScratchBlocks = window.ScratchBlocks;
454456
if (!ScratchBlocks || !ScratchBlocks.customPrompt) {
455-
const op = parseFloat(prompt('Shadow Opacity (0-1):', this.shadowOpacity));
457+
const op = parseFloat(prompt('Overlay Opacity (0-1):', this.shadowOpacity));
456458
if (!isNaN(op)) this.shadowOpacity = Math.max(0, Math.min(1, op));
457459
const amb = prompt('Ambient color (hex):', this.ambient);
458460
if (amb) {
@@ -466,6 +468,7 @@ self.onmessage = ({ data: msg }) => {
466468

467469
const snapshot = {
468470
shadowOpacity: this.shadowOpacity,
471+
bgOpacity: this.bgOpacity,
469472
ambient: this.ambient,
470473
bloomAmount: this.bloomAmount,
471474
bloomRadius: this.bloomRadius,
@@ -489,6 +492,7 @@ self.onmessage = ({ data: msg }) => {
489492

490493
const applyPending = () => {
491494
this.shadowOpacity = pending.shadowOpacity;
495+
this.bgOpacity = pending.bgOpacity;
492496
this.ambient = pending.ambient;
493497
this._ambientRGB = this.hexToRgb(pending.ambient);
494498
this.bloomAmount = pending.bloomAmount;
@@ -526,6 +530,7 @@ self.onmessage = ({ data: msg }) => {
526530
role: 'cancel',
527531
callback: () => {
528532
this.shadowOpacity = snapshot.shadowOpacity;
533+
this.bgOpacity = snapshot.bgOpacity;
529534
this.ambient = snapshot.ambient;
530535
this._ambientRGB = this.hexToRgb(snapshot.ambient);
531536
this.bloomAmount = snapshot.bloomAmount;
@@ -592,7 +597,14 @@ self.onmessage = ({ data: msg }) => {
592597
return (v / 100).toFixed(2);
593598
});
594599
shadowVal.textContent = this.shadowOpacity.toFixed(2);
595-
container.appendChild(makeRow('Shadow Opacity', shadowSlider, shadowVal));
600+
container.appendChild(makeRow('Overlay Opacity', shadowSlider, shadowVal));
601+
602+
const [bgOpacitySlider, bgOpacityVal] = makeSlider(0, 100, 1, Math.round(this.bgOpacity * 100), v => {
603+
pending.bgOpacity = v / 100;
604+
return (v / 100).toFixed(2);
605+
});
606+
bgOpacityVal.textContent = this.bgOpacity.toFixed(2);
607+
container.appendChild(makeRow('Background Opacity', bgOpacitySlider, bgOpacityVal));
596608

597609
const ambientInput = document.createElement('input');
598610
ambientInput.type = 'color';
@@ -812,6 +824,8 @@ self.onmessage = ({ data: msg }) => {
812824
};
813825
shadowSlider.value = Math.round(RENDER_DEFAULTS.shadowOpacity * 100);
814826
shadowVal.textContent = RENDER_DEFAULTS.shadowOpacity.toFixed(2);
827+
bgOpacitySlider.value = Math.round(RENDER_DEFAULTS.bgOpacity * 100);
828+
bgOpacityVal.textContent = RENDER_DEFAULTS.bgOpacity.toFixed(2);
815829
ambientInput.value = RENDER_DEFAULTS.ambient;
816830
bloomSlider.value = Math.round(RENDER_DEFAULTS.bloomAmount * 100);
817831
bloomVal.textContent = RENDER_DEFAULTS.bloomAmount.toFixed(2);
@@ -858,6 +872,7 @@ self.onmessage = ({ data: msg }) => {
858872
if (!vm.runtime.extensionStorage.simpleLighting) vm.runtime.extensionStorage.simpleLighting = {};
859873
Object.assign(vm.runtime.extensionStorage.simpleLighting, {
860874
shadowOpacity: this.shadowOpacity,
875+
bgOpacity: this.bgOpacity,
861876
ambient: this.ambient,
862877
bloomAmount: this.bloomAmount,
863878
bloomRadius: this.bloomRadius,
@@ -879,6 +894,7 @@ self.onmessage = ({ data: msg }) => {
879894
const s = vm.runtime.extensionStorage?.simpleLighting;
880895
if (!s) return;
881896
if (typeof s.shadowOpacity === 'number') this.shadowOpacity = s.shadowOpacity;
897+
if (typeof s.bgOpacity === 'number') this.bgOpacity = s.bgOpacity;
882898
if (typeof s.ambient === 'string') {
883899
this.ambient = s.ambient;
884900
this._ambientRGB = this.hexToRgb(s.ambient);
@@ -902,6 +918,7 @@ self.onmessage = ({ data: msg }) => {
902918
if (isPenguinMod) {
903919
return {
904920
shadowOpacity: this.shadowOpacity,
921+
bgOpacity: this.bgOpacity,
905922
ambient: this.ambient,
906923
bloomAmount: this.bloomAmount,
907924
bloomRadius: this.bloomRadius,
@@ -924,6 +941,7 @@ self.onmessage = ({ data: msg }) => {
924941
deserialize(data) {
925942
if (isPenguinMod && data) {
926943
if (typeof data.shadowOpacity === 'number') this.shadowOpacity = data.shadowOpacity;
944+
if (typeof data.bgOpacity === 'number') this.bgOpacity = data.bgOpacity;
927945
if (typeof data.ambient === 'string') {
928946
this.ambient = data.ambient;
929947
this._ambientRGB = this.hexToRgb(data.ambient);
@@ -1481,6 +1499,16 @@ self.onmessage = ({ data: msg }) => {
14811499
this.canvas.style.imageRendering = '';
14821500
}
14831501

1502+
const targetOpacity = String(this.shadowOpacity);
1503+
if (this.canvas.style.opacity !== targetOpacity) {
1504+
this.canvas.style.opacity = targetOpacity;
1505+
}
1506+
1507+
const targetBlend = this.bgOpacity < 0.01 ? 'screen' : 'multiply';
1508+
if (this.canvas.style.mixBlendMode !== targetBlend) {
1509+
this.canvas.style.mixBlendMode = targetBlend;
1510+
}
1511+
14841512
if (this._mode === 'worker') {
14851513
const needsWorkerRender = this._dirty || (this.cameraFollow && isPenguinMod);
14861514
if (needsWorkerRender) {
@@ -1497,7 +1525,7 @@ self.onmessage = ({ data: msg }) => {
14971525
lightBuf: this._uboData,
14981526
n,
14991527
ambient: ambientRGB,
1500-
opacity: this.shadowOpacity,
1528+
opacity: this.bgOpacity,
15011529
bloomAmount: this.bloomAmount,
15021530
bloomRadius: this.bloomRadius,
15031531
bloomThreshold: this.bloomThreshold,
@@ -1567,7 +1595,7 @@ self.onmessage = ({ data: msg }) => {
15671595
gl.useProgram(prog);
15681596
gl.uniform2f(u.res, w, h);
15691597
gl.uniform3f(u.ambient, ambientRGB[0], ambientRGB[1], ambientRGB[2]);
1570-
gl.uniform1f(u.opacity, this.shadowOpacity);
1598+
gl.uniform1f(u.opacity, this.bgOpacity);
15711599
gl.uniform1i(u.nLights, n);
15721600
gl.uniform1f(u.bloomAmount, this.bloomAmount);
15731601
gl.uniform1f(u.bloomRadius, this.bloomRadius);
@@ -1604,6 +1632,12 @@ self.onmessage = ({ data: msg }) => {
16041632
this._markDirty();
16051633
}
16061634

1635+
settingSetBgOpacity(args) {
1636+
this.bgOpacity = Math.max(0, Math.min(1, Scratch.Cast.toNumber(args.OPACITY)));
1637+
this._saveRenderSettings();
1638+
this._markDirty();
1639+
}
1640+
16071641
settingSetAmbient(args) {
16081642
this.ambient = Scratch.Cast.toString(args.COLOR);
16091643
this._ambientRGB = this.hexToRgb(this.ambient);
@@ -1662,6 +1696,7 @@ self.onmessage = ({ data: msg }) => {
16621696

16631697
settingResetAll() {
16641698
this.shadowOpacity = RENDER_DEFAULTS.shadowOpacity;
1699+
this.bgOpacity = RENDER_DEFAULTS.bgOpacity;
16651700
this.ambient = RENDER_DEFAULTS.ambient;
16661701
this._ambientRGB = this.hexToRgb(RENDER_DEFAULTS.ambient);
16671702
this.bloomAmount = RENDER_DEFAULTS.bloomAmount;
@@ -1685,6 +1720,8 @@ self.onmessage = ({ data: msg }) => {
16851720
switch (key) {
16861721
case 'shadowOpacity':
16871722
return this.shadowOpacity;
1723+
case 'bgOpacity':
1724+
return this.bgOpacity;
16881725
case 'ambient':
16891726
return this.ambient;
16901727
case 'bloomAmount':
@@ -1880,14 +1917,25 @@ self.onmessage = ({ data: msg }) => {
18801917
{
18811918
opcode: 'setShadowOpacity',
18821919
blockType: Scratch.BlockType.COMMAND,
1883-
text: 'set shadow opacity to [OPACITY]%',
1920+
text: 'set overlay opacity to [OPACITY]%',
18841921
arguments: {
18851922
OPACITY: {
18861923
type: Scratch.ArgumentType.NUMBER,
18871924
defaultValue: 85
18881925
}
18891926
}
18901927
},
1928+
{
1929+
opcode: 'setBgOpacity',
1930+
blockType: Scratch.BlockType.COMMAND,
1931+
text: 'set background opacity to [OPACITY]%',
1932+
arguments: {
1933+
OPACITY: {
1934+
type: Scratch.ArgumentType.NUMBER,
1935+
defaultValue: 100
1936+
}
1937+
}
1938+
},
18911939
'---',
18921940
{
18931941
blockType: Scratch.BlockType.LABEL,
@@ -2131,14 +2179,26 @@ self.onmessage = ({ data: msg }) => {
21312179
opcode: 'settingSetShadowOpacity',
21322180
blockType: Scratch.BlockType.COMMAND,
21332181
hideFromPalette: false,
2134-
text: 'set shadow opacity to [OPACITY]',
2182+
text: 'set overlay opacity to [OPACITY]',
21352183
arguments: {
21362184
OPACITY: {
21372185
type: Scratch.ArgumentType.NUMBER,
21382186
defaultValue: 0.85
21392187
}
21402188
}
21412189
},
2190+
{
2191+
opcode: 'settingSetBgOpacity',
2192+
blockType: Scratch.BlockType.COMMAND,
2193+
hideFromPalette: false,
2194+
text: 'set background opacity to [OPACITY]',
2195+
arguments: {
2196+
OPACITY: {
2197+
type: Scratch.ArgumentType.NUMBER,
2198+
defaultValue: 1.0
2199+
}
2200+
}
2201+
},
21422202
{
21432203
opcode: 'settingSetAmbient',
21442204
blockType: Scratch.BlockType.COMMAND,
@@ -2321,7 +2381,7 @@ self.onmessage = ({ data: msg }) => {
23212381
},
23222382
settingMenu: {
23232383
acceptReporters: false,
2324-
items: ['shadowOpacity', 'ambient', 'bloomAmount', 'bloomRadius', 'bloomThreshold', 'pixelated', 'pixelSize', 'contrast', 'colorTemp', 'resetLightsOnStart', 'renderThread', 'cameraFollow', 'cameraFollowName']
2384+
items: ['shadowOpacity', 'bgOpacity', 'ambient', 'bloomAmount', 'bloomRadius', 'bloomThreshold', 'pixelated', 'pixelSize', 'contrast', 'colorTemp', 'resetLightsOnStart', 'renderThread', 'cameraFollow', 'cameraFollowName']
23252385
},
23262386
renderThreadMenu: {
23272387
acceptReporters: false,
@@ -2349,6 +2409,11 @@ self.onmessage = ({ data: msg }) => {
23492409
this._markDirty();
23502410
}
23512411

2412+
setBgOpacity(args) {
2413+
this.bgOpacity = Math.max(0, Math.min(100, Scratch.Cast.toNumber(args.OPACITY))) / 100;
2414+
this._markDirty();
2415+
}
2416+
23522417
_cacheRGB(li, hex) {
23532418
[li.cr, li.cg, li.cb] = this.hexToRgb(hex || '#ffffff');
23542419
}

0 commit comments

Comments
 (0)