Skip to content

Commit c6c9b25

Browse files
committed
Camera.alpha (and its related method Camera.setAlpha) allows you to get an alpha level for the entire camera. This impacts everything it is rendering, even if those objects also have their own alpha values too. You can tween the property to make the camera contents fade in / out, or you can set it as needed in your game.
1 parent 2373681 commit c6c9b25

7 files changed

Lines changed: 23 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Camera - New Features, Updates and Fixes
66

7+
* `Camera.alpha` (and its related method `Camera.setAlpha`) allows you to get an alpha level for the entire camera. This impacts everything it is rendering, even if those objects also have their own alpha values too. You can tween the property to make the camera contents fade in / out, or you can set it as needed in your game.
78
* `Camera.deadzone` (and its related method `Camera.setDeadzone`) allows you to specify the deadzone for a camera. The deadzone is a rectangular region used when a camera is following a target. If the target is within the deadzone then the camera will not scroll. As soon as the target leaves the deadzone, the camera will begin tracking it (applying lerp if needed.) It allows you to set a region of the camera in which a player can move freely before tracking begins. The deadzone is re-centered on the camera mid point every frame, meaning you can also use the rectangle for other in-game chcecks as needed.
89
* `Camera.midPoint` is a new Vec2 property that is updated every frame. Use it to obtain exactly where in the world the center of the camera is currently looking.
910
* `Camera.displayWidth` is a new property that returns the display width of the camera, factoring in the current zoom level.

src/renderer/canvas/utils/DrawImage.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ var DrawImage = function (src, camera, parentMatrix)
3131
// Nothing to see, so abort early
3232
return;
3333
}
34-
else if (renderer.currentAlpha !== alpha)
35-
{
36-
renderer.currentAlpha = alpha;
37-
ctx.globalAlpha = alpha;
38-
}
34+
35+
ctx.globalAlpha = alpha;
3936

4037
// Blend Mode
4138

