Add Chromatic Aberration effect
This commit is contained in:
40
orx-fx/src/main/kotlin/color/ChromaticAberration.kt
Normal file
40
orx-fx/src/main/kotlin/color/ChromaticAberration.kt
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package org.openrndr.extra.fx.color
|
||||||
|
|
||||||
|
import org.openrndr.draw.*
|
||||||
|
import org.openrndr.extra.fx.filterFragmentCode
|
||||||
|
import org.openrndr.math.Vector2
|
||||||
|
import org.openrndr.resourceUrl
|
||||||
|
|
||||||
|
class ChromaticAberration : Filter(Shader.createFromCode(Filter.filterVertexCode, filterFragmentCode("color/chromatic-aberration.frag"))){
|
||||||
|
/**
|
||||||
|
* aberration factor, default value is 1.0
|
||||||
|
*/
|
||||||
|
var aberrationFactor: Double by parameters
|
||||||
|
|
||||||
|
|
||||||
|
init {
|
||||||
|
aberrationFactor = 8.0
|
||||||
|
}
|
||||||
|
|
||||||
|
private var intermediate: ColorBuffer? = null
|
||||||
|
|
||||||
|
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
|
||||||
|
intermediate?.let {
|
||||||
|
if (it.width != target[0].width || it.height != target[0].height) {
|
||||||
|
intermediate = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (intermediate == null) {
|
||||||
|
intermediate = colorBuffer(target[0].width, target[0].height, target[0].contentScale, target[0].format, target[0].type)
|
||||||
|
}
|
||||||
|
|
||||||
|
intermediate?.let {
|
||||||
|
parameters["dimensions"] = Vector2(it.effectiveWidth.toDouble(), it.effectiveHeight.toDouble())
|
||||||
|
|
||||||
|
super.apply(source, arrayOf(it))
|
||||||
|
|
||||||
|
it.copyTo(target[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#version 330
|
||||||
|
|
||||||
|
in vec2 v_texCoord0;
|
||||||
|
|
||||||
|
uniform sampler2D tex0;
|
||||||
|
|
||||||
|
uniform float aberrationFactor;
|
||||||
|
uniform vec2 dimensions;
|
||||||
|
|
||||||
|
out vec4 o_color;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
vec2 uv = v_texCoord0;
|
||||||
|
float factor = (1.0 / dimensions.x) * aberrationFactor;
|
||||||
|
|
||||||
|
vec4 tex = texture(tex0, uv);
|
||||||
|
|
||||||
|
float r = texture(tex0, vec2(uv.x - factor, uv.y)).r;
|
||||||
|
float g = tex.g;
|
||||||
|
float b = texture(tex0, vec2(uv.x + factor, uv.y)).b;
|
||||||
|
|
||||||
|
o_color = vec4(vec3(r, g, b), tex.a);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user