Help finding a problem. I built a plugin for RPG Maker MZ that draws directional real-time shadows from tiles (static objects).
Overall, it works, but there are two issues:
I couldn’t find a solution on Gemini… I’m not very familiar with the code myself.
Overall, it works, but there are two issues:
- Shadows are drawn on top of the object that casts them (visible at certain angles, see screenshot).
- Some objects produce duplicate shadows (circled in red on the screenshot).
JavaScript:
/*:
* @target MZ
* @plugindesc [v18] Натуральные тени (Монолитная склейка текстур).
* @author Gemini
*
* @param shadowRegion
* @text Region ID для основания
* @type number
* @default 10
*
* @param shadowOpacity
* @text Прозрачность
* @type number
* @default 140
*
* @param skewX
* @text Наклон по X
* @type number
* @decimals 2
* @default -0.5
*
* @param heightScale
* @text Вертикальное сжатие
* @type number
* @decimals 2
* @default 0.5
*/
(() => {
const pluginName = "_RealTileShadows";
const parameters = PluginManager.parameters(pluginName);
const SHADOW_REGION = Number(parameters['shadowRegion'] || 10);
const SHADOW_OPACITY = Number(parameters['shadowOpacity'] || 140);
const SKEW_X = Number(parameters['skewX'] || -0.5);
const H_SCALE = Number(parameters['heightScale'] || 0.5);
const _Spriteset_Map_createLowerLayer = Spriteset_Map.prototype.createLowerLayer;
Spriteset_Map.prototype.createLowerLayer = function() {
_Spriteset_Map_createLowerLayer.call(this);
this.createShadowLayer();
this.createTileShadows();
};
Spriteset_Map.prototype.createShadowLayer = function() {
if (this._shadowLayer) this._baseSprite.removeChild(this._shadowLayer);
this._shadowLayer = new Sprite();
const mapIndex = this._baseSprite.children.indexOf(this._tilemap);
this._baseSprite.addChildAt(this._shadowLayer, mapIndex + 1);
};
Spriteset_Map.prototype.createTileShadows = function() {
this._tileShadows = [];
for (let x = 0; x < $gameMap.width(); x++) {
for (let y = 0; y < $gameMap.height(); y++) {
if ($gameMap.regionId(x, y) === SHADOW_REGION) {
this.createMonolithShadow(x, y);
}
}
}
};
Spriteset_Map.prototype.createMonolithShadow = function(bx, by) {
const tw = $gameMap.tileWidth();
const th = $gameMap.tileHeight();
const maxHeight = 6;
// Создаем один большой битмап для всего объекта
const fullBitmap = new Bitmap(tw, th * maxHeight);
let hasData = false;
for (let h = 0; h < maxHeight; h++) {
const ty = by - h;
if (ty < 0) break;
let tileId = 0;
for (let l = 3; l >= 1; l--) {
const id = $gameMap.tileId(bx, ty, l);
if (id > 0 && id < 1024) { tileId = id; break; }
}
if (tileId > 0) {
const setIndex = Math.floor(tileId / 256);
const tileset = $gameMap.tileset();
const bitmapName = tileset.tilesetNames[setIndex + 5];
const sourceBitmap = ImageManager.loadTileset(bitmapName);
sourceBitmap.addLoadListener(() => {
const sx = (Math.floor(tileId / 128) % 2 * 8 + tileId % 8) * tw;
const sy = (Math.floor(tileId % 256 / 8) % 16) * th;
// Рисуем кусок тайла в наш большой битмап (снизу вверх)
fullBitmap.blt(sourceBitmap, sx, sy, tw, th, 0, (maxHeight - 1 - h) * th);
});
hasData = true;
}
}
if (!hasData) return;
const shadow = new Sprite(fullBitmap);
shadow.tint = 0x000000;
shadow.opacity = SHADOW_OPACITY;
// Точка привязки — самый низ нашего длинного спрайта
shadow.anchor.x = 0.5;
shadow.anchor.y = 1.0;
shadow._baseX = bx;
shadow._baseY = by;
// Применяем трансформации к ЕДИНОМУ объекту
shadow.scale.y = -H_SCALE;
shadow.skew.x = SKEW_X;
this._shadowLayer.addChild(shadow);
this._tileShadows.push(shadow);
};
const _Spriteset_Map_update = Spriteset_Map.prototype.update;
Spriteset_Map.prototype.update = function() {
_Spriteset_Map_update.call(this);
if (this._tileShadows && this._shadowLayer) {
const tw = $gameMap.tileWidth();
const th = $gameMap.tileHeight();
this._shadowLayer.x = -$gameMap.displayX() * tw;
this._shadowLayer.y = -$gameMap.displayY() * th;
for (const s of this._tileShadows) {
s.x = s._baseX * tw + tw / 2;
s.y = s._baseY * th + th;
}
}
};
})();
I couldn’t find a solution on Gemini… I’m not very familiar with the code myself.