forked from LeenkxTeam/LNXSDK
Object Occlusion Culling
This commit is contained in:
79
leenkx/Sources/iron/culling/TemporalState.hx
Normal file
79
leenkx/Sources/iron/culling/TemporalState.hx
Normal file
@ -0,0 +1,79 @@
|
||||
package iron.culling;
|
||||
|
||||
class TemporalState {
|
||||
|
||||
public var visibleLastFrame: haxe.ds.IntMap<Bool>;
|
||||
public var visibleLastShadow: haxe.ds.IntMap<Bool>;
|
||||
public var frameLastTested: haxe.ds.IntMap<Int>;
|
||||
public var occlusionConfidence: haxe.ds.IntMap<Int>;
|
||||
public var capacity: Int = 0;
|
||||
public var frameCount: Int = 0;
|
||||
|
||||
public function new() {
|
||||
visibleLastFrame = new haxe.ds.IntMap<Bool>();
|
||||
visibleLastShadow = new haxe.ds.IntMap<Bool>();
|
||||
frameLastTested = new haxe.ds.IntMap<Int>();
|
||||
occlusionConfidence = new haxe.ds.IntMap<Int>();
|
||||
}
|
||||
|
||||
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<Bool>();
|
||||
visibleLastShadow = new haxe.ds.IntMap<Bool>();
|
||||
frameLastTested = new haxe.ds.IntMap<Int>();
|
||||
occlusionConfidence = new haxe.ds.IntMap<Int>();
|
||||
}
|
||||
|
||||
public inline function getConfidence(i: Int): Int {
|
||||
var c = occlusionConfidence.get(i);
|
||||
return c == null ? 0 : c;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user