Added sphere geometry and proper opacity

This commit is contained in:
Alexander Nozik 2019-07-05 21:13:55 +03:00
parent b6cfb8b841
commit 67def6cdb5
9 changed files with 113 additions and 19 deletions

View File

@ -29,7 +29,17 @@ class ThreeDemoApp : ApplicationBase() {
}.build() }.build()
context.plugins.load(ThreeDemoGrid()).run { context.plugins.load(ThreeDemoGrid()).run {
demo("dynamic", "Dynamic properties demo") { demo("shapes", "Basic shapes"){
box(100.0,100.0,100.0) {
z = 110.0
}
sphere(50.0){
x = 110
detail = 200
}
}
demo("dynamic", "Dynamic properties") {
val group = group { val group = group {
box { box {
z = 110.0 z = 110.0
@ -97,7 +107,10 @@ class ThreeDemoApp : ApplicationBase() {
rotationY = PI / 4 rotationY = PI / 4
} }
box(100, 100, 100) box(100, 100, 100)
color(Colors.green) color{
"color" to Colors.lightgreen
"opacity" to 0.3
}
} }
composite(CompositeType.INTERSECT) { composite(CompositeType.INTERSECT) {
box(100, 100, 100) { box(100, 100, 100) {

View File

@ -1,9 +1,6 @@
package hep.dataforge.vis.spatial.three package hep.dataforge.vis.spatial.three
import hep.dataforge.meta.MetaItem import hep.dataforge.meta.*
import hep.dataforge.meta.double
import hep.dataforge.meta.get
import hep.dataforge.meta.int
import hep.dataforge.values.ValueType import hep.dataforge.values.ValueType
import hep.dataforge.vis.common.Colors import hep.dataforge.vis.common.Colors
import info.laht.threekt.materials.Material import info.laht.threekt.materials.Material
@ -53,6 +50,7 @@ fun MetaItem<*>?.material(): Material {
is MetaItem.NodeItem -> MeshPhongMaterial().apply { is MetaItem.NodeItem -> MeshPhongMaterial().apply {
(node["color"] ?: this@material).let { color = it.color() } (node["color"] ?: this@material).let { color = it.color() }
opacity = node["opacity"]?.double ?: 1.0 opacity = node["opacity"]?.double ?: 1.0
transparent = node["transparent"].boolean ?: (opacity < 1.0)
node["specularColor"]?.let { specular = it.color() } node["specularColor"]?.let { specular = it.color() }
} }
} }

View File

@ -0,0 +1,14 @@
package hep.dataforge.vis.spatial.three
import hep.dataforge.vis.spatial.Box
import hep.dataforge.vis.spatial.detail
import info.laht.threekt.geometries.BoxBufferGeometry
import kotlin.math.pow
object ThreeBoxFactory : MeshThreeFactory<Box>(Box::class) {
override fun buildGeometry(obj: Box) =
obj.detail?.let {
val segments = it.toDouble().pow(1.0 / 3.0).toInt()
BoxBufferGeometry(obj.xSize, obj.ySize, obj.zSize, segments, segments, segments)
} ?: BoxBufferGeometry(obj.xSize, obj.ySize, obj.zSize)
}

View File

@ -13,7 +13,6 @@ import hep.dataforge.vis.spatial.three.ThreeFactory.Companion.buildMesh
import info.laht.threekt.core.BufferGeometry import info.laht.threekt.core.BufferGeometry
import info.laht.threekt.core.Object3D import info.laht.threekt.core.Object3D
import info.laht.threekt.external.geometries.ConvexBufferGeometry import info.laht.threekt.external.geometries.ConvexBufferGeometry
import info.laht.threekt.geometries.BoxBufferGeometry
import info.laht.threekt.geometries.EdgesGeometry import info.laht.threekt.geometries.EdgesGeometry
import info.laht.threekt.geometries.WireframeGeometry import info.laht.threekt.geometries.WireframeGeometry
import info.laht.threekt.objects.LineSegments import info.laht.threekt.objects.LineSegments
@ -132,11 +131,6 @@ object ThreeShapeFactory : MeshThreeFactory<Shape>(Shape::class) {
} }
} }
object ThreeBoxFactory : MeshThreeFactory<Box>(Box::class) {
override fun buildGeometry(obj: Box) =
BoxBufferGeometry(obj.xSize, obj.ySize, obj.zSize)
}
//FIXME not functional yet //FIXME not functional yet
object ThreeConvexFactory : MeshThreeFactory<Convex>(Convex::class) { object ThreeConvexFactory : MeshThreeFactory<Convex>(Convex::class) {
override fun buildGeometry(obj: Convex): ConvexBufferGeometry { override fun buildGeometry(obj: Convex): ConvexBufferGeometry {

View File

@ -27,6 +27,7 @@ class ThreePlugin : AbstractPlugin() {
//Add specialized factories here //Add specialized factories here
objectFactories[Box::class] = ThreeBoxFactory objectFactories[Box::class] = ThreeBoxFactory
objectFactories[Convex::class] = ThreeConvexFactory objectFactories[Convex::class] = ThreeConvexFactory
objectFactories[Sphere::class] = ThreeSphereFactory
} }
private fun findObjectFactory(type: KClass<out DisplayObject>): ThreeFactory<*>? { private fun findObjectFactory(type: KClass<out DisplayObject>): ThreeFactory<*>? {

View File

@ -0,0 +1,30 @@
package hep.dataforge.vis.spatial.three
import hep.dataforge.vis.spatial.Sphere
import hep.dataforge.vis.spatial.detail
import info.laht.threekt.core.BufferGeometry
import info.laht.threekt.geometries.SphereBufferGeometry
import kotlin.math.pow
object ThreeSphereFactory : MeshThreeFactory<Sphere>(Sphere::class) {
override fun buildGeometry(obj: Sphere): BufferGeometry {
return obj.detail?.let {
val segments = it.toDouble().pow(0.5).toInt()
SphereBufferGeometry(
radius = obj.radius,
phiStart = obj.phiStart,
phiLength = obj.phi,
thetaStart = obj.thetaStart,
thetaLength = obj.theta,
widthSegments = segments,
heightSegments = segments
)
}?: SphereBufferGeometry(
radius = obj.radius,
phiStart = obj.phiStart,
phiLength = obj.phi,
thetaStart = obj.thetaStart,
thetaLength = obj.theta
)
}
}

View File

@ -8,9 +8,9 @@ import hep.dataforge.vis.common.DisplayObject
import hep.dataforge.vis.common.double import hep.dataforge.vis.common.double
class Box(parent: DisplayObject?, meta: Meta) : DisplayLeaf(parent, meta), Shape { class Box(parent: DisplayObject?, meta: Meta) : DisplayLeaf(parent, meta), Shape {
var xSize by double(1.0) var xSize by double(100.0)
var ySize by double(1.0) var ySize by double(100.0)
var zSize by double(1.0) var zSize by double(100.0)
//TODO add helper for color configuration //TODO add helper for color configuration

View File

@ -0,0 +1,32 @@
package hep.dataforge.vis.spatial
import hep.dataforge.meta.EmptyMeta
import hep.dataforge.meta.Meta
import hep.dataforge.vis.common.DisplayGroup
import hep.dataforge.vis.common.DisplayLeaf
import hep.dataforge.vis.common.DisplayObject
import hep.dataforge.vis.common.double
import kotlin.math.PI
class Sphere(parent: DisplayObject?, meta: Meta) : DisplayLeaf(parent, meta) {
var radius by double(50.0)
var phiStart by double(0.0)
var phi by double(2 * PI)
var thetaStart by double(0.0)
var theta by double(PI)
}
fun DisplayGroup.sphere(meta: Meta = EmptyMeta, action: Sphere.() -> Unit = {}) =
Sphere(this, meta).apply(action).also { add(it) }
fun DisplayGroup.sphere(
radius: Number,
phi: Number = 2 * PI,
theta: Number = PI,
meta: Meta = EmptyMeta,
action: Sphere.() -> Unit = {}
) = Sphere(this, meta).apply(action).apply {
this.radius = radius.toDouble()
this.phi = phi.toDouble()
this.theta = theta.toDouble()
}.also { add(it) }

View File

@ -97,7 +97,7 @@ enum class RotationOrder {
* Rotation order. Not inherited * Rotation order. Not inherited
*/ */
var DisplayObject.rotationOrder: RotationOrder var DisplayObject.rotationOrder: RotationOrder
get() = properties["rotation.order"].enum<RotationOrder>() ?: RotationOrder.XYZ get() = getProperty("rotation.order").enum<RotationOrder>() ?: RotationOrder.XYZ
set(value) { set(value) {
properties["rotation.order"] = value properties["rotation.order"] = value
} }
@ -139,15 +139,27 @@ fun DisplayObject.color(meta: Meta) {
this.properties["material"] = meta this.properties["material"] = meta
} }
fun DisplayObject.color(r: Int, g: Int, b: Int) = color(buildMeta { fun DisplayObject.color(builder: MetaBuilder.()->Unit) {
color(buildMeta(builder))
}
fun DisplayObject.color(r: Int, g: Int, b: Int) = color{
"red" to r "red" to r
"green" to g "green" to g
"blue" to b "blue" to b
}) }
//TODO add inherited scale //TODO add inherited scale
/**
* Preferred number of polygons for displaying the object. If not defined, uses shape or renderer default
*/
var DisplayObject.detail: Int?
get() = properties["detail"]?.int
set(value) {
properties["detail"] = value
}
object World { object World {
const val CAMERA_INITIAL_DISTANCE = -500.0 const val CAMERA_INITIAL_DISTANCE = -500.0
const val CAMERA_INITIAL_X_ANGLE = -50.0 const val CAMERA_INITIAL_X_ANGLE = -50.0