美文网首页程序员技术栈
Java3D实例引用-加载Obj 三维模型

Java3D实例引用-加载Obj 三维模型

作者: 编程小世界 | 来源:发表于2019-04-29 19:09 被阅读1次

OBJ 文件是 Alias|Wavefront 公司为它的一套基于工作站的 3D 建模和动画软件 "Advanced Visualizer" 开发的一种标准 3D 模型文件格式,很适合用于 3D 软件模型之间的互导,也可以通过 Maya 读写。比如你在 3dsMax 或 LightWave 中建了一个模型,想把它调到 Maya 里面渲染或动画,导出 OBJ 文件就是一种很好的选择。目前几乎所有知名的 3D 软件都支持 OBJ 文件的读写,不过其中很多需要通过插件才能实现。 Java3D 内置了 OBJ 文件的相关操作.

实例比较简单 , 制作的效果是模型加载后绕 Y 轴旋转 . 我们可以通过模型动态展示观察 3D效果

OBJ 文件读取工具类代码如下 :在使用前别忘了添加 Java3D 驱动包 

进群:697699179可以获取Java各类入门学习资料!

这是我的微信公众号【编程study】各位大佬有空可以关注下,每天更新Java学习方法,感谢!

学习中遇到问题有不明白的地方,推荐加小编Java学习群:697699179内有视频教程 ,直播课程 ,等学习资料,期待你的加入

废话不多说 , 直接上代码 , 说明见代码注释

package com.java3d.dennist.loader;importjavax.media.j3d.BranchGroup;importcom.sun.j3d.loaders.Scene;importcom.sun.j3d.loaders.objectfile.ObjectFile;/** *

*

*  @version : 1.1

*  @author  : 苏若年        <a href="mailto:DennisIT@163.com">发送邮件</a>

*   

*  @since  : 1.0      创建时间:    2013-5-7 下午02:13:43

*   

*  TODO    :  Java3D读取OBJ文件

*

*/publicclassObjFileReaderextendsBranchGroup{privatedoublecreaseAngle =60.0;

    /**    *

    * 读取ObjModel文件

    *

    * @param filePath    obj文件路径

    *

    */publicObjFileReader(StringfilePath){BranchGroupbranchGroup=newBranchGroup();intflags =ObjectFile.RESIZE;ObjectFileobjFile=newObjectFile(flags, (float)(creaseAngle*Math.PI)/180);Scenescenen=null;

        try {

            scenen = objFile.load(filePath);

        } catch(Exceptione) {            e.printStackTrace();System.out.println("OBJ模型加载失败"+ e.getMessage());

        }

        branchGroup.addChild(scenen.getSceneGroup());

        this.addChild(branchGroup);

    }

}

Java 加载 OBJ 模型到应用场景

package com.java3d.dennist.loader;import java.applet.Applet;import java.awt.BorderLayout;import javax.media.j3d.Alpha;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.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.universe.SimpleUniverse;/** *

*

*  @version : 1.1

*  @author  : 苏若年        <a href="mailto:DennisIT@163.com">发送邮件</a>

*   

*  @since  : 1.0      创建时间:    2013-5-7 下午02:21:18

*   

*  TODO    : Java3D 实例 OBJ模型加载 展示

*

*/publicclassJavaModelObjLoaderAppextends Applet{

    /**    *

    */privatestaticfinallongserialVersionUID =5841679659336190804L;

    public BranchGroup createSceneGraph(){

        // 创建场景图分支BranchGroupgroup=new BranchGroup();

        // 几何变换组节点TransformGroup transGroup =new TransformGroup();

        // 几何变换Transform3D trans3d =new Transform3D();

        // 缩放变换trans3d.setScale(0.8);

        //将几何变换节点对象添加到节点组        transGroup.setTransform(trans3d);

        //将几何变化组添加到场景group.addChild(transGroup);// 球体作用范围边界对象BoundingSphere bound=newBoundingSphere(newPoint3d(0.0,0.0,0.0),100.0);

        Color3f bgColor =newColor3f(0.05f,0.05f,0.2f);

        Background bg =newBackground(bgColor);        bg.setApplicationBounds(bound);group.addChild(bg);// 设置光源Color3f lightColor =newColor3f(1.0f,1.0f,0.9f);

        Vector3f lightDirection =newVector3f(4.0f,-7.0f,-12.0f);

        //设置定向光的颜色和影响范围DirectionalLight light =new DirectionalLight(lightColor, lightDirection);

        light.setInfluencingBounds(bound);

        //将光源添加到场景group.addChild(light);//几何变换组节点 - 加载外部模型TransformGroup objTrans =new TransformGroup();

        objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        //加载Obj格式的模型文件objTrans.addChild(newObjFileReader("F:/guanyu.obj"));

        //将模型添加到变换组节点        transGroup.addChild(objTrans);

        //设置几何变化,绕Y轴中心旋转Transform3D yAxis =new Transform3D();

        Alpha rotationAlpha =newAlpha(-1,Alpha.INCREASING_ENABLE,

                0,0,

                4000,0,0,

                0,0,0);

        RotationInterpolator rotator =newRotationInterpolator(rotationAlpha, objTrans,yAxis,0.0f,(float)Math.PI*2.0f);        rotator.setSchedulingBounds(bound);        objTrans.addChild(rotator);group.compile();returngroup;    }public JavaModelObjLoaderApp(){

        setLayout(new BorderLayout());

        // 创建3D场景绘制画布Canvas3D对象Canvas3D canvas =newCanvas3D(null);

        add("Center",canvas);

        BranchGroup scene = createSceneGraph();

        SimpleUniverse universe =new SimpleUniverse(canvas);

        universe.getViewingPlatform().setNominalViewingTransform();

        universe.addBranchGraph(scene);

    }

    publicstaticvoid main(String[] args) {

        newMainFrame(newJavaModelObjLoaderApp(),360,360);

    }

}

