Skip to content

Commit

Permalink
Fix default color for color inputs to ISF shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
xian committed Oct 4, 2023
1 parent 39d8c25 commit 297d158
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package baaahs.gl.shader.dialect

import baaahs.Color
import baaahs.englishize
import baaahs.gl.glsl.*
import baaahs.gl.patch.ContentType
Expand All @@ -10,6 +11,7 @@ import baaahs.gl.shader.ShaderSubstitutions
import baaahs.listOf
import baaahs.plugin.PluginRef
import baaahs.plugin.Plugins
import baaahs.plugin.core.feed.ColorPickerFeed
import baaahs.plugin.core.feed.XyPadFeed
import baaahs.show.Shader
import baaahs.util.Logger
Expand Down Expand Up @@ -248,7 +250,16 @@ class IsfShaderAnalyzer(
private fun createColor(input: IsfColorInput): InputPort {
return InputPort(
input.NAME, ContentType.Color, title = input.LABEL ?: input.NAME.englishize(),
pluginRef = PluginRef("baaahs.Core", "ColorPicker")
pluginRef = ColorPickerFeed.pluginRef,
pluginConfig = buildJsonObject {
input.DEFAULT?.let {
if (it.size == 4) {
val (r, g, b, a) = it
val defaultColor = Color(r.toFloat(), g.toFloat(), b.toFloat(), a.toFloat())
put("default", JsonPrimitive(defaultColor.toHexString()))
}
}
}
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import baaahs.gl.data.singleUniformFeedContext
import baaahs.gl.glsl.GlslType
import baaahs.gl.patch.ContentType
import baaahs.gl.shader.InputPort
import baaahs.plugin.PluginRef
import baaahs.plugin.classSerializer
import baaahs.plugin.core.CorePlugin
import baaahs.show.Feed
Expand Down Expand Up @@ -51,13 +52,14 @@ data class ColorPickerFeed(
override val resourceName: String get() = "ColorPicker"
override val contentType: ContentType get() = ContentType.Color
override val serializerRegistrar get() = classSerializer(serializer())
val pluginRef = PluginRef(CorePlugin.id, resourceName)

override fun build(inputPort: InputPort): ColorPickerFeed {
val default = inputPort.pluginConfig?.get("default")?.jsonPrimitive?.contentOrNull

return ColorPickerFeed(
inputPort.title,
initialValue = default?.let { Color.Companion.from(it) } ?: Color.WHITE
initialValue = default?.let { Color.from(it) } ?: Color.WHITE
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient

/**
* This feed provides `gl_FragColor` for quad previews.
* This feed provides `gl_FragCoord` for quad previews.
*
* Because `gl_FragColor` is always given as absolute pixels relative to the bottom-left
* of the screen/canvas, _not_ relative to the the viewport, and we might be rendering
* Because `gl_FragCoord` is always given as absolute pixels relative to the bottom-left
* of the screen/canvas, _not_ relative to the viewport, and we might be rendering
* into a `SharedGlContext` (which adjusts the viewport to a rectangle within the shared
* canvas), we need to adjust `gl_FragColor` to account for any offset.
* canvas), we need to adjust `gl_FragCoord` to account for any offset.
*/
@Serializable
@SerialName("baaahs.Core:RasterCoordinate")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package baaahs.gl.shader.dialect

import baaahs.*
import baaahs.app.ui.editor.stringify
import baaahs.describe
import baaahs.device.PixelArrayDevice
import baaahs.gl.autoWire
import baaahs.gl.autoWireWithDefaults
Expand All @@ -15,14 +15,12 @@ import baaahs.gl.shader.InputPort
import baaahs.gl.shader.OutputPort
import baaahs.gl.shader.ShaderSubstitutions
import baaahs.gl.testToolchain
import baaahs.only
import baaahs.plugin.PluginRef
import baaahs.plugin.core.feed.ColorPickerFeed
import baaahs.plugin.core.feed.RasterCoordinateFeed
import baaahs.plugin.core.feed.SelectFeed
import baaahs.show.live.LinkedPatch
import baaahs.show.mutable.MutableFeedPort
import baaahs.toBeSpecified
import baaahs.toEqual
import baaahs.ui.diagnostics.DotDag
import ch.tutteli.atrium.api.fluent.en_GB.contains
import ch.tutteli.atrium.api.fluent.en_GB.containsExactly
Expand Down Expand Up @@ -73,6 +71,17 @@ object IsfShaderDialectSpec : Spek({
"NAME": "channel",
"TYPE": "long",
"VALUES": [ 1, 2, 3 ]
},
{
"NAME": "fillColor",
"LABEL": "Fill Color",
"TYPE": "color",
"DEFAULT": [
1.0,
0.0,
0.0,
1.0
]
}
]
}*/
Expand Down Expand Up @@ -117,15 +126,28 @@ object IsfShaderDialectSpec : Spek({
put("default", 1.json)
},
isImplicit = true
),
InputPort(
"fillColor", ContentType.Color, GlslType.Vec4, "Fill Color",
pluginRef = PluginRef("baaahs.Core", "ColorPicker"),
pluginConfig = buildJsonObject {
put("default", "#ff0000".json)
},
isImplicit = true
)
)
}

it("generates correct data sources") {
it("generates correct select feed") {
expect(SelectFeed.build(openShader.inputPorts[1]))
.toEqual(SelectFeed("Color Channel", listOf(1 to "Red", 2 to "Green", 3 to "Blue"), 0))
}

it("generates correct color picker feed") {
expect(ColorPickerFeed.build(openShader.inputPorts[2]))
.toEqual(ColorPickerFeed("Fill Color", Color.from("#ff0000")))
}

it("finds the output port") {
expect(shaderAnalysis.outputPorts).containsExactly(
OutputPort(ContentType.Color, description = "Output Color", id = "gl_FragColor")
Expand Down

0 comments on commit 297d158

Please sign in to comment.