Skip to content

Commit

Permalink
Sending data to the nineslice shader
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 21, 2024
1 parent 1148cbc commit c759458
Show file tree
Hide file tree
Showing 11 changed files with 236 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import indigo.shared.collections.Batch
import indigo.shared.datatypes.Fill
import indigo.shared.datatypes.RGB
import indigo.shared.datatypes.RGBA
import indigo.shared.datatypes.Rectangle
import indigo.shared.materials.FillType
import indigo.shared.materials.Material
import indigo.shared.shader.EntityShader
Expand Down Expand Up @@ -60,6 +61,10 @@ final case class LegacyEffects(
withFillType(FillType.Stretch)
def tile: LegacyEffects =
withFillType(FillType.Tile)
def nineSlice(center: Rectangle): LegacyEffects =
withFillType(FillType.NineSlice(center))
def nineSlice(top: Int, right: Int, bottom: Int, left: Int): LegacyEffects =
withFillType(FillType.NineSlice(top, right, bottom, left))

lazy val toShaderData: ShaderData =
val overlayType: Float =
Expand All @@ -70,10 +75,23 @@ final case class LegacyEffects(

val imageFillType: Float =
fillType match
case FillType.Normal => 0.0
case FillType.Stretch => 1.0
case FillType.Tile => 2.0
case FillType.NineSlice => 3.0
case FillType.Normal => 0.0
case FillType.Stretch => 1.0
case FillType.Tile => 2.0
case FillType.NineSlice(_) => 3.0

val nineSliceCenter: scalajs.js.Array[Float] =
fillType match
case FillType.NineSlice(center) =>
scalajs.js.Array(
center.x.toFloat,
center.y.toFloat,
center.width.toFloat,
center.height.toFloat
)

case _ =>
scalajs.js.Array(0.0f, 0.0f, 0.0f, 0.0f)

ShaderData(
LegacyEffects.entityShader.id,
Expand All @@ -87,12 +105,15 @@ final case class LegacyEffects(
alpha.toFloat,
saturation.toFloat,
overlayType,
imageFillType,
tint.r.toFloat,
tint.g.toFloat,
tint.b.toFloat,
tint.a.toFloat
)
imageFillType
) ++
nineSliceCenter ++
scalajs.js.Array(
tint.r.toFloat,
tint.g.toFloat,
tint.b.toFloat,
tint.a.toFloat
)
)
) ++ overlay.toUniformData("LegacyEffects") ++
// BORDER_COLOR (vec4), GLOW_COLOR (vec4), EFFECT_AMOUNTS (vec4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indigoextras.effectmaterials
import indigo.shared.assets.AssetName
import indigo.shared.collections.Batch
import indigo.shared.datatypes.RGBA
import indigo.shared.datatypes.Rectangle
import indigo.shared.materials.BlendMaterial
import indigo.shared.materials.FillType
import indigo.shared.materials.Material
Expand All @@ -13,6 +14,7 @@ import indigo.shared.shader.EntityShader
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderId
import indigo.shared.shader.ShaderPrimitive.float
import indigo.shared.shader.ShaderPrimitive.rawJSArray
import indigo.shared.shader.ShaderProgram
import indigo.shared.shader.UltravioletShader
import indigo.shared.shader.Uniform
Expand Down Expand Up @@ -71,20 +73,38 @@ final case class RefractionEntity(diffuse: AssetName, fillType: FillType) extend
withFillType(FillType.Stretch)
def tile: RefractionEntity =
withFillType(FillType.Tile)
def nineSlice(center: Rectangle): RefractionEntity =
withFillType(FillType.NineSlice(center))
def nineSlice(top: Int, right: Int, bottom: Int, left: Int): RefractionEntity =
withFillType(FillType.NineSlice(top, right, bottom, left))

lazy val toShaderData: ShaderData =
val imageFillType: Double =
fillType match
case FillType.Normal => 0.0
case FillType.Stretch => 1.0
case FillType.Tile => 2.0
case FillType.NineSlice => 3.0
case FillType.Normal => 0.0
case FillType.Stretch => 1.0
case FillType.Tile => 2.0
case FillType.NineSlice(_) => 3.0

val nineSliceCenter: scalajs.js.Array[Float] =
fillType match
case FillType.NineSlice(center) =>
scalajs.js.Array(
center.x.toFloat,
center.y.toFloat,
center.width.toFloat,
center.height.toFloat
)

case _ =>
scalajs.js.Array(0.0f, 0.0f, 0.0f, 0.0f)

