forked from kscience/visionforge
Transparency rules for gdml
This commit is contained in:
parent
8f1dcac6cd
commit
39b70a6064
@ -4,10 +4,9 @@ import hep.dataforge.meta.Meta
|
||||
import hep.dataforge.meta.buildMeta
|
||||
import hep.dataforge.meta.builder
|
||||
import hep.dataforge.vis.spatial.VisualGroup3D
|
||||
import scientifik.gdml.GDML
|
||||
import scientifik.gdml.GDMLGroup
|
||||
import scientifik.gdml.GDMLMaterial
|
||||
import scientifik.gdml.GDMLSolid
|
||||
import hep.dataforge.vis.spatial.VisualObject3D
|
||||
import hep.dataforge.vis.spatial.material
|
||||
import scientifik.gdml.*
|
||||
import kotlin.random.Random
|
||||
|
||||
class GDMLTransformer(val root: GDML) {
|
||||
@ -26,23 +25,29 @@ class GDMLTransformer(val root: GDML) {
|
||||
val templates by lazy { VisualGroup3D() }
|
||||
|
||||
var lUnit: LUnit = LUnit.MM
|
||||
var resolveColor: ColorResolver = { material, _ ->
|
||||
|
||||
var solidAction: (GDMLSolid) -> Action = { Action.CACHE }
|
||||
var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT }
|
||||
|
||||
|
||||
var transparent: GDMLVolume.(GDMLSolid?) -> Boolean = { !physVolumes.isEmpty() }
|
||||
|
||||
internal fun configureSolid(obj: VisualObject3D, parent: GDMLVolume, solid: GDMLSolid?) {
|
||||
val material = parent.materialref.resolve(root) ?: GDMLElement(parent.materialref.ref)
|
||||
|
||||
val materialColor = materialCache.getOrPut(material) {
|
||||
buildMeta {
|
||||
"color" to random.nextInt(0, Int.MAX_VALUE)
|
||||
}
|
||||
}
|
||||
|
||||
if (this?.physVolumes?.isEmpty() != false) {
|
||||
materialColor
|
||||
} else {
|
||||
obj.material = if (parent.transparent(solid)) {
|
||||
materialColor.builder().apply { "opacity" to 0.5 }
|
||||
} else {
|
||||
materialColor
|
||||
}
|
||||
}
|
||||
|
||||
var solidAction: (GDMLSolid) -> Action = { Action.CACHE }
|
||||
var volumeAction: (GDMLGroup) -> Action = { Action.ACCEPT }
|
||||
|
||||
fun printStatistics() {
|
||||
println("Solids:")
|
||||
solidCounter.entries.sortedByDescending { it.value }.forEach {
|
||||
|
@ -1,6 +1,5 @@
|
||||
package hep.dataforge.vis.spatial.gdml
|
||||
|
||||
import hep.dataforge.meta.Meta
|
||||
import hep.dataforge.names.EmptyName
|
||||
import hep.dataforge.names.plus
|
||||
import hep.dataforge.vis.common.asName
|
||||
@ -38,6 +37,7 @@ private fun VisualObject3D.withPosition(
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline operator fun Number.times(d: Double) = toDouble() * d
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline operator fun Number.times(f: Float) = toFloat() * f
|
||||
|
||||
@ -211,18 +211,18 @@ private fun volume(
|
||||
if (group is GDMLVolume) {
|
||||
val solid = group.solidref.resolve(context.root)
|
||||
?: error("Solid with tag ${group.solidref.ref} for volume ${group.name} not defined")
|
||||
val material = group.materialref.resolve(context.root) ?: GDMLElement(group.materialref.ref)
|
||||
//val material = group.materialref.resolve(context.root) ?: GDMLElement(group.materialref.ref)
|
||||
|
||||
when (context.solidAction(solid)) {
|
||||
GDMLTransformer.Action.ACCEPT -> {
|
||||
addSolid(context, solid, solid.name) {
|
||||
this.material = context.resolveColor(group, material, solid)
|
||||
context.configureSolid(this, group, solid)
|
||||
}
|
||||
}
|
||||
GDMLTransformer.Action.CACHE -> {
|
||||
if (context.templates.get(solid.name) == null) {
|
||||
if (context.templates[solid.name] == null) {
|
||||
context.templates.addSolid(context, solid, solid.name) {
|
||||
this.material = context.resolveColor(group, material, solid)
|
||||
context.configureSolid(this, group, solid)
|
||||
}
|
||||
}
|
||||
ref(solid.name.asName(), solid.name)
|
||||
@ -244,9 +244,6 @@ private fun volume(
|
||||
}
|
||||
}
|
||||
|
||||
typealias ColorResolver = GDMLGroup?.(GDMLMaterial, GDMLSolid?) -> Meta
|
||||
|
||||
|
||||
fun GDML.toVisual(block: GDMLTransformer.() -> Unit = {}): VisualGroup3D {
|
||||
|
||||
val context = GDMLTransformer(this).apply(block)
|
||||
|
@ -100,6 +100,8 @@ private class GDMLDemoApp : ApplicationBase() {
|
||||
}
|
||||
}
|
||||
|
||||
transparent = { !physVolumes.isEmpty() || (it?.name?.startsWith("Coil") ?: false) }
|
||||
|
||||
}
|
||||
launch { message("Rendering") }
|
||||
val output = three.output(canvas) {
|
||||
|
Loading…
Reference in New Issue
Block a user