tiling
This commit is contained in:
147
fx_graph.cm
147
fx_graph.cm
@@ -411,23 +411,142 @@ function collect_drawables(node, camera, parent_tint, parent_opacity) {
|
||||
]
|
||||
var world_opacity = parent_opacity * (node.opacity != null ? node.opacity : 1)
|
||||
|
||||
// Handle different node types
|
||||
// Handle different node types
|
||||
if (node.type == 'sprite' || (node.image && !node.type)) {
|
||||
if (node.slice && node.tile) {
|
||||
throw Error('Sprite cannot have both "slice" and "tile" parameters.')
|
||||
}
|
||||
|
||||
var pos = node.pos || {x: 0, y: 0}
|
||||
drawables.push({
|
||||
type: 'sprite',
|
||||
layer: node.layer || 0,
|
||||
world_y: pos.y != null ? pos.y : (pos[1] || 0),
|
||||
pos: pos,
|
||||
image: node.image,
|
||||
texture: node.texture,
|
||||
width: node.width || 1,
|
||||
height: node.height || 1,
|
||||
anchor_x: node.anchor_x || 0,
|
||||
anchor_y: node.anchor_y || 0,
|
||||
color: tint_to_color(world_tint, world_opacity),
|
||||
material: node.material
|
||||
})
|
||||
var px = pos.x != null ? pos.x : (pos[0] || 0)
|
||||
var py = pos.y != null ? pos.y : (pos[1] || 0)
|
||||
var w = node.width || 1
|
||||
var h = node.height || 1
|
||||
var ax = node.anchor_x || 0
|
||||
var ay = node.anchor_y || 0
|
||||
var tint = tint_to_color(world_tint, world_opacity)
|
||||
|
||||
// Helper to add a sprite drawable
|
||||
function add_sprite_drawable(rect, uv) {
|
||||
drawables.push({
|
||||
type: 'sprite',
|
||||
layer: node.layer || 0,
|
||||
world_y: py,
|
||||
pos: {x: rect.x, y: rect.y},
|
||||
image: node.image,
|
||||
texture: node.texture,
|
||||
width: rect.width,
|
||||
height: rect.height,
|
||||
anchor_x: 0,
|
||||
anchor_y: 0,
|
||||
uv_rect: uv,
|
||||
color: tint,
|
||||
material: node.material
|
||||
})
|
||||
}
|
||||
|
||||
// Helper to emit tiled area
|
||||
function emit_tiled(rect, uv, tile_size) {
|
||||
var tx = tile_size ? (tile_size.x || tile_size) : rect.width
|
||||
var ty = tile_size ? (tile_size.y || tile_size) : rect.height
|
||||
|
||||
var nx = number.ceiling(rect.width / tx - 0.00001)
|
||||
var ny = number.ceiling(rect.height / ty - 0.00001)
|
||||
|
||||
for (var ix = 0; ix < nx; ix++) {
|
||||
for (var iy = 0; iy < ny; iy++) {
|
||||
var qw = number.min(tx, rect.width - ix * tx)
|
||||
var qh = number.min(ty, rect.height - iy * ty)
|
||||
|
||||
var quv = {
|
||||
x: uv.x,
|
||||
y: uv.y,
|
||||
width: uv.width * (qw / tx),
|
||||
height: uv.height * (qh / ty)
|
||||
}
|
||||
|
||||
add_sprite_drawable({x: rect.x + ix * tx, y: rect.y + iy * ty, width: qw, height: qh}, quv)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Top-left of whole sprite
|
||||
var x0 = px - w * ax
|
||||
var y0 = py - h * ay
|
||||
|
||||
if (node.slice) {
|
||||
// 9-Slice logic
|
||||
var s = node.slice
|
||||
var L = s.left != null ? s.left : (typeof s == 'number' ? s : 0)
|
||||
var R = s.right != null ? s.right : (typeof s == 'number' ? s : 0)
|
||||
var T = s.top != null ? s.top : (typeof s == 'number' ? s : 0)
|
||||
var B = s.bottom != null ? s.bottom : (typeof s == 'number' ? s : 0)
|
||||
|
||||
var stretch = s.stretch != null ? s.stretch : node.stretch
|
||||
|
||||
var Sx = stretch != null ? (stretch.x || stretch) : w
|
||||
var Sy = stretch != null ? (stretch.y || stretch) : h
|
||||
|
||||
// World sizes of borders
|
||||
var WL = L * Sx
|
||||
var WR = R * Sx
|
||||
var HT = T * Sy
|
||||
var HB = B * Sy
|
||||
|
||||
// Middle areas
|
||||
var WM = w - WL - WR
|
||||
var HM = h - HT - HB
|
||||
|
||||
// UV mid dimensions
|
||||
var UM = 1 - L - R
|
||||
var VM = 1 - T - B
|
||||
|
||||
// Natural tile sizes for middle parts
|
||||
var TW = stretch != null ? UM * Sx : WM
|
||||
var TH = stretch != null ? VM * Sy : HM
|
||||
|
||||
// TL
|
||||
add_sprite_drawable({x: x0, y: y0, width: WL, height: HT}, {x:0, y:0, width: L, height: T})
|
||||
// TM
|
||||
emit_tiled({x: x0 + WL, y: y0, width: WM, height: HT}, {x:L, y:0, width: UM, height: T}, {x: TW, y: HT})
|
||||
// TR
|
||||
add_sprite_drawable({x: x0 + WL + WM, y: y0, width: WR, height: HT}, {x: 1-R, y:0, width: R, height: T})
|
||||
|
||||
// ML
|
||||
emit_tiled({x: x0, y: y0 + HT, width: WL, height: HM}, {x:0, y:T, width: L, height: VM}, {x: WL, y: TH})
|
||||
// MM
|
||||
emit_tiled({x: x0 + WL, y: y0 + HT, width: WM, height: HM}, {x:L, y:T, width: UM, height: VM}, {x: TW, y: TH})
|
||||
// MR
|
||||
emit_tiled({x: x0 + WL + WM, y: y0 + HT, width: WR, height: HM}, {x: 1-R, y:T, width: R, height: VM}, {x: WR, y: TH})
|
||||
|
||||
// BL
|
||||
add_sprite_drawable({x: x0, y: y0 + HT + HM, width: WL, height: HB}, {x:0, y: 1-B, width: L, height: B})
|
||||
// BM
|
||||
emit_tiled({x: x0 + WL, y: y0 + HT + HM, width: WM, height: HB}, {x:L, y: 1-B, width: UM, height: B}, {x: TW, y: HB})
|
||||
// BR
|
||||
add_sprite_drawable({x: x0 + WL + WM, y: y0 + HT + HM, width: WR, height: HB}, {x: 1-R, y: 1-B, width: R, height: B})
|
||||
|
||||
} else if (node.tile) {
|
||||
// Full sprite tiling
|
||||
emit_tiled({x: x0, y: y0, width: w, height: h}, {x:0, y:0, width: 1, height: 1}, node.tile)
|
||||
} else {
|
||||
// Normal sprite
|
||||
drawables.push({
|
||||
type: 'sprite',
|
||||
layer: node.layer || 0,
|
||||
world_y: py,
|
||||
pos: pos,
|
||||
image: node.image,
|
||||
texture: node.texture,
|
||||
width: w,
|
||||
height: h,
|
||||
anchor_x: ax,
|
||||
anchor_y: ay,
|
||||
color: tint,
|
||||
material: node.material
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (node.type == 'text') {
|
||||
|
||||
Reference in New Issue
Block a user