javajavafx3duv-mappingjavafx-3d

Is there a way to show uv from meshview to flat drawing in javafx?


is there a way to show uv coords on gui like blender blender uv ?


Solution

  • Displaying uv coordinates to path

    uv coords javafx There is no built in gui object , but we can create a custom one with paths . We can retrieve uv indices from faces[] in mesh object and get its values in texcoords[]

    This is a single javafx app you can try . as far as i know there is no way to get mesh instance from a Shape3D object , so this works for trianglefaces custom mesh in meshview object only . I can't set mesh.fxml 'cause it will overflow char limit .

    App.java

    public class App extends Application {
        
        @Override
        public void start(Stage stage) throws IOException {
            Image uvImage = new Image("/uv.jpg");
            
            PerspectiveCamera pc = new PerspectiveCamera(true);
            pc.setTranslateZ(-6);
            MeshView mv = loadMeshView();
            PhongMaterial pm = new PhongMaterial();
            pm.setDiffuseMap(uvImage);
            mv.setMaterial(pm);
            Group group3d = new Group(pc, mv);
            
            RotateTransition animation = new RotateTransition(Duration.seconds(4), mv);
             animation.setInterpolator(Interpolator.LINEAR);
            animation.setAxis(Rotate.Y_AXIS);
            animation.setByAngle(360);
            animation.setCycleCount(100);
           
            
            animation.play();
            SubScene subScene = new SubScene(group3d, 400, 400, true, SceneAntialiasing.BALANCED);
            subScene.setFill(Color.AQUAMARINE);
            subScene.setCamera(pc);
            
            ImageView imageView = new ImageView(uvImage);
            imageView.setFitHeight(400);
            imageView.setFitWidth(400);
            Group groupUv = new Group(imageView);
            
            makeUvLines(groupUv, mv);
            Text u = new Text("U");
            Text v = new Text("V");
            v.setTranslateY(200);
            v.setTranslateX(-20);
            u.setTranslateX(200);
            u.setTranslateY(420);
            
            Line uLine = new Line(0, 0, 0, 400);
            Polygon uArrow = new Polygon(0, 0, 8, 8, -8, 8);
            Polygon vArrow = new Polygon(400, 400, 392, 408, 392, 392);
            
            Line vLine = new Line(0, 400, 400, 400);
            
            Pane ap = new Pane(groupUv, uLine, vLine, uArrow, vArrow, u, v);
            ap.setPrefSize(400, 400);
            ap.setMaxSize(400, 400);
            ap.setStyle("-fx-background-color:yellow");
            HBox.setHgrow(ap, Priority.NEVER);
            
            HBox hb = new HBox(subScene, ap);
            hb.setSpacing(30);
            hb.setAlignment(Pos.CENTER);
            
            var scene = new Scene(hb, 800, 600);
            stage.setTitle("JavaFX UV Visualizer");
            stage.setScene(scene);
            stage.show();
        }
        
        public static void main(String[] args) {
            launch();
        }
        
        private void makeUvLines(Group groupUv, MeshView meshView) {
            TriangleMesh mesh = (TriangleMesh) meshView.getMesh();
            
            int[] faces = mesh.getFaces().toArray(new int[mesh.getFaces().size()]);
            float[] texCoords = mesh.getTexCoords().toArray(new float[mesh.getTexCoords().size()]);
            
            for (int i = 0; i < faces.length; i++) {
                float scale = 400;
                float startU = texCoords[2 * faces[i + 1]];
                float startV = texCoords[((2 * faces[i + 1]) + 1)];
                
                float u1 = texCoords[2 * faces[i + 3]];
                float v1 = texCoords[((2 * faces[i + 3]) + 1)];
                
                float endU = texCoords[2 * faces[i + 5]];
                float endV = texCoords[((2 * faces[i + 5]) + 1)];
                
                Path p = new Path(
                        new MoveTo(startU * scale, startV * scale),
                        new LineTo(u1 * scale, v1 * scale),
                        new LineTo(endU * scale, endV * scale),
                        new ClosePath()
                );
                p.setStroke(Color.ORANGE);
                p.setFill(Color.color(0.1, 0.3, 0.1, 0.7));
                groupUv.getChildren().add(p);
                
                i += 5;
            }        
            
        }
        
        private MeshView loadMeshView() throws IOException {
            FXMLLoader loader = new FXMLLoader(App.class.getResource("/mesh.fxml"));
            loader.load();
            MeshView meshView = loader.getRoot();
            
            return meshView;
        }
        
    }
    

    uv.jpguv test image