src/renderer/webgl/pipelines/FlatTintPipeline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ var FlatTintPipeline = new Class({
732732
var srcScaleY = graphics.scaleY;
733733
var srcRotation = graphics.rotation;
734734
var commands = graphics.commandBuffer;
735-
var alpha = graphics.alpha;
735+
var alpha = camera.alpha * graphics.alpha;
736736
var lineAlpha = 1.0;
737737
var fillAlpha = 1.0;
738738
var lineColor = 0;

src/renderer/webgl/pipelines/TextureTintPipeline.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -813,10 +813,10 @@ var TextureTintPipeline = new Class({
813813
var scaleX = sprite.scaleX;
814814
var scaleY = sprite.scaleY;
815815
var rotation = sprite.rotation;
816-
var alphaTL = sprite._alphaTL;
817-
var alphaTR = sprite._alphaTR;
818-
var alphaBL = sprite._alphaBL;
819-
var alphaBR = sprite._alphaBR;
816+
var alphaTL = camera.alpha * sprite._alphaTL;
817+
var alphaTR = camera.alpha * sprite._alphaTR;
818+
var alphaBL = camera.alpha * sprite._alphaBL;
819+
var alphaBR = camera.alpha * sprite._alphaBR;
820820
var tintTL = sprite._tintTL;
821821
var tintTR = sprite._tintTR;
822822
var tintBL = sprite._tintBL;
@@ -1062,7 +1062,7 @@ var TextureTintPipeline = new Class({
10621062
vertexViewF32[vertexOffset + 1] = ty;
10631063
vertexViewF32[vertexOffset + 2] = uvs[index + 0];
10641064
vertexViewF32[vertexOffset + 3] = uvs[index + 1];
1065-
vertexViewU32[vertexOffset + 4] = getTint(colors[index0], alphas[index0]);
1065+
vertexViewU32[vertexOffset + 4] = getTint(colors[index0], camera.alpha * alphas[index0]);
10661066

10671067
vertexOffset += 5;
10681068
index0 += 1;
@@ -1116,7 +1116,7 @@ var TextureTintPipeline = new Class({
11161116
var lineHeight = fontData.lineHeight;
11171117
var scale = (bitmapText.fontSize / fontData.size);
11181118
var chars = fontData.chars;
1119-
var alpha = bitmapText.alpha;
1119+
var alpha = camera.alpha * bitmapText.alpha;
11201120
var vTintTL = getTint(bitmapText._tintTL, alpha);
11211121
var vTintTR = getTint(bitmapText._tintTR, alpha);
11221122
var vTintBL = getTint(bitmapText._tintBL, alpha);
@@ -1393,7 +1393,7 @@ var TextureTintPipeline = new Class({
13931393
var lineHeight = fontData.lineHeight;
13941394
var scale = (bitmapText.fontSize / fontData.size);
13951395
var chars = fontData.chars;
1396-
var alpha = bitmapText.alpha;
1396+
var alpha = camera.alpha * bitmapText.alpha;
13971397
var vTintTL = getTint(bitmapText._tintTL, alpha);
13981398
var vTintTR = getTint(bitmapText._tintTR, alpha);
13991399
var vTintBL = getTint(bitmapText._tintBL, alpha);
@@ -1724,10 +1724,10 @@ var TextureTintPipeline = new Class({
17241724
text.scrollFactorX, text.scrollFactorY,
17251725
text.displayOriginX, text.displayOriginY,
17261726
0, 0, text.canvasTexture.width, text.canvasTexture.height,
1727-
getTint(text._tintTL, text._alphaTL),
1728-
getTint(text._tintTR, text._alphaTR),
1729-
getTint(text._tintBL, text._alphaBL),
1730-
getTint(text._tintBR, text._alphaBR),
1727+
getTint(text._tintTL, camera.alpha * text._alphaTL),
1728+
getTint(text._tintTR, camera.alpha * text._alphaTR),
1729+
getTint(text._tintBL, camera.alpha * text._alphaBL),
1730+
getTint(text._tintBR, camera.alpha * text._alphaBR),
17311731
0, 0,
17321732
camera,
17331733
parentTransformMatrix
@@ -1752,7 +1752,7 @@ var TextureTintPipeline = new Class({
17521752
var tileset = tilemapLayer.tileset;
17531753
var scrollFactorX = tilemapLayer.scrollFactorX;
17541754
var scrollFactorY = tilemapLayer.scrollFactorY;
1755-
var alpha = tilemapLayer.alpha;
1755+
var alpha = camera.alpha * tilemapLayer.alpha;
17561756
var x = tilemapLayer.x;
17571757
var y = tilemapLayer.y;
17581758
var sx = tilemapLayer.scaleX;
@@ -1818,10 +1818,10 @@ var TextureTintPipeline = new Class({
18181818
tileSprite.scrollFactorX, tileSprite.scrollFactorY,
18191819
tileSprite.originX * tileSprite.width, tileSprite.originY * tileSprite.height,
18201820
0, 0, tileSprite.width, tileSprite.height,
1821-
getTint(tileSprite._tintTL, tileSprite._alphaTL),
1822-
getTint(tileSprite._tintTR, tileSprite._alphaTR),
1823-
getTint(tileSprite._tintBL, tileSprite._alphaBL),
1824-
getTint(tileSprite._tintBR, tileSprite._alphaBR),
1821+
getTint(tileSprite._tintTL, camera.alpha * tileSprite._alphaTL),
1822+
getTint(tileSprite._tintTR, camera.alpha * tileSprite._alphaTR),
1823+
getTint(tileSprite._tintBL, camera.alpha * tileSprite._alphaBL),
1824+
getTint(tileSprite._tintBR, camera.alpha * tileSprite._alphaBR),
18251825
(tileSprite.tilePositionX % tileSprite.frame.width) / tileSprite.frame.width,
18261826
(tileSprite.tilePositionY % tileSprite.frame.height) / tileSprite.frame.height,
18271827
camera,

src/tilemaps/dynamiclayer/DynamicTilemapLayerCanvasRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var DynamicTilemapLayerCanvasRenderer = function (renderer, src, interpolationPe
6767
ctx.scale(tile.flipX ? -1 : 1, tile.flipY ? -1 : 1);
6868
}
6969

70-
ctx.globalAlpha = src.alpha * tile.alpha;
70+
ctx.globalAlpha = camera.alpha * src.alpha * tile.alpha;
7171

7272
ctx.drawImage(
7373
image,

src/tilemaps/staticlayer/StaticTilemapLayer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ var StaticTilemapLayer = new Class({
281281
var ty2 = tyh;
282282
var tx3 = txw;
283283
var ty3 = ty;
284-
var tint = Utils.getTintAppendFloatAlpha(0xffffff, this.alpha * tile.alpha);
284+
var tint = Utils.getTintAppendFloatAlpha(0xffffff, camera.alpha * this.alpha * tile.alpha);
285285

286286
vertexViewF32[voffset + 0] = tx0;
287287
vertexViewF32[voffset + 1] = ty0;

src/tilemaps/staticlayer/StaticTilemapLayerCanvasRenderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var StaticTilemapLayerCanvasRenderer = function (renderer, src, interpolationPer
4242
ctx.rotate(src.rotation);
4343
ctx.scale(src.scaleX, src.scaleY);
4444
ctx.scale(src.flipX ? -1 : 1, src.flipY ? -1 : 1);
45-
ctx.globalAlpha = src.alpha;
45+
ctx.globalAlpha = camera.alpha * src.alpha;
4646

4747
for (var index = 0; index < tileCount; ++index)
4848
{

0 commit comments

Comments
 (0)