@@ -1,21 +1,23 @@
|
||||
/**
|
||||
* Unity 2D 绘制排序(对齐主站 GraphicsSettings + 预制体 SortingOrder + TilemapRenderer Individual)
|
||||
* 等距绘制排序(重设计)
|
||||
*
|
||||
* - GraphicsSettings: TransparencySortMode=CustomAxis, axis=(0, 1, -0.26)
|
||||
* - TilemapRenderer: SortingOrder=2, Mode=Individual, SortOrder=TopRight → 每格 -(cellX+cellY)
|
||||
* - nProp=1, Tilemap/Vehicle=2, Prop=3, Player=4
|
||||
* - 仅贴图 WallBlock 可遮挡实体;可行走砖永在实体之下
|
||||
* - 砖↔砖:格子深度(南 y 小 → 前,同 y 东 x 大 → 前)→ JumpBlock 抬高 Y → Individual order
|
||||
* 单一全序标量,无成环、无特判互打:
|
||||
* frontKey = -(cellX + cellY) * CELL // 更小 x+y → 屏幕更靠下 → 更靠前
|
||||
* + typeRank // 同对角条带内:地板 < 金币 < 载具 < 角色 < 墙
|
||||
*
|
||||
* 移动中采样拆开:
|
||||
* - 角色:朝前落点 / 朝后起点
|
||||
* - 载具:朝前起点(防浮到前砖上)/ 朝后落点(防途中比落点更靠前而不被下方砖挡)
|
||||
* 贴墙南/西立面格上的实体:抬到该墙之上(露脚)。
|
||||
* 角色永远盖过同条带载具。
|
||||
*/
|
||||
|
||||
import { Vec3 } from 'cc';
|
||||
import { CommonDefine } from '../core/Define';
|
||||
import { cellToWorldCenter } from '../core/GridCoords';
|
||||
|
||||
/** Unity GraphicsSettings.asset m_TransparencySortAxis */
|
||||
export const UNITY_TRANSPARENCY_AXIS = { x: 0, y: 1, z: -0.26 } as const;
|
||||
|
||||
/** Unity SpriteRenderer / TilemapRenderer m_SortingOrder */
|
||||
export const UNITY_SORTING_ORDER = {
|
||||
nProp: 1,
|
||||
tilemap: 2,
|
||||
@@ -24,10 +26,13 @@ export const UNITY_SORTING_ORDER = {
|
||||
player: 4,
|
||||
} as const;
|
||||
|
||||
const ORDER_SCALE = 10_000;
|
||||
const DEPTH_SCALE = 100;
|
||||
/** JumpBlock 行走面比 Baseblock 抬高的最小 Y 差(px),超过则优先比 depthY */
|
||||
const TILE_ELEVATION_Y_THRESHOLD = 12;
|
||||
const CELL = 100;
|
||||
/** 同对角条带内层级:地板 < 金币 < 载具 < 角色 < 墙立面 */
|
||||
const RANK_FLOOR = 0;
|
||||
const RANK_PICKABLE = 20;
|
||||
const RANK_VEHICLE = 30;
|
||||
const RANK_PLAYER = 35;
|
||||
const RANK_WALL = 40;
|
||||
|
||||
export function isWallBlockTileName(tileName: string): boolean {
|
||||
return tileName === CommonDefine.BlockWall;
|
||||
@@ -37,7 +42,6 @@ export function isWalkableTileName(tileName: string): boolean {
|
||||
return tileName === CommonDefine.BlockBase || tileName === CommonDefine.BlockJump;
|
||||
}
|
||||
|
||||
/** Unity TilemapRenderer Individual + TopRight:baseOrder - (cellX+cellY) */
|
||||
export function unityTileIndividualSortingOrder(cellX: number, cellY: number): number {
|
||||
return UNITY_SORTING_ORDER.tilemap - cellX - cellY;
|
||||
}
|
||||
@@ -55,7 +59,7 @@ export function unityCompositeSortKey(
|
||||
bias = 0,
|
||||
): number {
|
||||
const depth = unityTransparencyDepth(0, depthWorldY, depthWorldZ);
|
||||
return sortingOrder * ORDER_SCALE + depth * DEPTH_SCALE + bias;
|
||||
return sortingOrder * 10_000 + depth * 100 + bias;
|
||||
}
|
||||
|
||||
export function unityCellCenterY(cellX: number, cellY: number): number {
|
||||
@@ -88,95 +92,95 @@ export function compareIsoDrawOrder(ax: number, ay: number, bx: number, by: numb
|
||||
return ax - bx;
|
||||
}
|
||||
|
||||
export function compareTileCellDepth(ax: number, ay: number, bx: number, by: number): number {
|
||||
return compareIsoDrawOrder(ax, ay, bx, by);
|
||||
}
|
||||
|
||||
function wallFaceSamples(wallX: number, wallY: number): { x: number; y: number }[] {
|
||||
return [{ x: wallX, y: wallY - 1 }, { x: wallX - 1, y: wallY }];
|
||||
}
|
||||
|
||||
function compareWallActorIso(
|
||||
actor: { x: number; y: number },
|
||||
wallX: number,
|
||||
wallY: number,
|
||||
): number {
|
||||
let actorAhead = 0;
|
||||
for (const face of wallFaceSamples(wallX, wallY)) {
|
||||
const iso = compareIsoDrawOrder(actor.x, actor.y, face.x, face.y);
|
||||
if (iso < 0) return iso;
|
||||
if (iso > actorAhead) actorAhead = iso;
|
||||
}
|
||||
return actorAhead;
|
||||
function typeRankForTile(tileName: string): number {
|
||||
return isWallBlockTileName(tileName) ? RANK_WALL : RANK_FLOOR;
|
||||
}
|
||||
|
||||
/** 返回值 > 0 实体更靠前;< 0 墙应挡住 */
|
||||
function compareUnityEntityToWall(
|
||||
function typeRankForEntity(entity: UnitySortableEntity): number {
|
||||
if (entity.isPickable) return RANK_PICKABLE;
|
||||
if (entity.isPlayer) return RANK_PLAYER;
|
||||
return RANK_VEHICLE;
|
||||
}
|
||||
|
||||
/** 越大越靠前(后绘制) */
|
||||
export function isoDrawFrontKey(cellX: number, cellY: number, typeRank: number): number {
|
||||
return -(cellX + cellY) * CELL + typeRank;
|
||||
}
|
||||
|
||||
export function tileDrawFrontKey(tile: UnitySortableTile): number {
|
||||
return isoDrawFrontKey(tile.cellX, tile.cellY, typeRankForTile(tile.tileName));
|
||||
}
|
||||
|
||||
export function entityDrawFrontKey(entity: UnitySortableEntity): number {
|
||||
return isoDrawFrontKey(entity.cellX, entity.cellY, typeRankForEntity(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* 若实体踩在某墙南/西立面上,应露在该墙前面。
|
||||
* 返回需要超过的墙 frontKey,否则 null。
|
||||
*/
|
||||
export function entityFaceLiftOverWall(
|
||||
entity: UnitySortableEntity,
|
||||
tile: UnitySortableTile,
|
||||
): number {
|
||||
): number | null {
|
||||
if (!isWallBlockTileName(tile.tileName)) return null;
|
||||
const cell = { x: entity.cellX, y: entity.cellY };
|
||||
for (const face of wallFaceSamples(tile.cellX, tile.cellY)) {
|
||||
if (face.x === cell.x && face.y === cell.y) return 1;
|
||||
if (face.x === cell.x && face.y === cell.y) {
|
||||
return tileDrawFrontKey(tile);
|
||||
}
|
||||
}
|
||||
|
||||
const tileOrder = unityTileIndividualSortingOrder(tile.cellX, tile.cellY);
|
||||
if (tileOrder > entity.sortingOrder) return -1;
|
||||
|
||||
const wallIso = compareWallActorIso(cell, tile.cellX, tile.cellY);
|
||||
if (wallIso < 0) {
|
||||
if (tile.cellX === cell.x && tile.cellY === cell.y - 1) return -1;
|
||||
if (tile.cellY < cell.y) return 1;
|
||||
return -1;
|
||||
}
|
||||
|
||||
const tileAhead = (tile.cellX + tile.cellY) - (cell.x + cell.y);
|
||||
if (wallIso > 0 && tileAhead > 0) return -1;
|
||||
if (wallIso > 0) return wallIso;
|
||||
|
||||
const yCmp = Math.round(entity.depthY * DEPTH_SCALE) - Math.round(tile.depthY * DEPTH_SCALE);
|
||||
if (yCmp !== 0) return yCmp;
|
||||
if (tileAhead !== 0) return -tileAhead;
|
||||
return compareIsoDrawOrder(tile.cellX, tile.cellY, cell.x, cell.y);
|
||||
return null;
|
||||
}
|
||||
|
||||
/** 砖块格子深度:南侧(cellY 小)更靠前;同行西侧(cellX 小)更靠前 */
|
||||
export function compareTileCellDepth(ax: number, ay: number, bx: number, by: number): number {
|
||||
if (ay !== by) return by - ay;
|
||||
return bx - ax;
|
||||
/** 实体相对整张关卡的 frontKey(含贴墙抬升) */
|
||||
export function entityDrawFrontKeyWithWalls(
|
||||
entity: UnitySortableEntity,
|
||||
walls: UnitySortableTile[],
|
||||
): number {
|
||||
let key = entityDrawFrontKey(entity);
|
||||
for (const w of walls) {
|
||||
const lift = entityFaceLiftOverWall(entity, w);
|
||||
if (lift != null && key <= lift) key = lift + 1;
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 砖↔砖:格子深度优先(下方砖挡上方砖),JumpBlock 抬高再比 depthY。
|
||||
*/
|
||||
export function compareUnityTileToTile(a: UnitySortableTile, b: UnitySortableTile): number {
|
||||
const cellCmp = compareTileCellDepth(a.cellX, a.cellY, b.cellX, b.cellY);
|
||||
if (cellCmp !== 0) return cellCmp;
|
||||
const d = tileDrawFrontKey(a) - tileDrawFrontKey(b);
|
||||
if (d !== 0) return d;
|
||||
return a.cellX - b.cellX;
|
||||
}
|
||||
|
||||
const yCmp = Math.round(a.depthY * DEPTH_SCALE) - Math.round(b.depthY * DEPTH_SCALE);
|
||||
if (Math.abs(yCmp) >= TILE_ELEVATION_Y_THRESHOLD) return yCmp;
|
||||
|
||||
return unityTileIndividualSortingOrder(a.cellX, a.cellY)
|
||||
- unityTileIndividualSortingOrder(b.cellX, b.cellY);
|
||||
export function compareUnityEntityToEntity(a: UnitySortableEntity, b: UnitySortableEntity): number {
|
||||
const d = entityDrawFrontKey(a) - entityDrawFrontKey(b);
|
||||
if (d !== 0) return d;
|
||||
if (a.isPlayer && !b.isPlayer) return 1;
|
||||
if (b.isPlayer && !a.isPlayer) return -1;
|
||||
return a.sortingOrder - b.sortingOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实体 vs 砖块
|
||||
* 非 WallBlock 永在实体之下;仅墙砖可遮挡。
|
||||
* 实体 vs 砖:统一用 frontKey。
|
||||
* (保留函数供外部/测试调用;排序主路径用 build 里的 key)
|
||||
*/
|
||||
export function compareUnityEntityToTile(
|
||||
entity: UnitySortableEntity,
|
||||
tile: UnitySortableTile,
|
||||
): number {
|
||||
if (!isWallBlockTileName(tile.tileName)) {
|
||||
// 地板永在实体下
|
||||
return 1;
|
||||
}
|
||||
return compareUnityEntityToWall(entity, tile);
|
||||
}
|
||||
|
||||
export function compareUnityEntityToEntity(a: UnitySortableEntity, b: UnitySortableEntity): number {
|
||||
const keyA = unityCompositeSortKey(a.sortingOrder, a.depthY);
|
||||
const keyB = unityCompositeSortKey(b.sortingOrder, b.depthY);
|
||||
if (keyA !== keyB) return keyA - keyB;
|
||||
const iso = (a.cellX + a.cellY) - (b.cellX + b.cellY);
|
||||
if (iso !== 0) return iso;
|
||||
if (a.isPlayer && !b.isPlayer) return 1;
|
||||
if (b.isPlayer && !a.isPlayer) return -1;
|
||||
return 0;
|
||||
const lift = entityFaceLiftOverWall(entity, tile);
|
||||
const eKey = lift != null ? lift + 1 : entityDrawFrontKey(entity);
|
||||
return eKey - tileDrawFrontKey(tile);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user