实例扩展 : 实现场景中多个模型

实例代码:

package com.java3d.dennist.loader;import java.applet.Applet;import java.awt.BorderLayout;import javax.media.j3d.Alpha;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.Group;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.Vector3d;import javax.vecmath.Vector3f;import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.universe.ViewingPlatform;/** *

*

*  @version : 1.1

*  @author  : 苏若年        <a href="mailto:DennisIT@163.com">发送邮件</a>

*   

*  @since  : 1.0      创建时间:    2013-5-7 下午03:46:59

*   

*  TODO    :

*

*/publicclassJavaModelObjMultiLoaderAppextendsApplet{/**    *

    */privatestaticfinallongserialVersionUID =3645520149291184985L;

    publicBranchGroupcreateSceneGraph(){// 创建场景图分支BranchGroup group =new BranchGroup();

        // 几何变换组节点TransformGroup transGroup =new TransformGroup();

        // 几何变换Transform3D trans3d =new Transform3D();

        // 缩放变换trans3d.setScale(0.8);

        //将几何变换节点对象添加到节点组        transGroup.setTransform(trans3d);

        //将几何变化组添加到场景        group.addChild(transGroup);

        // 球体作用范围边界对象BoundingSphere bound=newBoundingSphere(newPoint3d(0.0,0.0,0.0),100.0);

        Color3f bgColor =newColor3f(0.05f,0.05f,0.2f);

        Background bg =new Background(bgColor);

        bg.setApplicationBounds(bound);

        group.addChild(bg);

        // 设置光源Color3f lightColor =newColor3f(1.0f,1.0f,0.9f);

        Vector3f lightDirection =newVector3f(4.0f,-7.0f,-12.0f);

        //设置定向光的颜色和影响范围DirectionalLight light =new DirectionalLight(lightColor, lightDirection);

        light.setInfluencingBounds(bound);

        //将光源添加到场景        group.addChild(light);

        BranchGroup branchGroup1 =newObjFileReader("F:/guanyu.obj");

        BranchGroup branchGroup2 =newObjFileReader("F:/guanyu.obj");

        group.addChild(createObject(branchGroup1,bound, -1.3f,0.0f,0.0f,3000));

        group.addChild(createObject(branchGroup2,bound, 1.3f,0.0f,0.0f,4000));

        group.compile();

        return group;

    }

    /**    *

    * Description:  创建模型行为  绕Y轴旋转

    * @param group      模型结点

    * @param bound      模型作用范围边界

    * @param xpos      模型展示左边X轴位置

    * @param ypos      模型展示左边y轴位置

    * @param zpos  模型展示左边z轴位置

    * @param time  模型转动速度

    * @return    */privateGroupcreateObject(BranchGroup group,BoundingSphere bound, float xpos, float ypos,float zpos, int time){        Transform3D trans3d=new Transform3D();

        trans3d.setTranslation(new Vector3f(xpos, ypos, zpos));

        TransformGroup objTrans =new TransformGroup(trans3d);

        TransformGroup sgroup =new TransformGroup();

        sgroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

        sgroup.addChild(group);

        //设置几何变化,绕Y轴中心旋转Transform3D yAxis =new Transform3D();

        Alpha rotationAlpha =newAlpha(-1,Alpha.INCREASING_ENABLE,

                0,0,

                time,0,0,

                0,0,0);

        RotationInterpolator rotator =newRotationInterpolator(rotationAlpha, sgroup,yAxis,0.0f,(float)Math.PI*2.0f);

        rotator.setSchedulingBounds(bound);

        objTrans.addChild(rotator);

        objTrans.addChild(sgroup);

        return objTrans;

    }

    publicJavaModelObjMultiLoaderApp(){                setLayout(new BorderLayout());

        // 创建3D场景绘制画布Canvas3D对象Canvas3D canvas =newCanvas3D(null);

        add("Center",canvas);

        BranchGroup scene = createSceneGraph();

        SimpleUniverse universe =new SimpleUniverse(canvas);

        //设定用户视角Point3d userPosition =newPoint3d(0,3,6);

        initUserPosition(universe,userPosition);

        //universe.getViewingPlatform().setNominalViewingTransform();        universe.addBranchGraph(scene);

    }

    //初始化用户视角private voidinitUserPosition(SimpleUniverse universe,Point3d userPosition){        ViewingPlatform vp= universe.getViewingPlatform();

        TransformGroup steerTG = vp.getViewPlatformTransform();

        Transform3D t3d =new Transform3D();

        steerTG.getTransform(t3d);

        t3d.lookAt(userPosition, newPoint3d(0,0,0),newVector3d(0,1,0));

        t3d.invert();

        steerTG.setTransform(t3d);

    } 

    public static voidmain(String[] args){newMainFrame(newJavaModelObjMultiLoaderApp(),350,210);

    }

}

相关文章

网友评论

    本文标题:Java3D实例引用-加载Obj 三维模型

    本文链接:https://www.haomeiwen.com/subject/anuunqtx.html