Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created August 25, 2020 17:00
Show Gist options
  • Save ylegall/c8b55e833bb37e43a975a590fd6ee24e to your computer and use it in GitHub Desktop.
Save ylegall/c8b55e833bb37e43a975a590fd6ee24e to your computer and use it in GitHub Desktop.
looping spiral animation in openRNDR
package org.ygl.openrndr.demos
import org.openrndr.application
import org.openrndr.color.ColorRGBa
import org.openrndr.color.rgb
import org.openrndr.draw.shadeStyle
import org.openrndr.extra.compositor.compose
import org.openrndr.extra.compositor.draw
import org.openrndr.extra.compositor.layer
import org.openrndr.ffmpeg.ScreenRecorder
import org.openrndr.math.Vector2
import org.openrndr.math.smootherstep
import org.openrndr.math.smoothstep
import org.openrndr.shape.contour
import kotlin.math.abs
import kotlin.math.pow
private const val WIDTH = 920
private const val HEIGHT = 920
private const val TOTAL_FRAMES = 360 * 4
private const val RECORDING = true
fun main() = application {
configure {
width = WIDTH
height = HEIGHT
}
program {
var time = 0.0
//val numPoints = 2000
val numTurns = 6
val bgColor = rgb("eacdc2")
//val bgColor = rgb("1a1423")
val fgColor = rgb("372549")
fun gain(x: Double, k: Double): Double {
return if (x < 0.5) {
val a = 0.5 * (2 * x).pow(k)
a
} else {
val a = 0.5 * (2 * (1 - x)).pow(k)
1 - a
}
}
val leftPoints = mutableListOf<Vector2>()
val rightPoints = mutableListOf<Vector2>()
fun computePoints() {
rightPoints.clear()
leftPoints.clear()
val dir = Vector2.UNIT_X
val step = 0.0005
var pct = 0.0
while (true) {
val p = numTurns * (pct - time)
//val t = smoothstep(0.30, 0.7, (2 * numTurns + p) % 1.0)
//val t = smootherstep(0.30, 0.7, (2 * numTurns + p) % 1.0)
val t = gain((2 * numTurns + p) % 1.0, 5.0)
val rotation = -360 * t
val pos = dir.rotate(rotation) * (width/1.2 * pct)
rightPoints.add(pos)
leftPoints.add(-pos)
if (abs(rotation) < 1 && pct >= 1.0) {
break
}
pct += step
}
}
val composite = compose {
draw {
drawer.shadeStyle = shadeStyle {
fragmentTransform = """
float dist = distance(c_boundsPosition.xy, vec2(0.5, 0.5));
vec3 red = vec3(0.72, 0.36, 0.41);
vec3 blue = vec3(0.07, 0.16, 0.90);
vec3 color = mix(blue, red, 1 - 2 * dist);
x_fill.rgb = mix(x_fill.rgb, color, 0.2);
"""
}
drawer.rectangle(drawer.bounds)
}
layer {
draw {
drawer.clear(bgColor)
val shape = contour {
for (p in leftPoints.reversed()) {
moveOrLineTo(p)
}
for (p in rightPoints) {
moveOrLineTo(p)
}
lineTo(600.0, 0.0)
lineTo(600.0, -600.0)
lineTo(-600.0, -600.0)
close()
}
drawer.fill = fgColor
drawer.stroke = null
drawer.contour(shape)
}
}
}
if (RECORDING) {
extend(ScreenRecorder()) {
outputFile = "video/SpiralLoop.mp4"
frameRate = 60
frameClock = true
}
}
extend {
time = ((frameCount - 1) % TOTAL_FRAMES) / TOTAL_FRAMES.toDouble()
computePoints()
drawer.translate(drawer.bounds.center)
composite.draw(drawer)
if (RECORDING && frameCount >= TOTAL_FRAMES) {
application.exit()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment