Reputation: 43
I'm trying to understand the JCSG library by using the following test code, where I take the difference between a FXyz mesh/cloth and a sphere to create a hole in the mesh so as to display the underlying mesh image. Is it possible to do this, if so how? All my attempts produce a red circle.
import org.fxyz.shapes.primitives.SpheroidMesh
import org.fxyz.utils.MeshUtils
import org.fxyz.shapes.complex.cloth.ClothMesh
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.image.Image
import scalafx.scene.paint.PhongMaterial
import scalafx.scene.shape._
import scalafx.scene.{Group, PerspectiveCamera, Scene, SceneAntialiasing}
import scalafx.scene.paint.Color
import scala.collection.JavaConversions._
object ClothTest1 extends JFXApp {
stage = new PrimaryStage {
scene = new Scene(600, 600, true, SceneAntialiasing.Balanced) {
fill = Color.LightGray
val rose = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Redoute_-_Rosa_gallica_purpuro-violacea_magna.jpg/800px-Redoute_-_Rosa_gallica_purpuro-violacea_magna.jpg"
val img = new Image(rose, 400, 400, false, true)
val meshView = new ClothMesh(4, 4, 400, 400)
meshView.setDrawMode(DrawMode.Fill)
meshView.setCullFace(CullFace.None)
meshView.setStyle("-fx-background-color: #00000000") // transparent
meshView.setWidth(img.width.value)
meshView.setHeight(img.height.value)
meshView.setMaterial(new PhongMaterial(Color.White, img, null, null, null))
val fxyzSphere = new SpheroidMesh(120.0) {
setDrawMode(DrawMode.Fill)
setStyle("-fx-background-color: #00000000") // transparent
}
val csgHole = MeshUtils.mesh2CSG(fxyzSphere.getMesh())
val csgMesh = MeshUtils.mesh2CSG(meshView.getMesh())
val fxMesh = csgMesh.difference(csgHole).toJavaFXMesh.getAsMeshViews.head
root = new Group() {
children += fxMesh
translateX = 250
translateY = 250
}
camera = new PerspectiveCamera(false)
}
}
}
Upvotes: 1
Views: 1041
Reputation: 45486
FXyz and JSCG are different JavaFX 3D libraries. In order to be possible to exchange objects from one to the other, we created this method in FXyz:
public static CSG mesh2CSG(MeshView mesh);
which takes the TriangleMesh
of a JavaFX 3D object (not from those on the API) and generates a CSG one, by filling its polygons and vertices, but without textures. So in this process, any texture that the initial object had is lost.
As far as I know, JCSG primitives don't have textures.
So we also added this class on FXyz: CSGMesh
, in order to create a TriangleMesh
based on a CSG primitive, with something close to textures.
For obvious reasons, textures coordinates are based on the geometry of the body, and once you perform boolean operations, you don't have control over the result anymore.
With CSGMesh
, we create a TexturedMesh
, which allows:
but it doesn't allow diffuse maps with images or patterns.
Now you can take two FXyz 3D shapes, convert them to CSG primitives, perform boolean operations over them, and the resulting CSG primitive can be converted back to a 'textured' FXyz object, but without real texture coordinates.
As an example:
Given these two FXyz shapes:
CuboidMesh cuboidMesh = new CuboidMesh(400f,400f,4f,4,new Point3D(0f, 0f, 0f));
cuboidMesh.setTextureModeNone(Color.FIREBRICK);
SegmentedSphereMesh spheroidMesh = new SegmentedSphereMesh(40,0,0,120,new Point3D(0f,0f,0f));
spheroidMesh.setTextureModeNone(Color.GREENYELLOW);
You can generate the CSG objects of both, and get the difference:
CSG csgMesh = MeshUtils.mesh2CSG(cuboidMesh.getMesh());
CSG csgHole = MeshUtils.mesh2CSG(spheroidMesh.getMesh());
CSG difference = csgMesh.difference(csgHole);
convert this primitive to a TexturedMesh
:
CSGMesh meshResult = new CSGMesh(difference);
meshResult.setTextureModeNone(Color.FORESTGREEN);
You can see that with the resulting mesh is really hard to set the texture coordinates.
Finally, we can apply a density map with with some function of (x,y,z) coordinates for every vertex:
meshResult.setTextureModeVertices3D(1530,p->(double)p.x*p.y);
Unless you have some criteria like we have a 2D mesh (Z values are neglectable), and we could map texture coordinates with the (x,y) coordinates of the vertices... This is not done yet on FXyz, you are welcome to try it.
Upvotes: 2