Merge branch 'orx-filters-chromatic-aberration' of https://github.com/ricardomatias/orx into ricardomatias-orx-filters-chromatic-aberration

This commit is contained in:
Edwin Jakobs
2020-01-13 14:12:04 +01:00
2 changed files with 63 additions and 0 deletions

View 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])
}
}
}

View File

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