From 199cad1dc162b087d4867806b8c62f6368f09f27 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Sat, 10 Jul 2021 16:51:57 +0300 Subject: [PATCH] Refactor to ThreeJS v 130 --- .../ru/mipt/npm/muon/monitor/Monitor.kt | 14 +-- .../ru/mipt/npm/muon/monitor/sim/Pixel.kt | 20 ++-- .../ru/mipt/npm/muon/monitor/sim/line.kt | 2 +- .../ru/mipt/npm/muon/monitor/sim/monitor.kt | 8 +- .../mipt/npm/muon/monitor/sim/simulation.kt | 12 +- .../playground/src/jvmMain/kotlin/gdmlIaxo.kt | 3 +- .../visionforge/solid/demo/VariableBox.kt | 4 +- .../visionforge-gdml-jupyter/build.gradle.kts | 16 ++- settings.gradle.kts | 2 - .../space/kscience/visionforge/VisionGroup.kt | 1 + .../kscience/visionforge/solid/FXCanvas3D.kt | 42 +++---- .../visionforge/solid/FXConvexFactory.kt | 5 +- .../visionforge/solid/OrbitControls.kt | 53 +++++---- .../visionforge/gdml/GdmlTransformerEnv.kt | 12 +- .../space/kscience/visionforge/solid/Solid.kt | 18 +-- .../kscience/visionforge/solid/geometry.kt | 45 ++++++-- visionforge-threejs/build.gradle.kts | 4 +- .../info/laht/threekt/core/BufferAttribute.kt | 34 ++++++ .../info/laht/threekt/core/BufferGeometry.kt | 6 +- .../info/laht/threekt/core/DirectGeometry.kt | 48 -------- .../kotlin/info/laht/threekt/core/Geometry.kt | 109 ------------------ .../laht/threekt/geometries/BoxGeometry.kt | 11 -- .../laht/threekt/geometries/ConeGeometry.kt | 12 +- .../threekt/geometries/CylinderGeometry.kt | 12 -- .../laht/threekt/geometries/EdgesGeometry.kt | 5 +- ...ExtrudedGeometry.kt => ExtrudeGeometry.kt} | 13 --- .../laht/threekt/geometries/PlaneGeometry.kt | 10 -- .../laht/threekt/geometries/SphereGeometry.kt | 11 -- ...{TextBufferGeometry.kt => TextGeometry.kt} | 7 +- .../laht/threekt/geometries/TorusGeometry.kt | 10 +- .../laht/threekt/geometries/TubeGeometry.kt | 21 +--- .../threekt/geometries/WireframeGeometry.kt | 8 +- .../info/laht/threekt/objects/LineSegments.kt | 2 - .../kotlin/info/laht/threekt/objects/Mesh.kt | 10 +- .../solid/three/MeshThreeFactory.kt | 3 - .../solid/three/ThreeBoxFactory.kt | 8 +- .../solid/three/ThreeCanvasLabelFactory.kt | 4 +- .../solid/three/ThreeCompositeFactory.kt | 3 +- .../solid/three/ThreeConeFactory.kt | 6 +- .../visionforge/solid/three/ThreeFactory.kt | 8 +- .../solid/three/ThreeGeometryBuilder.kt | 63 +++++----- .../solid/three/ThreeLineFactory.kt | 6 +- .../solid/three/ThreeSphereFactory.kt | 6 +- .../kscience/visionforge/solid/three/csg.kt | 84 +++++++------- .../kscience/visionforge/solid/three/three.kt | 44 +------ 45 files changed, 304 insertions(+), 521 deletions(-) delete mode 100644 visionforge-threejs/src/main/kotlin/info/laht/threekt/core/DirectGeometry.kt delete mode 100644 visionforge-threejs/src/main/kotlin/info/laht/threekt/core/Geometry.kt rename visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/{ExtrudedGeometry.kt => ExtrudeGeometry.kt} (83%) rename visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/{TextBufferGeometry.kt => TextGeometry.kt} (83%) diff --git a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt index e87a2abc..17d5ac86 100644 --- a/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt +++ b/demo/muon-monitor/src/commonMain/kotlin/ru/mipt/npm/muon/monitor/Monitor.kt @@ -11,7 +11,7 @@ import space.kscience.visionforge.solid.plus class SC1( val name: String, val center: Point3D, - val xSize: Double = PIXEL_XY_SIZE, val ySize: Double = PIXEL_XY_SIZE, val zSize: Double = PIXEL_Z_SIZE + val xSize: Float = PIXEL_XY_SIZE, val ySize: Float = PIXEL_XY_SIZE, val zSize: Float = PIXEL_Z_SIZE ) class SC16( @@ -121,12 +121,12 @@ internal expect fun readMonitorConfig(): String object Monitor { const val GEOMETRY_TOLERANCE = 0.01 - const val PIXEL_XY_SIZE = 122.0 - const val PIXEL_XY_SPACING = 123.2 - const val PIXEL_Z_SIZE = 30.0 - const val CENTRAL_LAYER_Z = 0.0 - const val UPPER_LAYER_Z = -166.0 - const val LOWER_LAYER_Z = 180.0 + const val PIXEL_XY_SIZE = 122.0f + const val PIXEL_XY_SPACING = 123.2f + const val PIXEL_Z_SIZE = 30.0f + const val CENTRAL_LAYER_Z = 0.0f + const val UPPER_LAYER_Z = -166.0f + const val LOWER_LAYER_Z = 180.0f /** * Build map for the whole monitor diff --git a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/Pixel.kt b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/Pixel.kt index 71a96647..579bca15 100644 --- a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/Pixel.kt +++ b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/Pixel.kt @@ -17,19 +17,19 @@ import kotlin.random.Random internal class SC1Aux(val sc: SC1, var efficiency: Double = 1.0) { // val layer: Layer = findLayer(center.z); private val upLayer = - findLayer(sc.center.z + sc.zSize / 2.0)//Layer("${name}_up", center.z + zSize / 2.0); + findLayer(sc.center.z + sc.zSize / 2f)//Layer("${name}_up", center.z + zSize / 2.0); private val bottomLayer = - findLayer(sc.center.z - sc.zSize / 2.0)//Layer("${name}_bottom", center.z - zSize / 2.0); + findLayer(sc.center.z - sc.zSize / 2f)//Layer("${name}_bottom", center.z - zSize / 2.0); private val centralLayer = findLayer(sc.center.z) - private val center = Vector3D(sc.center.x, sc.center.y, sc.center.z) + private val center = Vector3D(sc.center.x.toDouble(), sc.center.y.toDouble(), sc.center.z.toDouble()) private val sideLayers: Array = arrayOf( - Plane(center.add(Vector3D(PIXEL_XY_SIZE / 2, 0.0, 0.0)), Vector3D(1.0, 0.0, 0.0), GEOMETRY_TOLERANCE), - Plane(center.add(Vector3D(-PIXEL_XY_SIZE / 2, 0.0, 0.0)), Vector3D(-1.0, 0.0, 0.0), GEOMETRY_TOLERANCE), - Plane(center.add(Vector3D(0.0, PIXEL_XY_SIZE / 2, 0.0)), Vector3D(0.0, 1.0, 0.0), GEOMETRY_TOLERANCE), - Plane(center.add(Vector3D(0.0, -PIXEL_XY_SIZE / 2, 0.0)), Vector3D(0.0, -1.0, 0.0), GEOMETRY_TOLERANCE) - ); + Plane(center.add(Vector3D(PIXEL_XY_SIZE / 2.0, 0.0, 0.0)), Vector3D(1.0, 0.0, 0.0), GEOMETRY_TOLERANCE), + Plane(center.add(Vector3D(-PIXEL_XY_SIZE / 2.0, 0.0, 0.0)), Vector3D(-1.0, 0.0, 0.0), GEOMETRY_TOLERANCE), + Plane(center.add(Vector3D(0.0, PIXEL_XY_SIZE / 2.0, 0.0)), Vector3D(0.0, 1.0, 0.0), GEOMETRY_TOLERANCE), + Plane(center.add(Vector3D(0.0, -PIXEL_XY_SIZE / 2.0, 0.0)), Vector3D(0.0, -1.0, 0.0), GEOMETRY_TOLERANCE) + ) //TODO add efficiency private fun containsPoint(x: Double, y: Double, z: Double, tolerance: Double = GEOMETRY_TOLERANCE): Boolean { @@ -63,8 +63,8 @@ internal class SC1Aux(val sc: SC1, var efficiency: Double = 1.0) { * The layer number from up to bottom */ fun getLayerNumber(): Int { - return when (this.center.z) { - UPPER_LAYER_Z -> 1; + return when (this.center.z.toFloat()) { + UPPER_LAYER_Z -> 1 CENTRAL_LAYER_Z -> 2; LOWER_LAYER_Z -> 3; else -> throw RuntimeException("Unknown layer"); diff --git a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/line.kt b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/line.kt index de704441..c2578783 100644 --- a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/line.kt +++ b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/line.kt @@ -45,7 +45,7 @@ fun makeTrack(start: Vector3D, direction: Vector3D): Line { fun makeTrack(x: Double, y: Double, theta: Double, phi: Double): Line { //TODO check angle definitions return makeTrack( - Vector3D(x, y, CENTRAL_LAYER_Z), + Vector3D(x, y, CENTRAL_LAYER_Z.toDouble()), Vector3D(phi, theta) ) } diff --git a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/monitor.kt b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/monitor.kt index a4e83c8b..d2ec7235 100644 --- a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/monitor.kt +++ b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/monitor.kt @@ -13,12 +13,12 @@ import ru.mipt.npm.muon.monitor.readResource internal const val MINIMAL_TRACK_LENGTH = 10.0 -private val layerCache = HashMap() +private val layerCache = HashMap() -fun findLayer(z: Double): Plane = layerCache.getOrPut(z) { +fun findLayer(z: Float): Plane = layerCache.getOrPut(z) { Plane( - Vector3D(0.0, 0.0, z), Vector3D(0.0, 0.0, 1.0), - Monitor.GEOMETRY_TOLERANCE + Vector3D(0.0, 0.0, z.toDouble()), Vector3D(0.0, 0.0, 1.0), + Monitor.GEOMETRY_TOLERANCE.toDouble() ) } diff --git a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/simulation.kt b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/simulation.kt index 6019a94a..f8d45fd4 100644 --- a/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/simulation.kt +++ b/demo/muon-monitor/src/jvmMain/kotlin/ru/mipt/npm/muon/monitor/sim/simulation.kt @@ -28,8 +28,8 @@ interface TrackGenerator { */ class UniformTrackGenerator( override val rnd: RandomGenerator, - val maxX: Double = 4 * PIXEL_XY_SIZE, - val maxY: Double = 4 * PIXEL_XY_SIZE + val maxX: Float = 4 * PIXEL_XY_SIZE, + val maxY: Float = 4 * PIXEL_XY_SIZE ) : TrackGenerator { override fun generate(): Line { @@ -44,8 +44,8 @@ class UniformTrackGenerator( class FixedAngleGenerator( override val rnd: RandomGenerator, val phi: Double, val theta: Double, - val maxX: Double = 4 * PIXEL_XY_SIZE, - val maxY: Double = 4 * PIXEL_XY_SIZE + val maxX: Float = 4 * PIXEL_XY_SIZE, + val maxY: Float = 4 * PIXEL_XY_SIZE ) : TrackGenerator { override fun generate(): Line { val x = (1 - rnd.nextDouble() * 2.0) * maxX @@ -60,8 +60,8 @@ class FixedAngleGenerator( class Cos2TrackGenerator( override val rnd: RandomGenerator, val power: Double = 2.0, - val maxX: Double = 4 * PIXEL_XY_SIZE, - val maxY: Double = 4 * PIXEL_XY_SIZE + val maxX: Float = 4 * PIXEL_XY_SIZE, + val maxY: Float = 4 * PIXEL_XY_SIZE ) : TrackGenerator { override fun generate(): Line { diff --git a/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt b/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt index a9070af7..fc2ebce3 100644 --- a/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt +++ b/demo/playground/src/jvmMain/kotlin/gdmlIaxo.kt @@ -3,6 +3,7 @@ package space.kscience.visionforge.examples import space.kscience.dataforge.context.Context import space.kscience.gdml.GdmlShowCase import space.kscience.visionforge.gdml.toVision +import space.kscience.visionforge.html.ResourceLocation import space.kscience.visionforge.solid.Solids fun main() { @@ -10,7 +11,7 @@ fun main() { plugin(Solids) } - context.makeVisionFile { + context.makeVisionFile(resourceLocation = ResourceLocation.EMBED) { vision("canvas") { GdmlShowCase.babyIaxo().toVision() } } } \ No newline at end of file diff --git a/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/VariableBox.kt b/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/VariableBox.kt index e3961775..f372fa38 100644 --- a/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/VariableBox.kt +++ b/demo/solid-showcase/src/jsMain/kotlin/space/kscience/visionforge/solid/demo/VariableBox.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.solid.demo import info.laht.threekt.core.Object3D -import info.laht.threekt.geometries.BoxBufferGeometry +import info.laht.threekt.geometries.BoxGeometry import info.laht.threekt.objects.Mesh import space.kscience.dataforge.meta.int import space.kscience.dataforge.meta.number @@ -26,7 +26,7 @@ internal fun SolidGroup.varBox( internal class VariableBox(val xSize: Number, val ySize: Number) : ThreeVision() { override fun render(three: ThreePlugin): Object3D { - val geometry = BoxBufferGeometry(xSize, ySize, 1) + val geometry = BoxGeometry(xSize, ySize, 1) val material = ThreeMaterials.DEFAULT.clone() diff --git a/jupyter/visionforge-gdml-jupyter/build.gradle.kts b/jupyter/visionforge-gdml-jupyter/build.gradle.kts index e2d1db03..896b44a4 100644 --- a/jupyter/visionforge-gdml-jupyter/build.gradle.kts +++ b/jupyter/visionforge-gdml-jupyter/build.gradle.kts @@ -4,9 +4,9 @@ plugins { description = "Jupyter api artifact for GDML rendering" -kotlin{ +kotlin { explicitApi = null - js{ + js { useCommonJs() browser { webpackTask { @@ -25,19 +25,17 @@ kotlin{ tasks.getByName("jvmProcessResources") { dependsOn(jsBrowserDistribution) - afterEvaluate { - from(jsBrowserDistribution) - } + from(jsBrowserDistribution) } } - sourceSets{ + sourceSets { commonMain { dependencies { api(project(":visionforge-solid")) } } - jvmMain{ + jvmMain { dependencies { implementation(project(":visionforge-gdml")) } @@ -52,10 +50,10 @@ kotlin{ } } -kscience{ +kscience { useJupyter() } -readme{ +readme { maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL } \ No newline at end of file diff --git a/settings.gradle.kts b/settings.gradle.kts index bc62b31e..6311841e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -16,8 +16,6 @@ pluginManagement { } } -//enableFeaturePreview("GRADLE_METADATA") - rootProject.name = "visionforge" diff --git a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt index 4f16982b..6b35d971 100644 --- a/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt +++ b/visionforge-core/src/commonMain/kotlin/space/kscience/visionforge/VisionGroup.kt @@ -58,6 +58,7 @@ public operator fun VisionGroup.iterator(): Iterator = children.values.i public val VisionGroup.isEmpty: Boolean get() = this.children.isEmpty() public interface VisionContainerBuilder { + //TODO add documentation public operator fun set(name: Name?, child: V?) } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXCanvas3D.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXCanvas3D.kt index 171bc5a3..a05c844a 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXCanvas3D.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXCanvas3D.kt @@ -4,6 +4,7 @@ import javafx.application.Platform import javafx.beans.property.ObjectProperty import javafx.beans.property.SimpleObjectProperty import javafx.scene.* +import javafx.scene.layout.BorderPane import javafx.scene.paint.Color import org.fxyz3d.scene.Axes import space.kscience.dataforge.context.Context @@ -11,31 +12,32 @@ import space.kscience.dataforge.context.ContextAware import space.kscience.visionforge.solid.specifications.Canvas3DOptions import tornadofx.* -class FXCanvas3D( - val plugin: FX3DPlugin, - val spec: Canvas3DOptions = Canvas3DOptions.empty(), +public class FXCanvas3D( + public val fx3d: FX3DPlugin, + public val options: Canvas3DOptions = Canvas3DOptions.empty(), ) : Fragment(), ContextAware { - override val context: Context get() = plugin.context + override val context: Context get() = fx3d.context - val world = Group().apply { + public val world: Group = Group().apply { //transforms.add(Rotate(180.0, Rotate.Z_AXIS)) } - val axes = Axes().also { - it.setHeight(spec.axes.size) - it.setRadius(spec.axes.width) - it.isVisible = spec.axes.visible + public val axes: Axes = Axes().also { + it.setHeight(options.axes.size) + it.setRadius(options.axes.width) + it.isVisible = options.axes.visible world.add(it) } - val light = AmbientLight() + public val light: AmbientLight = AmbientLight() private val camera = PerspectiveCamera().apply { - nearClip = spec.camera.nearClip - farClip = spec.camera.farClip - fieldOfView = spec.camera.fov.toDouble() - this.add(light) + nearClip = options.camera.nearClip + farClip = options.camera.farClip + fieldOfView = options.camera.fov.toDouble() + + add(light) } private val canvas = SubScene( @@ -49,19 +51,19 @@ class FXCanvas3D( scene.camera = camera } - override val root = borderpane { + override val root: BorderPane = borderpane { center = canvas } - val controls = camera.orbitControls(canvas, spec.camera).also { + public val controls: OrbitControls = camera.orbitControls(canvas, options.camera).also { world.add(it.centerMarker) } - val rootObjectProperty: ObjectProperty = SimpleObjectProperty() - var rootObject: Solid? by rootObjectProperty + public val rootObjectProperty: ObjectProperty = SimpleObjectProperty() + public var rootObject: Solid? by rootObjectProperty private val rootNodeProperty = rootObjectProperty.objectBinding { - it?.let { plugin.buildNode(it) } + it?.let { fx3d.buildNode(it) } } init { @@ -79,7 +81,7 @@ class FXCanvas3D( } } - fun render(vision: Solid) { + public fun render(vision: Solid) { rootObject = vision } } \ No newline at end of file diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXConvexFactory.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXConvexFactory.kt index f2bbba54..7538d6c6 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXConvexFactory.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/FXConvexFactory.kt @@ -11,7 +11,10 @@ object FXConvexFactory : FX3DFactory { override val type: KClass get() = Convex::class override fun invoke(obj: Convex, binding: VisualObjectFXBinding): Node { - val hull = HullUtil.hull(obj.points.map { Vector3d.xyz(it.x, it.y, it.z) }, PropertyStorage()) + val hull = HullUtil.hull( + obj.points.map { Vector3d.xyz(it.x.toDouble(), it.y.toDouble(), it.z.toDouble()) }, + PropertyStorage() + ) return hull.toNode() } diff --git a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/OrbitControls.kt b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/OrbitControls.kt index a32e44f5..38a04a16 100644 --- a/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/OrbitControls.kt +++ b/visionforge-fx/src/main/kotlin/space/kscience/visionforge/solid/OrbitControls.kt @@ -1,6 +1,7 @@ package space.kscience.visionforge.solid import javafx.beans.InvalidationListener +import javafx.beans.property.SimpleBooleanProperty import javafx.beans.property.SimpleDoubleProperty import javafx.event.EventHandler import javafx.geometry.Point3D @@ -17,26 +18,26 @@ import kotlin.math.* import space.kscience.visionforge.solid.specifications.Camera as CameraSpec -class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: CameraSpec) { +public class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: CameraSpec) { - val distanceProperty = SimpleDoubleProperty(spec.distance) - var distance by distanceProperty + public val distanceProperty: SimpleDoubleProperty = SimpleDoubleProperty(spec.distance) + public var distance: Double by distanceProperty - val azimuthProperty = SimpleDoubleProperty(spec.azimuth) - var azimuth by azimuthProperty + public val azimuthProperty: SimpleDoubleProperty = SimpleDoubleProperty(spec.azimuth) + public var azimuth: Double by azimuthProperty - val zenithProperty = SimpleDoubleProperty(PI / 2 - spec.latitude) - var zenith by zenithProperty + public val zenithProperty: SimpleDoubleProperty = SimpleDoubleProperty(PI / 2 - spec.latitude) + public var zenith: Double by zenithProperty +// +// public val latitudeProperty: DoubleBinding = zenithProperty.unaryMinus().plus(PI / 2) +// public val latitude by latitudeProperty - val latitudeProperty = zenithProperty.unaryMinus().plus(PI / 2) - val latitude by latitudeProperty - - val baseXProperty = SimpleDoubleProperty(0.0) - var x by baseXProperty - val baseYProperty = SimpleDoubleProperty(0.0) - var y by baseYProperty - val baseZProperty = SimpleDoubleProperty(0.0) - var z by baseZProperty + public val baseXProperty: SimpleDoubleProperty = SimpleDoubleProperty(0.0) + public var x: Double by baseXProperty + public val baseYProperty: SimpleDoubleProperty = SimpleDoubleProperty(0.0) + public var y: Double by baseYProperty + public val baseZProperty: SimpleDoubleProperty = SimpleDoubleProperty(0.0) + public var z: Double by baseZProperty private val baseTranslate = Translate() @@ -47,13 +48,15 @@ class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: // // val basePosition by basePositionProperty - val centerMarker by lazy { + private val inProgressProperty = SimpleBooleanProperty(false) + + public val centerMarker: Node by lazy { Sphere(10.0).also { it.transforms.setAll(baseTranslate) + it.visibleProperty().bind(inProgressProperty) } } - //private val center = Translate() private val rx = Rotate(0.0, Rotate.X_AXIS) @@ -65,7 +68,7 @@ class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: init { - camera.transforms.setAll(rx, ry, rz, translate) + camera.transforms.setAll(rx, ry, rz, baseTranslate, translate) update() val listener = InvalidationListener { update() @@ -136,6 +139,7 @@ class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: mousePosY = me.sceneY mouseOldX = me.sceneX mouseOldY = me.sceneY + inProgressProperty.set(true) } onMouseDragged = EventHandler { me -> @@ -154,18 +158,23 @@ class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: if (me.isPrimaryButtonDown) { azimuth = (azimuth - mouseDeltaX * MOUSE_SPEED * modifier * ROTATION_SPEED).coerceIn(0.0, 2 * PI) - zenith = (zenith - mouseDeltaY * MOUSE_SPEED * modifier * ROTATION_SPEED).coerceIn(0.0,PI) + zenith = (zenith - mouseDeltaY * MOUSE_SPEED * modifier * ROTATION_SPEED).coerceIn(0.0, PI) } else if (me.isSecondaryButtonDown) { x += MOUSE_SPEED * modifier * TRACK_SPEED * (mouseDeltaX * cos(azimuth) + mouseDeltaY * sin(azimuth)) z += MOUSE_SPEED * modifier * TRACK_SPEED * (-mouseDeltaX * sin(azimuth) + mouseDeltaY * cos(azimuth)) } } + + onMouseReleased = EventHandler { + inProgressProperty.set(false) + } + onScroll = EventHandler { event -> distance = max(1.0, distance - MOUSE_SPEED * event.deltaY * RESIZE_SPEED) } } - companion object { + public companion object { private const val CONTROL_MULTIPLIER = 0.1 private const val SHIFT_MULTIPLIER = 10.0 private const val MOUSE_SPEED = 0.1 @@ -175,5 +184,5 @@ class OrbitControls internal constructor(camera: Camera, canvas: SubScene, spec: } } -fun Camera.orbitControls(canvas: SubScene, spec: CameraSpec) = +public fun Camera.orbitControls(canvas: SubScene, spec: CameraSpec): OrbitControls = OrbitControls(this, canvas, spec) diff --git a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformerEnv.kt b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformerEnv.kt index ef41a214..76561fe8 100644 --- a/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformerEnv.kt +++ b/visionforge-gdml/src/commonMain/kotlin/space/kscience/visionforge/gdml/GdmlTransformerEnv.kt @@ -41,7 +41,7 @@ public class GdmlTransformer { internal val styleCache = HashMap() - public fun Solid.useStyle(name: String, builder: MetaBuilder.() -> Unit) { + public fun Solid.registerAndUseStyle(name: String, builder: MetaBuilder.() -> Unit) { styleCache.getOrPut(name.toName()) { Meta(builder) } @@ -49,7 +49,7 @@ public class GdmlTransformer { } public fun Solid.transparent() { - useStyle("transparent") { + registerAndUseStyle("transparent") { SolidMaterial.MATERIAL_OPACITY_KEY put 0.3 "edges.enabled" put true } @@ -75,7 +75,7 @@ public class GdmlTransformer { if (parent.physVolumes.isNotEmpty()) transparent() - useStyle(styleName) { + registerAndUseStyle(styleName) { val vfMaterial = SolidMaterial().apply { configurePaint(material, solid) } @@ -125,7 +125,11 @@ private class GdmlTransformerEnv(val settings: GdmlTransformer) { fun Solid.configureSolid(root: Gdml, parent: GdmlVolume, solid: GdmlSolid) { val material = parent.materialref.resolve(root) ?: GdmlElement(parent.materialref.ref) - settings.run { configureSolid(parent, solid, material) } + with(settings) { + with(this@configureSolid) { + configureSolid(parent, solid, material) + } + } } private fun proxySolid(root: Gdml, group: SolidGroup, solid: GdmlSolid, name: String): SolidReferenceGroup { diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt index fda8531e..9855c835 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/Solid.kt @@ -156,21 +156,21 @@ private fun Solid.position(): Point3D = public var Solid.x: Number get() = position?.x ?: 0f set(value) { - position().x = value.toDouble() + position().x = value.toFloat() invalidateProperty(Solid.X_POSITION_KEY) } public var Solid.y: Number get() = position?.y ?: 0f set(value) { - position().y = value.toDouble() + position().y = value.toFloat() invalidateProperty(Solid.Y_POSITION_KEY) } public var Solid.z: Number get() = position?.z ?: 0f set(value) { - position().z = value.toDouble() + position().z = value.toFloat() invalidateProperty(Solid.Z_POSITION_KEY) } @@ -180,21 +180,21 @@ private fun Solid.rotation(): Point3D = public var Solid.rotationX: Number get() = rotation?.x ?: 0f set(value) { - rotation().x = value.toDouble() + rotation().x = value.toFloat() invalidateProperty(Solid.X_ROTATION_KEY) } public var Solid.rotationY: Number get() = rotation?.y ?: 0f set(value) { - rotation().y = value.toDouble() + rotation().y = value.toFloat() invalidateProperty(Solid.Y_ROTATION_KEY) } public var Solid.rotationZ: Number get() = rotation?.z ?: 0f set(value) { - rotation().z = value.toDouble() + rotation().z = value.toFloat() invalidateProperty(Solid.Z_ROTATION_KEY) } @@ -204,20 +204,20 @@ private fun Solid.scale(): Point3D = public var Solid.scaleX: Number get() = scale?.x ?: 1f set(value) { - scale().x = value.toDouble() + scale().x = value.toFloat() invalidateProperty(Solid.X_SCALE_KEY) } public var Solid.scaleY: Number get() = scale?.y ?: 1f set(value) { - scale().y = value.toDouble() + scale().y = value.toFloat() invalidateProperty(Solid.Y_SCALE_KEY) } public var Solid.scaleZ: Number get() = scale?.z ?: 1f set(value) { - scale().z = value.toDouble() + scale().z = value.toFloat() invalidateProperty(Solid.Z_SCALE_KEY) } \ No newline at end of file diff --git a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt index 35fc731f..28a90398 100644 --- a/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt +++ b/visionforge-solid/src/commonMain/kotlin/space/kscience/visionforge/solid/geometry.kt @@ -3,29 +3,31 @@ package space.kscience.visionforge.solid import kotlinx.serialization.Serializable import space.kscience.dataforge.meta.Meta import space.kscience.dataforge.meta.MetaBuilder -import space.kscience.dataforge.meta.double +import space.kscience.dataforge.meta.float import space.kscience.dataforge.meta.get import kotlin.math.PI +import kotlin.math.pow +import kotlin.math.sqrt public const val PI2: Float = 2 * PI.toFloat() @Serializable -public data class Point2D(public var x: Double, public var y: Double) +public data class Point2D(public var x: Float, public var y: Float) -public fun Point2D(x: Number, y: Number): Point2D = Point2D(x.toDouble(), y.toDouble()) +public fun Point2D(x: Number, y: Number): Point2D = Point2D(x.toFloat(), y.toFloat()) public fun Point2D.toMeta(): Meta = Meta { Solid.X_KEY put x Solid.Y_KEY put y } -internal fun Meta.point2D(): Point2D = Point2D(this["x"].double ?: 0.0, this["y"].double ?: 0.0) +internal fun Meta.point2D(): Point2D = Point2D(this["x"].float ?: 0f, this["y"].float ?: 0f) @Serializable public data class Point3D( - public var x: Double, - public var y: Double, - public var z: Double, + public var x: Float, + public var y: Float, + public var z: Float, ) { public companion object { public val ZERO: Point3D = Point3D(0.0, 0.0, 0.0) @@ -33,7 +35,7 @@ public data class Point3D( } } -public fun Point3D(x: Number, y: Number, z: Number): Point3D = Point3D(x.toDouble(), y.toDouble(), z.toDouble()) +public fun Point3D(x: Number, y: Number, z: Number): Point3D = Point3D(x.toFloat(), y.toFloat(), z.toFloat()) public operator fun Point3D.plus(other: Point3D): Point3D = Point3D( this.x + other.x, @@ -41,7 +43,32 @@ public operator fun Point3D.plus(other: Point3D): Point3D = Point3D( this.z + other.z ) -internal fun Meta.point3D() = Point3D(this["x"].double ?: 0.0, this["y"].double ?: 0.0, this["y"].double ?: 0.0) +public operator fun Point3D.minus(other: Point3D): Point3D = Point3D( + this.x - other.x, + this.y - other.y, + this.z - other.z +) + +public operator fun Point3D.unaryMinus(): Point3D = Point3D( + -x, + -y, + -z +) + +public infix fun Point3D.cross(other: Point3D): Point3D = Point3D( + y * other.z - z * other.y, + z * other.x - x * other.z, + x * other.y - y * other.x +) + +public fun Point3D.normalizeInPlace(){ + val norm = sqrt(x.pow(2) + y.pow(2) + z.pow(2)) + x /= norm + y /= norm + z /= norm +} + +internal fun Meta.point3D() = Point3D(this["x"].float ?: 0.0, this["y"].float ?: 0.0, this["y"].float ?: 0.0) public fun Point3D.toMeta(): MetaBuilder = Meta { Solid.X_KEY put x diff --git a/visionforge-threejs/build.gradle.kts b/visionforge-threejs/build.gradle.kts index 60c00061..32803a61 100644 --- a/visionforge-threejs/build.gradle.kts +++ b/visionforge-threejs/build.gradle.kts @@ -4,6 +4,6 @@ plugins { dependencies { api(project(":visionforge-solid")) - implementation(npm("three", "0.124.0")) - implementation(npm("three-csg-ts", "2.2.2")) + implementation(npm("three", "0.130.1")) + implementation(npm("three-csg-ts", "3.1.4")) } diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferAttribute.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferAttribute.kt index 001aa20c..938deb7e 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferAttribute.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferAttribute.kt @@ -44,6 +44,7 @@ abstract external class BufferAttribute protected constructor( * Default is 0. Position at whcih to start update. */ var offset: Int + /** * Default is -1, which means don't use update ranges. @@ -56,16 +57,19 @@ abstract external class BufferAttribute protected constructor( * UUID of this object instance. This gets automatically assigned and this shouldn't be edited. */ val uuid: String + /** * Optional name for this attribute instance. Default is an empty string. */ var name: String var array: dynamic + /** * The length of vectors that are being stored in the array. */ val itemSize: Int + /** * Stores the array's length divided by the itemSize. * @@ -73,6 +77,7 @@ abstract external class BufferAttribute protected constructor( * then this will count the number of such vectors stored. */ val count: Int + /** * Indicates how the underlying data in the buffer maps to the values in the GLSL shader code. See the constructor above for details. */ @@ -86,6 +91,7 @@ abstract external class BufferAttribute protected constructor( * This corresponds to the gl.DYNAMIC_DRAW flag. */ var dynamic: Boolean + /** * This can be used to only update some components of stored vectors ( * for example, just the component related to color). @@ -99,8 +105,16 @@ abstract external class BufferAttribute protected constructor( */ var needsUpdate: Boolean + /** + * A callback function that is executed after the Renderer has transferred the attribute array data to the GPU. + */ var onUploadCallback: () -> Unit + /** + * Sets the value of the [onUploadCallback] property. + */ + fun onUpload(callback: () -> Unit) + /** * A version number, incremented every time the needsUpdate property is set to true. */ @@ -119,6 +133,7 @@ abstract external class BufferAttribute protected constructor( fun getW(index: Int): Number fun copy(source: BufferAttribute): BufferAttribute + /** * Copy a vector from bufferAttribute[index2] to array[index1]. */ @@ -205,3 +220,22 @@ abstract external class BufferAttribute protected constructor( */ fun setXYZW(index: Int, x: Number, y: Number, z: Number, w: Number) } + + +external class Float32BufferAttribute( + array: FloatArray, + itemSize: Int, + normalized: Boolean = definedExternally +) : BufferAttribute + +external class Int32BufferAttribute( + array: IntArray, + itemSize: Int, + normalized: Boolean = definedExternally +) : BufferAttribute + +external class Int16BufferAttribute( + array: ShortArray, + itemSize: Int, + normalized: Boolean = definedExternally +) : BufferAttribute \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferGeometry.kt index c3fd093b..251627a7 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/BufferGeometry.kt @@ -85,14 +85,14 @@ open external class BufferGeometry { fun clearGroups() fun addGroup(start: Int, count: Int, materialIndex: Int = definedExternally) - fun addAttribute(name: String, attribute: BufferAttribute) + fun setAttribute(name: String, attribute: BufferAttribute) fun getAttribute(name: String): BufferAttribute - fun removeAttribute(name: String): BufferGeometry + fun deleteAttribute(name: String): BufferGeometry fun setIndex(index: BufferAttribute) + fun setIndex(index: Array) fun setDrawRange(start: Int, count: Int) - fun fromGeometry(geometry: Geometry) fun setFromObject(`object`: Object3D): BufferGeometry fun updateFromObject(`object`: Object3D): BufferGeometry fun setFromPoints(points: Array): BufferGeometry diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/DirectGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/DirectGeometry.kt deleted file mode 100644 index bae8ce28..00000000 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/DirectGeometry.kt +++ /dev/null @@ -1,48 +0,0 @@ -/* - * The MIT License - * - * Copyright 2017-2018 Lars Ivar Hatledal - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -@file:JsModule("three") -@file:JsNonModule - -package info.laht.threekt.core - -import info.laht.threekt.math.Box3 -import info.laht.threekt.math.Sphere - -external class DirectGeometry { - - var verticesNeedUpdate: Boolean - var normalsNeedUpdate: Boolean - var colorsNeedUpdate: Boolean - var uvsNeedUpdate: Boolean - var groupsNeedUpdate: Boolean - - fun computeBoundingBox(): Box3 - fun computeBoundingSphere(): Sphere - - fun dispose() - - fun fromGeometry(geometry: Geometry) - -} \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/Geometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/Geometry.kt deleted file mode 100644 index 522ab07c..00000000 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/core/Geometry.kt +++ /dev/null @@ -1,109 +0,0 @@ -/* - * The MIT License - * - * Copyright 2017-2018 Lars Ivar Hatledal - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -@file:JsModule("three") -@file:JsNonModule - -package info.laht.threekt.core - -import info.laht.threekt.math.* -import info.laht.threekt.objects.Mesh - -external interface MorphTarget { - val name: String - val vertices: Array -} - -external interface MorphNormal { - val name: String - val normals: Array -} - -open external class Geometry { - - val id: Int - - var vertices: Array - var colors: Array - var faces: Array - var faceVertexUvs: Array> - - var morphTargets: Array - var morphNormals: Array - - var skinWeights: Array - var skinIndices: Array - - var lineDistances: List - - var boundingBox: Box3? - var boundingSphere: Sphere? - - // update flags - - var elementsNeedUpdate: Boolean - var verticesNeedUpdate: Boolean - var uvsNeedUpdate: Boolean - var normalsNeedUpdate: Boolean - var colorsNeedUpdate: Boolean - var lineDistancesNeedUpdate: Boolean - var groupsNeedUpdate: Boolean - - fun applyMatrix4(matrix: Matrix4): Geometry - fun rotateX(angle: Number): Geometry - fun rotateY(angle: Number): Geometry - fun rotateZ(angle: Number): Geometry - fun translate(x: Number, y: Number, z: Number): Geometry - fun scale(x: Number, y: Number, z: Number): Geometry - fun lookAt(vector: Vector3): Geometry - fun fromBufferGeometry(geometry: BufferGeometry): Geometry - fun addFace(a: Int, b: Int, c: Int, materialIndexOffset: Int = definedExternally) - fun center(): Vector3 - fun normalize(): Geometry - fun computeFaceNormals() - fun computeVertexNormals(areaWeighted: Boolean = definedExternally) - fun computeFlatVertexNormals() - fun computeMorphNormals() - fun computeLineDistances() - fun computeBoundingBox() - fun computeBoundingSphere() - - fun merge(geometry: Geometry, matrix: Matrix4 = definedExternally, materialIndexOffset: Int = definedExternally) - - fun mergeMesh(mesh: Mesh) - - fun mergeVertices() - - fun setFromPoint(points: Array): Geometry - - fun sortFacesByMaterialIndex() - - fun toJSON(): Any - - open fun clone(): Geometry - fun copy(geometry: Geometry): Geometry - - fun dispose() - -} diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/BoxGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/BoxGeometry.kt index 6cf5d5a7..8d0e5ec5 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/BoxGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/BoxGeometry.kt @@ -4,7 +4,6 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry external class BoxGeometry( @@ -14,14 +13,4 @@ external class BoxGeometry( widthSegments: Int = definedExternally, heightSegments: Int = definedExternally, depthSegments: Int = definedExternally -) : Geometry - - -external class BoxBufferGeometry( - width: Number, - height: Number, - depth: Number, - widthSegments: Int = definedExternally, - heightSegments: Int = definedExternally, - depthSegments: Int = definedExternally ) : BufferGeometry diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ConeGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ConeGeometry.kt index ae143fa8..f98ad2dd 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ConeGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ConeGeometry.kt @@ -4,7 +4,7 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry + external class ConeGeometry( radius: Number = definedExternally, @@ -14,14 +14,4 @@ external class ConeGeometry( openEnded: Boolean = definedExternally, thetaStart: Boolean = definedExternally, thetaLength: Boolean = definedExternally -) : Geometry - -external class ConeBufferGeometry( - radius: Number = definedExternally, - height: Number = definedExternally, - radialSegments: Int = definedExternally, - heightSegments: Int = definedExternally, - openEnded: Boolean = definedExternally, - thetaStart: Boolean = definedExternally, - thetaLength: Boolean = definedExternally ) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/CylinderGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/CylinderGeometry.kt index a4801453..b10e5c11 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/CylinderGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/CylinderGeometry.kt @@ -4,7 +4,6 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry external class CylinderGeometry( radiusTop: Number, @@ -15,15 +14,4 @@ external class CylinderGeometry( openEnded: Boolean = definedExternally, thetaStart: Number = definedExternally, thetaLength: Number = definedExternally -) : Geometry - -external class CylinderBufferGeometry( - radiusTop: Number, - radiusBottom: Number, - height: Number, - radialSegments: Int = definedExternally, - heightSegments: Int = definedExternally, - openEnded: Boolean = definedExternally, - thetaStart: Number = definedExternally, - thetaLength: Number = definedExternally ) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/EdgesGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/EdgesGeometry.kt index 8e6568da..b0a5c91b 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/EdgesGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/EdgesGeometry.kt @@ -4,8 +4,5 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry -external class EdgesGeometry(geometry: Geometry, thresholdAngle: Int = definedExternally) : BufferGeometry { - constructor(geometry: BufferGeometry, thresholdAngle: Int = definedExternally) -} \ No newline at end of file +public external class EdgesGeometry(geometry: BufferGeometry, thresholdAngle: Int = definedExternally) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ExtrudedGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ExtrudeGeometry.kt similarity index 83% rename from visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ExtrudedGeometry.kt rename to visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ExtrudeGeometry.kt index ab17c116..4b30e7af 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ExtrudedGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/ExtrudeGeometry.kt @@ -11,7 +11,6 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry import info.laht.threekt.extras.core.Shape import info.laht.threekt.math.Vector2 @@ -78,15 +77,3 @@ external open class ExtrudeBufferGeometry : BufferGeometry { var WorldUVGenerator: UVGenerator } } - -external open class ExtrudeGeometry : Geometry { - constructor(shapes: Shape, options: ExtrudeGeometryOptions?) - constructor(shapes: Array, options: ExtrudeGeometryOptions?) - - open fun addShapeList(shapes: Array, options: Any? = definedExternally) - open fun addShape(shape: Shape, options: Any? = definedExternally) - - companion object { - var WorldUVGenerator: UVGenerator - } -} \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/PlaneGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/PlaneGeometry.kt index ad758af4..0b1072ef 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/PlaneGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/PlaneGeometry.kt @@ -4,7 +4,6 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry external class PlaneGeometry( @@ -13,13 +12,4 @@ external class PlaneGeometry( widthSegments: Int = definedExternally, heightSegments: Int = definedExternally -) : Geometry - -external class PlaneBufferGeometry( - - width: Number, - height: Number, - widthSegments: Int = definedExternally, - heightSegments: Int = definedExternally - ) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/SphereGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/SphereGeometry.kt index 8117ff4c..5b0c9fc6 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/SphereGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/SphereGeometry.kt @@ -4,7 +4,6 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry external class SphereGeometry( radius: Number, @@ -14,14 +13,4 @@ external class SphereGeometry( phiLength: Number = definedExternally, thetaStart: Number = definedExternally, thetaLength: Number = definedExternally -) : Geometry - -external class SphereBufferGeometry( - radius: Number, - widthSegments: Int = definedExternally, - heightSegments: Int = definedExternally, - phiStart: Number = definedExternally, - phiLength: Number = definedExternally, - thetaStart: Number = definedExternally, - thetaLength: Number = definedExternally ) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TextBufferGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TextGeometry.kt similarity index 83% rename from visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TextBufferGeometry.kt rename to visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TextGeometry.kt index ca9f93b3..b8c935f3 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TextBufferGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TextGeometry.kt @@ -34,10 +34,7 @@ external interface TextGeometryParameters { set(value) = definedExternally } -external class TextBufferGeometry(text: String, parameters: TextGeometryParameters? = definedExternally) : ExtrudeBufferGeometry { - val parameters: TextGeometryParameters -} - -external class TextGeometry(text: String, parameters: TextGeometryParameters? = definedExternally) : ExtrudeGeometry { +external class TextBufferGeometry(text: String, parameters: TextGeometryParameters? = definedExternally) : + ExtrudeBufferGeometry { val parameters: TextGeometryParameters } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TorusGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TorusGeometry.kt index 7406afb3..0a53525f 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TorusGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TorusGeometry.kt @@ -4,7 +4,7 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry + external class TorusGeometry( radius: Number = definedExternally, @@ -12,12 +12,4 @@ external class TorusGeometry( radialSegments: Int = definedExternally, tubularSegments: Int = definedExternally, arc: Number = definedExternally -) : Geometry - -external class TorusBufferGeometry( - radius: Number = definedExternally, - tube: Number = definedExternally, - radialSegments: Int = definedExternally, - tubularSegments: Int = definedExternally, - arc: Number = definedExternally ) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TubeGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TubeGeometry.kt index 38bc560a..36afa6ae 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TubeGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/TubeGeometry.kt @@ -1,10 +1,10 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry import info.laht.threekt.extras.core.Curve import info.laht.threekt.math.Vector3 + /** * Creates a tube that extrudes along a 3d curve. */ @@ -16,25 +16,6 @@ external class TubeGeometry( radiusSegments: Int = definedExternally, closed: Boolean = definedExternally -) : Geometry { - - var tangents: Array - var normals: Array - var binormals: Array - -} - -/** - * Creates a tube that extrudes along a 3d curve. - */ -external class TubeBufferGeometry( - - path: Curve, - tubularSegments: Int = definedExternally, - radius: Number = definedExternally, - radiusSegments: Int = definedExternally, - closed: Boolean = definedExternally - ) : BufferGeometry { val parameters: dynamic diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/WireframeGeometry.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/WireframeGeometry.kt index 4c0a72ee..f52c61cc 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/WireframeGeometry.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/geometries/WireframeGeometry.kt @@ -4,14 +4,8 @@ package info.laht.threekt.geometries import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry /** * This can be used as a helper object to view a Geometry object as a wireframe. */ -external class WireframeGeometry : BufferGeometry { - - constructor(geometry: Geometry) - constructor(geometry: BufferGeometry) - -} \ No newline at end of file +external class WireframeGeometry(geometry: BufferGeometry) : BufferGeometry \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/LineSegments.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/LineSegments.kt index b5a83ff0..03bef34e 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/LineSegments.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/LineSegments.kt @@ -28,12 +28,10 @@ package info.laht.threekt.objects import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Geometry import info.laht.threekt.core.Object3D import info.laht.threekt.materials.Material open external class LineSegments(geometry: BufferGeometry, material: Material) : Object3D { - constructor(geometry: Geometry, material: Material) var geometry: BufferGeometry var material: Material diff --git a/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/Mesh.kt b/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/Mesh.kt index 78dcb9f6..54669b1f 100644 --- a/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/Mesh.kt +++ b/visionforge-threejs/src/main/kotlin/info/laht/threekt/objects/Mesh.kt @@ -27,14 +27,14 @@ package info.laht.threekt.objects -import info.laht.threekt.core.* +import info.laht.threekt.core.BufferGeometry +import info.laht.threekt.core.Intersect +import info.laht.threekt.core.Object3D +import info.laht.threekt.core.Raycaster import info.laht.threekt.materials.Material -open external class Mesh : Object3D { - - constructor(geometry: Geometry?, material: Material?) - constructor(geometry: BufferGeometry?, material: Material?) +open external class Mesh(geometry: BufferGeometry?, material: Material?) : Object3D { var geometry: dynamic var material: Material diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt index b01dbd1a..00313d3a 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/MeshThreeFactory.kt @@ -30,9 +30,6 @@ public abstract class MeshThreeFactory( override fun invoke(three: ThreePlugin, obj: T): Mesh { val geometry = buildGeometry(obj) - //JS sometimes tries to pass Geometry as BufferGeometry - @Suppress("USELESS_IS_CHECK") if (geometry !is BufferGeometry) error("BufferGeometry expected") - //val meshMeta: Meta = obj.properties[Material3D.MATERIAL_KEY]?.node ?: Meta.empty val mesh = Mesh(geometry, ThreeMaterials.DEFAULT).apply { diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeBoxFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeBoxFactory.kt index dad57066..a5b50688 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeBoxFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeBoxFactory.kt @@ -1,12 +1,12 @@ package space.kscience.visionforge.solid.three -import info.laht.threekt.geometries.BoxBufferGeometry +import info.laht.threekt.geometries.BoxGeometry import space.kscience.visionforge.solid.Box import space.kscience.visionforge.solid.detail public object ThreeBoxFactory : MeshThreeFactory(Box::class) { - override fun buildGeometry(obj: Box): BoxBufferGeometry = + override fun buildGeometry(obj: Box): BoxGeometry = obj.detail?.let { detail -> - BoxBufferGeometry(obj.xSize, obj.ySize, obj.zSize, detail, detail, detail) - } ?: BoxBufferGeometry(obj.xSize, obj.ySize, obj.zSize) + BoxGeometry(obj.xSize, obj.ySize, obj.zSize, detail, detail, detail) + } ?: BoxGeometry(obj.xSize, obj.ySize, obj.zSize) } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt index bbce8f19..4855abcd 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCanvasLabelFactory.kt @@ -2,7 +2,7 @@ package space.kscience.visionforge.solid.three import info.laht.threekt.DoubleSide import info.laht.threekt.core.Object3D -import info.laht.threekt.geometries.PlaneBufferGeometry +import info.laht.threekt.geometries.PlaneGeometry import info.laht.threekt.materials.MeshBasicMaterial import info.laht.threekt.objects.Mesh import info.laht.threekt.textures.Texture @@ -46,7 +46,7 @@ public object ThreeCanvasLabelFactory : ThreeFactory { } val mesh = Mesh( - PlaneBufferGeometry(canvas.width, canvas.height), + PlaneGeometry(canvas.width, canvas.height), material ) diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCompositeFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCompositeFactory.kt index 1f8c9328..270e63a6 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCompositeFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeCompositeFactory.kt @@ -2,6 +2,7 @@ package space.kscience.visionforge.solid.three import CSG import info.laht.threekt.core.BufferGeometry +import info.laht.threekt.math.Matrix4 import info.laht.threekt.objects.Mesh import space.kscience.visionforge.solid.Composite import space.kscience.visionforge.solid.CompositeType @@ -23,7 +24,7 @@ public class ThreeCompositeFactory(public val three: ThreePlugin) : MeshThreeFac CompositeType.INTERSECT -> firstCSG.intersect(secondCSG) CompositeType.SUBTRACT -> firstCSG.subtract(secondCSG) } - return resultCSG.toGeometry().toBufferGeometry() + return resultCSG.toGeometry(Matrix4()) } } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeConeFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeConeFactory.kt index c8eece9a..5789c30e 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeConeFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeConeFactory.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.solid.three import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.geometries.CylinderBufferGeometry +import info.laht.threekt.geometries.CylinderGeometry import space.kscience.visionforge.solid.ConeSegment import space.kscience.visionforge.solid.detail import kotlin.math.PI @@ -11,7 +11,7 @@ public object ThreeConeFactory : MeshThreeFactory(ConeSegment::clas override fun buildGeometry(obj: ConeSegment): BufferGeometry { val cylinder = obj.detail?.let { val segments = it.toDouble().pow(0.5).toInt() - CylinderBufferGeometry( + CylinderGeometry( radiusTop = obj.topRadius, radiusBottom = obj.bottomRadius, height = obj.height, @@ -21,7 +21,7 @@ public object ThreeConeFactory : MeshThreeFactory(ConeSegment::clas thetaStart = obj.startAngle, thetaLength = obj.angle ) - } ?: CylinderBufferGeometry( + } ?: CylinderGeometry( radiusTop = obj.topRadius, radiusBottom = obj.bottomRadius, height = obj.height, diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt index e231c4ce..c62abd77 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeFactory.kt @@ -63,9 +63,7 @@ public fun Object3D.updateProperty(source: Vision, propertyName: Name) { * Generic factory for elements which provide inside geometry builder */ public object ThreeShapeFactory : MeshThreeFactory(GeometrySolid::class) { - override fun buildGeometry(obj: GeometrySolid): BufferGeometry { - return obj.run { - ThreeGeometryBuilder().apply { toGeometry(this) }.build() - } - } + override fun buildGeometry(obj: GeometrySolid): BufferGeometry = ThreeGeometryBuilder().apply { + obj.toGeometry(this) + }.build() } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt index c1568ffd..4ba19cf9 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeGeometryBuilder.kt @@ -1,52 +1,61 @@ package space.kscience.visionforge.solid.three import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.core.Face3 -import info.laht.threekt.core.Geometry +import info.laht.threekt.core.Float32BufferAttribute import info.laht.threekt.math.Vector3 import space.kscience.dataforge.meta.Meta -import space.kscience.dataforge.meta.get -import space.kscience.dataforge.meta.int import space.kscience.visionforge.solid.GeometryBuilder import space.kscience.visionforge.solid.Point3D +import space.kscience.visionforge.solid.cross +import space.kscience.visionforge.solid.minus internal fun Point3D.toVector() = Vector3(x, y, z) +internal fun MutableList.add(f1: T, f2: T, f3: T) { + add(f1) + add(f2) + add(f3) +} + /** * An implementation of geometry builder for Three.js [BufferGeometry] */ public class ThreeGeometryBuilder : GeometryBuilder { - private val vertices = ArrayList() - private val faces = ArrayList() + private val indices = ArrayList() + private val positions = ArrayList() + private val normals = ArrayList() + private val colors = ArrayList() - private val vertexCache = HashMap() + private val vertexCache = HashMap() + private var counter: Short = -1 - private fun append(vertex: Point3D): Int { - val index = vertexCache[vertex] ?: -1//vertices.indexOf(vertex) - return if (index > 0) { - index - } else { - vertices.add(vertex.toVector()) - vertexCache[vertex] = vertices.size - 1 - vertices.size - 1 - } + private fun indexOf(vertex: Point3D, normal: Point3D): Short = vertexCache.getOrPut(vertex) { + //add vertex and update cache if needed + positions.add(vertex.x, vertex.y, vertex.z) + normals.add(normal.x, vertex.y, vertex.z) + colors.add(1f, 1f, 1f) + counter++ + counter } override fun face(vertex1: Point3D, vertex2: Point3D, vertex3: Point3D, normal: Point3D?, meta: Meta) { - val face = Face3(append(vertex1), append(vertex2), append(vertex3), normal?.toVector() ?: Vector3(0, 0, 0)) - meta["materialIndex"].int?.let { face.materialIndex = it } - meta["color"]?.getColor()?.let { face.color = it } - faces.add(face) + val actualNormal: Point3D = normal ?: (vertex3 - vertex2) cross (vertex1 - vertex2) + indices.add( + indexOf(vertex1, actualNormal), + indexOf(vertex2, actualNormal), + indexOf(vertex3, actualNormal) + ) } - override fun build(): BufferGeometry { - return Geometry().apply { - vertices = this@ThreeGeometryBuilder.vertices.toTypedArray() - faces = this@ThreeGeometryBuilder.faces.toTypedArray() - computeBoundingSphere() - computeFaceNormals() - }.toBufferGeometry() + override fun build(): BufferGeometry = BufferGeometry().apply { + //setIndex(Int16BufferAttribute(indices.toShortArray(), 1)) + setIndex(indices.toTypedArray()) + setAttribute("position", Float32BufferAttribute(positions.toFloatArray(), 3)) + setAttribute("normal", Float32BufferAttribute(normals.toFloatArray(), 3)) + //setAttribute("color", Float32BufferAttribute(colors.toFloatArray(), 3)) + + computeBoundingSphere() } } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt index cc9c58ce..b2858fdb 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeLineFactory.kt @@ -1,6 +1,6 @@ package space.kscience.visionforge.solid.three -import info.laht.threekt.core.Geometry +import info.laht.threekt.core.BufferGeometry import info.laht.threekt.core.Object3D import info.laht.threekt.math.Color import info.laht.threekt.objects.LineSegments @@ -16,8 +16,8 @@ public object ThreeLineFactory : ThreeFactory { override val type: KClass get() = PolyLine::class override fun invoke(three: ThreePlugin, obj: PolyLine): Object3D { - val geometry = Geometry().apply { - vertices = Array(obj.points.size) { obj.points[it].toVector() } + val geometry = BufferGeometry().apply { + setFromPoints(Array(obj.points.size) { obj.points[it].toVector() }) } val material = ThreeMaterials.getLineMaterial(obj.getProperty(MeshThreeFactory.EDGES_MATERIAL_KEY).node, true) diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSphereFactory.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSphereFactory.kt index 8f117516..8932a6c8 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSphereFactory.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/ThreeSphereFactory.kt @@ -1,14 +1,14 @@ package space.kscience.visionforge.solid.three import info.laht.threekt.core.BufferGeometry -import info.laht.threekt.geometries.SphereBufferGeometry +import info.laht.threekt.geometries.SphereGeometry import space.kscience.visionforge.solid.Sphere import space.kscience.visionforge.solid.detail public object ThreeSphereFactory : MeshThreeFactory(Sphere::class) { override fun buildGeometry(obj: Sphere): BufferGeometry { return obj.detail?.let {detail -> - SphereBufferGeometry( + SphereGeometry( radius = obj.radius, phiStart = obj.phiStart, phiLength = obj.phi, @@ -17,7 +17,7 @@ public object ThreeSphereFactory : MeshThreeFactory(Sphere::class) { widthSegments = detail, heightSegments = detail ) - }?: SphereBufferGeometry( + }?: SphereGeometry( radius = obj.radius, phiStart = obj.phiStart, phiLength = obj.phi, diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/csg.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/csg.kt index af99f66e..d7b0d46b 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/csg.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/csg.kt @@ -1,28 +1,34 @@ -@file:Suppress("INTERFACE_WITH_SUPERCLASS", +@file:Suppress( + "INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS", - "EXTERNAL_DELEGATION") + "EXTERNAL_DELEGATION" +) @file:JsModule("three-csg-ts") @file:JsNonModule +import info.laht.threekt.core.BufferGeometry import info.laht.threekt.math.Matrix4 import info.laht.threekt.math.Vector3 import info.laht.threekt.objects.Mesh -external open class CSG { - open fun clone(): CSG - open fun toPolygons(): Array - open fun union(csg: CSG): CSG - open fun subtract(csg: CSG): CSG - open fun intersect(csg: CSG): CSG - open fun inverse(): CSG +external class CSG { + fun clone(): CSG + fun toPolygons(): Array + fun toGeometry(toMatrix: Matrix4): BufferGeometry + fun union(csg: CSG): CSG + fun subtract(csg: CSG): CSG + fun intersect(csg: CSG): CSG + fun inverse(): CSG + companion object { fun fromPolygons(polygons: Array): CSG - fun fromGeometry(geom: Any): CSG - fun fromMesh(mesh: Mesh): CSG + fun fromGeometry(geom: BufferGeometry, objectIndex: dynamic = definedExternally): CSG + fun fromMesh(mesh: Mesh, objectIndex: dynamic = definedExternally): CSG + fun toGeometry(csg: CSG, toMatrix: Matrix4): BufferGeometry fun toMesh(csg: CSG, toMatrix: Matrix4): Mesh fun iEval(tokens: Mesh, index: Number? = definedExternally) fun eval(tokens: Mesh, doRemove: Boolean): Mesh @@ -35,15 +41,15 @@ external open class CSG { } } -external open class Vector(x: Number, y: Number, z: Number) : Vector3 { - open fun negated(): Vector - open fun plus(a: Vector): Vector - open fun minus(a: Vector): Vector - open fun times(a: Number): Vector - open fun dividedBy(a: Number): Vector - open fun lerp(a: Vector, t: Number): Any - open fun unit(): Vector - open fun cross(a: Vector): Any +external class Vector(x: Number, y: Number, z: Number) : Vector3 { + fun negated(): Vector + fun plus(a: Vector): Vector + fun minus(a: Vector): Vector + fun times(a: Number): Vector + fun dividedBy(a: Number): Vector + fun lerp(a: Vector, t: Number): Any + fun unit(): Vector + fun cross(a: Vector): Any } external interface IVector { @@ -52,21 +58,21 @@ external interface IVector { var z: Number } -external open class Vertex(pos: IVector, normal: IVector, uv: IVector? = definedExternally) { - open var pos: Vector - open var normal: Vector - open var uv: Vector - open fun clone(): Vertex - open fun flip() - open fun interpolate(other: Vertex, t: Number): Vertex +external class Vertex(pos: IVector, normal: IVector, uv: IVector? = definedExternally) { + var pos: Vector + var normal: Vector + var uv: Vector + fun clone(): Vertex + fun flip() + fun interpolate(other: Vertex, t: Number): Vertex } -external open class Plane(normal: Vector, w: Number) { - open var normal: Vector - open var w: Number - open fun clone(): Plane - open fun flip() - open fun splitPolygon( +external class Plane(normal: Vector, w: Number) { + var normal: Vector + var w: Number + fun clone(): Plane + fun flip() + fun splitPolygon( polygon: Polygon, coplanarFront: Array, coplanarBack: Array, @@ -80,10 +86,10 @@ external open class Plane(normal: Vector, w: Number) { } } -external open class Polygon(vertices: Array, shared: Any? = definedExternally) { - open var plane: Plane - open var vertices: Array - open var shared: Any - open fun clone(): Polygon - open fun flip() +external class Polygon(vertices: Array, shared: Any? = definedExternally) { + var plane: Plane + var vertices: Array + var shared: Any + fun clone(): Polygon + fun flip() } \ No newline at end of file diff --git a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt index fa13e91d..9e78800b 100644 --- a/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt +++ b/visionforge-threejs/src/main/kotlin/space/kscience/visionforge/solid/three/three.kt @@ -1,7 +1,7 @@ package space.kscience.visionforge.solid.three -import CSG -import info.laht.threekt.core.* +import info.laht.threekt.core.BufferGeometry +import info.laht.threekt.core.Layers import info.laht.threekt.external.controls.OrbitControls import info.laht.threekt.materials.Material import info.laht.threekt.math.Euler @@ -19,53 +19,13 @@ public val Solid.euler: Euler get() = Euler(rotationX, rotationY, rotationZ, rot public val MetaItem.vector: Vector3 get() = Vector3(node["x"].float ?: 0f, node["y"].float ?: 0f, node["z"].float ?: 0f) -public fun Geometry.toBufferGeometry(): BufferGeometry = BufferGeometry().apply { fromGeometry(this@toBufferGeometry) } internal fun Double.toRadians() = this * PI / 180 -public fun CSG.toGeometry(): Geometry { - val geom = Geometry() - - val vertices = ArrayList() - val faces = ArrayList() - - for (polygon in toPolygons()) { - val v0 = vertices.size - val pvs = polygon.vertices - - for (pv in pvs) { - vertices.add(Vector3().copy(pv.pos)) - } - - for (j in 3..polygon.vertices.size) { - val fc = Face3(v0, v0 + j - 2, v0 + j - 1, Vector3()) - fc.vertexNormals = arrayOf( - Vector3().copy(pvs[0].normal), - Vector3().copy(pvs[j - 2].normal), - Vector3().copy(pvs[j - 1].normal) - ) - - fc.normal = Vector3().copy(polygon.plane.normal) - faces.add(fc) - } - } - geom.vertices = vertices.toTypedArray() - geom.faces = faces.toTypedArray() -// val inv: Matrix4 = Matrix4().apply { getInverse(toMatrix) } -// geom.applyMatrix(toMatrix) - geom.verticesNeedUpdate = true - geom.elementsNeedUpdate = true - geom.normalsNeedUpdate = true - geom.computeBoundingSphere() - geom.computeBoundingBox() - return geom -} internal fun Any.dispose() { when (this) { - is Geometry -> dispose() is BufferGeometry -> dispose() - is DirectGeometry -> dispose() is Material -> dispose() is Mesh -> { geometry.dispose()