OBJ?文件是?Alias|Wavefront?公司為它的一套基于工作站的?3D?建模和動(dòng)畫軟件?"Advanced?Visualizer"?開發(fā)的一種標(biāo)準(zhǔn)?3D?模型文件格式,很適合用于?3D?軟件模型之間的互導(dǎo),也可以通過?Maya?讀寫。比如你在?3dsMax?或?LightWave?中建了一個(gè)模型,想把它調(diào)到?Maya?里面渲染或動(dòng)畫,導(dǎo)出?OBJ?文件就是一種很好的選擇。目前幾乎所有知名的?3D?軟件都支持?OBJ?文件的讀寫,不過其中很多需要通過插件才能實(shí)現(xiàn)。?Java3D?內(nèi)置了?OBJ?文件的相關(guān)操作.
實(shí)例比較簡(jiǎn)單?,?制作的效果是模型加載后繞?Y?軸旋轉(zhuǎn)?.?我們可以通過模型動(dòng)態(tài)展示觀察?3D效果
OBJ?文件讀取工具類代碼如下?:在使用前別忘了添加?Java3D?驅(qū)動(dòng)包?
進(jìn)群:697699179可以獲取Java各類入門學(xué)習(xí)資料!
這是我的微信公眾號(hào)【編程study】各位大佬有空可以關(guān)注下,每天更新Java學(xué)習(xí)方法,感謝!
學(xué)習(xí)中遇到問題有不明白的地方,推薦加小編Java學(xué)習(xí)群:697699179內(nèi)有視頻教程 ,直播課程 ,等學(xué)習(xí)資料,期待你的加入
廢話不多說?,?直接上代碼?,?說明見代碼注釋
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">發(fā)送郵件</a>
*? ?
*? @since? : 1.0? ? ? 創(chuàng)建時(shí)間:? ? 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?模型到應(yīng)用場(chǎng)景
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">發(fā)送郵件</a>
*? ?
*? @since? : 1.0? ? ? 創(chuàng)建時(shí)間:? ? 2013-5-7 下午02:21:18
*? ?
*? TODO? ? : Java3D 實(shí)例 OBJ模型加載 展示
*
*/publicclassJavaModelObjLoaderAppextends Applet{
? ? /**? ? *
? ? */privatestaticfinallongserialVersionUID =5841679659336190804L;
? ? public BranchGroup createSceneGraph(){
? ? ? ? // 創(chuàng)建場(chǎng)景圖分支BranchGroupgroup=new BranchGroup();
? ? ? ? // 幾何變換組節(jié)點(diǎn)TransformGroup transGroup =new TransformGroup();
? ? ? ? // 幾何變換Transform3D trans3d =new Transform3D();
? ? ? ? // 縮放變換trans3d.setScale(0.8);
? ? ? ? //將幾何變換節(jié)點(diǎn)對(duì)象添加到節(jié)點(diǎn)組? ? ? ? transGroup.setTransform(trans3d);
? ? ? ? //將幾何變化組添加到場(chǎng)景group.addChild(transGroup);// 球體作用范圍邊界對(duì)象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);// 設(shè)置光源Color3f lightColor =newColor3f(1.0f,1.0f,0.9f);
? ? ? ? Vector3f lightDirection =newVector3f(4.0f,-7.0f,-12.0f);
? ? ? ? //設(shè)置定向光的顏色和影響范圍DirectionalLight light =new DirectionalLight(lightColor, lightDirection);
? ? ? ? light.setInfluencingBounds(bound);
? ? ? ? //將光源添加到場(chǎng)景group.addChild(light);//幾何變換組節(jié)點(diǎn) - 加載外部模型TransformGroup objTrans =new TransformGroup();
? ? ? ? objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
? ? ? ? //加載Obj格式的模型文件objTrans.addChild(newObjFileReader("F:/guanyu.obj"));
? ? ? ? //將模型添加到變換組節(jié)點(diǎn)? ? ? ? transGroup.addChild(objTrans);
? ? ? ? //設(shè)置幾何變化,繞Y軸中心旋轉(zhuǎn)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());
? ? ? ? // 創(chuàng)建3D場(chǎng)景繪制畫布Canvas3D對(duì)象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);
? ? }
}
實(shí)例擴(kuò)展?:?實(shí)現(xiàn)場(chǎng)景中多個(gè)模型
實(shí)例代碼:
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">發(fā)送郵件</a>
*? ?
*? @since? : 1.0? ? ? 創(chuàng)建時(shí)間:? ? 2013-5-7 下午03:46:59
*? ?
*? TODO? ? :
*
*/publicclassJavaModelObjMultiLoaderAppextendsApplet{/**? ? *
? ? */privatestaticfinallongserialVersionUID =3645520149291184985L;
? ? publicBranchGroupcreateSceneGraph(){// 創(chuàng)建場(chǎng)景圖分支BranchGroup group =new BranchGroup();
? ? ? ? // 幾何變換組節(jié)點(diǎn)TransformGroup transGroup =new TransformGroup();
? ? ? ? // 幾何變換Transform3D trans3d =new Transform3D();
? ? ? ? // 縮放變換trans3d.setScale(0.8);
? ? ? ? //將幾何變換節(jié)點(diǎn)對(duì)象添加到節(jié)點(diǎn)組? ? ? ? transGroup.setTransform(trans3d);
? ? ? ? //將幾何變化組添加到場(chǎng)景? ? ? ? group.addChild(transGroup);
? ? ? ? // 球體作用范圍邊界對(duì)象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);
? ? ? ? // 設(shè)置光源Color3f lightColor =newColor3f(1.0f,1.0f,0.9f);
? ? ? ? Vector3f lightDirection =newVector3f(4.0f,-7.0f,-12.0f);
? ? ? ? //設(shè)置定向光的顏色和影響范圍DirectionalLight light =new DirectionalLight(lightColor, lightDirection);
? ? ? ? light.setInfluencingBounds(bound);
? ? ? ? //將光源添加到場(chǎng)景? ? ? ? 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:? 創(chuàng)建模型行為? 繞Y軸旋轉(zhuǎn)
? ? * @param group? ? ? 模型結(jié)點(diǎn)
? ? * @param bound? ? ? 模型作用范圍邊界
? ? * @param xpos? ? ? 模型展示左邊X軸位置
? ? * @param ypos? ? ? 模型展示左邊y軸位置
? ? * @param zpos? 模型展示左邊z軸位置
? ? * @param time? 模型轉(zhuǎn)動(dòng)速度
? ? * @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);
? ? ? ? //設(shè)置幾何變化,繞Y軸中心旋轉(zhuǎn)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());
? ? ? ? // 創(chuàng)建3D場(chǎng)景繪制畫布Canvas3D對(duì)象Canvas3D canvas =newCanvas3D(null);
? ? ? ? add("Center",canvas);
? ? ? ? BranchGroup scene = createSceneGraph();
? ? ? ? SimpleUniverse universe =new SimpleUniverse(canvas);
? ? ? ? //設(shè)定用戶視角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);
? ? }
}