Created
February 8, 2022 15:36
-
-
Save AvneeshSarwate/cabe46ff6f99c7f3b2a63675af2ea9b4 to your computer and use it in GitHub Desktop.
Feedback drawing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openrndr.application | |
import org.openrndr.color.ColorRGBa | |
import org.openrndr.draw.* | |
import org.openrndr.extra.olive.oliveProgram | |
import kotlin.math.cos | |
import kotlin.math.sin | |
fun main(args: Array<String>) { | |
application { | |
configure { | |
width = (1920 * 0.7).toInt() | |
height = (1080 * 0.7).toInt() | |
} | |
oliveProgram { | |
class FeedbackTarget(width: Int, height: Int) { | |
var backbuffer = renderTarget(width, height) { colorBuffer() } | |
var target = renderTarget(width, height) { colorBuffer() } | |
fun swapBuffers() { backbuffer = target.also { target = backbuffer } } | |
} | |
var fdbkTarget = FeedbackTarget(program.width, program.height) | |
var brushTarget = renderTarget(width, height) { colorBuffer() } | |
fun bindTrailShader(brush: ColorBuffer, backbuffer: ColorBuffer): ShadeStyle { | |
return shadeStyle { | |
fragmentTransform = """ | |
vec2 texCoord = c_boundsPosition.xy; | |
vec2 bbN = vec2(texCoord.x, 1.-texCoord.y); | |
vec4 bb = texture(p_backbuffer, bbN); | |
vec4 brush = texture(p_brush, texCoord); | |
float decay = 0.02; | |
float lastFdbk = bb.a; | |
float drawBrush = brush.r > 0 ? 1 : 0; | |
vec4 fdbkCol = lastFdbk > 0.1 ? vec4(bb.rgb, lastFdbk-decay) : vec4(0, 0, 0, 1); | |
vec4 outCol = mix(fdbkCol, brush, drawBrush); | |
x_fill = outCol; | |
""".trimIndent() | |
parameter("brush", brush) | |
parameter("backbuffer", backbuffer) | |
} | |
} | |
fun sinN(n: Double): Double = (sin(n)+1)/2 | |
fun cosN(n: Double): Double = (cos(n)+1)/2 | |
val numCircles = 1000 | |
extend { | |
fdbkTarget.swapBuffers() | |
var w = program.width | |
var h = program.height | |
var t = program.seconds | |
drawer.isolatedWithTarget(brushTarget) { | |
drawer.clear(ColorRGBa.BLACK) | |
drawer.fill = ColorRGBa.PINK | |
drawer.stroke = null | |
drawer.circles { | |
for (i in 1..numCircles) { | |
var speed = (1.0 + i/numCircles.toDouble())*0.025 | |
circle(sinN(t*speed+i)*w, cosN(t+i)*h, 10.0) | |
} | |
} | |
} | |
drawer.isolatedWithTarget(fdbkTarget.target) { | |
drawer.clear(ColorRGBa.BLACK) | |
// can change filtering via - rt.colorBuffer(0).filter( [...] ) | |
drawer.shadeStyle = bindTrailShader(brushTarget.colorBuffer(0), fdbkTarget.backbuffer.colorBuffer(0)) | |
drawer.rectangle(0.0, 0.0, program.width.toDouble(), program.height.toDouble()) | |
} | |
drawer.image(fdbkTarget.target.colorBuffer(0)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment