Add ZoomBlur, EdgesWork and Sepia filters

This commit is contained in:
Ricardo Matias
2020-02-19 12:25:34 +01:00
parent 81ce0ad9b2
commit 48be2eff5f
12 changed files with 264 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
package org.openrndr.extra.fx
import org.openrndr.draw.ColorFormat
import org.openrndr.draw.ColorType
import org.openrndr.resourceUrl
import java.net.URL
@@ -8,4 +10,6 @@ internal class FilterTools
internal fun filterFragmentCode(resourceId: String): String {
val urlString = resourceUrl("gl3/$resourceId", FilterTools::class.java)
return URL(urlString).readText()
}
}
internal data class ColorBufferDescription(val width: Int, val height: Int, val contentScale: Double, val format: ColorFormat, val type: ColorType)

View File

@@ -2,6 +2,7 @@ package org.openrndr.extra.fx.blur
import org.openrndr.draw.*
import org.openrndr.extra.fx.filterFragmentCode
import org.openrndr.extra.fx.ColorBufferDescription
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.extra.parameters.IntParameter
@@ -14,9 +15,6 @@ import org.openrndr.math.Vector2
@Description("Approximate Gaussian blur")
class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
filterFragmentCode("blur/approximate-gaussian-blur.frag"))) {
data class ColorBufferDescription(val width: Int, val height: Int, val contentScale: Double, val format: ColorFormat, val type: ColorType)
/**
* blur sample window, default value is 5
*/
@@ -43,8 +41,6 @@ class ApproximateGaussianBlur : Filter(Shader.createFromCode(Filter.filterVertex
private var intermediateCache = mutableMapOf<ColorBufferDescription, ColorBuffer>()
init {
window = 5
spread = 1.0

View File

@@ -35,7 +35,7 @@ class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
@DoubleParameter("gain", 0.0, 4.0)
var gain: Double by parameters
private var intermediateCache = mutableMapOf<ApproximateGaussianBlur.ColorBufferDescription, ColorBuffer>()
private var intermediateCache = mutableMapOf<ColorBufferDescription, ColorBuffer>()
init {
window = 5
@@ -44,7 +44,7 @@ class BoxBlur : Filter(Shader.createFromCode(Filter.filterVertexCode,
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
val intermediateDescription = ApproximateGaussianBlur.ColorBufferDescription(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
val intermediateDescription = ColorBufferDescription(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
val intermediate = intermediateCache.getOrPut(intermediateDescription) {
colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
}

View File

@@ -0,0 +1,45 @@
package org.openrndr.extra.fx.blur
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.Filter
import org.openrndr.draw.Shader
import org.openrndr.draw.colorBuffer
import org.openrndr.extra.fx.filterFragmentCode
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
@Description("Zoom Blur")
class ZoomBlur : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("blur/zoom-blur.frag"))) {
var center: Vector2 by parameters
@DoubleParameter("strength", 0.0, 1.0)
var strength: Double by parameters
init {
center = Vector2.ONE / 2.0
strength = 0.2
}
private var intermediate: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
intermediate?.let {
if (it.width != target[0].width || it.height != target[0].height) {
intermediate = null
}
}
if (intermediate == null) {
intermediate = colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
}
intermediate?.let {
parameters["dimensions"] = Vector2(it.effectiveWidth.toDouble(), it.effectiveHeight.toDouble())
super.apply(source, arrayOf(it))
it.copyTo(target[0])
}
}
}

View File

@@ -0,0 +1,17 @@
package org.openrndr.extra.fx.color
import org.openrndr.draw.Filter
import org.openrndr.draw.Shader
import org.openrndr.extra.fx.filterFragmentCode
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
@Description("Sepia")
class Sepia : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("color/sepia.frag"))) {
@DoubleParameter("amount", 0.0, 1.0)
var amount: Double by parameters
init {
amount = 0.5
}
}

View File

@@ -0,0 +1,53 @@
package org.openrndr.extra.fx.edges
import org.openrndr.draw.*
import org.openrndr.extra.fx.filterFragmentCode
import org.openrndr.extra.fx.ColorBufferDescription
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.IntParameter
import org.openrndr.math.Vector2
internal class EdgesWork1 : Filter(Shader.createFromCode(filterVertexCode, filterFragmentCode("edges/edges-work-1.frag"))) {
var delta: Vector2 by parameters
init {
delta = Vector2.ZERO
}
}
@Description("Edges Work")
open class EdgesWork : Filter(Shader.createFromCode(filterVertexCode, filterFragmentCode("edges/edges-work-2.frag"))) {
/**
* radius, default value is 1.0
*/
@IntParameter("radius", 1, 400)
var radius: Int by parameters
private var delta: Vector2 by parameters
private val work1 = EdgesWork1()
private var intermediateCache = mutableMapOf<ColorBufferDescription, ColorBuffer>()
init {
radius = 1
delta = Vector2.ZERO
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
val intermediateDescription = ColorBufferDescription(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
val intermediate = intermediateCache.getOrPut(intermediateDescription) {
colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
}
intermediate.let {
work1.delta = Vector2(radius / it.effectiveWidth.toDouble(), 0.0)
work1.apply(source, arrayOf(it))
parameters["delta"] = Vector2(0.0, radius / it.effectiveHeight.toDouble())
super.apply(arrayOf(it), target)
}
}
}

View File

@@ -8,7 +8,7 @@ import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
@Description("Luma threshold ")
@Description("Luma Sobel")
class LumaSobel : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("edges/luma-sobel.frag"))) {
@ColorParameter("background color")