Adjust materials for 3D

This commit is contained in:
Alexander Nozik 2024-01-01 15:54:29 +03:00
parent 067ed4aa3d
commit 58d7bd9383
8 changed files with 62 additions and 19 deletions

View File

@ -8,6 +8,16 @@ kscience {
jvm()
js{
binaries.executable()
browser{
commonWebpackConfig{
cssSupport{
enabled = true
}
scssSupport{
enabled = true
}
}
}
}
dependencies {
implementation(projects.visionforgeSolid)

View File

@ -24,6 +24,11 @@ fun VisionLayout<Solid>.demo(name: String, title: String = name, block: SolidGro
block()
ambientLight {
color(Colors.white)
intensity = 0.5
}
pointLight(0, 0, 1000) {
color(Colors.white)
intensity = 10.0
}
}
render(Name.parse(name), vision, meta)
@ -122,13 +127,16 @@ fun VisionLayout<Solid>.showcase() {
demo("extrude", "extruded shape") {
extruded {
shape {
polygon(8, 50)
polygon(32, 50)
}
for (i in 0..100) {
layer(i * 5, 20 * sin(2 * PI / 100 * i), 20 * cos(2 * PI / 100 * i))
}
rotationY = -PI / 2
material {
type = "lambert"
color(Colors.teal)
rotationX = -PI / 2
}
}
}
@ -161,7 +169,15 @@ fun VisionLayout<Solid>.showcase() {
}
demo("STL", "STL loaded from URL") {
stl("https://ozeki.hu/attachments/116/Menger_sponge_sample.stl")
stl("Menger_sponge_sample.stl") {
scale(100f)
material {
type = "phong"
color("red")
specularColor("blue")
}
}
//stl("https://ozeki.hu/attachments/116/Menger_sponge_sample.stl")
}
}

View File

@ -203,6 +203,10 @@ public var Solid.position: Float32Vector3D? by float32Vector(POSITION_KEY, 0f)
public var Solid.rotation: Float32Vector3D? by float32Vector(ROTATION_KEY, 0f)
public var Solid.scale: Float32Vector3D? by float32Vector(SCALE_KEY, 1f)
public fun Solid.scale(scaleFactor: Number) {
scale = Float32Vector3D(scaleFactor, scaleFactor, scaleFactor)
}
public var Solid.x: Number by float32(X_POSITION_KEY, 0f)
public var Solid.y: Number by float32(Y_POSITION_KEY, 0f)
public var Solid.z: Number by float32(Z_POSITION_KEY, 0f)

View File

@ -18,6 +18,8 @@ import space.kscience.visionforge.solid.SolidMaterial.Companion.MATERIAL_OPACITY
@VisionBuilder
public class SolidMaterial : Scheme() {
public var type: String by string("default", key = TYPE_KEY)
/**
* Primary web-color for the material
*/
@ -67,7 +69,7 @@ public class SolidMaterial : Scheme() {
value(TYPE_KEY, ValueType.STRING) {
inherited = true
allowedValues = listOf("default".asValue(), "simple".asValue())
allowedValues = listOf("default", "basic", "lambert", "phong").map { it.asValue() }
default("default")
}

View File

@ -10,10 +10,7 @@ import space.kscience.visionforge.getStyleNodes
import space.kscience.visionforge.solid.ColorAccessor
import space.kscience.visionforge.solid.SolidMaterial
import space.kscience.visionforge.solid.SolidReference
import three.materials.LineBasicMaterial
import three.materials.Material
import three.materials.MeshBasicMaterial
import three.materials.MeshStandardMaterial
import three.materials.*
import three.math.Color
import three.objects.Mesh
@ -56,11 +53,23 @@ public object ThreeMaterials {
}
internal fun buildMaterial(meta: Meta): Material = when (meta[SolidMaterial.TYPE_KEY]?.string) {
"simple" -> MeshBasicMaterial().apply {
"basic" -> MeshBasicMaterial().apply {
color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_COLOR
wireframe = meta[SolidMaterial.WIREFRAME_KEY].boolean ?: false
}
"lambert" -> MeshLambertMaterial().apply {
color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_COLOR
emissive = meta[SolidMaterial.EMISSIVE_COLOR_KEY]?.threeColor() ?: DEFAULT_EMISSIVE_COLOR
wireframe = meta[SolidMaterial.WIREFRAME_KEY].boolean ?: false
}
"phong" -> MeshPhongMaterial().apply {
color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_COLOR
emissive = meta[SolidMaterial.EMISSIVE_COLOR_KEY]?.threeColor() ?: DEFAULT_EMISSIVE_COLOR
wireframe = meta[SolidMaterial.WIREFRAME_KEY].boolean ?: false
}
else -> MeshStandardMaterial().apply {
color = meta[SolidMaterial.COLOR_KEY]?.threeColor() ?: DEFAULT_COLOR
emissive = meta[SolidMaterial.EMISSIVE_COLOR_KEY]?.threeColor() ?: DEFAULT_EMISSIVE_COLOR

View File

@ -44,16 +44,15 @@ public class ThreePlugin : AbstractPlugin(), ComposeVisionRenderer {
objectFactories[SolidLabel::class] = ThreeCanvasLabelFactory
objectFactories[AmbientLightSource::class] = ThreeAmbientLightFactory
objectFactories[PointLightSource::class] = ThreePointLightFactory
objectFactories[StlSolid::class] = ThreeStlFactory
objectFactories[StlUrlSolid::class] = ThreeStlFactory
objectFactories[StlBinarySolid::class] = ThreeStlFactory
objectFactories[AxesSolid::class] = ThreeAxesFactory
}
@Suppress("UNCHECKED_CAST")
private fun findObjectFactory(type: KClass<out Vision>): ThreeFactory<Solid>? {
return (objectFactories[type]
?: context.gather<ThreeFactory<*>>(ThreeFactory.TYPE).values.find { it.type == type })
private fun findObjectFactory(type: KClass<out Vision>): ThreeFactory<Solid>? =
(objectFactories[type] ?: context.gather<ThreeFactory<*>>(ThreeFactory.TYPE).values.find { it.type == type })
as ThreeFactory<Solid>?
}
/**
* Build an Object3D representation of the given [Solid].

View File

@ -16,7 +16,9 @@ fun ArrayBuffer.toByteArray(): ByteArray = Int8Array(this).unsafeCast<ByteArray>
public object ThreeStlFactory : ThreeMeshFactory<StlSolid>(StlSolid::class) {
private val loader = STLLoader().apply {
requestHeader = listOf("Access-Control-Allow-Origin: *")
requestHeader = listOf(
"Access-Control-Allow-Origin: *",
)
}
override suspend fun buildGeometry(obj: StlSolid): BufferGeometry = when (obj) {
@ -25,6 +27,7 @@ public object ThreeStlFactory : ThreeMeshFactory<StlSolid>(StlSolid::class) {
loader.load(
url = obj.url,
onLoad = {
console.info("Loaded STL from ${obj.url}")
continuation.resume(it)
},
onError = {