From d1ab80097a099c70918185b117d528e70c5b710a Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Wed, 18 Jan 2023 23:52:27 +0100 Subject: [PATCH] [orx-compositor] Clean up API, clear mask layer --- .../src/commonMain/kotlin/Compositor.kt | 33 +++++++------------ .../src/jvmDemo/kotlin/DemoAside01.kt | 3 -- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/orx-compositor/src/commonMain/kotlin/Compositor.kt b/orx-compositor/src/commonMain/kotlin/Compositor.kt index e50ea10a..ec79397f 100644 --- a/orx-compositor/src/commonMain/kotlin/Compositor.kt +++ b/orx-compositor/src/commonMain/kotlin/Compositor.kt @@ -8,8 +8,6 @@ import org.openrndr.extra.fx.blend.SourceIn import org.openrndr.extra.fx.blend.SourceOut import org.openrndr.extra.parameters.BooleanParameter import org.openrndr.extra.parameters.Description -import org.openrndr.math.Matrix44 - fun RenderTarget.deepDestroy() { val cbcopy = colorAttachments.map { it } @@ -34,16 +32,17 @@ enum class LayerType { ASIDE } +private val sourceOut = persistent { SourceOut() } +private val sourceIn = persistent { SourceIn() } + /** * A single layer representation */ @Description("Layer") open class Layer internal constructor( val type: LayerType, - val bufferMultisample: BufferMultisample = BufferMultisample.Disabled + private val bufferMultisample: BufferMultisample = BufferMultisample.Disabled ) { - var sourceOut = SourceOut() - var sourceIn = SourceIn() var maskLayer: Layer? = null var drawFunc: () -> Unit = {} val children: MutableList = mutableListOf() @@ -61,9 +60,9 @@ open class Layer internal constructor( var clearColor: ColorRGBa? = ColorRGBa.TRANSPARENT private var layerTarget: RenderTarget? = null - val result: ColorBuffer? + val result: ColorBuffer get() { - return layerTarget?.colorBuffer(0) + return layerTarget?.colorBuffer(0) ?: error("layer result not ready") } /** @@ -90,6 +89,7 @@ open class Layer internal constructor( drawer.isolatedWithTarget(maskRt) { drawer.fill = ColorRGBa.WHITE drawer.stroke = ColorRGBa.WHITE + drawer.clear(ColorRGBa.TRANSPARENT) it.drawFunc() } } @@ -118,7 +118,7 @@ open class Layer internal constructor( for ((i, filter) in filters.withIndex()) { filter.first.apply(filter.third) val sources = - arrayOf(localSource) + filter.second.map { it.result ?: error("no result for layer $it") } + arrayOf(localSource) + filter.second.map { it.result } .toTypedArray() filter.first.apply(sources, arrayOf(targets[i % targets.size])) localSource = targets[i % targets.size] @@ -134,7 +134,7 @@ open class Layer internal constructor( if (type == LayerType.ASIDE) { if (postFilters.isNotEmpty()) { require(layerPost != result) - layerPost.copyTo(result ?: error("no result")) + layerPost.copyTo(result) } } else if (type == LayerType.LAYER) { val localBlendFilter = blendFilter @@ -211,9 +211,7 @@ open class Layer internal constructor( fun Drawer.image(layer: Layer) { val cb = layer.result - if (cb != null) { - image(cb) - } + image(cb) } } @@ -249,8 +247,7 @@ fun Layer.apply(drawer: Drawer, val layer = Layer(LayerType.ASIDE) layer.colorType = colorType layer.draw { - drawer.image(source.result!!) - //source.result!!.copyTo(result!!) + drawer.image(source.result) } layer.post(filter, function) children.add(layer) @@ -264,8 +261,7 @@ fun Layer.apply(drawer: Drawer, val layer = Layer(LayerType.ASIDE) layer.colorType = colorType layer.draw { - //source0.result!!.copyTo(result!!) - drawer.image(source0.result!!) + drawer.image(source0.result) } layer.post(filter, source1, function) children.add(layer) @@ -273,7 +269,6 @@ fun Layer.apply(drawer: Drawer, } - /** * set the draw contents of the layer */ @@ -347,7 +342,6 @@ class ColorBufferCache(val width: Int, val height: Int) { it.value.forEach { cb -> cb.destroy() } } } - } class Composite : Layer(LayerType.LAYER) { @@ -373,9 +367,6 @@ fun compose(function: Layer.() -> Unit): Composite { return root } - - - class Compositor : Extension { override var enabled: Boolean = true var composite = Composite() diff --git a/orx-compositor/src/jvmDemo/kotlin/DemoAside01.kt b/orx-compositor/src/jvmDemo/kotlin/DemoAside01.kt index 426b92f6..2f7027b6 100644 --- a/orx-compositor/src/jvmDemo/kotlin/DemoAside01.kt +++ b/orx-compositor/src/jvmDemo/kotlin/DemoAside01.kt @@ -2,7 +2,6 @@ import org.openrndr.application import org.openrndr.color.ColorRGBa import org.openrndr.draw.ColorType import org.openrndr.extra.compositor.* -import org.openrndr.extra.fx.blur.DirectionalBlur import org.openrndr.extra.fx.blur.HashBlurDynamic import org.openrndr.extra.fx.patterns.Checkers import kotlin.math.cos @@ -32,6 +31,4 @@ fun main() { } } } - - } \ No newline at end of file