package iron.culling; class TemporalState { public var visibleLastFrame: haxe.ds.IntMap; public var visibleLastShadow: haxe.ds.IntMap; public var frameLastTested: haxe.ds.IntMap; public var occlusionConfidence: haxe.ds.IntMap; public var capacity: Int = 0; public var frameCount: Int = 0; public function new() { visibleLastFrame = new haxe.ds.IntMap(); visibleLastShadow = new haxe.ds.IntMap(); frameLastTested = new haxe.ds.IntMap(); occlusionConfidence = new haxe.ds.IntMap(); } public function ensureCapacity(cap: Int) { if (capacity < cap) capacity = cap; } public inline function wasVisibleLastFrame(i: Int): Bool { return visibleLastFrame.exists(i); } public inline function wasVisibleShadowLastFrame(i: Int): Bool { return visibleLastShadow.exists(i); } public inline function needsTest(i: Int, maxSkipFrames: Int): Bool { if (visibleLastFrame.exists(i)) return false; // assumed visible var lastTested = frameLastTested.get(i); if (lastTested == null) return true; return frameCount - lastTested >= maxSkipFrames; } public inline function needsVisibleReTest(i: Int, retestInterval: Int): Bool { var lastTested = frameLastTested.get(i); if (lastTested == null) return true; return frameCount - lastTested >= retestInterval; } public inline function setVisible(i: Int, visible: Bool) { if (visible) { visibleLastFrame.set(i, true); occlusionConfidence.remove(i); } else { visibleLastFrame.remove(i); var conf = occlusionConfidence.get(i); occlusionConfidence.set(i, conf == null ? 1 : Std.int(Math.min(255, conf + 1))); } } public inline function markTested(i: Int) { frameLastTested.set(i, frameCount); } public inline function setVisibleShadow(i: Int, visible: Bool) { if (visible) visibleLastShadow.set(i, true); else visibleLastShadow.remove(i); } public inline function advanceFrame() { frameCount++; } public function clear() { visibleLastFrame = new haxe.ds.IntMap(); visibleLastShadow = new haxe.ds.IntMap(); frameLastTested = new haxe.ds.IntMap(); occlusionConfidence = new haxe.ds.IntMap(); } public inline function getConfidence(i: Int): Int { var c = occlusionConfidence.get(i); return c == null ? 0 : c; } }