2014-03-04 89 views
0

我想爲我的遊戲項目創建類似於人體的複雜形狀。可以使用trianglemesh在javafx 8中創建複雜的形狀。我也讀過,可以將3ds模型導入到javafx中。任何一個可以告訴我如何導入和JavaFX代碼做的事情一樣旋轉移動Javafx 8 3D複雜形狀

感謝您的幫助

+0

作爲@Itachi說,最好使用3D軟件開發工具包,或者如果你想提高你的手的JavaFX。然後繼續。祝你好運 :) – TheLittleNaruto

回答

2

認爲這是個人意見。

雖然,我並不懷疑的JavaFX的功率和​​,I would not建議使用它建立這樣complex structures,當你可以輕鬆地使用他們和AutoDesk其他軟件,以較少的努力實現!

你有選擇使用early access可以在這裏

http://www.interactivemesh.org/models/jfx3dimporter.html

這似乎是非常有前途的導入他們在您的JavaFX應用程序!

看看這個問題,以及

http://www.interactivemesh.org/models/jfx3dbrowser.html

2

此外,如果你去到Oracle,一個3DViewer是JFX樣本,這將在3D文件導出到FXML,從而產生相應的組中.. 。還能夠將FXML重新導入到Viewer中... Samples ...

雖然如上所述,InteractiveMesh有一些很好的工具。

這就是簡單的方法,如果你已經有一個3D文件...否則點,texCoord和麪陣列可以是相當艱鉅的。 我花了相當長一段時間來創建一個圓環..點都很好,texCoords都很好,臉上是痛苦...

這只是我的自訂圓環

