diff --git a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt index 200ab034..953474ca 100644 --- a/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt +++ b/dataforge-vis-spatial-gdml/src/commonMain/kotlin/hep/dataforge/vis/spatial/gdml/GDMLTransformer.kt @@ -2,7 +2,6 @@ package hep.dataforge.vis.spatial.gdml import hep.dataforge.meta.Meta import hep.dataforge.meta.buildMeta -import hep.dataforge.meta.builder import hep.dataforge.vis.spatial.VisualGroup3D import hep.dataforge.vis.spatial.VisualObject3D import hep.dataforge.vis.spatial.material @@ -30,9 +29,9 @@ class GDMLTransformer(val root: GDML) { var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT } - var transparent: GDMLVolume.(GDMLSolid?) -> Boolean = { !physVolumes.isEmpty() } + var configure: VisualObject3D.(parent: GDMLVolume, solid: GDMLSolid) -> Unit = { _, _ -> } - internal fun configureSolid(obj: VisualObject3D, parent: GDMLVolume, solid: GDMLSolid?) { + internal fun configureSolid(obj: VisualObject3D, parent: GDMLVolume, solid: GDMLSolid) { val material = parent.materialref.resolve(root) ?: GDMLElement(parent.materialref.ref) val materialColor = materialCache.getOrPut(material) { @@ -41,11 +40,8 @@ class GDMLTransformer(val root: GDML) { } } - obj.material = if (parent.transparent(solid)) { - materialColor.builder().apply { "opacity" to 0.5 } - } else { - materialColor - } + obj.material = materialColor + obj.configure(parent, solid) } fun printStatistics() { diff --git a/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt b/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt index 444961fc..bad2b014 100644 --- a/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt +++ b/dataforge-vis-spatial-gdml/src/jsMain/kotlin/hep/dataforge/vis/spatial/gdml/demo/GDMLDemoApp.kt @@ -6,6 +6,7 @@ import hep.dataforge.vis.hmr.startApplication import hep.dataforge.vis.spatial.gdml.GDMLTransformer import hep.dataforge.vis.spatial.gdml.LUnit import hep.dataforge.vis.spatial.gdml.toVisual +import hep.dataforge.vis.spatial.opacity import hep.dataforge.vis.spatial.three.ThreePlugin import hep.dataforge.vis.spatial.three.output import kotlinx.coroutines.CoroutineScope @@ -93,22 +94,41 @@ private class GDMLDemoApp : ApplicationBase() { lUnit = LUnit.CM volumeAction = { volume -> when { - volume.name.startsWith("ecal") -> GDMLTransformer.Action.REJECT + volume.name.startsWith("ecal01lay") -> GDMLTransformer.Action.REJECT + volume.name.startsWith("ecal") -> GDMLTransformer.Action.CACHE + volume.name.startsWith("UPBL") -> GDMLTransformer.Action.REJECT + volume.name.startsWith("USCL") -> GDMLTransformer.Action.REJECT volume.name.startsWith("U") -> GDMLTransformer.Action.CACHE + volume.name.startsWith("VPBL") -> GDMLTransformer.Action.REJECT + volume.name.startsWith("VSCL") -> GDMLTransformer.Action.REJECT volume.name.startsWith("V") -> GDMLTransformer.Action.CACHE else -> GDMLTransformer.Action.ACCEPT } } - transparent = { !physVolumes.isEmpty() || (it?.name?.startsWith("Coil") ?: false) } - + configure = { parent, solid -> + if (!parent.physVolumes.isEmpty()) { + opacity = 0.3 + } + if (solid.name.startsWith("Coil") + || solid.name.startsWith("Yoke") + || solid.name.startsWith("Magnet") + || solid.name.startsWith("Pole") + ) { + opacity = 0.3 + } + } } launch { message("Rendering") } val output = three.output(canvas) { - "axis" to { - "size" to 100 - } +// "axis" to { +// "size" to 100 +// } } + //make top layer visible + //output.camera.layers.disable(0) + output.camera.layers.enable(1) + output.render(visual) launch { message(null) diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt index 5cf750a0..98995f59 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualGroup3D.kt @@ -17,7 +17,7 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers @Serializable -class VisualGroup3D() : AbstractVisualGroup(), VisualObject3D { +class VisualGroup3D : AbstractVisualGroup(), VisualObject3D { /** * A container for templates visible inside this group */ diff --git a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt index f36a33d0..c61a9df2 100644 --- a/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt +++ b/dataforge-vis-spatial/src/commonMain/kotlin/hep/dataforge/vis/spatial/VisualObject3D.kt @@ -94,12 +94,20 @@ var VisualObject.material: Meta? get() = getProperty(MATERIAL_KEY).node set(value) = setProperty(MATERIAL_KEY, value) +var VisualObject.opacity: Double + get() = material?.get("opacity").double ?: 1.0 + set(value) { + material = (material?.builder() ?: MetaBuilder()).apply { + "opacity" to value + } + } + var VisualObject.visible: Boolean? get() = getProperty(VISIBLE_KEY).boolean set(value) = setProperty(VISIBLE_KEY, value) fun VisualObject.color(rgb: Int) { - material = buildMeta { "color" to rgb } + material = (material?.builder() ?: MetaBuilder()).apply { "color" to rgb } } fun VisualObject3D.material(builder: MetaBuilder.() -> Unit) { diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeFactory.kt b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeFactory.kt index 8d39a08f..bb5dc5b2 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeFactory.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeFactory.kt @@ -43,6 +43,13 @@ internal fun Object3D.updatePosition(obj: VisualObject3D) { updateMatrix() } +/** + * Count number of layers to the top object. Return 1 if this is top layer + */ +val VisualObject3D.layer: Int + get() = getProperty(MeshThreeFactory.LAYER_KEY).int ?: ((parent as? VisualObject3D)?.layer ?: 0 + 1) + + internal fun Mesh.updateFrom(obj: T) { matrixAutoUpdate = false @@ -61,8 +68,9 @@ internal fun Mesh.updateFrom(obj: T) { //set position for mesh updatePosition(obj) - obj.getProperty(MeshThreeFactory.LAYER_KEY).int?.let { - layers.set(it) + layers.enable(obj.layer) + children.forEach { + it.layers.enable(obj.layer) } } diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeOutput.kt b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeOutput.kt index 81de1a89..fb191900 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeOutput.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/hep/dataforge/vis/spatial/three/ThreeOutput.kt @@ -27,7 +27,7 @@ class ThreeOutput(val three: ThreePlugin, val meta: Meta = EmptyMeta) : Output Int = { element.clientWidth }) { val width by meta.number(computeWidth(element)).int diff --git a/dataforge-vis-spatial/src/jsMain/kotlin/info/laht/threekt/core/Layers.kt b/dataforge-vis-spatial/src/jsMain/kotlin/info/laht/threekt/core/Layers.kt index b33a302b..503878d1 100644 --- a/dataforge-vis-spatial/src/jsMain/kotlin/info/laht/threekt/core/Layers.kt +++ b/dataforge-vis-spatial/src/jsMain/kotlin/info/laht/threekt/core/Layers.kt @@ -71,5 +71,5 @@ external class Layers { * Returns true if this and the passed layers object are members of the same set of layers. * @param layers a Layers object */ - fun test(layers: Int) + fun test(layers: Layers): Boolean } \ No newline at end of file