korge icon indicating copy to clipboard operation
korge copied to clipboard

Implement/show how to implement motion blur

Open soywiz opened this issue 3 years ago • 0 comments

class MyScene : Scene() {
	override suspend fun SContainer.sceneMain() {
        val view = circle(16.0, fill = Colors.RED).xy(100, 100)
        //val view = image(resourcesVfs["korge.png"].readBitmap().toBMP32().scaleLinear(0.1, 0.1)) { scale = 0.1 }
        //speed = 0.1
        speed = 1.0
        view.motionBlur(scale = 2.0, exp = 0.75)
        while (true) {
            //tween(circle::pos[buildVectorPath { circle(100, 100, 100) }], time = 2.seconds)
            tween(view::x[300], view::y[300], time = 1.seconds)
            tween(view::x[100], view::y[300], time = 1.seconds)
            tween(view::x[100], view::y[100], time = 1.seconds)
        }
    }
}

fun View.motionBlur(scale: Double = 5.0, exp: Double = 0.75, min: Double = 0.0, max: Double = 20.0): Cancellable {
    val view = this
    val filter = DirectionalBlurFilter()
    this.addFilter(filter)
    val now = Point()
    val last = Point()
    last.copyFrom(view.pos)
    val updater = addUpdater {
        val timeScale = 16.milliseconds / it
        now.copyFrom(view.pos)
        if (now != last) {
            filter.angle = Angle.between(last, now)
            filter.radius = (Point.distance(last, now) * scale * timeScale).pow(exp).clamp(min, max)
        } else {
            filter.radius = 0.0
        }
        last.copyFrom(now)
    }
    return Cancellable {
        this.removeFilter(filter)
        updater.cancel()
    }
}

soywiz avatar Sep 05 '22 18:09 soywiz