我想为我的游戏项目创建像人体一样复杂的形状。我可以使用trianglemesh在javafx 8中创建复杂的形状。而且我读过,可以将3ds模型导入javafx。任何人都可以告诉我如何导入它们并做一些事情,比如通过javafx代码旋转移动
感谢您的帮助
答案 0 :(得分: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 :(得分:1)
认为这是个人观点。
尽管如此,我不会质疑Javafx的力量,而possibility to build complex 3d models using Javafx
,I would not
建议使用它来构建这样的complex structures
,当你可以使用{{1}轻松实现它们时和其他软件,省力!
您可以选择使用此处提供的AutoDesk
http://www.interactivemesh.org/models/jfx3dimporter.html
这似乎很有希望!
也看看这个
答案 2 :(得分:1)
我在素描中制作了一个2x6并将其与小鸡/花花公子/雄蕊一起导入。他/她/它只是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);
}
}