Skip to content

Commit 3b22d55

Browse files
Merge pull request #342 from baaahs/editor-improvements
Add simple preview for moving head shaders
2 parents bd2f525 + d7cb0f1 commit 3b22d55

File tree

16 files changed

+233
-41
lines changed

16 files changed

+233
-41
lines changed

src/commonMain/kotlin/baaahs/gl/render/ShaderPreview.kt renamed to src/commonMain/kotlin/baaahs/gl/preview/ShaderPreview.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package baaahs.gl.render
1+
package baaahs.gl.preview
22

33
import baaahs.gl.glsl.GlslProgram
4+
import baaahs.gl.render.RenderEngine
45

56
interface ShaderPreview {
67
val renderEngine: RenderEngine
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package baaahs.gl.preview
2+
3+
expect interface ShaderPreviewBootstrapper
4+
5+
expect object MovingHeadPreviewBootstrapper : ShaderPreviewBootstrapper
6+
7+
expect object ProjectionPreviewBootstrapper : ShaderPreviewBootstrapper
8+
9+
expect object QuadPreviewBootstrapper : ShaderPreviewBootstrapper

src/commonMain/kotlin/baaahs/gl/shader/type/DistortionShader.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import baaahs.app.ui.CommonIcons
44
import baaahs.gl.glsl.ShaderAnalysis
55
import baaahs.gl.patch.ContentType
66
import baaahs.gl.preview.PreviewShaders
7+
import baaahs.gl.preview.QuadPreviewBootstrapper
8+
import baaahs.gl.preview.ShaderPreviewBootstrapper
79
import baaahs.gl.shader.OpenShader
810
import baaahs.ui.Icon
911

@@ -20,6 +22,9 @@ object DistortionShader : ShaderType {
2022
}
2123
""".trimIndent()
2224

25+
override val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
26+
get() = QuadPreviewBootstrapper
27+
2328
override fun matches(shaderAnalysis: ShaderAnalysis): ShaderType.MatchLevel {
2429
return if (shaderAnalysis.signatureMatches(ContentType.UvCoordinate, ContentType.UvCoordinate))
2530
ShaderType.MatchLevel.MatchAndFilter

src/commonMain/kotlin/baaahs/gl/shader/type/FilterShader.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import baaahs.app.ui.CommonIcons
44
import baaahs.gl.glsl.ShaderAnalysis
55
import baaahs.gl.patch.ContentType
66
import baaahs.gl.preview.PreviewShaders
7+
import baaahs.gl.preview.QuadPreviewBootstrapper
8+
import baaahs.gl.preview.ShaderPreviewBootstrapper
79
import baaahs.gl.shader.OpenShader
810
import baaahs.ui.Icon
911

@@ -20,6 +22,9 @@ object FilterShader : ShaderType {
2022
}
2123
""".trimIndent()
2224

25+
override val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
26+
get() = QuadPreviewBootstrapper
27+
2328
override fun matches(shaderAnalysis: ShaderAnalysis): ShaderType.MatchLevel {
2429
return if (shaderAnalysis.signatureMatches(ContentType.Color, ContentType.Color))
2530
ShaderType.MatchLevel.MatchAndFilter

src/commonMain/kotlin/baaahs/gl/shader/type/MoverShader.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package baaahs.gl.shader.type
33
import baaahs.app.ui.CommonIcons
44
import baaahs.gl.glsl.ShaderAnalysis
55
import baaahs.gl.patch.ContentType
6+
import baaahs.gl.preview.MovingHeadPreviewBootstrapper
67
import baaahs.gl.preview.PreviewShaders
8+
import baaahs.gl.preview.ShaderPreviewBootstrapper
79
import baaahs.gl.shader.OpenShader
810
import baaahs.plugin.core.MovingHeadParams
911
import baaahs.ui.Icon
@@ -30,6 +32,9 @@ object MoverShader : ShaderType {
3032
}
3133
""".trimIndent()
3234

35+
override val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
36+
get() = MovingHeadPreviewBootstrapper
37+
3338
override fun matches(shaderAnalysis: ShaderAnalysis): ShaderType.MatchLevel {
3439
return if (shaderAnalysis.outputIs(MovingHeadParams.contentType))
3540
ShaderType.MatchLevel.Match

src/commonMain/kotlin/baaahs/gl/shader/type/PaintShader.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import baaahs.app.ui.CommonIcons
44
import baaahs.gl.glsl.ShaderAnalysis
55
import baaahs.gl.patch.ContentType
66
import baaahs.gl.preview.PreviewShaders
7+
import baaahs.gl.preview.QuadPreviewBootstrapper
8+
import baaahs.gl.preview.ShaderPreviewBootstrapper
79
import baaahs.gl.shader.OpenShader
810
import baaahs.ui.Icon
911

@@ -20,6 +22,9 @@ object PaintShader : ShaderType {
2022
}
2123
""".trimIndent()
2224

25+
override val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
26+
get() = QuadPreviewBootstrapper
27+
2328
override fun matches(shaderAnalysis: ShaderAnalysis): ShaderType.MatchLevel {
2429
return if (shaderAnalysis.outputIs(ContentType.Color))
2530
ShaderType.MatchLevel.Match

src/commonMain/kotlin/baaahs/gl/shader/type/ProjectionShader.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import baaahs.app.ui.CommonIcons
44
import baaahs.gl.glsl.ShaderAnalysis
55
import baaahs.gl.patch.ContentType
66
import baaahs.gl.preview.PreviewShaders
7+
import baaahs.gl.preview.ProjectionPreviewBootstrapper
8+
import baaahs.gl.preview.ShaderPreviewBootstrapper
79
import baaahs.gl.shader.OpenShader
810
import baaahs.ui.Icon
911

@@ -30,6 +32,9 @@ object ProjectionShader : ShaderType {
3032

3133
override val injectUvCoordinateForPreview: Boolean get() = false
3234

35+
override val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
36+
get() = ProjectionPreviewBootstrapper
37+
3338
override fun matches(shaderAnalysis: ShaderAnalysis): ShaderType.MatchLevel {
3439
return if (shaderAnalysis.signatureMatches(ContentType.XyzCoordinate, ContentType.UvCoordinate))
3540
ShaderType.MatchLevel.Match

src/commonMain/kotlin/baaahs/gl/shader/type/ShaderType.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import baaahs.app.ui.CommonIcons
44
import baaahs.gl.glsl.ShaderAnalysis
55
import baaahs.gl.patch.ContentType
66
import baaahs.gl.preview.PreviewShaders
7+
import baaahs.gl.preview.QuadPreviewBootstrapper
8+
import baaahs.gl.preview.ShaderPreviewBootstrapper
79
import baaahs.gl.shader.OpenShader
810
import baaahs.show.mutable.MutableShader
911
import baaahs.ui.Icon
@@ -13,6 +15,7 @@ interface ShaderType {
1315
val icon: Icon
1416
val template: String
1517
val injectUvCoordinateForPreview: Boolean get() = true
18+
val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
1619

1720
fun newShaderFromTemplate(): MutableShader {
1821
return MutableShader("Untitled $title Shader", template)
@@ -38,6 +41,8 @@ interface ShaderType {
3841
override val title: String get() = "Unknown"
3942
override val icon: Icon get() = CommonIcons.UnknownShader
4043
override val template: String get() = error("n/a")
44+
override val shaderPreviewBootstrapper: ShaderPreviewBootstrapper
45+
get() = QuadPreviewBootstrapper // TODO: Something else, what?
4146

4247
override fun matches(shaderAnalysis: ShaderAnalysis): MatchLevel = MatchLevel.NoMatch
4348

src/jsMain/js/simulator/MosaicApp.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import {hot} from 'react-hot-loader';
21
import styles from './MosaicUI.scss';
32
import React, {useContext, useEffect, useState} from 'react';
43
import {Mosaic, MosaicWindow, MosaicZeroState} from 'react-mosaic-component';
@@ -114,4 +113,4 @@ const MosaicUI = (props) => {
114113
);
115114
};
116115

117-
export default hot(module)(MosaicApp);
116+
export default MosaicApp;

src/jsMain/kotlin/baaahs/app/ui/ShaderPreview.kt

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package baaahs.app.ui
22

33
import baaahs.document
4-
import baaahs.gl.GlBase
54
import baaahs.gl.GlContext
65
import baaahs.gl.Toolchain
76
import baaahs.gl.openShader
87
import baaahs.gl.preview.GadgetAdjuster
98
import baaahs.gl.preview.PreviewShaderBuilder
109
import baaahs.gl.preview.ShaderBuilder
11-
import baaahs.gl.render.ProjectionPreview
12-
import baaahs.gl.render.QuadPreview
13-
import baaahs.gl.render.ShaderPreview
14-
import baaahs.gl.shader.type.ProjectionShader
10+
import baaahs.gl.preview.ShaderPreview
1511
import baaahs.jsx.useResizeListener
1612
import baaahs.show.Shader
1713
import baaahs.ui.addObserver
@@ -29,6 +25,7 @@ import materialui.components.typography.typography
2925
import materialui.icon
3026
import materialui.icons.Icons
3127
import org.w3c.dom.HTMLCanvasElement
28+
import org.w3c.dom.HTMLDivElement
3229
import org.w3c.dom.events.EventTarget
3330
import react.*
3431
import react.dom.*
@@ -41,39 +38,40 @@ val ShaderPreview = xComponent<ShaderPreviewProps>("ShaderPreview") { props ->
4138
val appContext = useContext(appContext)
4239
val toolchain = props.toolchain ?: appContext.toolchain
4340

44-
val canvas = ref<HTMLCanvasElement?> { null }
45-
var gl by state<GlContext?> { null }
41+
val canvasParent = ref<HTMLDivElement?> { null }
4642
var shaderPreview by state<ShaderPreview?> { null }
4743
var errorPopupAnchor by state<EventTarget?> { null }
4844
val preRenderHook = ref { {} }
4945

5046
val width = props.width ?: 150.px
5147
val height = props.height ?: 150.px
5248

53-
onMount(canvas.current) {
54-
val canvasEl = canvas.current ?: return@onMount
49+
val canvas = memo(props.previewShaderBuilder?.openShader?.shaderType) {
50+
document.createElement("canvas").apply {
51+
this.setAttribute("width", width.toString())
52+
this.setAttribute("height", height.toString())
53+
} as HTMLCanvasElement
54+
}
55+
var gl by state<GlContext?> { null }
56+
57+
onMount(canvasParent.current, canvas) {
58+
canvasParent.current?.let { parent ->
59+
parent.insertBefore(canvas, parent.firstChild)
60+
}
61+
62+
withCleanup { canvasParent.current?.removeChild(canvas) }
63+
}
5564

65+
onChange("shader type", canvas, props.previewShaderBuilder?.openShader?.shaderType) {
5666
val shaderType = props.previewShaderBuilder?.openShader?.shaderType ?: run {
5767
// TODO: This is duplicating work that happens later in PreviewShaderBuilder, which is rotten.
5868
toolchain.openShader(props.shader!!).shaderType
5969
}
6070

61-
val preview = if (shaderType == ProjectionShader) {
62-
val canvas2d = canvasEl
63-
val canvas3d = document.createElement("canvas") as HTMLCanvasElement
64-
val glslContext = GlBase.jsManager.createContext(canvas3d)
65-
gl = glslContext
66-
67-
ProjectionPreview(canvas2d, glslContext, canvasEl.width, canvasEl.height, appContext.webClient.model) {
68-
preRenderHook.current()
69-
}
70-
} else {
71-
val glslContext = GlBase.jsManager.createContext(canvasEl)
72-
gl = glslContext
73-
QuadPreview(glslContext, canvasEl.width, canvasEl.height) {
74-
preRenderHook.current()
75-
}
76-
}
71+
val preview = shaderType.shaderPreviewBootstrapper.bootstrap(
72+
canvas, appContext.webClient.model, preRenderHook
73+
)
74+
gl = preview.renderEngine.gl
7775

7876
val intersectionObserver = IntersectionObserver { entries ->
7977
if (entries.any { it.isIntersecting }) {
@@ -82,7 +80,7 @@ val ShaderPreview = xComponent<ShaderPreviewProps>("ShaderPreview") { props ->
8280
preview.stop()
8381
}
8482
}
85-
intersectionObserver.observe(canvasEl)
83+
intersectionObserver.observe(canvas)
8684

8785
shaderPreview = preview
8886

@@ -132,22 +130,20 @@ val ShaderPreview = xComponent<ShaderPreviewProps>("ShaderPreview") { props ->
132130
}
133131
}
134132

135-
useResizeListener(canvas) {
133+
useResizeListener(canvasParent) {
136134
// Tell Kotlin controller the window was resized
137-
shaderPreview?.resize(canvas.current!!.width, canvas.current!!.height)
135+
shaderPreview?.resize(
136+
canvasParent.current!!.clientWidth,
137+
canvasParent.current!!.clientHeight
138+
)
138139
}
139140

140141
styledDiv {
142+
ref = canvasParent
141143
css { +ShaderPreviewStyles.container }
142144
css.width = width
143145
css.height = height
144146

145-
canvas {
146-
ref = canvas
147-
attrs.width = width.toString()
148-
attrs.height = height.toString()
149-
}
150-
151147
when (builder?.state ?: ShaderBuilder.State.Unbuilt) {
152148
ShaderBuilder.State.Unbuilt,
153149
ShaderBuilder.State.Analyzing,

0 commit comments

Comments
 (0)