From 346b61724f0101b601b16d630a86ce4861754141 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 26 Dec 2020 17:11:52 +0300 Subject: [PATCH] Fix material generations for SolidReference --- .../kotlin/hep/dataforge/vision/VisionBase.kt | 26 +++++++++---------- .../dataforge/vision/solid/SolidReference.kt | 7 +++-- .../vision/solid/SolidReferenceTest.kt | 26 +++++++++++++++++++ .../vision/solid/three/ThreeMaterials.kt | 7 ++--- .../solid/three/ThreeReferenceFactory.kt | 3 +++ 5 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt diff --git a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionBase.kt b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionBase.kt index 899c2877..b5d03eb7 100644 --- a/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionBase.kt +++ b/visionforge-core/src/commonMain/kotlin/hep/dataforge/vision/VisionBase.kt @@ -131,8 +131,20 @@ public open class VisionBase(internal var properties: Config? = null) : Vision { } override fun update(change: VisionChange) { + change.properties?.let { + updateProperties(Name.EMPTY, it.asMetaItem()) + } + } - fun updateProperties(at: Name, item: MetaItem) { + public companion object { + public val descriptor: NodeDescriptor = NodeDescriptor { + value(STYLE_KEY) { + type(ValueType.STRING) + multiple = true + } + } + + public fun Vision.updateProperties(at: Name, item: MetaItem) { when (item) { is ValueItem -> { if (item.value == Null) { @@ -146,18 +158,6 @@ public open class VisionBase(internal var properties: Config? = null) : Vision { } } - change.properties?.let { - updateProperties(Name.EMPTY, it.asMetaItem()) - } - } - - public companion object { - public val descriptor: NodeDescriptor = NodeDescriptor { - value(STYLE_KEY) { - type(ValueType.STRING) - multiple = true - } - } } } diff --git a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt index 261f16d8..e49d8d0f 100644 --- a/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt +++ b/visionforge-solid/src/commonMain/kotlin/hep/dataforge/vision/solid/SolidReference.kt @@ -3,6 +3,7 @@ package hep.dataforge.vision.solid import hep.dataforge.meta.* import hep.dataforge.meta.descriptors.NodeDescriptor import hep.dataforge.names.* +import hep.dataforge.values.Null import hep.dataforge.vision.* import kotlinx.coroutines.CoroutineScope import kotlinx.serialization.SerialName @@ -99,7 +100,7 @@ public class SolidReferenceGroup( ReferenceChild(childName + key.asName()) } ?: emptyMap() - override val meta: Meta get() =TODO()// getChildProperty(childName, Name.EMPTY).node ?: Meta.EMPTY + override val meta: Meta get() = TODO()// getChildProperty(childName, Name.EMPTY).node ?: Meta.EMPTY override fun getOwnProperty(name: Name): MetaItem? = getChildProperty(childName, name) @@ -136,7 +137,9 @@ public class SolidReferenceGroup( } override fun update(change: VisionChange) { - TODO("Not yet implemented") + change.properties?.let { + updateProperties(Name.EMPTY, it.asMetaItem()) + } } override val descriptor: NodeDescriptor get() = prototype.descriptor diff --git a/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt new file mode 100644 index 00000000..8b3c85e0 --- /dev/null +++ b/visionforge-solid/src/commonTest/kotlin/hep/dataforge/vision/solid/SolidReferenceTest.kt @@ -0,0 +1,26 @@ +package hep.dataforge.vision.solid + +import hep.dataforge.vision.get +import hep.dataforge.vision.style +import hep.dataforge.vision.styles +import hep.dataforge.vision.useStyle +import kotlin.test.Test +import kotlin.test.assertEquals + +class SolidReferenceTest { + val groupWithReference = SolidGroup { + val referenceStyle by style { + SolidMaterial.MATERIAL_COLOR_KEY put "red" + } + ref("test", Box(100f,100f,100f).apply { + color("blue") + useStyle(referenceStyle) + }) + } + + + @Test + fun testReferenceProperty(){ + assertEquals("blue", (groupWithReference["test"] as Solid).color.string) + } +} \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeMaterials.kt b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeMaterials.kt index 2d459217..0403fdb9 100644 --- a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeMaterials.kt +++ b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeMaterials.kt @@ -8,7 +8,6 @@ import hep.dataforge.values.string import hep.dataforge.vision.Colors import hep.dataforge.vision.Vision import hep.dataforge.vision.allStyles -import hep.dataforge.vision.merge import hep.dataforge.vision.solid.SolidMaterial import info.laht.threekt.materials.LineBasicMaterial import info.laht.threekt.materials.Material @@ -143,6 +142,7 @@ public fun Mesh.updateMaterial(vision: Vision) { includeStyles = false, includeDefaults = false ) + material = when { ownMaterialMeta == null && stylesMaterialMeta == null && parentMaterialMeta == null -> { //use default is not material properties are defined @@ -153,8 +153,9 @@ public fun Mesh.updateMaterial(vision: Vision) { ThreeMaterials.cacheMeta(stylesMaterialMeta.node ?: Meta.EMPTY) } else -> { - val merge = sequenceOf(ownMaterialMeta, stylesMaterialMeta, parentMaterialMeta).merge().node ?: Meta.EMPTY - ThreeMaterials.buildMaterial(merge) + vision.getProperty(SolidMaterial.MATERIAL_KEY).node?.let { + ThreeMaterials.buildMaterial(it) + } ?: ThreeMaterials.DEFAULT } } } diff --git a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeReferenceFactory.kt b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeReferenceFactory.kt index 8ccbc37f..a1a98572 100644 --- a/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeReferenceFactory.kt +++ b/visionforge-threejs/src/main/kotlin/hep/dataforge/vision/solid/three/ThreeReferenceFactory.kt @@ -1,9 +1,11 @@ package hep.dataforge.vision.solid.three +import hep.dataforge.meta.node import hep.dataforge.names.cutFirst import hep.dataforge.names.firstOrNull import hep.dataforge.names.toName import hep.dataforge.vision.solid.Solid +import hep.dataforge.vision.solid.SolidMaterial import hep.dataforge.vision.solid.SolidReferenceGroup import hep.dataforge.vision.solid.SolidReferenceGroup.Companion.REFERENCE_CHILD_PROPERTY_PREFIX import info.laht.threekt.core.BufferGeometry @@ -40,6 +42,7 @@ public object ThreeReferenceFactory : ThreeFactory { object3D.updatePosition(obj) if(object3D is Mesh){ + //object3D.material = ThreeMaterials.buildMaterial(obj.getProperty(SolidMaterial.MATERIAL_KEY).node!!) object3D.applyProperties(obj) }