Skip to content

Instantly share code, notes, and snippets.

@ylegall
Last active August 15, 2020 23:25
Show Gist options
  • Save ylegall/00730624b368dbf490eeb2184a75d853 to your computer and use it in GitHub Desktop.
Save ylegall/00730624b368dbf490eeb2184a75d853 to your computer and use it in GitHub Desktop.
openRNDR demo for doing an inversion distortion effect
import org.openrndr.application
import org.openrndr.draw.isolatedWithTarget
import org.openrndr.draw.renderTarget
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.compositor.compose
import org.openrndr.extra.compositor.draw
import org.openrndr.ffmpeg.ScreenRecorder
// shader-based inversion distortion effect
// this demonstrates inversion (1/z) (https://mathworld.wolfram.com/ConformalMapping.html)
fun main() = application {
configure {
width = 920
height = 920
}
program {
val backgroundImage = renderTarget(width, height) { colorBuffer() }
extend {
// render checkerboard for example:
drawer.isolatedWithTarget(backgroundImage) {
shadeStyle = shadeStyle {
fragmentTransform = """
vec2 pos = c_boundsPosition.xy;
pos *= 10;
float value = mod(int(pos.x) + int(pos.y), 2);
x_fill.rgb = vec3(value);
"""
}
rectangle(bounds)
}
// this does the actual distortion effect
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
vec2 pos = c_boundsPosition.xy; // current pixel
vec2 origin = vec2(0.5, 0.5); // the center, which could vary with time
float dist = distance(pos, origin);
// play around with clamping the distance for different effect in the center
dist += 0.1;
// get the "direction vector" to the pixel:
vec2 dir = normalize(pos - origin);
// the position along the "boundary circle":
vec2 circlePosition = 0.1 * dir;
// perform inversion with respect to the circle:
vec2 newPos = circlePosition / dist;
// convert back to shader screen coordinates:
newPos += origin;
// mod the values back into the 0-1 range if necessary:
newPos = fract(newPos);
// set the final pixel color:
x_fill = texture(p_image, newPos);
""".trimIndent()
parameter("image", backgroundImage.colorBuffer(0))
}
// draw the final output
drawer.image(backgroundImage.colorBuffer(0))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment