[orx-shade-styles] Add ImageFit shade style

This commit is contained in:
Edwin Jakobs
2022-12-08 12:34:33 +01:00
parent 4c6ab148d9
commit 172c2254b7
3 changed files with 87 additions and 0 deletions

View File

@@ -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"))
}
}
}

View File

@@ -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
}

View File

@@ -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)
}
}
}