-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
235 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
orx-shade-styles/src/demo/kotlin/DemoNPointLinearGradient01.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import org.openrndr.application | ||
import org.openrndr.color.ColorRGBa | ||
import org.openrndr.color.ColorXSVa | ||
import org.openrndr.color.rgb | ||
import org.openrndr.extensions.SingleScreenshot | ||
import org.openrndr.extra.shadestyles.NPointLinearGradient | ||
import org.openrndr.shape.Rectangle | ||
import kotlin.math.pow | ||
import kotlin.math.sin | ||
|
||
/** | ||
* Demonstrate using a multi color linear gradient. | ||
* The gradient has 8 static saturated colors. | ||
* The positions of the colors are first distributed | ||
* uniformly between 0.0 and 1.0 and then animated towards one of | ||
* the ends over time using pow() and sin(seconds). | ||
*/ | ||
fun main() { | ||
application { | ||
program { | ||
if (System.getProperty("takeScreenshot") == "true") { | ||
extend(SingleScreenshot()) { | ||
this.outputFile = System.getProperty("screenshotPath") | ||
} | ||
} | ||
|
||
val numPoints = 8 | ||
val gradient = NPointLinearGradient(Array(numPoints) { | ||
ColorXSVa(it * 360.0 / numPoints, 1.0, 1.0).toRGBa() | ||
}) | ||
|
||
extend { | ||
drawer.run { | ||
clear(rgb(0.2)) | ||
// The points should be sorted values between 0.0 and 1.0 | ||
gradient.points = Array(numPoints) { | ||
// uniform distribution | ||
// (it / (numPoints - 1.0)) | ||
|
||
// skewed and animated distribution | ||
(it / (numPoints - 1.0)).pow(1.0 + 0.5 * sin(seconds)) | ||
} | ||
gradient.rotation = seconds * 10 | ||
shadeStyle = gradient | ||
stroke = rgb(0.35) | ||
fill = ColorRGBa.WHITE | ||
strokeWeight = 8.0 | ||
|
||
gradient.rotation = seconds * 10 | ||
circle(bounds.position(0.34, 0.5), 110.0) | ||
|
||
gradient.rotation += 90 | ||
rectangle(Rectangle.fromCenter( | ||
bounds.position(0.655, 0.5), 200.0, 200.0)) | ||
} | ||
} | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
orx-shade-styles/src/demo/kotlin/DemoNPointRadialGradient01.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import org.openrndr.application | ||
import org.openrndr.color.ColorRGBa | ||
import org.openrndr.color.rgb | ||
import org.openrndr.extensions.SingleScreenshot | ||
import org.openrndr.extra.shadestyles.NPointRadialGradient | ||
import org.openrndr.shape.Circle | ||
import kotlin.random.Random | ||
|
||
/** | ||
* Demonstrate using a multi color radial gradient. | ||
* The gradient has 5 colors (first and last ones are transparent). | ||
* Any of the properties can be animated, including colors and points. | ||
* See DemoNPointLinearGradient01.kt for an example of animated properties. | ||
*/ | ||
fun main() { | ||
application { | ||
program { | ||
if (System.getProperty("takeScreenshot") == "true") { | ||
extend(SingleScreenshot()) { | ||
this.outputFile = System.getProperty("screenshotPath") | ||
} | ||
} | ||
|
||
val gradient = NPointRadialGradient(arrayOf( | ||
ColorRGBa.PINK.opacify(0.0), | ||
ColorRGBa.PINK, ColorRGBa.WHITE, ColorRGBa.PINK, | ||
ColorRGBa.PINK.opacify(0.0) | ||
), arrayOf(0.0, 0.4, 0.5, 0.6, 1.0)) | ||
|
||
val circles = List(25) { | ||
Circle(Random.nextDouble() * drawer.width, | ||
Random.nextDouble() * drawer.height, | ||
Random.nextDouble() * 150.0) | ||
} | ||
|
||
extend { | ||
drawer.run { | ||
clear(rgb(0.2)) | ||
shadeStyle = gradient | ||
fill = ColorRGBa.WHITE | ||
stroke = null | ||
circles(circles) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.openrndr.extra.shadestyles | ||
|
||
import org.openrndr.color.ColorRGBa | ||
import org.openrndr.draw.ShadeStyle | ||
import org.openrndr.extra.parameters.Description | ||
import org.openrndr.math.Vector2 | ||
|
||
@Description("Multicolor linear gradient") | ||
class NPointLinearGradient( | ||
colors: Array<ColorRGBa>, | ||
points: Array<Double> = Array(colors.size) { it / (colors.size - 1.0) }, | ||
offset: Vector2 = Vector2.ZERO, | ||
rotation: Double = 0.0) : ShadeStyle() { | ||
|
||
var colors: Array<ColorRGBa> by Parameter() | ||
// Sorted normalized values defining relative positions of colors | ||
var points: Array<Double> by Parameter() | ||
var offset: Vector2 by Parameter() | ||
var rotation: Double by Parameter() | ||
|
||
init { | ||
this.colors = colors | ||
this.points = points | ||
this.offset = offset | ||
this.rotation = rotation | ||
|
||
fragmentTransform = """ | ||
vec2 coord = (c_boundsPosition.xy - 0.5 + p_offset); | ||
float cr = cos(radians(p_rotation)); | ||
float sr = sin(radians(p_rotation)); | ||
mat2 rm = mat2(cr, -sr, sr, cr); | ||
vec2 rc = rm * coord; | ||
float f = clamp(rc.y + 0.5, 0.0, 1.0); | ||
int i=0; | ||
while(i < p_points_SIZE - 1 && f >= p_points[i+1]) { i++; } | ||
vec4 color0 = p_colors[i]; | ||
color0.rgb *= color0.a; | ||
vec4 color1 = p_colors[i+1]; | ||
color1.rgb *= color1.a; | ||
float g = (f - p_points[i]) / (p_points[i+1] - p_points[i]); | ||
vec4 gradient = mix(color0, color1, clamp(g, 0.0, 1.0)); | ||
vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a; | ||
x_fill = fn * gradient; | ||
if (x_fill.a != 0) { | ||
x_fill.rgb /= x_fill.a; | ||
} | ||
""" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.openrndr.extra.shadestyles | ||
|
||
import org.openrndr.color.ColorRGBa | ||
import org.openrndr.draw.ShadeStyle | ||
import org.openrndr.extra.parameters.Description | ||
import org.openrndr.math.Vector2 | ||
|
||
@Description("Multicolor radial gradient") | ||
class NPointRadialGradient( | ||
colors: Array<ColorRGBa>, | ||
points: Array<Double> = Array(colors.size) { it / (colors.size - 1.0) }, | ||
offset: Vector2 = Vector2.ZERO, | ||
rotation: Double = 0.0, | ||
length: Double = 1.0) : ShadeStyle() { | ||
|
||
var colors: Array<ColorRGBa> by Parameter() | ||
|
||
// Sorted normalized values defining relative positions of colors | ||
var points: Array<Double> by Parameter() | ||
var offset: Vector2 by Parameter() | ||
var rotation: Double by Parameter() | ||
var length: Double by Parameter() | ||
|
||
init { | ||
this.colors = colors | ||
this.points = points | ||
this.offset = offset | ||
this.rotation = rotation | ||
this.length = length | ||
|
||
fragmentTransform = """ | ||
vec2 coord = (c_boundsPosition.xy - 0.5 + p_offset/2.0) * 2.0; | ||
float cr = cos(radians(p_rotation)); | ||
float sr = sin(radians(p_rotation)); | ||
mat2 rm = mat2(cr, -sr, sr, cr); | ||
vec2 rc = rm * coord; | ||
float f = clamp(p_length * length(rc), 0.0, 1.0); | ||
int i=0; | ||
while(i < p_points_SIZE - 1 && f >= p_points[i+1]) { i++; } | ||
vec4 color0 = p_colors[i]; | ||
color0.rgb *= color0.a; | ||
vec4 color1 = p_colors[i+1]; | ||
color1.rgb *= color1.a; | ||
float g = (f - p_points[i]) / (p_points[i+1] - p_points[i]); | ||
vec4 gradient = mix(color0, color1, clamp(g, 0.0, 1.0)); | ||
vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a; | ||
x_fill = fn * gradient; | ||
if (x_fill.a != 0) { | ||
x_fill.rgb /= x_fill.a; | ||
} | ||
""" | ||
} | ||
} |