講完了基本的min3d模型顯示,接着講模型導入。min3d引擎支持高效率的3ds,obj,md2等格式的導入。但最好不要導入大於5MB的模型,那樣會比較慢。
先來個xml文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.keyshare3d.MainActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id="@+id/frame3d"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</FrameLayout>
然後是MainActivity.java
import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Light;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.Toast;
public class MainActivity extends RendererActivity {
Object3dContainer faceObject3D; //3D對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FrameLayout ll = (FrameLayout) this.findViewById(R.id.frame3d);//在xml中,指定顯示的控件,這裏是一個FrameLayout。
ll.addView(_glSurfaceView);//加入到_glSurfaceView中顯示。
Toast.makeText(MainActivity.this, "執行正常", Toast.LENGTH_SHORT).show();
}
public void initScene() {
//scene.backgroundColor().setAll(0xffff0000);//設置場景底色 0x0 ffff0000
scene.lights().add(new Light()); //設置燈光,否則一片黑,看不見
IParser myParser = Parser.createParser(Parser.Type.MAX_3DS,
getResources(), "com.example.keyshare3d:raw/ffffff", false);/*在\res\raw\中,模型的文件名爲fffff.3ds,貼圖的文件路徑在\res\drawable\裏,是demo.jpg*/
myParser.parse();
faceObject3D = myParser.getParsedObject();
//faceObject3D = new Box(1,1,1);//新建一個立方體,大小是長寬高各一個單位
faceObject3D.scale().x = 0.007f;
faceObject3D.scale().y = 0.007f;
faceObject3D.scale().z = 0.007f;
faceObject3D.position().x =0;//設置初始位置,平截頭體中xyz的座標位置
faceObject3D.position().y =0;
faceObject3D.position().z =0;
//faceObject3D.normalsEnabled(false);
scene.camera().target = faceObject3D.position();
scene.addChild(faceObject3D);//把模型加入到場景,這樣模型才能顯示,還可以在faceObject3D中加入新模型,同步顯示。
// faceObject3D.colorMaterialEnabled(true); //打開材質支持,默認是關閉的,記住函數不影響貼圖。
// scene.lightingEnabled(false); //關閉場景的光效,false關掉了啥也看不見。。。
}
public void updateScene() {
// faceObject3D.rotation().x++;//圍繞着X軸旋轉,正數爲順時針,速度由X的數值變化決定
faceObject3D.rotation().y++;//可以單獨圍繞着Y軸(或XZ)旋轉,這個任意,根據外部傳感器同步姿態也可以。
faceObject3D.rotation().z++;//旋轉在飛行器上的參數,對應的是pitch,roll,yaw
}
}
運行效果圖如下。。。
注意:如果是自己用3D MAX製作 .3ds文件, 最好用UV貼圖,把整個模型包裹起來, obj格式的貼圖,樓主試了很多次,老是隻顯示模型。md2是老格式,用的人少。