从搅拌机导出的JAVA3D加载.obj文件

时间:2018-05-23 13:00:07

标签: blender java-3d

我正在尝试将.obj文件从blender加载到Java3D但始终出现错误:

com.sun.j3d.utils.image.ImageException:javax.imageio.IIOException:无法从URL获取输入流!

最奇怪的是,当我从网上加载模型时,一切都还可以,但是当我将相同的文件导入到blender然后导出它(没有任何更改)时,它不起作用。

    import java.applet.*;
    import java.awt.*;

    import javax.media.j3d.*;
    import javax.vecmath.*;

    import com.sun.j3d.utils.applet.MainFrame;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import com.sun.j3d.utils.universe.PlatformGeometry;
    import com.sun.j3d.utils.behaviors.keyboard.*;

    import com.sun.j3d.loaders.Scene;

    import com.sun.j3d.loaders.objectfile.ObjectFile;

    import java.io.*;

    import java.awt.event.KeyListener;
    import java.awt.event.KeyEvent;

    public class Mykeynavbeh extends Applet implements KeyListener {

     private SimpleUniverse universe = null;
     private Canvas3D canvas = null;
     private TransformGroup viewtrans = null;

     private TransformGroup tg = null;
     private Transform3D t3d = null;
     private Transform3D t3dstep = new Transform3D();
     private Matrix4d matrix = new Matrix4d();

     public Mykeynavbeh() {
      setLayout(new BorderLayout());
      GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();

      canvas = new Canvas3D(config);
      add("Center", canvas);
      universe = new SimpleUniverse(canvas);

      BranchGroup scene = createSceneGraph();
      universe.getViewingPlatform().setNominalViewingTransform();

      universe.getViewer().getView().setBackClipDistance(100.0);

      canvas.addKeyListener(this);

      universe.addBranchGraph(scene);
     }

     private BranchGroup createSceneGraph() {
      BranchGroup objRoot = new BranchGroup();

      BoundingSphere bounds = new BoundingSphere(new Point3d(), 10000.0);

      viewtrans = universe.getViewingPlatform().getViewPlatformTransform();

      KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(viewtrans);
      keyNavBeh.setSchedulingBounds(bounds);
      PlatformGeometry platformGeom = new PlatformGeometry();
      platformGeom.addChild(keyNavBeh);
      universe.getViewingPlatform().setPlatformGeometry(platformGeom);

      objRoot.addChild(createLadybird());

      Background background = new Background();
      background.setColor(0.75f, 0.69f, 0.680f);
      background.setApplicationBounds(bounds);
      objRoot.addChild(background);

      return objRoot;
     }

     private BranchGroup createLadybird() {

      BranchGroup objRoot = new BranchGroup();
      tg = new TransformGroup();
      t3d = new Transform3D();

      tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

      t3d.setTranslation(new Vector3d(-0.15, -0.3, -5.0));
      t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f));
      t3d.setScale(1.0);

      tg.setTransform(t3d);

      ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);
      Scene s = null;

      File file = new java.io.File("model/female2.obj");

      try {
       s = loader.load(file.toURI().toURL());
      } catch (Exception e) {
       System.err.println(e);
       System.exit(1);
      }

      tg.addChild(s.getSceneGroup());

      objRoot.addChild(tg);
      objRoot.addChild(createLight());

      objRoot.compile();

      return objRoot;

     }

     private Light createLight() {
      DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f,
        1.0f, 1.0f), new Vector3f(-0.3f, 0.2f, -1.0f));

      light.setInfluencingBounds(new BoundingSphere(new Point3d(), 10000.0));

      return light;
     }

     public static void main(String[] args) {
      Mykeynavbeh applet = new Mykeynavbeh();
      Frame frame = new MainFrame(applet, 800, 600);
     }

     public void keyTyped(KeyEvent e) {
      char key = e.getKeyChar();

      if (key == 'd') {
       t3dstep.set(new Vector3d(0.0, 0.0, -0.1));
       tg.getTransform(t3d);
       t3d.mul(t3dstep);
       tg.setTransform(t3d);
      }

      if (key == 's') {

       t3dstep.rotY(Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'f') {

       t3dstep.rotY(-Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'r') {

       t3dstep.rotX(Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'v') {

       t3dstep.rotX(-Math.PI / 32);
       tg.getTransform(t3d);
       t3d.get(matrix);
       t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0));
       t3d.mul(t3dstep);
       t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23));
       tg.setTransform(t3d);

      }

      if (key == 'e') {
       t3dstep.set(new Vector3d(0.0, 0.1, 0.0));
       tg.getTransform(t3d);
       t3d.mul(t3dstep);
       tg.setTransform(t3d);
      }

      if (key == 'c') {
       t3dstep.set(new Vector3d(0.0, -0.1, 0.0));
       tg.getTransform(t3d);
       t3d.mul(t3dstep);
       tg.setTransform(t3d);
      }
     }

     public void keyReleased(KeyEvent e) {
     }

     public void keyPressed(KeyEvent e) {
     }
    }

1 个答案:

答案 0 :(得分:0)

请尝试以下步骤:

[1]打开.mtl文件,并确保所有纹理路径都是相对的:

map_Kd female02_1.jpg

不是这样的:

map_Kd C:\ Users \ Username \ MyJava3D \ Female02 \ model \ female02_1.jpg(绝对路径)

在我的PC上,如果纹理路径不正确,则该错误在Java3D 1.5.1中发生。 在Java3D 1.6.0中,不会发生任何错误,但纹理不会显示。

您是否在谈论Sweet Home 3D中的female02.obj? (我只是从文件名“ female2.obj”中猜测得出。)如果是这样,则需要[2]和[3]。

[2]对于多个纹理, 在导出模型之前,请检查“对象作为OBJ组”和“材料组”(在左下角)。 否则,纹理将不会显示。

[3]对于.obj文件,纹理可能无法正确显示。如果是这样,请将所有法线向外翻转。

搅拌器2.77:

对于每个部分(头发,脸部,衣服……)

1)在“对象”模式下,右键单击零件。

2)按Tab键进入“编辑”模式。

3)在“ 3D视图”窗口中,按Ctrl-n几次。

4)按Tab键进入对象模式。

5)重复上述步骤1)至4)。

[4]另外,在导出的.obj文件中,注释掉所有以'o'开头的行。 否则,将发生以下错误: com.sun.j3d.loaders.ParsingErrorException:无法识别的令牌,行〜