From 3fa33a4537487f911bff0833b686849e90f607c9 Mon Sep 17 00:00:00 2001 From: Ricardo Matias Date: Wed, 25 Mar 2020 11:36:58 +0100 Subject: [PATCH] I can use other layers as base --- orx-compositor/src/main/kotlin/Compositor.kt | 51 +++++++++++++++++--- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/orx-compositor/src/main/kotlin/Compositor.kt b/orx-compositor/src/main/kotlin/Compositor.kt index 6b79b847..83ace9e7 100644 --- a/orx-compositor/src/main/kotlin/Compositor.kt +++ b/orx-compositor/src/main/kotlin/Compositor.kt @@ -4,6 +4,7 @@ import org.openrndr.Extension import org.openrndr.Program import org.openrndr.color.ColorRGBa import org.openrndr.draw.* +import org.openrndr.extra.fx.blend.DestinationOut import org.openrndr.extra.fx.blend.SourceIn import org.openrndr.extra.fx.blend.SourceOut import org.openrndr.extra.parameters.BooleanParameter @@ -29,8 +30,9 @@ fun RenderTarget.deepDestroy() { */ @Description("Layer") open class Layer internal constructor() { + var copyLayers: List = listOf() var sourceOut = SourceOut() - var destinationIn = SourceIn() + var sourceIn = SourceIn() var maskLayer: Layer? = null var drawFunc: () -> Unit = {} val children: MutableList = mutableListOf() @@ -46,6 +48,11 @@ open class Layer internal constructor() { var clearColor: ColorRGBa? = ColorRGBa.TRANSPARENT private var layerTarget: RenderTarget? = null + val result: ColorBuffer? + get() { + return layerTarget?.colorBuffer(0) + } + /** * draw the layer */ @@ -67,6 +74,20 @@ open class Layer internal constructor() { } layerTarget?.let { target -> + if (copyLayers.isNotEmpty()) { + copyLayers.forEach { + drawer.isolatedWithTarget(target) { + clearColor?.let { + drawer.background(it) + } + + it.layerTarget?.let { copyTarget -> + drawer.image(copyTarget.colorBuffer(0)) + } + } + } + } + maskLayer?.let { if (it.shouldCreateLayerTarget(activeRenderTarget)) { it.createLayerTarget(activeRenderTarget, drawer) @@ -74,18 +95,23 @@ open class Layer internal constructor() { it.layerTarget?.let { maskRt -> drawer.isolatedWithTarget(maskRt) { - clearColor?.let { - drawer.background(it) + if (copyLayers.isEmpty()) { + clearColor?.let { color -> + drawer.background(color) + } } - + drawer.fill = ColorRGBa.WHITE + drawer.stroke = ColorRGBa.WHITE it.drawFunc() } } } drawer.isolatedWithTarget(target) { - clearColor?.let { - drawer.background(it) + if (copyLayers.isEmpty()) { + clearColor?.let { + drawer.background(it) + } } drawFunc() children.forEach { @@ -123,9 +149,9 @@ open class Layer internal constructor() { } maskLayer?.let { - val maskFilter = if (invertMask) sourceOut else destinationIn + val maskFilter = if (invertMask) sourceOut else sourceIn - maskFilter.apply(arrayOf(it.layerTarget!!.colorBuffer(0), layerPost), layerPost) + maskFilter.apply(arrayOf(layerPost, it.layerTarget!!.colorBuffer(0)), layerPost) } val localBlendFilter = blendFilter @@ -140,6 +166,8 @@ open class Layer internal constructor() { localBlendFilter.first.apply(localBlendFilter.second) localBlendFilter.first.apply(arrayOf(activeRenderTarget.colorBuffer(0), layerPost), activeRenderTarget.colorBuffer(0)) } + + accumulation?.copyTo(target.colorBuffer(0)) } } @@ -177,6 +205,13 @@ fun Layer.draw(function: () -> Unit) { drawFunc = function } +/** + * use the layer as a base + */ +fun Layer.use(vararg layer: Layer) { + copyLayers = layer.toList() +} + /** * the drawing acts as a mask on the layer */