Add exponent to all gradients, cleanup

This commit is contained in:
Abe Pazos
2020-04-07 19:03:35 +02:00
parent 5d3eadd156
commit fa51fad405
4 changed files with 105 additions and 50 deletions

View File

@@ -4,10 +4,17 @@ import org.openrndr.color.ColorRGBa
import org.openrndr.draw.ShadeStyle import org.openrndr.draw.ShadeStyle
import org.openrndr.extra.parameters.ColorParameter import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2 import org.openrndr.math.Vector2
@Description("Angular gradient") @Description("Angular gradient")
class AngularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Vector2.ZERO, rotation: Double = 0.0) : ShadeStyle() { class AngularGradient(
color0: ColorRGBa,
color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0,
exponent: Double = 1.0) : ShadeStyle() {
@ColorParameter("start color", order = 0) @ColorParameter("start color", order = 0)
var color0 : ColorRGBa by Parameter() var color0 : ColorRGBa by Parameter()
@ColorParameter("end color", order = 1) @ColorParameter("end color", order = 1)
@@ -16,37 +23,49 @@ class AngularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Ve
var offset : Vector2 by Parameter() var offset : Vector2 by Parameter()
@ColorParameter("rotation", order = 3) @ColorParameter("rotation", order = 3)
var rotation : Double by Parameter() var rotation : Double by Parameter()
@DoubleParameter("exponent", 0.01, 10.0, order = 4)
var exponent: Double by Parameter()
init { init {
this.color0 = color0 this.color0 = color0
this.color1 = color1 this.color1 = color1
this.offset = offset this.offset = offset
this.rotation = rotation this.rotation = rotation
this.exponent = exponent
fragmentTransform = """ fragmentTransform = """
vec2 coord = (c_boundsPosition.xy - vec2(0.5) + p_offset/2.0) * 2.0; vec2 coord = (c_boundsPosition.xy - 0.5 + p_offset/2.0) * 2.0;
float cr = cos(radians(p_rotation)); float cr = cos(radians(p_rotation));
float sr = sin(radians(p_rotation)); float sr = sin(radians(p_rotation));
mat2 rm = mat2(cr, -sr, sr, cr); mat2 rm = mat2(cr, -sr, sr, cr);
vec2 rc = rm * coord; vec2 rc = rm * coord;
float f = (atan(rc.y, rc.x) + 3.1415926536) / (2.0 * 3.1415926536); float f = (atan(rc.y, rc.x) + 3.1415926536) / (2.0 * 3.1415926536);
vec4 color0 = p_color0 * vec4(p_color0.aaa, 1.0); vec4 color0 = p_color0;
vec4 color1 = p_color1 * vec4(p_color1.aaa, 1.0); color0.rgb *= color0.a;
vec4 gradient = color0 * (1.0-f) + color1 * f; vec4 color1 = p_color1;
color1.rgb *= color1.a;
vec4 gradient = mix(color0, color1, pow(f, p_exponent));
vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a; vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a;
x_fill = fn * gradient; x_fill = fn * gradient;
if (x_fill.a !=0) { if (x_fill.a != 0) {
x_fill.rgb /= x_fill.a; x_fill.rgb /= x_fill.a;
} }
""" """
} }
} }
fun angularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Vector2.ZERO, rotation: Double = 0.0): ShadeStyle { fun angularGradient(
return AngularGradient(color0, color1, offset, rotation) color0: ColorRGBa,
color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0,
exponent: Double = 1.0
): ShadeStyle {
return AngularGradient(color0, color1, offset, rotation, exponent)
} }

View File

