Add Layer.enabled and make it @BooleanParameter annotated

This commit is contained in:
Edwin Jakobs
2020-02-12 18:12:53 +01:00
parent 8a016fb97d
commit 9212d75033
2 changed files with 180 additions and 149 deletions

View File

@@ -0,0 +1,3 @@
dependencies {
compile project(":orx-parameters")
}

View File

@@ -1,150 +1,178 @@
package org.openrndr.extra.compositor package org.openrndr.extra.compositor
import org.openrndr.color.ColorRGBa import org.openrndr.Extension
import org.openrndr.draw.* import org.openrndr.Program
import org.openrndr.math.Matrix44 import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.parameters.BooleanParameter
private val postBufferCache = mutableListOf<ColorBuffer>() import org.openrndr.extra.parameters.Description
import org.openrndr.math.Matrix44
fun RenderTarget.deepDestroy() {
val cbcopy = colorBuffers.map { it}
val dbcopy = depthBuffer private val postBufferCache = mutableListOf<ColorBuffer>()
detachDepthBuffer()
detachColorBuffers() fun RenderTarget.deepDestroy() {
cbcopy.forEach { val cbcopy = colorBuffers.map { it}
it.destroy() val dbcopy = depthBuffer
} detachDepthBuffer()
dbcopy?.destroy() detachColorBuffers()
destroy() cbcopy.forEach {
} it.destroy()
}
/** dbcopy?.destroy()
* A single layer representation destroy()
*/ }
class Layer internal constructor() {
var drawFunc: () -> Unit = {} /**
val children: MutableList<Layer> = mutableListOf() * A single layer representation
var blendFilter: Pair<Filter, Filter.() -> Unit>? = null */
val postFilters: MutableList<Pair<Filter, Filter.() -> Unit>> = mutableListOf() @Description("Layer")
class Layer internal constructor() {
var clearColor: ColorRGBa? = ColorRGBa.TRANSPARENT var drawFunc: () -> Unit = {}
private var layerTarget:RenderTarget? = null val children: MutableList<Layer> = mutableListOf()
var blendFilter: Pair<Filter, Filter.() -> Unit>? = null
/** val postFilters: MutableList<Pair<Filter, Filter.() -> Unit>> = mutableListOf()
* draw the layer
*/ @BooleanParameter("enabled")
fun draw(drawer: Drawer) { var enabled = true
val rt = RenderTarget.active var clearColor: ColorRGBa? = ColorRGBa.TRANSPARENT
private var layerTarget:RenderTarget? = null
val llt = layerTarget
if (llt == null || (llt.width != rt.width || llt.height != rt.height)) { /**
layerTarget?.deepDestroy() * draw the layer
layerTarget = renderTarget(rt.width, rt.height) { */
colorBuffer() fun draw(drawer: Drawer) {
depthBuffer()
} if (!enabled) {
layerTarget?.let { return
drawer.withTarget(it) { }
drawer.background(ColorRGBa.TRANSPARENT)
} val rt = RenderTarget.active
}
} val llt = layerTarget
if (llt == null || (llt.width != rt.width || llt.height != rt.height)) {
layerTarget?.let { target -> layerTarget?.deepDestroy()
drawer.isolatedWithTarget(target) { layerTarget = renderTarget(rt.width, rt.height) {
clearColor?.let { colorBuffer()
drawer.background(it) depthBuffer()
} }
drawFunc() layerTarget?.let {
children.forEach { drawer.withTarget(it) {
it.draw(drawer) drawer.background(ColorRGBa.TRANSPARENT)
} }
} }
}
if (postFilters.size > 0) {
val sizeMismatch = if (postBufferCache.isNotEmpty()) { layerTarget?.let { target ->
postBufferCache[0].width != rt.width || postBufferCache[0].height != rt.height drawer.isolatedWithTarget(target) {
} else { clearColor?.let {
false drawer.background(it)
} }
drawFunc()
if (sizeMismatch) { children.forEach {
postBufferCache.forEach { it.destroy() } it.draw(drawer)
postBufferCache.clear() }
} }
if (postBufferCache.isEmpty()) { if (postFilters.size > 0) {
postBufferCache += colorBuffer(rt.width, rt.height).apply { val sizeMismatch = if (postBufferCache.isNotEmpty()) {
Session.active.untrack(this) postBufferCache[0].width != rt.width || postBufferCache[0].height != rt.height
} } else {
postBufferCache += colorBuffer(rt.width, rt.height).apply { false
Session.active.untrack(this) }
}
} if (sizeMismatch) {
} postBufferCache.forEach { it.destroy() }
postBufferCache.clear()
val layerPost = postFilters.let { filters -> }
val targets = postBufferCache
val result = filters.foldIndexed(target.colorBuffer(0)) { i, source, filter -> if (postBufferCache.isEmpty()) {
val target = targets[i % targets.size] postBufferCache += colorBuffer(rt.width, rt.height).apply {
filter.first.apply(filter.second) Session.active.untrack(this)
filter.first.apply(source, target) }
target postBufferCache += colorBuffer(rt.width, rt.height).apply {
} Session.active.untrack(this)
result }
} }
}
val lblend = blendFilter
if (lblend == null) { val layerPost = postFilters.let { filters ->
drawer.isolatedWithTarget(rt) { val targets = postBufferCache
//drawer.ortho(rt) val result = filters.foldIndexed(target.colorBuffer(0)) { i, source, filter ->
drawer.ortho() val target = targets[i % targets.size]
drawer.view = Matrix44.IDENTITY filter.first.apply(filter.second)
drawer.model = Matrix44.IDENTITY filter.first.apply(source, target)
drawer.image(layerPost, layerPost.bounds, drawer.bounds) target
} }
} else { result
lblend.first.apply(lblend.second) }
lblend.first.apply(arrayOf(rt.colorBuffer(0), layerPost), rt.colorBuffer(0))
} val lblend = blendFilter
} if (lblend == null) {
} drawer.isolatedWithTarget(rt) {
} //drawer.ortho(rt)
drawer.ortho()
/** drawer.view = Matrix44.IDENTITY
* create a layer within the composition drawer.model = Matrix44.IDENTITY
*/ drawer.image(layerPost, layerPost.bounds, drawer.bounds)
fun Layer.layer(function: Layer.() -> Unit) { }
children.add(Layer().apply { function() }) } else {
} lblend.first.apply(lblend.second)
lblend.first.apply(arrayOf(rt.colorBuffer(0), layerPost), rt.colorBuffer(0))
/** }
* set the draw contents of the layer }
*/ }
fun Layer.draw(function: () -> Unit) { }
drawFunc = function
} /**
* create a layer within the composition
/** */
* add a post-processing filter to the layer fun Layer.layer(function: Layer.() -> Unit) : Layer {
*/ val layer = Layer().apply { function() }
fun <F : Filter> Layer.post(filter: F, configure: F.() -> Unit = {}) { children.add(layer)
postFilters.add(Pair(filter as Filter, configure as Filter.() -> Unit)) return layer
} }
/** /**
* add a blend filter to the layer * set the draw contents of the layer
*/ */
fun <F : Filter> Layer.blend(filter: F, configure: F.() -> Unit = {}) { fun Layer.draw(function: () -> Unit) {
blendFilter = Pair(filter as Filter, configure as Filter.() -> Unit) drawFunc = function
} }
/** /**
* create a layered composition * add a post-processing filter to the layer
*/ */
fun compose(function: Layer.() -> Unit): Layer { fun <F : Filter> Layer.post(filter: F, configure: F.() -> Unit = {}) : F {
val root = Layer() postFilters.add(Pair(filter as Filter, configure as Filter.() -> Unit))
root.function() return filter
return root }
/**
* add a blend filter to the layer
*/
fun <F : Filter> Layer.blend(filter: F, configure: F.() -> Unit = {}) : F {
blendFilter = Pair(filter as Filter, configure as Filter.() -> Unit)
return filter
}
/**
* create a layered composition
*/
fun compose(function: Layer.() -> Unit): Layer {
val root = Layer()
root.function()
return root
}
class Compositor: Extension {
override var enabled: Boolean = true
var composite = Layer()
override fun afterDraw(drawer: Drawer, program: Program) {
drawer.isolated {
drawer.defaults()
composite.draw(drawer)
}
}
} }