Optimizations... optimizations

This commit is contained in:
Alexander Nozik 2022-08-14 20:28:47 +03:00
parent e2f281debe
commit 98bb935de5
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
7 changed files with 30 additions and 19 deletions

View File

@ -10,6 +10,6 @@ kotlin{
dependencies {
api(project(":visionforge-solid"))
implementation(npm("three", "0.137.5"))
implementation(npm("three", "0.143.0"))
implementation(npm("three-csg-ts", "3.1.10"))
}

View File

@ -212,7 +212,7 @@ public class ThreeCanvas(
}
//find first non-static parent in this object ancestry
private fun Object3D?.upTrace(): Object3D? = if (this?.name?.startsWith("@") == true) parent else this
private tailrec fun Object3D.upTrace(): Object3D? = if (!name.startsWith("@")) this else parent?.upTrace()
private fun pick(): Object3D? {
// update the picking ray with the camera and mouse position
@ -222,8 +222,8 @@ public class ThreeCanvas(
return root?.let { root ->
val intersects = raycaster.intersectObject(root, true)
//skip invisible objects
val obj = intersects.map { it.`object` }.firstOrNull { it.visible }
obj.upTrace()
val obj: Object3D? = intersects.map { it.`object` }.firstOrNull { it.visible }
obj?.upTrace()
}
}
@ -280,20 +280,22 @@ public class ThreeCanvas(
edgesName: String,
material: LineBasicMaterial = SELECTED_MATERIAL,
) {
if (userData[DO_NOT_HIGHLIGHT_TAG] == true) {
return
}
if (this is Mesh) {
val edges = getObjectByName(edgesName) ?: LineSegments(
if (isMesh(this)) {
val highlightMesh = getObjectByName(edgesName) ?: LineSegments(
EdgesGeometry(geometry),
material
).also {
it.name = edgesName
add(it)
}
edges.visible = highlight
highlightMesh.visible = highlight
} else {
children.filter { it.name != edgesName }.forEach {
children.filter { !it.name.startsWith("@") && it.name != edgesName }.forEach {
it.toggleHighlight(highlight, edgesName, material)
}
}

View File

@ -3,7 +3,6 @@ package space.kscience.visionforge.solid.three
import info.laht.threekt.core.BufferGeometry
import info.laht.threekt.core.Object3D
import info.laht.threekt.math.Euler
import info.laht.threekt.objects.Mesh
import space.kscience.dataforge.misc.Type
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.startsWith
@ -50,7 +49,7 @@ public fun Object3D.updatePosition(vision: Vision) {
// setRotationFromEuler( Euler(obj.rotationX, obj.rotationY, obj.rotationZ, obj.rotationOrder.name))
// }
setRotationFromEuler( Euler(vision.rotationX, vision.rotationY, vision.rotationZ, vision.rotationOrder.name))
setRotationFromEuler(Euler(vision.rotationX, vision.rotationY, vision.rotationZ, vision.rotationOrder.name))
scale.set(vision.scaleX, vision.scaleY, vision.scaleZ)
updateMatrix()
@ -62,7 +61,7 @@ public fun Object3D.updatePosition(vision: Vision) {
*/
public fun Object3D.updateProperty(source: Vision, propertyName: Name) {
// console.log("$source updated $propertyName with ${source.computeProperty(propertyName)}")
if (this is Mesh && propertyName.startsWith(MATERIAL_KEY)) {
if (isMesh(this) && propertyName.startsWith(MATERIAL_KEY)) {
updateMaterialProperty(source, propertyName)
} else if (
propertyName.startsWith(Solid.POSITION_KEY)

View File

@ -39,13 +39,13 @@ public object ThreeMaterials {
public val SELECTED_MATERIAL: LineBasicMaterial = LineBasicMaterial().apply {
color.set(Colors.ivory)
linewidth = 8.0
linewidth = 2.0
cached = true
}
public val HIGHLIGHT_MATERIAL: LineBasicMaterial = LineBasicMaterial().apply {
color.set(Colors.blue)
linewidth = 8.0
linewidth = 2.0
cached = true
}

View File

@ -17,6 +17,7 @@ import space.kscience.visionforge.solid.SolidMaterial
import space.kscience.visionforge.solid.layer
import space.kscience.visionforge.solid.three.ThreeMeshFactory.Companion.EDGES_ENABLED_KEY
import space.kscience.visionforge.solid.three.ThreeMeshFactory.Companion.EDGES_MATERIAL_KEY
import space.kscience.visionforge.solid.three.ThreeMeshFactory.Companion.EDGES_OBJECT_NAME
import kotlin.reflect.KClass
/**
@ -66,6 +67,7 @@ public abstract class ThreeMeshFactory<in T : Solid>(
public companion object {
public val EDGES_KEY: Name = "edges".asName()
internal const val EDGES_OBJECT_NAME: String = "@edges"
//public val WIREFRAME_KEY: Name = "wireframe".asName()
public val ENABLED_KEY: Name = "enabled".asName()
@ -93,7 +95,7 @@ internal fun Mesh.applyProperties(vision: Solid): Mesh = apply {
}
public fun Mesh.applyEdges(vision: Solid) {
val edges = children.find { it.name == "@edges" } as? LineSegments
val edges = children.find { it.name == EDGES_OBJECT_NAME } as? LineSegments
//inherited edges definition, enabled by default
if (vision.properties.getValue(EDGES_ENABLED_KEY, inherit = true)?.boolean != false) {
val bufferGeometry = geometry as? BufferGeometry ?: return
@ -104,7 +106,7 @@ public fun Mesh.applyEdges(vision: Solid) {
EdgesGeometry(bufferGeometry),
material
).apply {
name = "@edges"
name = EDGES_OBJECT_NAME
}
)
} else {

View File

@ -17,11 +17,10 @@ public object ThreeReferenceFactory : ThreeFactory<SolidReference> {
override val type: KClass<SolidReference> = SolidReference::class
private fun Object3D.replicate(): Object3D {
return when (this) {
is Mesh -> Mesh(geometry, material).also {
return when {
isMesh(this) -> Mesh(geometry, material).also {
it.applyMatrix4(matrix)
}
else -> clone(false)
}.also { obj: Object3D ->
obj.name = this.name
@ -40,7 +39,7 @@ public object ThreeReferenceFactory : ThreeFactory<SolidReference> {
val object3D: Object3D = cachedObject.replicate()
object3D.updatePosition(vision)
if (object3D is Mesh) {
if (isMesh(object3D)) {
//object3D.material = ThreeMaterials.buildMaterial(obj.getProperty(SolidMaterial.MATERIAL_KEY).node!!)
object3D.applyProperties(vision)
}

View File

@ -11,6 +11,7 @@ import info.laht.threekt.textures.Texture
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.meta.float
import space.kscience.dataforge.meta.get
import kotlin.contracts.contract
import kotlin.math.PI
public val Meta.vector: Vector3 get() = Vector3(this["x"].float ?: 0f, this["y"].float ?: 0f, this["z"].float ?: 0f)
@ -34,6 +35,14 @@ internal fun Any.dispose() {
public fun Layers.check(layer: Int): Boolean = (mask shr (layer) and 0x00000001) > 0
internal fun isMesh(object3D: Object3D): Boolean{
contract {
returns(true) implies (object3D is Mesh)
}
return object3D.asDynamic().isMesh as? Boolean ?: false
}
internal fun Object3D.takeIfMesh(): Mesh? {
val d = asDynamic()
return if(d.isMesh as Boolean){