Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added filtering to BlurFilter & DropShadowFilter & View.maskFiltering #1688

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ class BlurFilter(
expandBorder: Boolean = true,
@ViewProperty
var optimize: Boolean = true
) : ComposedFilter() {
) : ComposedFilter(), FilterWithFiltering {
private val horizontal = DirectionalBlurFilter(angle = 0.degrees, radius, expandBorder).also { filters.add(it) }
private val vertical = DirectionalBlurFilter(angle = 90.degrees, radius, expandBorder).also { filters.add(it) }
override var filtering: Boolean
get() = horizontal.filtering
set(value) {
horizontal.filtering = value
vertical.filtering = value
}
@ViewProperty
var expandBorder: Boolean
get() = horizontal.expandBorder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ open class DropshadowFilter(
var blurRadius: Float = 4f,
@ViewProperty
var smoothing: Boolean = true
) : Filter {
) : FilterWithFiltering {
private val blur = BlurFilter(16f)

override var filtering: Boolean by blur::filtering

override fun computeBorder(texWidth: Int, texHeight: Int): MarginInt {
val out = blur.computeBorder(texWidth, texHeight)
var top = out.top
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package korlibs.korge.view.filter

interface FilterWithFiltering : Filter {
var filtering: Boolean
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import korlibs.memory.*
* When building shaders by calling FragmentShader { ... }, you will have additionally access to:
* [fragmentCoords], [fragmentCoords01] properties and [tex] method to be used inside the shader.
*/
abstract class ShaderFilter : Filter {
abstract class ShaderFilter : FilterWithFiltering {
object TexInfoUB : UniformBlock(fixedLocation = 4) {
val u_TextureSize by vec2()
val u_MaxTexCoords by vec2()
Expand Down Expand Up @@ -73,7 +73,7 @@ abstract class ShaderFilter : Filter {
override fun getProgram(): Program = _program
}

var filtering = true
override var filtering = true

private var textureSizeHolder = Point()
private var textureMaxTexCoords = Point()
Expand Down
29 changes: 22 additions & 7 deletions korge/src/commonMain/kotlin/korlibs/korge/view/mask/ViewMask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,38 @@ import kotlin.native.concurrent.*
@ThreadLocal
private var View.__mask: View? by extraProperty { null }

fun <T : View> T.mask(mask: View?): T {
@ThreadLocal
private var View.__maskFiltering: Boolean by extraProperty { true }

fun <T : View> T.mask(mask: View?, filtering: Boolean = true): T {
this.maskFiltering = filtering
this.mask = mask
return this
}

var View.maskFiltering: Boolean
get() = __maskFiltering
set(value) {
__maskFiltering = value
updatedMask()
}
var View.mask: View?
get() = __mask
set(value) {
removeRenderPhaseOfType<ViewRenderPhaseMask>()
if (value != null) {
addRenderPhase(ViewRenderPhaseMask(value))
}
__mask = value
updatedMask()
}

private fun View.updatedMask() {
val value = __mask
removeRenderPhaseOfType<ViewRenderPhaseMask>()
if (value != null) {
addRenderPhase(ViewRenderPhaseMask(value, __maskFiltering))
}
}

@OptIn(KoragExperimental::class)
class ViewRenderPhaseMask(var mask: View) : ViewRenderPhase {
class ViewRenderPhaseMask(var mask: View, var maskFiltering: Boolean) : ViewRenderPhase {
companion object {
const val PRIORITY = -100
}
Expand Down Expand Up @@ -59,7 +74,7 @@ class ViewRenderPhaseMask(var mask: View) : ViewRenderPhase {
//batcher.drawQuad(Texture(viewFB), 300f, 200f, m = view.parent!!.globalMatrix)
batcher.temporalTextureUnit(DefaultShaders.u_Tex, viewFB.tex, DefaultShaders.u_TexEx, maskFB.tex) {
batcher.drawQuad(
Texture(viewFB), m = mask.globalMatrix, program = DefaultShaders.MERGE_ALPHA_PROGRAM,
Texture(viewFB), m = mask.globalMatrix, program = DefaultShaders.MERGE_ALPHA_PROGRAM, filtering = maskFiltering
)
//batcher.createBatchIfRequired()
}
Expand Down