--!strict -- ============================================================ -- @thebrokedesignerxx -- ============================================================ -- VENETIAN BLINDS — After Effects style blinds wipe (Path Effect). -- Reveals/hides the shape through parallel strips. -- -- HOW TO USE: apply as a Path Effect on the shape and keyframe -- completion from 0 to 100 for the wipe. -- -- INPUTS: -- completion : % (0 = fully visible, 100 = blinds closed). -- direction : strip angle in degrees (0 = vertical strips). -- width : width of each strip, in px. -- offset : slides the strip pattern along its axis, in px. -- invert : 0/1 — swaps which bands disappear first. -- speed : px/s — slides the pattern automatically (0 = still; -- adds to the manual offset; keyframable, no jumps). -- -- Works stacked with other Path Effects and on shapes with holes. -- ============================================================ type VenezianBlinds = { completion: Input, direction: Input, width: Input, offset: Input, invert: Input, speed: Input, -- px/s, auto-slides along the `offset` axis _outPath: Path?, _animOffset: number, context: Context?, } -- Clips a polygon (plain Lua table of {x, y}) against the line s = c, where -- s(p) = p.x * cosT + p.y * sinT, via Sutherland-Hodgman. Keeps the points -- with s <= c (keepBelow) or s >= c (false). The cyclic vertex order is -- preserved, so the fragments INHERIT the winding of the source contour -- (never reverse it — forcing a winding turns fills invisible, silently). local function clipHalf(poly: { { x: number, y: number } }, cosT: number, sinT: number, c: number, keepBelow: boolean): { { x: number, y: number } } local result: { { x: number, y: number } } = {} local n = #poly if n == 0 then return result end for idx = 1, n do local v1 = poly[idx] local nxt = poly[(idx % n) + 1] -- wrap: last -> first local d1 = (v1.x * cosT) + (v1.y * sinT) - c local d2 = (nxt.x * cosT) + (nxt.y * sinT) - c -- inside = on the side we want to keep (s <= c or s >= c). local in1 = (keepBelow and (d1 <= 0)) or (not keepBelow and (d1 >= 0)) local in2 = (keepBelow and (d2 <= 0)) or (not keepBelow and (d2 >= 0)) if in2 then -- v2 inside: if entering (v1 outside), emit the intersection BEFORE v2. if not in1 then local t = d1 / (d1 - d2) table.insert(result, { x = v1.x + (nxt.x - v1.x) * t, y = v1.y + (nxt.y - v1.y) * t, }) end table.insert(result, { x = nxt.x, y = nxt.y }) elseif in1 then -- v2 outside, v1 inside: leaving, emit the intersection. local t = d1 / (d1 - d2) table.insert(result, { x = v1.x + (nxt.x - v1.x) * t, y = v1.y + (nxt.y - v1.y) * t, }) end end return result end function init(self: VenezianBlinds, context: Context): boolean self._outPath = Path.new() self.context = context self._animOffset = 0 print("[venetian-blinds-v3.1] init ok") return true end function advance(self: VenezianBlinds, seconds: number): boolean if self.speed ~= 0 then -- ACCUMULATED clock: never time*speed (jumps when speed is keyframed) self._animOffset = self._animOffset + (seconds * self.speed) local ctx = self.context if ctx then ctx:markNeedsUpdate() end end return true -- ALWAYS true end function update(self: VenezianBlinds, pathData: PathData, _node: NodeReadData): PathData | Path -- Single measure read (outside the pcall). local measure = pathData:measure() if self._outPath == nil then return pathData end -- completion in PERCENT (0..100, like AE); fraction internally. local completion = math.clamp(self.completion, 0, 100) / 100 -- Fast paths. if completion <= 0 then return pathData -- shape untouched, original geometry end if completion >= 1 then local empty: Path = self._outPath :: Path empty:reset() return empty -- fully hidden (nothing drawn) end if self.width <= 0 or measure.length <= 0 then return pathData -- invalid period or empty contour end -- All clipping inside pcall: on error, return the original geometry. local ok, out = pcall(function(): PathData | Path -- Step A: collect contours with a global point budget. local POINT_BUDGET = 700 local measures: { ContourMeasure } = {} local lens: { number } = {} local totalLen = 0.0 local c = pathData:contours() while c do local clen = c.length if clen > 0.5 then table.insert(measures, c) table.insert(lens, clen) totalLen = totalLen + clen end c = c.next end if #measures == 0 or totalLen <= 0 then -- no valid contour: legitimate when stacked (e.g. the effect -- above emptied the geometry) -> return original, NO error. return pathData end local theta = self.direction * (math.pi / 180) local cosT = math.cos(theta) local sinT = math.sin(theta) local vis = 1 - completion local offset = self.offset + self._animOffset local width = self.width local invert = self.invert local outPath: Path = self._outPath :: Path outPath:reset() -- Step B: CONTOUR BY CONTOUR — resample, measure ITS own s range -- and clip only against the bands inside that range. for ci = 1, #measures do local cm = measures[ci] local clen = lens[ci] local steps = math.floor(POINT_BUDGET * (clen / totalLen)) local dens = math.floor(clen / 2) if steps > dens then steps = dens end if steps < 8 then steps = 8 end local pts: { { x: number, y: number } } = {} local sMin = math.huge local sMax = -math.huge for i = 0, steps do local d = (i / steps) * clen local pos, _tan = cm:positionAndTangent(d) pts[i + 1] = { x = pos.x, y = pos.y } local s = (pos.x * cosT) + (pos.y * sinT) if s < sMin then sMin = s end if s > sMax then sMax = s end end -- Bands in GLOBAL coordinates (aligned across contours), but -- only the ones touching THIS contour. local kMin = math.floor((sMin - offset) / width) - 1 local kMax = math.ceil((sMax - offset) / width) + 1 for k = kMin, kMax do local base = offset + (k * width) local bandLo: number local bandHi: number if invert == 1 then bandLo = base + (width * vis) bandHi = base + width else bandLo = base bandHi = base + (width * vis) end local poly1 = clipHalf(pts, cosT, sinT, bandLo, false) -- s >= lo if #poly1 >= 3 then local poly2 = clipHalf(poly1, cosT, sinT, bandHi, true) -- s <= hi if #poly2 >= 3 then outPath:moveTo(Vector.xy(poly2[1].x, poly2[1].y)) for j = 2, #poly2 do outPath:lineTo(Vector.xy(poly2[j].x, poly2[j].y)) end outPath:close() end end end end return outPath end) if not ok then print("[venetian-blinds-v3.1] ERROR: " .. tostring(out)) return pathData end return out end return function(): PathEffect return { completion = 0, direction = 0, width = 30, offset = 0, invert = 0, speed = 0, _outPath = nil, _animOffset = 0, context = nil, init = init, advance = advance, update = update, } end -- ============================================================ -- @thebrokedesignerxx -- ============================================================