From 557d3bb3ad3cd6ceaf483060d3587d10ef86da6e Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Mon, 10 Mar 2025 11:01:54 +0100 Subject: [PATCH] [orx-shade-styles] Add rotation support to pattern dots --- .../kotlin/fills/gradients/GradientBase.kt | 3 +++ .../kotlin/fills/patterns/PatternBase.kt | 9 +++++++++ .../kotlin/fills/patterns/PatternBuilder.kt | 3 ++- .../jvmDemo/kotlin/gradients/DemoGradient09.kt | 18 +++++++++++++++--- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/orx-shade-styles/src/commonMain/kotlin/fills/gradients/GradientBase.kt b/orx-shade-styles/src/commonMain/kotlin/fills/gradients/GradientBase.kt index 9292d7a7..2f0dae9f 100644 --- a/orx-shade-styles/src/commonMain/kotlin/fills/gradients/GradientBase.kt +++ b/orx-shade-styles/src/commonMain/kotlin/fills/gradients/GradientBase.kt @@ -46,11 +46,14 @@ open class GradientBase( this.resetFill = false fragmentPreamble = """ |vec4 g_fill; + |#ifndef SP_ROTATE2D + |#define SP_ROTATE2D |vec2 rotate2D(vec2 x, float angle){ | float rad = angle / 180.0 * $PI; | mat2 m = mat2(cos(rad),-sin(rad), sin(rad),cos(rad)); | return m * x; |} + |#endif |$oklabToLinearRgbPhrase |$linearRgbToOklabPhrase |${structure.gradientFunction} diff --git a/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBase.kt b/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBase.kt index b34fc20d..9de26f52 100644 --- a/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBase.kt +++ b/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBase.kt @@ -1,6 +1,7 @@ package org.openrndr.extra.shadestyles.fills.patterns import org.openrndr.draw.ShadeStyle +import kotlin.math.PI class PatternBaseStructure( @@ -16,6 +17,14 @@ class PatternBase(structure: PatternBaseStructure) : ShadeStyle() { init { fragmentPreamble = """ + #ifndef SP_ROTATE2D + #define SP_ROTATE2D + vec2 rotate2D(vec2 x, float angle){ + float rad = angle / 180.0 * $PI; + mat2 m = mat2(cos(rad),-sin(rad), sin(rad),cos(rad)); + return m * x; + } + #endif ${structure.domainWarpFunction} ${structure.patternFunction} """.trimIndent() diff --git a/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBuilder.kt b/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBuilder.kt index bd5a370a..47ff97e1 100644 --- a/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBuilder.kt +++ b/orx-shade-styles/src/commonMain/kotlin/fills/patterns/PatternBuilder.kt @@ -135,9 +135,10 @@ class XorMod2PatternBuilder(builder: PatternBuilder) { class DotsPatternBuilder(builder: PatternBuilder) { var dotSize: Double by builder.Parameter("patternDotSize", 0.25) var strokeWeight: Double by builder.Parameter("patternStrokeWeight", 1E10) + var rotation: Double by builder.Parameter("patternDotRotation", 0.0) init { builder.patternFunction = """float pattern(vec2 coord) { - vec2 scoord = coord * p_patternScale; + vec2 scoord = rotate2D(coord, p_patternDotRotation) * p_patternScale; vec2 mcoord = mod(scoord + vec2(0.5), vec2(1.0)) - vec2(0.5); float d = length(mcoord) - p_patternDotSize; float dw = fwidth(d); diff --git a/orx-shade-styles/src/jvmDemo/kotlin/gradients/DemoGradient09.kt b/orx-shade-styles/src/jvmDemo/kotlin/gradients/DemoGradient09.kt index 24770af9..dd38937d 100644 --- a/orx-shade-styles/src/jvmDemo/kotlin/gradients/DemoGradient09.kt +++ b/orx-shade-styles/src/jvmDemo/kotlin/gradients/DemoGradient09.kt @@ -9,6 +9,7 @@ import org.openrndr.extra.color.presets.LIME_GREEN import org.openrndr.extra.imageFit.imageFit import org.openrndr.extra.shadestyles.fills.SpreadMethod import org.openrndr.extra.shadestyles.fills.gradients.gradient +import org.openrndr.extra.shadestyles.fills.patterns.pattern fun main() = application { configure { @@ -18,10 +19,21 @@ fun main() = application { program { val image = loadImage("demo-data/images/image-001.png") extend { + drawer.shadeStyle = pattern { + foregroundColor = ColorRGBa.WHITE + backgroundColor = ColorRGBa.WHITE.shade(0.75) + checkers { + scale = 72.0 + } + } + + drawer.rectangle(drawer.bounds.offsetEdges(-10.0)) + drawer.shadeStyle = gradient { - stops[0.0] = ColorRGBa.CRIMSON - stops[0.7] = ColorRGBa.DODGER_BLUE - stops[1.0] = ColorRGBa.LIME_GREEN + stops[0.0] = ColorRGBa.CRIMSON.opacify(0.0) + stops[0.19] = ColorRGBa.CRIMSON.opacify(0.0) + stops[0.25] = ColorRGBa.DODGER_BLUE.opacify(1.0) + stops[1.0] = ColorRGBa.LIME_GREEN.opacify(1.0) spreadMethod = SpreadMethod.REFLECT luma {