From d19d51f8526d4abf1f954d305afdfda843b5423d Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Mon, 3 Aug 2020 21:59:53 +0200 Subject: [PATCH] [orx-jumpflood] Add modelViewMatrix, shader refactor --- orx-jumpflood/src/main/kotlin/ShapeSDF.kt | 9 ++++ .../main/resources/shaders/gl3/shape-sdf.frag | 42 +++++++++++-------- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/orx-jumpflood/src/main/kotlin/ShapeSDF.kt b/orx-jumpflood/src/main/kotlin/ShapeSDF.kt index aed9d4cc..9beb4dd2 100644 --- a/orx-jumpflood/src/main/kotlin/ShapeSDF.kt +++ b/orx-jumpflood/src/main/kotlin/ShapeSDF.kt @@ -2,6 +2,7 @@ package org.openrndr.extra.jumpfill import org.openrndr.draw.* import org.openrndr.extra.parameters.BooleanParameter +import org.openrndr.math.Matrix44 import org.openrndr.math.Vector4 import org.openrndr.resourceUrl import org.openrndr.shape.Shape @@ -19,10 +20,18 @@ class ShapeSDF : Filter(filterShaderFromUrl(resourceUrl("/shaders/gl3/shape-sdf. @BooleanParameter("rectify distance") var rectify: Boolean by parameters + private var modelViewMatrixInverse by parameters + + var modelViewMatrix = Matrix44.IDENTITY + set(value) { + modelViewMatrixInverse = modelViewMatrix.inversed + field = value + } init { useUV = false rectify = false + modelViewMatrix = Matrix44.IDENTITY } fun setShapes(shapes: List) { diff --git a/orx-jumpflood/src/main/resources/shaders/gl3/shape-sdf.frag b/orx-jumpflood/src/main/resources/shaders/gl3/shape-sdf.frag index 345713ac..5af4d7e4 100644 --- a/orx-jumpflood/src/main/resources/shaders/gl3/shape-sdf.frag +++ b/orx-jumpflood/src/main/resources/shaders/gl3/shape-sdf.frag @@ -7,6 +7,8 @@ out vec4 o_color; uniform bool useUV; uniform bool rectify; +uniform mat4 modelViewMatrixInverse; + uniform samplerBuffer toBuffer; uniform samplerBuffer fromBuffer; uniform int segmentCount; @@ -63,23 +65,7 @@ vec3 minimum_distance_and_perpendicular(vec4 v, vec4 w, vec2 p) { 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 = (fwidth(uv))*vec2(1280.0, 720.0); - } - } - - uv.y = 1.0 - uv.y; - uv *= targetSize; - +float shapeDistance(vec2 uv, out float perpDistOut, out float contourLengthOut ) { float mindist = 10E10; float perpdist = 0.0; float contourLength = 0.0; @@ -97,7 +83,29 @@ void main() { } } float signedDistance = mindist * (windingNr==0 ? 1.0 : -1.0); + contourLengthOut = contourLength; + perpDistOut = perpdist; + return signedDistance; +} +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 = (fwidth(uv))*vec2(1280.0, 720.0); + } + } + uv.y = 1.0 - uv.y; + uv *= targetSize; + uv = (modelViewMatrixInverse * vec4(uv, 0.0, 1.0)).xy; + + float perpdist; + float contourLength; + float signedDistance = shapeDistance(uv, perpdist, contourLength); o_color = vec4(signedDistance / length(fixDistance), perpdist/contourLength, contourLength, 1.0); } \ No newline at end of file