Add screenshot generation
This commit is contained in:
78
orx-jumpflood/src/main/kotlin/ShapeSDF.kt
Normal file
78
orx-jumpflood/src/main/kotlin/ShapeSDF.kt
Normal file
@@ -0,0 +1,78 @@
|
||||
package org.openrndr.extra.jumpfill
|
||||
|
||||
import org.openrndr.draw.*
|
||||
import org.openrndr.extra.parameters.BooleanParameter
|
||||
import org.openrndr.math.Vector4
|
||||
import org.openrndr.resourceUrl
|
||||
import org.openrndr.shape.Shape
|
||||
import org.openrndr.shape.ShapeContour
|
||||
|
||||
|
||||
class ShapeSDF : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/shape-sdf.frag"))) {
|
||||
private val fromBuffer = bufferTexture(1024, format = ColorFormat.RGBa, type = ColorType.FLOAT32)
|
||||
private val toBuffer = bufferTexture(1024, format = ColorFormat.RGBa, type = ColorType.FLOAT32)
|
||||
private var segmentCount = 0
|
||||
|
||||
@BooleanParameter("use UV map")
|
||||
var useUV:Boolean by parameters
|
||||
|
||||
@BooleanParameter("rectify distance")
|
||||
var rectify:Boolean by parameters
|
||||
|
||||
|
||||
init {
|
||||
useUV = false
|
||||
rectify = false
|
||||
}
|
||||
|
||||
fun setShapes(shapes: List<Shape>) {
|
||||
setContours(shapes.flatMap { it.contours })
|
||||
}
|
||||
|
||||
fun setContours(contours: List<ShapeContour>) {
|
||||
val from = mutableListOf<Vector4>()
|
||||
val to = mutableListOf<Vector4>()
|
||||
|
||||
for (contour in contours) {
|
||||
val lin = contour.sampleLinear()
|
||||
var contourLength = 0.0
|
||||
for (segment in lin.segments) {
|
||||
contourLength += segment.length
|
||||
}
|
||||
var offset = 0.0
|
||||
for (segment in lin.segments) {
|
||||
from.add(Vector4(segment.start.x, segment.start.y, offset, contourLength))
|
||||
offset += segment.length
|
||||
to.add(Vector4(segment.end.x, segment.end.y, offset, contourLength))
|
||||
}
|
||||
}
|
||||
|
||||
val fromShadow = fromBuffer.shadow
|
||||
val fromWriter = fromShadow.writer()
|
||||
fromWriter.rewind()
|
||||
for (v in from) {
|
||||
fromWriter.write(v)
|
||||
}
|
||||
fromShadow.upload(0, from.size*4*4)
|
||||
|
||||
val toShadow = toBuffer.shadow
|
||||
val toWriter = toShadow.writer()
|
||||
toWriter.rewind()
|
||||
for (v in to) {
|
||||
toWriter.write(v)
|
||||
}
|
||||
toShadow.upload(0, to.size*4*4)
|
||||
|
||||
segmentCount = from.size
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
|
||||
"needs a floating point target"
|
||||
}
|
||||
parameters["fromBuffer"] = fromBuffer
|
||||
parameters["toBuffer"] = toBuffer
|
||||
parameters["segmentCount"] = segmentCount
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
41
orx-jumpflood/src/main/kotlin/draw/SDFDraw.kt
Normal file
41
orx-jumpflood/src/main/kotlin/draw/SDFDraw.kt
Normal file
@@ -0,0 +1,41 @@
|
||||
package org.openrndr.extra.jumpfill.draw
|
||||
|
||||
import org.openrndr.color.ColorRGBa
|
||||
import org.openrndr.draw.ColorBuffer
|
||||
import org.openrndr.draw.Filter
|
||||
import org.openrndr.draw.filterShaderFromUrl
|
||||
import org.openrndr.extra.parameters.ColorParameter
|
||||
import org.openrndr.extra.parameters.Description
|
||||
import org.openrndr.extra.parameters.DoubleParameter
|
||||
import org.openrndr.resourceUrl
|
||||
|
||||
@Description("SDF stroke and fill")
|
||||
class SDFStrokeFill : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/draw/sdf-stroke-fill.frag"))) {
|
||||
@DoubleParameter("stroke weight", 0.0, 20.0, order = 0)
|
||||
var strokeWeight: Double by parameters
|
||||
|
||||
@DoubleParameter("stroke feather", 0.0, 20.0, order = 0)
|
||||
var strokeFeather: Double by parameters
|
||||
|
||||
@ColorParameter("stroke color", order = 1)
|
||||
var strokeColor: ColorRGBa by parameters
|
||||
|
||||
@DoubleParameter("fill feather", 0.0, 20.0, order = 0)
|
||||
var fillFeather: Double by parameters
|
||||
|
||||
|
||||
@ColorParameter("fill color", order = 2)
|
||||
var fillColor: ColorRGBa by parameters
|
||||
init {
|
||||
fillFeather = 1.0
|
||||
strokeFeather = 1.0
|
||||
strokeWeight = 1.0
|
||||
strokeColor = ColorRGBa.BLACK
|
||||
fillColor = ColorRGBa.WHITE
|
||||
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
86
orx-jumpflood/src/main/kotlin/ops/SDFOps.kt
Normal file
86
orx-jumpflood/src/main/kotlin/ops/SDFOps.kt
Normal file
@@ -0,0 +1,86 @@
|
||||
package org.openrndr.extra.jumpfill.ops
|
||||
|
||||
import org.openrndr.draw.ColorBuffer
|
||||
import org.openrndr.draw.ColorType
|
||||
import org.openrndr.draw.Filter
|
||||
import org.openrndr.draw.filterShaderFromUrl
|
||||
import org.openrndr.extra.parameters.Description
|
||||
import org.openrndr.extra.parameters.DoubleParameter
|
||||
import org.openrndr.resourceUrl
|
||||
|
||||
class SDFSmoothUnion : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/ops/sdf-smooth-union.frag"))) {
|
||||
var radius: Double by parameters
|
||||
|
||||
init {
|
||||
radius = 0.0
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
|
||||
"needs a floating point target"
|
||||
}
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
|
||||
class SDFSmoothIntersection : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/ops/sdf-smooth-intersection.frag"))) {
|
||||
var radius: Double by parameters
|
||||
|
||||
init {
|
||||
radius = 0.0
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
|
||||
"needs a floating point target"
|
||||
}
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
@Description("SDF smooth difference")
|
||||
class SDFSmoothDifference : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/ops/sdf-smooth-difference.frag"))) {
|
||||
@DoubleParameter("smooth radius", 0.0, 200.0, order = 0)
|
||||
var radius: Double by parameters
|
||||
|
||||
init {
|
||||
radius = 0.0
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
|
||||
"needs a floating point target"
|
||||
}
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
|
||||
class SDFRound : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/ops/sdf-round.frag"))) {
|
||||
@DoubleParameter("rounding radius", 0.0, 200.0, order = 0)
|
||||
var radius: Double by parameters
|
||||
|
||||
init {
|
||||
radius = 0.0
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
|
||||
"needs a floating point target"
|
||||
}
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
|
||||
class SDFOnion : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/ops/sdf-onion.frag"))) {
|
||||
var radius: Double by parameters
|
||||
|
||||
init {
|
||||
radius = 0.0
|
||||
}
|
||||
|
||||
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
|
||||
"needs a floating point target"
|
||||
}
|
||||
super.apply(source, target)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D tex0;// signed distance
|
||||
uniform float radius;
|
||||
|
||||
uniform vec4 strokeColor;
|
||||
uniform float strokeWeight;
|
||||
uniform float strokeFeather;
|
||||
|
||||
uniform float fillFeather;
|
||||
uniform vec4 fillColor;
|
||||
|
||||
in vec2 v_texCoord0;
|
||||
out vec4 o_color;
|
||||
|
||||
void main() {
|
||||
float d = texture(tex0, v_texCoord0).r;
|
||||
float strokeFactor = smoothstep(strokeWeight + strokeFeather, strokeWeight, abs(d));
|
||||
float fillFactor = smoothstep(0.0, fillFeather, -d);
|
||||
|
||||
vec4 fc = (fillColor * fillColor.a) * fillFactor;
|
||||
fc = fc * (1.0 - strokeFactor) + strokeFactor * (strokeColor * strokeColor.a);
|
||||
o_color = fc;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D tex0;// signed distance
|
||||
uniform float radius;
|
||||
|
||||
in vec2 v_texCoord0;
|
||||
out vec4 o_color;
|
||||
|
||||
void main() {
|
||||
float d0 = texture(tex0, v_texCoord0).r;
|
||||
o_color = vec4(abs(d0)- radius, 0.0, 0.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D tex0; // signed distance
|
||||
uniform float radius;
|
||||
|
||||
in vec2 v_texCoord0;
|
||||
out vec4 o_color;
|
||||
|
||||
void main() {
|
||||
float d0 = texture(tex0, v_texCoord0).r - radius;
|
||||
o_color = vec4(d0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D tex0;// signed distance
|
||||
uniform sampler2D tex1;// signed distance
|
||||
uniform float radius;
|
||||
|
||||
in vec2 v_texCoord0;
|
||||
out vec4 o_color;
|
||||
|
||||
float opSmoothDifference( float d1, float d2, float k ) {
|
||||
float h = clamp( 0.5 - 0.5*(d2+d1)/k, 0.0, 1.0 );
|
||||
return mix( d2, -d1, h ) + k*h*(1.0-h); }
|
||||
|
||||
|
||||
void main() {
|
||||
float d0 = texture(tex0, v_texCoord0).r;
|
||||
float d1 = texture(tex1, v_texCoord0).r;
|
||||
o_color = vec4(opSmoothDifference(d0, d1, radius), 0.0, 0.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D tex0;// signed distance
|
||||
uniform sampler2D tex1;// signed distance
|
||||
uniform float radius;
|
||||
|
||||
in vec2 v_texCoord0;
|
||||
out vec4 o_color;
|
||||
|
||||
float opSmoothIntersection(float d1, float d2, float k) {
|
||||
float h = clamp(0.5 - 0.5*(d2-d1)/k, 0.0, 1.0);
|
||||
return mix(d2, d1, h) + k*h*(1.0-h); }
|
||||
|
||||
|
||||
void main() {
|
||||
float d0 = texture(tex0, v_texCoord0).r;
|
||||
float d1 = texture(tex1, v_texCoord0).r;
|
||||
o_color = vec4(opSmoothIntersection(d0, d1, radius), 0.0, 0.0, 1.0);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#version 330 core
|
||||
|
||||
uniform sampler2D tex0; // signed distance
|
||||
uniform sampler2D tex1; // signed distance
|
||||
uniform float radius;
|
||||
|
||||
in vec2 v_texCoord0;
|
||||
out vec4 o_color;
|
||||
|
||||
float opSmoothUnion(float d1, float d2, float k) {
|
||||
float h = clamp(0.5 + 0.5*(d2-d1)/k, 0.0, 1.0);
|
||||
return mix(d2, d1, h) - k*h*(1.0-h);
|
||||
}
|
||||
|
||||
void main() {
|
||||
float d0 = texture(tex0, v_texCoord0).r;
|
||||
float d1 = texture(tex1, v_texCoord0).r;
|
||||
o_color = vec4(opSmoothUnion(d0, d1, radius), 0.0, 0.0, 1.0);
|
||||
}
|
||||
103
orx-jumpflood/src/main/resources/shaders/gl3/shape-sdf.frag
Normal file
103
orx-jumpflood/src/main/resources/shaders/gl3/shape-sdf.frag
Normal file
@@ -0,0 +1,103 @@
|
||||
#version 330
|
||||
#extension GL_ARB_derivative_control : enable
|
||||
in vec2 v_texCoord0;
|
||||
uniform float iTime;
|
||||
out vec4 o_color;
|
||||
|
||||
uniform bool useUV;
|
||||
uniform bool rectify;
|
||||
|
||||
uniform samplerBuffer toBuffer;
|
||||
uniform samplerBuffer fromBuffer;
|
||||
uniform int segmentCount;
|
||||
uniform vec2 targetSize;
|
||||
|
||||
uniform sampler2D tex0; // uv-map
|
||||
|
||||
float isLeft( vec2 P0, vec2 P1, vec2 P2 ) {
|
||||
return ( (P1.x - P0.x) * (P2.y - P0.y)
|
||||
- (P2.x - P0.x) * (P1.y - P0.y) );
|
||||
}
|
||||
|
||||
float length_squared( vec2 v, vec2 w ) {
|
||||
return dot(w-v, w-v);
|
||||
}
|
||||
|
||||
int winding_number( vec2 v, vec2 w, vec2 p ) {
|
||||
if (v.y <= p.y) { // start y <= P.y
|
||||
if (w.y > p.y) // an upward crossing
|
||||
if (isLeft( v, w, p) > 0.0) // P left of edge
|
||||
return 1; // ++wn; // have a valid up intersect
|
||||
}
|
||||
else { // start y > P.y (no test needed)
|
||||
if (w.y <= p.y) // a downward crossing
|
||||
if (isLeft( v,w,p) < 0.0) // P right of edge
|
||||
return -1; //--wn; // have a valid down intersect
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
float minimum_distance(vec2 v, vec2 w, vec2 p) {
|
||||
// Return minimum distance between line segment vw and point p
|
||||
float l2 = length_squared(v, w); // i.e. |w-v|^2 - avoid a sqrt
|
||||
if (l2 == 0.0) return distance(p, v); // v == w case
|
||||
// Consider the line extending the segment, parameterized as v + t (w - v).
|
||||
// We find projection of point p onto the line.
|
||||
// It falls where t = [(p-v) . (w-v)] / |w-v|^2
|
||||
// We clamp t from [0,1] to handle points outside the segment vw.
|
||||
float t = max(0.0, min(1.0, dot(p - v, w - v) / l2));
|
||||
vec2 projection = v + t * (w - v); // Projection falls on the segment
|
||||
return distance(p, projection);
|
||||
}
|
||||
|
||||
vec3 minimum_distance_and_perpendicular(vec4 v, vec4 w, vec2 p) {
|
||||
// Return minimum distance between line segment vw and point p
|
||||
float l2 = length_squared(v.xy, w.xy); // i.e. |w-v|^2 - avoid a sqrt
|
||||
if (l2 == 0.0) return vec3(distance(p, v.xy), v.z, v.w); // v == w case
|
||||
// Consider the line extending the segment, parameterized as v + t (w - v).
|
||||
// We find projection of point p onto the line.
|
||||
// It falls where t = [(p-v) . (w-v)] / |w-v|^2
|
||||
// We clamp t from [0,1] to handle points outside the segment vw.
|
||||
float t = max(0.0, min(1.0, dot(p - v.xy, w.xy - v.xy) / l2));
|
||||
vec3 projection = v.xyz + t * (w.xyz - v.xyz); // Projection falls on the segment
|
||||
return vec3(distance(p.xy, projection.xy), projection.z, v.w);
|
||||
}
|
||||
|
||||
|
||||
void main() {
|
||||
vec2 uv = v_texCoord0;
|
||||
|
||||
vec2 fixDistance = vec2(1.0);
|
||||
|
||||
if (useUV) {
|
||||
vec2 o = 0.5 / textureSize(tex0, 0);
|
||||
uv = texture(tex0, v_texCoord0 + o).xy;
|
||||
if (rectify) {
|
||||
fixDistance = (fwidthFine(uv))*vec2(1280.0, 720.0);
|
||||
}
|
||||
}
|
||||
|
||||
uv.y = 1.0 - uv.y;
|
||||
uv *= targetSize;
|
||||
|
||||
float mindist = 10E10;
|
||||
float perpdist = 0.0;
|
||||
float contourLength = 0.0;
|
||||
int windingNr = 0;
|
||||
for (int i = 0; i < segmentCount; i++) {
|
||||
vec4 from = texelFetch(fromBuffer, i);
|
||||
vec4 to = texelFetch(toBuffer, i);
|
||||
vec3 distline_and_perp = minimum_distance_and_perpendicular(from, to, uv.xy);
|
||||
windingNr += winding_number( from.xy, to.xy, uv.xy );
|
||||
float distline = distline_and_perp.x;
|
||||
if (abs(distline) <= mindist) {
|
||||
mindist = distline;
|
||||
perpdist = distline_and_perp.y;
|
||||
contourLength = distline_and_perp.z;
|
||||
}
|
||||
}
|
||||
float signedDistance = mindist * (windingNr==0 ? 1.0 : -1.0);
|
||||
|
||||
|
||||
o_color = vec4(signedDistance / length(fixDistance), perpdist/contourLength, contourLength, 1.0);
|
||||
}
|
||||
Reference in New Issue
Block a user