创建类似于Google Map Photo-Sphere(JavaFX 3D)的Photo-Sphere [英] Create Photo-Sphere similar to Google Map Photo-Sphere (JavaFX 3D)

查看:125
本文介绍了创建类似于Google Map Photo-Sphere(JavaFX 3D)的Photo-Sphere的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JavaFX中创建类似于Google地图中的photohpere的光球?如果是,怎么办?

解决方案

是的,您可以在JavaFX中创建光球.

关于方式,有一个基于3D API中球体的简单解决方案,但是我们可以使用自定义网格实现改进的解决方案.

让我们从使用常规球体开始.我们只需要一个360º图像,例如一个.

我们要从球体内部看到,我们必须水平翻转图像,并将其添加到球体材料的扩散贴图中.

然后,我们只需要在球体的正中央安装一个摄像头,添加一些灯光并开始旋转即可.

@Override
public void start(Stage primaryStage) {
    PerspectiveCamera camera = new PerspectiveCamera(true);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setFieldOfView(90);
    Sphere sphere = new Sphere(1000);
    sphere.setCullFace(CullFace.NONE);
    PhongMaterial material = new PhongMaterial();
    /*
    "SonyCenter 360panorama" by François Reincke - Own work. Made using autostitch (www.autostitch.net).. 
    Licensed under CC BY-SA 3.0 via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:SonyCenter_360panorama.jpg#mediaviewer/File:SonyCenter_360panorama.jpg
    */
    material.setDiffuseMap(new Image(getClass().getResource("SonyCenter_360panorama_reversed.jpg").toExternalForm()));
    sphere.setMaterial(material);

    Group root3D = new Group(camera,sphere,new AmbientLight(Color.WHITE));

    Scene scene = new Scene(root3D, 800, 600, true, SceneAntialiasing.BALANCED);

    scene.setCamera(camera);

    primaryStage.setTitle("PhotoSphere in JavaFX3D");
    primaryStage.setScene(scene);
    primaryStage.show();

    final Timeline rotateTimeline = new Timeline();
    rotateTimeline.setCycleCount(Timeline.INDEFINITE);
    camera.setRotationAxis(Rotate.Y_AXIS);
    final KeyValue kv1 = new KeyValue(camera.rotateProperty(), 360);
    final KeyFrame kf1 = new KeyFrame(Duration.millis(30000), kv1);
    rotateTimeline.getKeyFrames().addAll(kf1);
    rotateTimeline.play();
}

现在,您将要向相机添加一些控件(以便进行导航).您会发现该解决方案在球体的顶部和底部存在一个薄弱点,这是因为图像的所有顶部或底部都位于一个点:

您可以在F(X)yz库的此处找到解决方案.名为SegmentedSphereMesh的自定义网格允许您裁剪球体的极端,因此图像可以保持其纵横比而不会被拉伸.

如果克隆并构建存储库,则将FXyz.jar添加到您的项目中,并使用以下代码替换上一片段中的Sphere:

    SegmentedSphereMesh sphere = new SegmentedSphereMesh(100,0,26,1000);
    sphere.setCullFace(CullFace.NONE);
    sphere.setTextureModeImage(getClass().getResource("SonyCenter_360panorama_reversed.jpg").toExternalForm());

在同一个库中,您可以基于一个立方体和每个面上的一个正方形图像找到SkyBox.还要检查高级相机选项.

最后,请注意,现在已经在F(X)yz-Sampler 应用程序.

Is it possible to create a photosphere in JavaFX that is similar to photoshpere in Google map? If yes, how?

解决方案

The answer is yes, you can create a photosphere in JavaFX.

As for the how, there's an easy solution based on a sphere from the 3D API, but we can implement an improved solution, with a custom mesh.

Let's start by using a regular sphere. We just need a 360º image, like this one.

As we want to see from the inside of the sphere, we have to flip the image horizontally, and add it to the sphere material's diffusion map.

Then we just need to set up a camera in the very center of the sphere, add some lights and start spinning.

@Override
public void start(Stage primaryStage) {
    PerspectiveCamera camera = new PerspectiveCamera(true);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setFieldOfView(90);
    Sphere sphere = new Sphere(1000);
    sphere.setCullFace(CullFace.NONE);
    PhongMaterial material = new PhongMaterial();
    /*
    "SonyCenter 360panorama" by François Reincke - Own work. Made using autostitch (www.autostitch.net).. 
    Licensed under CC BY-SA 3.0 via Wikimedia Commons - http://commons.wikimedia.org/wiki/File:SonyCenter_360panorama.jpg#mediaviewer/File:SonyCenter_360panorama.jpg
    */
    material.setDiffuseMap(new Image(getClass().getResource("SonyCenter_360panorama_reversed.jpg").toExternalForm()));
    sphere.setMaterial(material);

    Group root3D = new Group(camera,sphere,new AmbientLight(Color.WHITE));

    Scene scene = new Scene(root3D, 800, 600, true, SceneAntialiasing.BALANCED);

    scene.setCamera(camera);

    primaryStage.setTitle("PhotoSphere in JavaFX3D");
    primaryStage.setScene(scene);
    primaryStage.show();

    final Timeline rotateTimeline = new Timeline();
    rotateTimeline.setCycleCount(Timeline.INDEFINITE);
    camera.setRotationAxis(Rotate.Y_AXIS);
    final KeyValue kv1 = new KeyValue(camera.rotateProperty(), 360);
    final KeyFrame kf1 = new KeyFrame(Duration.millis(30000), kv1);
    rotateTimeline.getKeyFrames().addAll(kf1);
    rotateTimeline.play();
}

Now you will want to add some controls to the camera (so you can navigate). You will discover that this solution has a weak spot at the top and bottom of the sphere, due to the fact that all the top or bottom side of the image is located in one point:

You can find a solution to this problem at the F(X)yz library here. A custom mesh called SegmentedSphereMesh allows you to crop the extremes of the sphere, so the image can keep its aspect ratio without being stretched.

If you clone and build the repo, add FXyz.jar to your project, and just replace Sphere in the previous snippet with this:

    SegmentedSphereMesh sphere = new SegmentedSphereMesh(100,0,26,1000);
    sphere.setCullFace(CullFace.NONE);
    sphere.setTextureModeImage(getClass().getResource("SonyCenter_360panorama_reversed.jpg").toExternalForm());

In the same library you can find SkyBox, based on a cube and one square image on each of its faces. Also check the advanced camera options.

Finally, note that this and more 3D advanced shapes are now demonstrated in the F(X)yz-Sampler application.

这篇关于创建类似于Google Map Photo-Sphere(JavaFX 3D)的Photo-Sphere的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