@@ -6,8 +6,6 @@ import de.fabmax.kool.math.Vec3i
66import de.fabmax.kool.modules.ui2.Ui2Shader
77import de.fabmax.kool.modules.ui2.UiNode
88import de.fabmax.kool.modules.ui2.UiRenderer
9- import de.fabmax.kool.pipeline.ComputePass
10- import de.fabmax.kool.pipeline.ComputeShader
119import de.fabmax.kool.pipeline.GpuType
1210import de.fabmax.kool.pipeline.StorageBuffer
1311import de.fabmax.kool.scene.Mesh
@@ -17,16 +15,14 @@ import de.fabmax.kool.scene.geometry.MeshBuilder
1715import de.fabmax.kool.scene.geometry.Usage
1816import de.fabmax.kool.util.Color
1917import de.fabmax.kool.util.Float32Buffer
20- import de.fabmax.kool.util.launchOnMainThread
21- import kotlinx.coroutines.CompletableDeferred
2218import kotlinx.coroutines.Deferred
2319import me.dvyy.particles.compute.forces.ForceWithParameters
2420import me.dvyy.particles.compute.forces.PairwiseForce
2521import me.dvyy.particles.compute.partitioning.WORK_GROUP_SIZE
2622
27- class LineGraphNode : UiRenderer <UiNode > {
23+ class GraphNode : UiRenderer <UiNode > {
2824 private val graphMesh: Mesh
29- private val graphGeom = IndexedVertexList (Ui2Shader .Companion . UI_MESH_ATTRIBS )
25+ private val graphGeom = IndexedVertexList (Ui2Shader .UI_MESH_ATTRIBS )
3026 private val graphBuilder = MeshBuilder (graphGeom).apply { isInvertFaceOrientation = true }
3127 private val clipBounds = MutableVec4f ()
3228 private var width = 0
@@ -35,6 +31,7 @@ class LineGraphNode : UiRenderer<UiNode> {
3531 private var valuesY = floatArrayOf()
3632 private var viewport = MutableVec4f (- 0.1f , - 0.1f , 1f , 1f ) // graph -> ui node positions
3733 private var updated = true
34+ var style: GraphStyle = GraphStyle .Line (Color .WHITE )
3835
3936 fun Vec2f.toUi (): Vec2f {
4037 val vWidth = viewport.z - viewport.x
@@ -119,72 +116,65 @@ class LineGraphNode : UiRenderer<UiNode> {
119116// })
120117// }
121118// }
122- if (node.clipBoundsPx != clipBounds || updated) {
123- node.surface.getMeshLayer(node.modifier.zLayer + 1 )
124- clipBounds.set(node.clipBoundsPx)
125- updated = false
126- println (" Ran render ui!" )
127- node.apply {
128- width = widthPx.toInt()
129- height = heightPx.toInt()
130- graphBuilder.clear()
131- // Render graph
132- if (valuesX.isEmpty() || valuesY.isEmpty()) return @apply
133- graphBuilder.configured(Color .Companion .WHITE ) {
134- var prev = Vec2f (valuesX[0 ], valuesY[0 ]).toUi()
135- for (i in 1 .. valuesX.lastIndex) {
136- val new = Vec2f (valuesX[i], valuesY[i]).toUi()
137- line(prev, new, 1f )
138- prev = new
119+ node.surface.getMeshLayer(node.modifier.zLayer - 1 ).addCustomLayer(" dt-graph" ) { graphMesh }
120+ if (node.clipBoundsPx == clipBounds && ! updated) return
121+ clipBounds.set(node.clipBoundsPx)
122+ updated = false
123+ println (" Ran render ui!" )
124+ node.apply {
125+ width = widthPx.toInt()
126+ height = heightPx.toInt()
127+ graphBuilder.clear()
128+ if (valuesX.isEmpty() || valuesY.isEmpty()) return @apply
129+ when (val style = style) {
130+ is GraphStyle .Line -> {
131+ graphBuilder.configured(style.color) {
132+ var prev = Vec2f (valuesX[0 ], valuesY[0 ]).toUi()
133+ for (i in 1 .. valuesX.lastIndex) {
134+ val new = Vec2f (valuesX[i], valuesY[i]).toUi()
135+ line(prev, new, 1f )
136+ prev = new
137+ }
139138 }
140139 }
141- // Render background grid
142- graphBuilder.configured(Color .Companion .GRAY ) {
143- // horizontal
144- line(Vec2f (0f , 0f ).toUi(), Vec2f (valuesX.max(), 0f ).toUi(), 1f )
145- line(Vec2f (0f , 0f ).toUi(), Vec2f (0f , valuesY.max()).toUi(), 1f )
140+
141+ is GraphStyle .Bar -> {
142+ graphBuilder.configured(Color .WHITE ) {
143+ val width = style.width.toFloat()
144+ valuesX.zip(valuesY).forEach { (x, y) ->
145+ val bottom = Vec2f (x, 0f ).toUi()// .plus(Vec2f(width /2, 0f))
146+ val top = Vec2f (x, y).toUi()// .plus(Vec2f(width /2, 0f))
147+ line(bottom, top, width)
148+ }
149+ }
146150 }
147151 }
148- }
149- node.surface.getMeshLayer(node.modifier.zLayer - 1 ).addCustomLayer(" dt-graph" ) { graphMesh }
150- }
151- }
152-
153- inline fun <T > execManyShaders (
154- scene : Scene ,
155- setup : (ComputePass ) -> Unit ,
156- crossinline read : suspend () -> T ,
157- ): Deferred <T > {
158- val computePass = ComputePass (" single-shot" )
159- setup(computePass)
160- scene.addComputePass(computePass)
161-
162- val deferred = CompletableDeferred <T >()
163-
164- computePass.onAfterPass {
165- computePass.isEnabled = false
166- launchOnMainThread {
167- try {
168- deferred.complete(read())
169- } catch (e: Exception ) {
170- deferred.completeExceptionally(e)
171- } finally {
172- scene.removeComputePass(computePass)
152+ // Render background grid
153+ graphBuilder.configured(Color .GRAY ) {
154+ // horizontal
155+ line(Vec2f (0f , 0f ).toUi(), Vec2f (valuesX.max(), 0f ).toUi(), 1f )
156+ line(Vec2f (0f , 0f ).toUi(), Vec2f (0f , valuesY.max()).toUi(), 1f )
173157 }
174158 }
175159 }
176-
177- return deferred
178-
179- }
180-
181- inline fun <T > execShader (
182- scene : Scene ,
183- shader : ComputeShader ,
184- numGroups : Vec3i = Vec3i .ONES ,
185- crossinline read : suspend () -> T ,
186- ): Deferred <T > {
187- return execManyShaders(scene, setup = {
188- it.addTask(shader, numGroups)
189- }, read)
160+ //
161+ // override fun renderUi(node: UiNode) {
162+ // node.apply {
163+ // getTextBuilder(sizes.normalText).configured(Color.WHITE) {
164+ // text(TextProps(sizes.normalText).apply {
165+ // text = "Hello world"; scale = 2f; isYAxisUp = false
166+ // this.origin.set(0f, 70f, 0f)
167+ // })
168+ // }
169+ // }
170+ // if (node.clipBoundsPx != clipBounds || updated) {
171+ // node.surface.getMeshLayer(node.modifier.zLayer + 1)
172+ // clipBounds.set(node.clipBoundsPx)
173+ // updated = false
174+ // println("Ran render ui!")
175+ // node.apply {
176+ // }
177+ // node.surface.getMeshLayer(node.modifier.zLayer - 1).addCustomLayer("dt-graph") { graphMesh }
178+ // }
179+ // }
190180}
0 commit comments