Skip to content

Commit 0233ac6

Browse files
committed
Simplified clip to div by using scissor rather than changing viewport+camera.
In this way we can treat coordinates equally for cliped and not clipped widgets!
1 parent 3189072 commit 0233ac6

File tree

1 file changed

+25
-62
lines changed

1 file changed

+25
-62
lines changed

spine-ts/spine-webgl/src/SpineWebComponentWidget.ts

Lines changed: 25 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,47 +1311,21 @@ class SpineWebComponentOverlay extends HTMLElement implements OverlayAttributes,
13111311
this.renderer.context.gl.clear(this.renderer.context.gl.COLOR_BUFFER_BIT);
13121312
}
13131313

1314-
const clipToBoundStart = (divBounds: Rectangle) => {
1315-
// break current batch and start a new one
1314+
const startScissor = (divBounds: Rectangle) => {
13161315
this.renderer.end();
1317-
1318-
// set the new viewport to the div bound
1319-
const viewportWidth = this.screenToWorldLength(divBounds.width);
1320-
const viewporthHeight = this.screenToWorldLength(divBounds.height);
1321-
this.renderer.context.gl.viewport(
1316+
this.renderer.begin();
1317+
this.renderer.context.gl.enable(this.renderer.context.gl.SCISSOR_TEST);
1318+
this.renderer.context.gl.scissor(
13221319
this.screenToWorldLength(divBounds.x),
13231320
this.canvas.height - this.screenToWorldLength(divBounds.y + divBounds.height),
1324-
viewportWidth,
1325-
viewporthHeight
1321+
this.screenToWorldLength(divBounds.width),
1322+
this.screenToWorldLength(divBounds.height)
13261323
);
1327-
this.renderer.camera.setViewport(viewportWidth, viewporthHeight);
1328-
this.renderer.camera.update();
1329-
1330-
// start the new batch that will be filled with only this skeleton
1331-
this.renderer.begin();
1332-
1333-
// Debug viewport
1334-
// if (true) {
1335-
// this.renderer.circle(true, -viewportWidth / 2, -viewporthHeight / 2, 20, red);
1336-
// this.renderer.circle(true, viewportWidth / 2, -viewporthHeight / 2, 20, red);
1337-
// this.renderer.circle(true, -viewportWidth / 2, viewporthHeight / 2, 20, red);
1338-
// this.renderer.circle(true, viewportWidth / 2, viewporthHeight / 2, 20, red);
1339-
// this.renderer.circle(true, 0, 0, 10, red);
1340-
1341-
// this.renderer.rect(true, 0, 0, -viewportWidth, -viewporthHeight, transparentWhite);
1342-
// this.renderer.rect(true, 0, 0, viewportWidth, viewporthHeight, transparentWhite);
1343-
// }
13441324
}
13451325

1346-
const clipToBoundEnd = () => {
1347-
// end clip batch
1326+
const endScissor = () => {
13481327
this.renderer.end();
1349-
1350-
this.renderer.context.gl.viewport(0, 0, this.canvas.width, this.canvas.height);
1351-
this.renderer.camera.setViewport(this.canvas.width, this.canvas.height);
1352-
this.renderer.camera.update();
1353-
1354-
// start new normal batch
1328+
this.renderer.context.gl.disable(this.renderer.context.gl.SCISSOR_TEST);
13551329
this.renderer.begin();
13561330
}
13571331

@@ -1380,33 +1354,27 @@ class SpineWebComponentOverlay extends HTMLElement implements OverlayAttributes,
13801354
const { padLeft, padRight, padTop, padBottom } = widget
13811355
const paddingShiftHorizontal = (padLeft - padRight) / 2;
13821356
const paddingShiftVertical = (padTop - padBottom) / 2;
1383-
let divOriginX = 0;
1384-
let divOriginY = 0;
1385-
if (clip) {
1386-
// in clip mode, the world origin is the div center (divBounds center)
1387-
clipToBoundStart(divBounds);
1388-
divOriginX = this.screenToWorldLength(divBounds.width * (xAxis + paddingShiftHorizontal));
1389-
divOriginY = this.screenToWorldLength(divBounds.height * (yAxis - paddingShiftVertical));
1390-
} else {
1391-
// get the desired point into the the div (center by default) in world coordinate
1392-
const divX = divBounds.x + divBounds.width * ((xAxis + .5) + paddingShiftHorizontal);
1393-
const divY = divBounds.y + divBounds.height * ((-yAxis + .5) + paddingShiftVertical) - 1;
1394-
this.screenToWorld(tempVector, divX, divY);
1395-
divOriginX = tempVector.x;
1396-
divOriginY = tempVector.y;
1397-
}
1357+
1358+
// get the desired point into the the div (center by default) in world coordinate
1359+
const divX = divBounds.x + divBounds.width * ((xAxis + .5) + paddingShiftHorizontal);
1360+
const divY = divBounds.y + divBounds.height * ((-yAxis + .5) + paddingShiftVertical) - 1;
1361+
this.screenToWorld(tempVector, divX, divY);
1362+
let divOriginX = tempVector.x;
1363+
let divOriginY = tempVector.y;
13981364

13991365
const paddingShrinkWidth = 1 - (padLeft + padRight);
14001366
const paddingShrinkHeight = 1 - (padTop + padBottom);
14011367
const divWidthWorld = this.screenToWorldLength(divBounds.width * paddingShrinkWidth);
14021368
const divHeightWorld = this.screenToWorldLength(divBounds.height * paddingShrinkHeight);
14031369

1370+
if (clip) startScissor(divBounds);
1371+
14041372
if (loading) {
14051373
if (loadingSpinner) {
14061374
if (!widget.loadingScreen) widget.loadingScreen = new LoadingScreen(renderer);
14071375
widget.loadingScreen!.drawInCoordinates(divOriginX, divOriginY);
14081376
}
1409-
if (clip) clipToBoundEnd();
1377+
if (clip) endScissor();
14101378
return;
14111379
}
14121380

@@ -1499,18 +1467,13 @@ class SpineWebComponentOverlay extends HTMLElement implements OverlayAttributes,
14991467
widget.dragBoundsRectangle.width = this.worldToScreenLength(aw * skeleton.scaleX);
15001468
widget.dragBoundsRectangle.height = this.worldToScreenLength(ah * skeleton.scaleY);
15011469

1502-
if (clip) {
1503-
widget.dragBoundsRectangle.x += divBounds.x;
1504-
widget.dragBoundsRectangle.y += divBounds.y;
1505-
}
1506-
1507-
if (debug && !widget.debugDragDiv.isConnected) {
1508-
document.body.appendChild(widget.debugDragDiv);
1470+
if (debug) {
1471+
widget.debugDragDiv.style.left = `${widget.dragBoundsRectangle.x - this.overflowLeftSize}px`;
1472+
widget.debugDragDiv.style.top = `${widget.dragBoundsRectangle.y - this.overflowTopSize}px`;
1473+
widget.debugDragDiv.style.width = `${widget.dragBoundsRectangle.width}px`;
1474+
widget.debugDragDiv.style.height = `${widget.dragBoundsRectangle.height}px`;
1475+
if (!widget.debugDragDiv.isConnected) document.body.appendChild(widget.debugDragDiv);
15091476
}
1510-
widget.debugDragDiv.style.left = `${widget.dragBoundsRectangle.x - this.overflowLeftSize}px`;
1511-
widget.debugDragDiv.style.top = `${widget.dragBoundsRectangle.y - this.overflowTopSize}px`;
1512-
widget.debugDragDiv.style.width = `${widget.dragBoundsRectangle.width}px`;
1513-
widget.debugDragDiv.style.height = `${widget.dragBoundsRectangle.height}px`;
15141477

15151478
if (!debug && widget.debugDragDiv.isConnected) widget.debugDragDiv.remove();
15161479
} else {
@@ -1549,7 +1512,7 @@ class SpineWebComponentOverlay extends HTMLElement implements OverlayAttributes,
15491512
if (elementRef === widget) widget.classList.remove("debug-background-color");
15501513
}
15511514

1552-
if (clip) clipToBoundEnd();
1515+
if (clip) endScissor();
15531516
}
15541517
});
15551518

0 commit comments

Comments
 (0)