val uniformBlock: UniformBlock =
UniformBlock(
UniformBlockName("IndigoBitmapData"),
Batch(
Uniform("FILLTYPE") -> float(imageFillType)
Uniform("FILLTYPE") -> float(imageFillType),
Uniform("NINE_SLICE_CENTER") -> rawJSArray(nineSliceCenter)
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ object LegacyEffectsShaders:

trait Env extends Lighting.LightEnv {
val ALPHA_SATURATION_OVERLAYTYPE_FILLTYPE: highp[vec4] = vec4(0.0f)
val NINE_SLICE_CENTER: highp[vec4] = vec4(0.0f)
val TINT: vec4 = vec4(0.0f)
val GRADIENT_FROM_TO: vec4 = vec4(0.0f)
val GRADIENT_FROM_COLOR: vec4 = vec4(0.0f)
Expand All @@ -78,6 +79,7 @@ object LegacyEffectsShaders:

final case class IndigoLegacyEffectsData(
ALPHA_SATURATION_OVERLAYTYPE_FILLTYPE: highp[vec4],
NINE_SLICE_CENTER: highp[vec4],
TINT: vec4,
GRADIENT_FROM_TO: vec4,
GRADIENT_FROM_COLOR: vec4,
Expand All @@ -104,7 +106,7 @@ object LegacyEffectsShaders:
calculateRadialGradientOverlay
val _calculateSaturation: (vec4, Float) => vec4 =
calculateSaturation
val _tileAndStretchChannel: (Int, vec4, sampler2D.type, vec2, vec2, vec2, vec2, vec2) => vec4 =
val _tileAndStretchChannel: (Int, vec4, sampler2D.type, vec2, vec2, vec2, vec2, vec2, vec4) => vec4 =
tileAndStretchChannel

@in val v_offsetTL: vec2 = null
Expand Down Expand Up @@ -273,7 +275,8 @@ object LegacyEffectsShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)
env.CHANNEL_1 = _tileAndStretchChannel(
fillType,
Expand All @@ -283,7 +286,8 @@ object LegacyEffectsShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)
env.CHANNEL_2 = _tileAndStretchChannel(
fillType,
Expand All @@ -293,7 +297,8 @@ object LegacyEffectsShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)
env.CHANNEL_3 = _tileAndStretchChannel(
fillType,
Expand All @@ -303,7 +308,8 @@ object LegacyEffectsShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)

val alpha: Float = env.ALPHA_SATURATION_OVERLAYTYPE_FILLTYPE.x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ object RefractionShaders:
val reference: BlendEnv = new BlendEnv {}

trait FragEnv extends Lighting.LightEnv {
val FILLTYPE: highp[Float] = 0.0f
val FILLTYPE: highp[Float] = 0.0f
val NINE_SLICE_CENTER: highp[vec4] = vec4(0.0f)
}
object FragEnv:
val reference: FragEnv = new FragEnv {}
Expand All @@ -32,14 +33,17 @@ object RefractionShaders:
texture2D(env.DST_CHANNEL, offset)
}

final case class IndigoBitmapData(FILLTYPE: highp[Float])
final case class IndigoBitmapData(
FILLTYPE: highp[Float],
NINE_SLICE_CENTER: highp[vec4]
)

inline def normalMinusBlue =
Shader[FragEnv] { env =>
import TileAndStretch.*

// Delegates
val _tileAndStretchChannel: (Int, vec4, sampler2D.type, vec2, vec2, vec2, vec2, vec2) => vec4 =
val _tileAndStretchChannel: (Int, vec4, sampler2D.type, vec2, vec2, vec2, vec2, vec2, vec4) => vec4 =
tileAndStretchChannel

ubo[IndigoBitmapData]
Expand All @@ -54,7 +58,8 @@ object RefractionShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)
env.CHANNEL_1 = _tileAndStretchChannel(
env.FILLTYPE.toInt,
Expand All @@ -64,7 +69,8 @@ object RefractionShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)
env.CHANNEL_2 = _tileAndStretchChannel(
env.FILLTYPE.toInt,
Expand All @@ -74,7 +80,8 @@ object RefractionShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)
env.CHANNEL_3 = _tileAndStretchChannel(
env.FILLTYPE.toInt,
Expand All @@ -84,7 +91,8 @@ object RefractionShaders:
env.CHANNEL_0_SIZE,
env.UV,
env.SIZE,
env.TEXTURE_SIZE
env.TEXTURE_SIZE,
env.NINE_SLICE_CENTER
)

val redGreen = vec3(env.CHANNEL_0.xy, 0.0f)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package indigo.shared.materials

import indigo.shared.datatypes.Rectangle

enum FillType derives CanEqual:
case Normal
case Stretch
case Tile
case NineSlice(center: Rectangle)

object FillType:
object NineSlice:
def apply(top: Int, right: Int, bottom: Int, left: Int): FillType =
FillType.NineSlice(
Rectangle(left, top, right - left, bottom - top)
)
Loading

0 comments on commit c759458

Please sign in to comment.