BMN rendering optimization

This commit is contained in:
Alexander Nozik 2019-09-15 19:09:08 +03:00
parent e5a27ab56f
commit e9d73b28fe
7 changed files with 52 additions and 20 deletions

View File

@ -2,7 +2,6 @@ package hep.dataforge.vis.spatial.gdml
import hep.dataforge.meta.Meta import hep.dataforge.meta.Meta
import hep.dataforge.meta.buildMeta import hep.dataforge.meta.buildMeta
import hep.dataforge.meta.builder
import hep.dataforge.vis.spatial.VisualGroup3D import hep.dataforge.vis.spatial.VisualGroup3D
import hep.dataforge.vis.spatial.VisualObject3D import hep.dataforge.vis.spatial.VisualObject3D
import hep.dataforge.vis.spatial.material import hep.dataforge.vis.spatial.material
@ -30,9 +29,9 @@ class GDMLTransformer(val root: GDML) {
var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT } 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 material = parent.materialref.resolve(root) ?: GDMLElement(parent.materialref.ref)
val materialColor = materialCache.getOrPut(material) { val materialColor = materialCache.getOrPut(material) {
@ -41,11 +40,8 @@ class GDMLTransformer(val root: GDML) {
} }
} }
obj.material = if (parent.transparent(solid)) { obj.material = materialColor
materialColor.builder().apply { "opacity" to 0.5 } obj.configure(parent, solid)
} else {
materialColor
}
} }
fun printStatistics() { fun printStatistics() {

View File

@ -6,6 +6,7 @@ import hep.dataforge.vis.hmr.startApplication
import hep.dataforge.vis.spatial.gdml.GDMLTransformer import hep.dataforge.vis.spatial.gdml.GDMLTransformer
import hep.dataforge.vis.spatial.gdml.LUnit import hep.dataforge.vis.spatial.gdml.LUnit
import hep.dataforge.vis.spatial.gdml.toVisual 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.ThreePlugin
import hep.dataforge.vis.spatial.three.output import hep.dataforge.vis.spatial.three.output
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -93,22 +94,41 @@ private class GDMLDemoApp : ApplicationBase() {
lUnit = LUnit.CM lUnit = LUnit.CM
volumeAction = { volume -> volumeAction = { volume ->
when { 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("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 volume.name.startsWith("V") -> GDMLTransformer.Action.CACHE
else -> GDMLTransformer.Action.ACCEPT 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") } launch { message("Rendering") }
val output = three.output(canvas) { val output = three.output(canvas) {
"axis" to { // "axis" to {
"size" to 100 // "size" to 100
} // }
} }
//make top layer visible
//output.camera.layers.disable(0)
output.camera.layers.enable(1)
output.render(visual) output.render(visual)
launch { launch {
message(null) message(null)

View File

@ -17,7 +17,7 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.UseSerializers import kotlinx.serialization.UseSerializers
@Serializable @Serializable
class VisualGroup3D() : AbstractVisualGroup(), VisualObject3D { class VisualGroup3D : AbstractVisualGroup(), VisualObject3D {
/** /**
* A container for templates visible inside this group * A container for templates visible inside this group
*/ */

View File

@ -94,12 +94,20 @@ var VisualObject.material: Meta?
get() = getProperty(MATERIAL_KEY).node get() = getProperty(MATERIAL_KEY).node
set(value) = setProperty(MATERIAL_KEY, value) 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? var VisualObject.visible: Boolean?
get() = getProperty(VISIBLE_KEY).boolean get() = getProperty(VISIBLE_KEY).boolean
set(value) = setProperty(VISIBLE_KEY, value) set(value) = setProperty(VISIBLE_KEY, value)
fun VisualObject.color(rgb: Int) { fun VisualObject.color(rgb: Int) {
material = buildMeta { "color" to rgb } material = (material?.builder() ?: MetaBuilder()).apply { "color" to rgb }
} }
fun VisualObject3D.material(builder: MetaBuilder.() -> Unit) { fun VisualObject3D.material(builder: MetaBuilder.() -> Unit) {

View File

@ -43,6 +43,13 @@ internal fun Object3D.updatePosition(obj: VisualObject3D) {
updateMatrix() 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 <T : VisualObject3D> Mesh.updateFrom(obj: T) { internal fun <T : VisualObject3D> Mesh.updateFrom(obj: T) {
matrixAutoUpdate = false matrixAutoUpdate = false
@ -61,8 +68,9 @@ internal fun <T : VisualObject3D> Mesh.updateFrom(obj: T) {
//set position for mesh //set position for mesh
updatePosition(obj) updatePosition(obj)
obj.getProperty(MeshThreeFactory.LAYER_KEY).int?.let { layers.enable(obj.layer)
layers.set(it) children.forEach {
it.layers.enable(obj.layer)
} }
} }

View File

@ -27,7 +27,7 @@ class ThreeOutput(val three: ThreePlugin, val meta: Meta = EmptyMeta) : Output<V
} }
} }
private val camera = three.buildCamera(meta["camera"].node ?: EmptyMeta) val camera = three.buildCamera(meta["camera"].node ?: EmptyMeta)
fun attach(element: Element, computeWidth: Element.() -> Int = { element.clientWidth }) { fun attach(element: Element, computeWidth: Element.() -> Int = { element.clientWidth }) {
val width by meta.number(computeWidth(element)).int val width by meta.number(computeWidth(element)).int

View File

@ -71,5 +71,5 @@ external class Layers {
* Returns true if this and the passed layers object are members of the same set of layers. * Returns true if this and the passed layers object are members of the same set of layers.
* @param layers a Layers object * @param layers a Layers object
*/ */
fun test(layers: Int) fun test(layers: Layers): Boolean
} }