@@ -8,7 +8,13 @@ import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2 import org.openrndr.math.Vector2
@Description("Half-angular gradient") @Description("Half-angular gradient")
class HalfAngularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Vector2.ZERO, rotation: Double = 0.0) : ShadeStyle() { class HalfAngularGradient(
color0: ColorRGBa,
color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0,
exponent: Double = 1.0) : ShadeStyle() {
@ColorParameter("start color", order = 0) @ColorParameter("start color", order = 0)
var color0: ColorRGBa by Parameter() var color0: ColorRGBa by Parameter()
@ColorParameter("end color", order = 1) @ColorParameter("end color", order = 1)
@@ -16,18 +22,18 @@ class HalfAngularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2
var offset: Vector2 by Parameter() var offset: Vector2 by Parameter()
@DoubleParameter("rotation", -180.0, 180.0, order = 2) @DoubleParameter("rotation", -180.0, 180.0, order = 2)
var rotation: Double by Parameter() var rotation: Double by Parameter()
@DoubleParameter("length", 0.0, 10.0, order = 3) @DoubleParameter("exponent", 0.01, 10.0, order = 3)
var length: Double by Parameter() var exponent: Double by Parameter()
init { init {
this.color0 = color0 this.color0 = color0
this.color1 = color1 this.color1 = color1
this.offset = offset this.offset = offset
this.rotation = rotation this.rotation = rotation
this.length = length this.exponent = exponent
fragmentTransform = """ fragmentTransform = """
vec2 coord = (c_boundsPosition.xy - vec2(0.5) + p_offset/2.0) * 2.0; vec2 coord = (c_boundsPosition.xy - 0.5 + p_offset/2.0) * 2.0;
float cr = cos(radians(p_rotation)); float cr = cos(radians(p_rotation));
float sr = sin(radians(p_rotation)); float sr = sin(radians(p_rotation));
@@ -37,21 +43,31 @@ class HalfAngularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2
//float f = abs(atan(rc.y/rc.x)) / (3.1415926536/2.0); //float f = abs(atan(rc.y/rc.x)) / (3.1415926536/2.0);
//float f = (atan(rc.y/rc.x) + 3.1415926536/2.0) / (3.1415926536); //float f = (atan(rc.y/rc.x) + 3.1415926536/2.0) / (3.1415926536);
vec4 color0 = p_color0 * vec4(p_color0.aaa, 1.0); vec4 color0 = p_color0;
vec4 color1 = p_color1 * vec4(p_color1.aaa, 1.0); color0.rgb *= color0.a;
vec4 gradient = color0 * (1.0-f) + color1 * f; vec4 color1 = p_color1;
vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a; color1.rgb *= color1.a;
vec4 gradient = mix(color0, color1, pow(f, p_exponent));
vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a;
x_fill = fn * gradient; x_fill = fn * gradient;
if (x_fill.a !=0) { if (x_fill.a != 0) {
x_fill.rgb /= x_fill.a; x_fill.rgb /= x_fill.a;
} }
} """
"""
} }
} }
fun halfAngularGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Vector2.ZERO, rotation: Double = 0.0): ShadeStyle { fun halfAngularGradient(
return HalfAngularGradient(color0, color1, offset, rotation) color0: ColorRGBa,
color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0,
exponent: Double = 1.0
): ShadeStyle {
return HalfAngularGradient(color0, color1, offset, rotation, exponent)
} }

View File

@@ -12,8 +12,8 @@ class LinearGradient(
color0: ColorRGBa, color0: ColorRGBa,
color1: ColorRGBa, color1: ColorRGBa,
offset: Vector2, offset: Vector2,
rotation: Double, rotation: Double = 0.0,
exponent: Double) : ShadeStyle() { exponent: Double = 1.0) : ShadeStyle() {
@ColorParameter("start color", order = 0) @ColorParameter("start color", order = 0)
var color0: ColorRGBa by Parameter() var color0: ColorRGBa by Parameter()
@@ -33,11 +33,13 @@ class LinearGradient(
this.exponent = exponent this.exponent = exponent
fragmentTransform = """ fragmentTransform = """
vec2 coord = (c_boundsPosition.xy - vec2(0.5) + p_offset); vec2 coord = (c_boundsPosition.xy - 0.5 + p_offset);
float cr = cos(radians(p_rotation)); float cr = cos(radians(p_rotation));
float sr = sin(radians(p_rotation)); float sr = sin(radians(p_rotation));
mat2 rm = mat2(cr, -sr, sr, cr); mat2 rm = mat2(cr, -sr, sr, cr);
float f = clamp((rm * coord).y + 0.5, 0.0, 1.0); vec2 rc = rm * coord;
float f = clamp(rc.y + 0.5, 0.0, 1.0);
vec4 color0 = p_color0; vec4 color0 = p_color0;
color0.rgb *= color0.a; color0.rgb *= color0.a;
@@ -57,9 +59,12 @@ class LinearGradient(
} }
} }
fun linearGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = fun linearGradient(
Vector2.ZERO, rotation: Double = 0.0, exponent: Double = 1.0) = color0: ColorRGBa,
LinearGradient(color0, color1, offset, rotation, exponent) color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0,
exponent: Double = 1.0
) : ShadeStyle {
return LinearGradient(color0, color1, offset, rotation, exponent)
}

