为什么我的物体的纹理没有显示?

2020年8月17日 17点热度 0条评论

这是我使用搅拌机制作的椅子:

现在,当我使用Java3D显示它时,我得到的是:

为什么我的纹理没有显示在Java中?这是我显示椅子的代码:

package com;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.Background;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.DirectionalLight;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;

import com.microcrowd.loader.java3d.max3ds.Loader3DS;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class LoadAnObject extends Applet
{
    public LoadAnObject()
    {
        setLayout(new BorderLayout());
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(config);
        add("Center", canvas);

        BranchGroup content = getScene();
        content.compile();

        SimpleUniverse universe = new SimpleUniverse(canvas);
        universe.getViewingPlatform().setNominalViewingTransform();
        universe.addBranchGraph(content);
    }

    public BranchGroup getScene()
    {
        BranchGroup group = new BranchGroup();

        Loader3DS loader = new Loader3DS();

        Scene scene = null;

        try
        {
            scene = loader.load("/Users/John/ArtOfIllusion/Chair.3ds");
        }catch(Exception e){e.printStackTrace();}

        TransformGroup rotateGroup = new TransformGroup();
        Transform3D rotate = new Transform3D();
        rotate.rotX(- Math.PI / 8);
        rotateGroup.setTransform(rotate);
        rotateGroup.addChild(scene.getSceneGroup());

        TransformGroup objSpin = new TransformGroup();
        objSpin.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        //objSpin.addChild(new ColorCube(.5));
        Alpha rotationAlpha = new Alpha(-1, 4000);
        RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objSpin);

        BoundingSphere boundSphere = new BoundingSphere(new Point3d(0.0f, 0.0f, 0.0f), 200);
        rotator.setSchedulingBounds(boundSphere);
        objSpin.addChild(rotator);
        objSpin.addChild(rotateGroup);

        TransformGroup moveGroup = new TransformGroup();
        Transform3D move = new Transform3D();
        move.setTranslation(new Vector3f(0.0f, 0.0f, -20.0f));
        moveGroup.setTransform(move);
        moveGroup.addChild(objSpin);

        group.addChild(moveGroup);

        Background background = new Background(0.0f, 1.0f, 1.0f);
        background.setApplicationBounds(new BoundingSphere());
        group.addChild(background);

        Color3f light1Color = new Color3f(1.0f, 1.0f, 1.0f);
        BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
        Vector3f light1Direction = new Vector3f(0, 0, 10);
        DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction);
        light1.setInfluencingBounds(bounds);
        group.addChild(light1);

        return group;
    }

    public static void main(String args[])
    {
        Frame frame = new MainFrame(new LoadAnObject(), 256, 256);
    }
}

我正在使用的文件格式是.3ds,这是我从中获取加载器的位置:


http://sourceforge.net/projects/java3dsloader/

我如何获取椅子的纹理以Java显示?

编辑:

我将椅子编译成.3ds文件而不是目标文件,现在我必须使用从这里得到的.3ds加载程序:


http://sourceforge.net/projects/java3dsloader/

代码并没有太大的改变,我现在使用Loader3DS而不是使用ObjectFile

所以现在很明显,您可以看到材质转移了,但纹理没有转移,我如何显示纹理?

解决方案如下:

除非纹理是从图像生成的,否则Java显然不会加载纹理。我能够加载纹理的唯一方法是从图像映射纹理。我尝试了几种不同的格式,但仍然一无所获。如果要尝试在Java3D中使用纹理,请确保使用“图像映射”纹理!