[orx-fx] Add fade to Contour and CannyEdgeDetector
This commit is contained in:
@@ -34,6 +34,8 @@ kotlin {
|
|||||||
implementation(project(":orx-color"))
|
implementation(project(":orx-color"))
|
||||||
implementation(project(":orx-fx"))
|
implementation(project(":orx-fx"))
|
||||||
implementation(project(":orx-noise"))
|
implementation(project(":orx-noise"))
|
||||||
|
implementation(project(":orx-shapes"))
|
||||||
|
implementation(project(":orx-image-fit"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,9 @@ class CannyEdgeDetector : Filter1to1(
|
|||||||
var backgroundOpacity: Double by parameters
|
var backgroundOpacity: Double by parameters
|
||||||
|
|
||||||
|
|
||||||
|
@DoubleParameter("fade", 0.0, 1.0, order = 7)
|
||||||
|
var fade: Double by parameters
|
||||||
|
|
||||||
init {
|
init {
|
||||||
threshold0 = 2.0
|
threshold0 = 2.0
|
||||||
threshold1 = 0.0
|
threshold1 = 0.0
|
||||||
@@ -45,6 +48,7 @@ class CannyEdgeDetector : Filter1to1(
|
|||||||
backgroundColor = ColorRGBa.BLACK
|
backgroundColor = ColorRGBa.BLACK
|
||||||
backgroundOpacity = 1.0
|
backgroundOpacity = 1.0
|
||||||
foregroundOpacity = 1.0
|
foregroundOpacity = 1.0
|
||||||
|
fade = 1.0
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,10 +6,7 @@ import org.openrndr.color.ColorRGBa
|
|||||||
import org.openrndr.draw.Filter1to1
|
import org.openrndr.draw.Filter1to1
|
||||||
import org.openrndr.extra.fx.fx_contour
|
import org.openrndr.extra.fx.fx_contour
|
||||||
import org.openrndr.extra.fx.mppFilterShader
|
import org.openrndr.extra.fx.mppFilterShader
|
||||||
import org.openrndr.extra.parameters.ColorParameter
|
import org.openrndr.extra.parameters.*
|
||||||
import org.openrndr.extra.parameters.Description
|
|
||||||
import org.openrndr.extra.parameters.DoubleParameter
|
|
||||||
import org.openrndr.extra.parameters.IntParameter
|
|
||||||
|
|
||||||
@Description("Contour")
|
@Description("Contour")
|
||||||
class Contour : Filter1to1(mppFilterShader(fx_contour, "contour")) {
|
class Contour : Filter1to1(mppFilterShader(fx_contour, "contour")) {
|
||||||
@@ -28,13 +25,18 @@ class Contour : Filter1to1(mppFilterShader(fx_contour, "contour")) {
|
|||||||
@DoubleParameter("bias", -1.0, 1.0)
|
@DoubleParameter("bias", -1.0, 1.0)
|
||||||
var bias: Double by parameters
|
var bias: Double by parameters
|
||||||
|
|
||||||
|
|
||||||
@ColorParameter("contour color")
|
@ColorParameter("contour color")
|
||||||
var contourColor: ColorRGBa by parameters
|
var contourColor: ColorRGBa by parameters
|
||||||
|
|
||||||
@IntParameter("window", 0, 10)
|
@IntParameter("window", 0, 10)
|
||||||
var window: Int by parameters
|
var window: Int by parameters
|
||||||
|
|
||||||
|
@BooleanParameter("output bands", order = 100)
|
||||||
|
var outputBands: Boolean by parameters
|
||||||
|
|
||||||
|
@DoubleParameter("fade", 0.0, 1.0, order = 200)
|
||||||
|
var fade: Double by parameters
|
||||||
|
|
||||||
init {
|
init {
|
||||||
levels = 6.0
|
levels = 6.0
|
||||||
contourWidth = 0.4
|
contourWidth = 0.4
|
||||||
@@ -43,5 +45,7 @@ class Contour : Filter1to1(mppFilterShader(fx_contour, "contour")) {
|
|||||||
contourOpacity = 1.0
|
contourOpacity = 1.0
|
||||||
window = 1
|
window = 1
|
||||||
bias = 0.0
|
bias = 0.0
|
||||||
|
outputBands = false
|
||||||
|
fade = 1.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
56
orx-fx/src/jvmDemo/kotlin/DemoContour01.kt
Normal file
56
orx-fx/src/jvmDemo/kotlin/DemoContour01.kt
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/**
|
||||||
|
* Demonstrate the Contour filter
|
||||||
|
* @author Edwin Jakobs
|
||||||
|
*/
|
||||||
|
|
||||||
|
import org.openrndr.application
|
||||||
|
import org.openrndr.color.ColorRGBa
|
||||||
|
import org.openrndr.draw.createEquivalent
|
||||||
|
import org.openrndr.draw.loadImage
|
||||||
|
import org.openrndr.extra.fx.edges.Contour
|
||||||
|
import org.openrndr.extra.imageFit.imageFit
|
||||||
|
import org.openrndr.extra.shapes.primitives.grid
|
||||||
|
|
||||||
|
fun main() = application {
|
||||||
|
configure {
|
||||||
|
width = 720
|
||||||
|
height = 720
|
||||||
|
}
|
||||||
|
program {
|
||||||
|
val image = loadImage("demo-data/images/image-001.png")
|
||||||
|
val contour = Contour()
|
||||||
|
contour.levels = 4.0
|
||||||
|
contour.window = 1
|
||||||
|
contour.outputBands = true
|
||||||
|
contour.contourColor = ColorRGBa.PINK
|
||||||
|
contour.backgroundOpacity = 0.0
|
||||||
|
|
||||||
|
val edges = image.createEquivalent()
|
||||||
|
extend {
|
||||||
|
val cells = drawer.bounds.grid(2, 2).flatten()
|
||||||
|
val actions = listOf(
|
||||||
|
{
|
||||||
|
contour.outputBands = true
|
||||||
|
contour.levels = 2.0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contour.outputBands = false
|
||||||
|
contour.levels = 2.0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contour.outputBands = false
|
||||||
|
contour.levels = 8.0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
contour.outputBands = true
|
||||||
|
contour.levels = 8.0
|
||||||
|
},
|
||||||
|
)
|
||||||
|
for ((cell, action) in cells zip actions) {
|
||||||
|
action()
|
||||||
|
contour.apply(image, edges)
|
||||||
|
drawer.imageFit(edges, cell)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,6 +15,8 @@ uniform vec4 foregroundColor;
|
|||||||
uniform float backgroundOpacity;
|
uniform float backgroundOpacity;
|
||||||
uniform float foregroundOpacity;
|
uniform float foregroundOpacity;
|
||||||
|
|
||||||
|
uniform float fade;
|
||||||
|
|
||||||
vec2 iResolution;
|
vec2 iResolution;
|
||||||
|
|
||||||
float getAve(vec2 uv) {
|
float getAve(vec2 uv) {
|
||||||
@@ -104,7 +106,11 @@ float cannyEdge(vec2 fragCoord, float mn, float mx){
|
|||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
iResolution = vec2(textureSize(tex0, 0));
|
iResolution = vec2(textureSize(tex0, 0));
|
||||||
|
vec4 original = texture(tex0, v_texCoord0);
|
||||||
vec2 fragCoord = v_texCoord0 * iResolution;
|
vec2 fragCoord = v_texCoord0 * iResolution;
|
||||||
float edge = cannyEdge(fragCoord, threshold0, threshold1);
|
float edge = cannyEdge(fragCoord, threshold0, threshold1);
|
||||||
o_output = mix(foregroundColor * foregroundOpacity, backgroundColor * backgroundOpacity, 1.-edge);
|
o_output = mix(original,
|
||||||
|
mix(foregroundColor * foregroundOpacity,
|
||||||
|
backgroundColor * backgroundOpacity, 1. - edge),
|
||||||
|
fade);
|
||||||
}
|
}
|
||||||
@@ -8,27 +8,44 @@ uniform vec4 contourColor;
|
|||||||
uniform float backgroundOpacity;
|
uniform float backgroundOpacity;
|
||||||
uniform int window;
|
uniform int window;
|
||||||
uniform float bias;
|
uniform float bias;
|
||||||
|
uniform bool outputBands;
|
||||||
|
uniform float fade;
|
||||||
|
|
||||||
float calc_contour(vec2 uv) {
|
vec2 calc_contour(vec2 uv) {
|
||||||
vec4 box = texture(tex0, uv);
|
vec4 box = texture(tex0, uv);
|
||||||
float v = sin(3.1415926535 * levels * (dot(vec3(1.0 / 3.0), box.xyz) + bias));
|
float v = sin(3.1415926535 * levels * (dot(vec3(1.0 / 3.0), box.xyz) + bias));
|
||||||
float level = floor((dot(vec3(1.0 / 3.0), box.xyz) + bias) * levels) / levels;
|
float level = floor((dot(vec3(1.0 / 3.0), box.xyz) + bias) * levels);
|
||||||
float contour = 1.0 - smoothstep(0., contourWidth, 0.5 * abs(v) / fwidth(v));
|
float contour = 1.0 - smoothstep(0., contourWidth, 0.5 * abs(v) / fwidth(v));
|
||||||
return contour;
|
return vec2(contour, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 step = 1.0 / vec2(textureSize(tex0, 0));
|
vec2 step = 1.0 / vec2(textureSize(tex0, 0));
|
||||||
float contour = 0.0;
|
float contour = 0.0;
|
||||||
float weight = 0.0;
|
float weight = 0.0;
|
||||||
|
float level = 0.0;
|
||||||
|
|
||||||
for (int i = -window; i <= window; ++i) {
|
for (int i = -window; i <= window; ++i) {
|
||||||
for (int j = -window; j <= window; ++j) {
|
for (int j = -window; j <= window; ++j) {
|
||||||
contour += calc_contour(v_texCoord0 + step / (float(window) + 1.0) * vec2(float(i), float(j)));
|
vec2 c = calc_contour(v_texCoord0 + step / (float(window) + 1.0) * vec2(float(i), float(j)));
|
||||||
|
contour += c.x;
|
||||||
|
level += c.y;
|
||||||
weight += 1.0;
|
weight += 1.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
contour /= weight;
|
contour /= weight;
|
||||||
|
|
||||||
vec4 t = texture(tex0, v_texCoord0);
|
vec4 t = texture(tex0, v_texCoord0);
|
||||||
o_output = t * backgroundOpacity * (1.0 - contour) + contour * contourColor * contourOpacity * t.a;
|
|
||||||
|
if (outputBands) {
|
||||||
|
level /= weight;
|
||||||
|
|
||||||
|
level = 1.0 - max(0.0, fract(level / 2.0) * 2.0);
|
||||||
|
contour = level;
|
||||||
|
}
|
||||||
|
|
||||||
|
o_output = mix(t,
|
||||||
|
t * backgroundOpacity * (1.0 - contour) + contour * contourColor * contourOpacity * clamp(t.a, 0.0, 1.0),
|
||||||
|
fade);
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user