/* 
Let the radius from the center of the hole to the center of the torus tube be "c", 
and the radius of the tube be "a". 
Then the equation in Cartesian coordinates for a torus azimuthally symmetric about the z-axis is 
(c-sqrt(x^2+y^2))^2+z^2=a^2 
and the parametric equations are 
x = (c + a * cos(v)) * cos(u) 
y = (c + a * cos(v)) * sin(u) 
z = a * sin(v)  
(for u,v in [0,2pi). 

Three types of torus, known as the standard tori, are possible, 
depending on the relative sizes of a and c. c>a corresponds to the ring torus (shown above), 
c=a corresponds to a horn torus which is tangent to itself at the point (0, 0, 0), 
and c<a corresponds to a self-intersecting spindle torus (Pinkall 1986). 
*/ 
public static TriangleMesh createToroidMesh(float radius, float tRadius, int tubeDivisions, int radiusDivisions) { 
    int POINT_SIZE = 3, TEXCOORD_SIZE = 2, FACE_SIZE = 6; 
    int numVerts = tubeDivisions * radiusDivisions; 
    int faceCount = numVerts * 2; 
    float[] points = new float[numVerts * POINT_SIZE], 
      texCoords = new float[numVerts * TEXCOORD_SIZE]; 
    int[] faces = new int[faceCount * FACE_SIZE], 
      smoothingGroups; 

    int pointIndex = 0, texIndex = 0, faceIndex = 0, smoothIndex = 0; 
    float tubeFraction = 1.0f/tubeDivisions; 
    float radiusFraction = 1.0f/radiusDivisions; 
    float x, y, z; 

    int p0 = 0, p1 = 0, p2 = 0, p3 = 0, t0 = 0, t1 = 0, t2 = 0, t3 = 0; 

    // create points 
    for (int tubeIndex = 0; tubeIndex < tubeDivisions; tubeIndex++) { 

     float radian = tubeFraction * tubeIndex * 2.0f * 3.141592653589793f; 

     for (int radiusIndex = 0; radiusIndex < radiusDivisions; radiusIndex++) { 

      float localRadian = radiusFraction * radiusIndex * 2.0f * 3.141592653589793f; 

      points[pointIndex]  = x = (radius + tRadius * ((float) Math.cos(radian))) * ((float) Math.cos(localRadian)); 
      points[pointIndex + 1] = y = (radius + tRadius * ((float) Math.cos(radian))) * ((float) Math.sin(localRadian)); 
      points[pointIndex + 2] = z = (tRadius * (float) Math.sin(radian)); 

      pointIndex += 3; 

      float r = radiusIndex < tubeDivisions ? tubeFraction * radiusIndex * 2.0F * 3.141592653589793f : 0.0f; 
      texCoords[texIndex] = (0.5F + (float) (Math.sin(r) * 0.5D));; 
      texCoords[texIndex + 1] = ((float) (Math.cos(r) * 0.5D) + 0.5F); 

      texIndex += 2; 

     } 

    } 
    //create faces   
    for (int point = 0; point < (tubeDivisions) ; point++) { 
     for (int crossSection = 0; crossSection < (radiusDivisions) ; crossSection++) { 
      p0 = point * radiusDivisions + crossSection; 
      p1 = p0 >= 0 ? p0 + 1 : p0 - (radiusDivisions); 
       p1 = p1 % (radiusDivisions) != 0 ? p0 + 1 : p0 - (radiusDivisions - 1); 
      p2 = (p0 + radiusDivisions) < ((tubeDivisions * radiusDivisions)) ? p0 + radiusDivisions : p0 - (tubeDivisions * radiusDivisions) + radiusDivisions ; 
      p3 = p2 < ((tubeDivisions * radiusDivisions) - 1) ? p2 + 1 : p2 - (tubeDivisions * radiusDivisions) + 1; 
       p3 = p3 % (radiusDivisions) != 0 ? p2 + 1 : p2 - (radiusDivisions - 1); 

      t0 = point * (radiusDivisions) + crossSection; 
      t1 = t0 >= 0 ? t0 + 1 : t0 - (radiusDivisions); 
       t1 = t1 % (radiusDivisions) != 0 ? t0 + 1 : t0 - (radiusDivisions - 1); 
      t2 = (t0 + radiusDivisions) < ((tubeDivisions * radiusDivisions)) ? t0 + radiusDivisions : t0 - (tubeDivisions * radiusDivisions) + radiusDivisions ; 
      t3 = t2 < ((tubeDivisions * radiusDivisions) - 1) ? t2 + 1 : t2 - (tubeDivisions * radiusDivisions) + 1; 
       t3 = t3 % (radiusDivisions) != 0 ? t2 + 1 : t2 - (radiusDivisions - 1); 

      try { 
       faces[faceIndex]  = (p2); 
       faces[faceIndex + 1] = (t3); 
       faces[faceIndex + 2] = (p0); 
       faces[faceIndex + 3] = (t2); 
       faces[faceIndex + 4] = (p1); 
       faces[faceIndex + 5] = (t0); 

       faceIndex += FACE_SIZE; 

       faces[faceIndex]  = (p2); 
       faces[faceIndex + 1] = (t3); 
       faces[faceIndex + 2] = (p1); 
       faces[faceIndex + 3] = (t0); 
       faces[faceIndex + 4] = (p3); 
       faces[faceIndex + 5] = (t1); 
       faceIndex += FACE_SIZE; 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    TriangleMesh localTriangleMesh = new TriangleMesh(); 
    localTriangleMesh.getPoints().setAll(points); 
    localTriangleMesh.getTexCoords().setAll(texCoords); 
    localTriangleMesh.getFaces().setAll(faces); 


    return localTriangleMesh; 
} 
1

我做了一個在sketchup 2x6並與小雞/夥計/ androgyne一起進口。他/她/它只是2D。你需要得到的jar文件(jimColModelImporterJFX.jar) here.

import com.interactivemesh.jfx.importer.Viewpoint; 
import com.interactivemesh.jfx.importer.col.ColAsset; 
import com.interactivemesh.jfx.importer.col.ColModelImporter; 
//import com.interactivemesh.jfx.importer.stl.StlMeshImporter; 

import javafx.application.Application; 
import javafx.scene.*; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 
import javafx.scene.paint.PhongMaterial; 
import javafx.scene.transform.Rotate; 
import javafx.scene.transform.Translate; 

import java.io.File; 
import java.util.Map; 

public class Importer3D extends Application { 
    //you'll have to make your own file in sketchup and save it as *.dae 
    //http://help.sketchup.com/en/article/114347 
    private static final String DAE_FILENAME = ("2x6stud-1.dae"); 

    private static final int VIEWPORT_SIZE = 800; 

    private Group root; 

    public Rotate rx = new Rotate(); 
    { rx.setAxis(Rotate.X_AXIS); } 
    public Rotate ry = new Rotate(); 
    { ry.setAxis(Rotate.Y_AXIS); } 
    public Rotate rz = new Rotate(); 
    { rz.setAxis(Rotate.Z_AXIS); } 
    Translate t = new Translate(); 

    static Node[] loadNodes() { 
     File file = new File(DAE_FILENAME); 
     ColModelImporter importer = new ColModelImporter(); 
     importer.read(file); 
     Node[] nodes = importer.getImport(); 

     ColAsset colAsset = importer.getAsset(); 
     System.out.println("asset title " + colAsset.getTitle()); 
     System.out.println("asset unit name " + colAsset.getUnitName()); 
     System.out.println("asset unit meter " + colAsset.getUnitMeter()); 
     System.out.println("asset up axis " + colAsset.getUpAxis()); 
     Map<String, PhongMaterial> materials = importer.getNamedMaterials(); 
     for (Map.Entry<String, PhongMaterial> e : materials.entrySet()) { 
      System.out.println("phong material " + e.getKey() + " -> " + e.getValue()); 
     } 
     Map<String, Node> namedNodes = importer.getNamedNodes(); 
     for (Map.Entry<String, Node> e : namedNodes.entrySet()) { 
      System.out.println("nodes " + e.getKey() + " -> " + e.getValue()); 
     } 
     Viewpoint[] viewpoints = importer.getViewpoints(); 
     if (viewpoints != null) for (Viewpoint v : viewpoints) { 
       System.out.println("viewpoint " + v); 
      } 

     return nodes; 
    } 

    private Group buildScene() { 
     Node[] nodes = loadNodes(); 
     root = new Group(nodes); 
     return root; 
    } 

    private PerspectiveCamera addCamera(Scene scene) { 
     PerspectiveCamera camera = new PerspectiveCamera(); 
     camera.getTransforms().addAll(t, rz, ry, rx); 

     camera.setVerticalFieldOfView(true); 
     camera.setFieldOfView(10d); 
     System.out.println("Near Clip: " + camera.getNearClip()); 
     System.out.println("Far Clip: " + camera.getFarClip()); 
     System.out.println("FOV:  " + camera.getFieldOfView()); 

     scene.setCamera(camera); 
     return camera; 
    } 

    @Override 
    public void start(Stage stage) { 
     Group group = buildScene(); 
     group.setScaleX(10); 
     group.setScaleY(10); 
     group.setScaleZ(10); 
     group.setTranslateX(VIEWPORT_SIZE/2); 
     group.setTranslateY(VIEWPORT_SIZE/2); 

     Scene scene = new Scene(group, VIEWPORT_SIZE, VIEWPORT_SIZE, true); 
     scene.setFill(Color.rgb(10, 10, 40)); 
     addCamera(scene); 
     stage.setTitle("Collada importer"); 
     stage.setScene(scene); 
     stage.show(); 

     scene.setOnKeyPressed((evt) -> { 
      switch (evt.getCode()) { 
       case UP: 
        rx.setAngle(rx.getAngle() + 5); 
        break; 
       case DOWN: 
        rx.setAngle(rx.getAngle() - 5); 
        break; 
       case RIGHT: 
        t.setX(t.getX() + 10); 
        //camera.setTranslateX(camera.getTranslateX()+10); 
        break; 
       case LEFT: 
        t.setX(t.getX() - 10); 
        //camera.setTranslateX(camera.getTranslateX()-10); 
        break; 
       case Z: 
        double zoom = evt.isShortcutDown() ? -10 : +10; 
        t.setZ(t.getZ() + zoom); 
        //camera.setTranslateZ(camera.getTranslateZ()+zoom); 
        break; 
      } 
     }); 
    } 

    public static void main(String[] args) { 
     System.setProperty("prism.dirtyopts", "false"); 
     launch(args); 
    } 
} 

enter image description here