diff --git a/orx-shade-styles/build.gradle.kts b/orx-shade-styles/build.gradle.kts index c54ba886..cf8b7b0a 100644 --- a/orx-shade-styles/build.gradle.kts +++ b/orx-shade-styles/build.gradle.kts @@ -35,6 +35,9 @@ kotlin { val jvmDemo by getting { dependencies { implementation(project(":orx-color")) + implementation(project(":orx-shade-styles")) + implementation(project(":orx-noise")) + implementation(project(":orx-shapes")) } } } diff --git a/orx-shade-styles/src/commonMain/kotlin/ImageFit.kt b/orx-shade-styles/src/commonMain/kotlin/ImageFit.kt new file mode 100644 index 00000000..17ea3d4d --- /dev/null +++ b/orx-shade-styles/src/commonMain/kotlin/ImageFit.kt @@ -0,0 +1,61 @@ +package org.openrndr.extra.shadestyles + +import org.openrndr.draw.ColorBuffer +import org.openrndr.draw.ShadeStyle +import org.openrndr.math.Vector2 + +class ImageFit : ShadeStyle() { + + var image: ColorBuffer by Parameter() + var flipV: Boolean by Parameter() + var position: Vector2 by Parameter() + + init { + position = Vector2.ZERO + fragmentTransform = """ + | vec2 uv = c_boundsPosition.xy; + | vec2 ts = textureSize(p_image, 0); + | float boundsAR = c_boundsSize.x / c_boundsSize.y; + | vec2 shift = (p_position + vec2(1.0, 1.0)) / 2.0; + | + | if (c_boundsSize.x > c_boundsSize.y) { + | uv.y -= shift.y; + | uv.y /= boundsAR; + | uv.y += shift.y; + | } else { + | uv.x -= shift.x; + | uv.x *= boundsAR; + | uv.x += shift.x; + | } + | float textureAR = ts.x / ts.y; + | if (ts.x > ts.y) { + | uv.x -= 0.5; + | uv.x /= textureAR; + | uv.x += 0.5; + | } else { + | uv.y -= 0.5; + | uv.y *= textureAR; + | uv.y += 0.5; + | } + | + | + | if (p_flipV) { + | uv.y = 1.0 - uv.y; + | } + | #ifndef OR_GL_TEXTURE2D + | vec4 img = texture(p_image, uv); + | #else + | vec4 img = texture2D(p_image, uv); + | #endif + | x_fill = img; + | """.trimMargin() + } +} + +fun imageFit(image: ColorBuffer, position: Vector2 = Vector2.ZERO) : ImageFit { + val im = ImageFit() + im.image = image + im.flipV = true + im.position = position + return im +} diff --git a/orx-shade-styles/src/demo/kotlin/DemoImageFit01.kt b/orx-shade-styles/src/demo/kotlin/DemoImageFit01.kt new file mode 100644 index 00000000..b988af22 --- /dev/null +++ b/orx-shade-styles/src/demo/kotlin/DemoImageFit01.kt @@ -0,0 +1,23 @@ +import org.openrndr.application +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.loadImage +import org.openrndr.extra.shadestyles.imageFit +import org.openrndr.extra.shadestyles.linearGradient +import org.openrndr.math.Vector2 +import org.openrndr.shape.Circle +import kotlin.math.cos +import kotlin.math.sin + +fun main() = application { + program { + val image = loadImage("demo-data/images/image-001.png") + extend { + drawer.shadeStyle = imageFit(image, Vector2(cos(seconds), sin(seconds))) + linearGradient(ColorRGBa.RED, ColorRGBa.BLUE) + drawer.circle(drawer.bounds.center, 200.0) + drawer.rectangle(10.0, 10.0, 400.0, 50.0) + drawer.rectangle(10.0, 10.0, 50.0, 400.0) + drawer.contour(Circle(width/2.0, height/2.0, 50.0).contour) + } + } +} +