View File

@@ -9,16 +9,25 @@ import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2 import org.openrndr.math.Vector2
@Description("Radial gradient") @Description("Radial gradient")
class RadialGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Vector2.ZERO, rotation: Double = 0.0, length: Double = 1.0) : ShadeStyle() { class RadialGradient(
color0: ColorRGBa,
color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0,
length: Double = 1.0,
exponent: Double = 1.0) : ShadeStyle() {
@ColorParameter("start color", order = 0) @ColorParameter("start color", order = 0)
var color0 : ColorRGBa by Parameter() var color0 : ColorRGBa by Parameter()
@ColorParameter("end color", order = 1) @ColorParameter("end color", order = 1)
var color1 : ColorRGBa by Parameter() var color1 : ColorRGBa by Parameter()
var offset : Vector2 by Parameter() var offset : Vector2 by Parameter()
@DoubleParameter("rotation", -180.0, 180.0) @DoubleParameter("rotation", -180.0, 180.0, order = 2)
var rotation : Double by Parameter() var rotation : Double by Parameter()
@DoubleParameter("length", 0.0, 10.0) @DoubleParameter("length", 0.0, 10.0)
var length: Double by Parameter() var length: Double by Parameter()
@DoubleParameter("exponent", 0.01, 10.0, order = 3)
var exponent: Double by Parameter()
init { init {
this.color0 = color0 this.color0 = color0
@@ -26,24 +35,29 @@ class RadialGradient(color0: ColorRGBa, color1: ColorRGBa, offset: Vector2 = Vec
this.offset = offset this.offset = offset
this.rotation = rotation this.rotation = rotation
this.length = length this.length = length
this.exponent = exponent
fragmentTransform = """ fragmentTransform = """
vec2 coord = (c_boundsPosition.xy - vec2(0.5) + p_offset/2.0) * 2.0; vec2 coord = (c_boundsPosition.xy - 0.5 + p_offset/2.0) * 2.0;
float cr = cos(radians(p_rotation)); float cr = cos(radians(p_rotation));
float sr = sin(radians(p_rotation)); float sr = sin(radians(p_rotation));
mat2 rm = mat2(cr, -sr, sr, cr); mat2 rm = mat2(cr, -sr, sr, cr);
float f = clamp(p_length * length(rm * coord), 0.0, 1.0); vec2 rc = rm * coord;
float f = clamp(p_length * length(rc), 0.0, 1.0);
vec4 color0 = p_color0 * vec4(p_color0.aaa, 1.0);
vec4 color1 = p_color1 * vec4(p_color1.aaa, 1.0); vec4 color0 = p_color0;
color0.rgb *= color0.a;
vec4 gradient = color0 * (1.0-f) + color1 * f;
vec4 color1 = p_color1;
color1.rgb *= color1.a;
vec4 gradient = mix(color0, color1, pow(f, p_exponent));
vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a; vec4 fn = vec4(x_fill.rgb, 1.0) * x_fill.a;
x_fill = fn * gradient; x_fill = fn * gradient;
if (x_fill.a !=0) { if (x_fill.a != 0) {
x_fill.rgb /= x_fill.a; x_fill.rgb /= x_fill.a;
} }
""" """
@@ -55,7 +69,8 @@ fun radialGradient(
color1: ColorRGBa, color1: ColorRGBa,
offset: Vector2 = Vector2.ZERO, offset: Vector2 = Vector2.ZERO,
rotation: Double = 0.0, rotation: Double = 0.0,
length: Double = 1.0 length: Double = 1.0,
exponent: Double = 1.0
): ShadeStyle { ): ShadeStyle {
return RadialGradient(color0, color1, offset, rotation, length) return RadialGradient(color0, color1, offset, rotation, length, exponent)
} }