Android Vuforia with jPCT-AE (4) – 載入 3DS 測試

延續 jPCT-AE Loader 載入模型的部份,這篇要記錄載入 3ds 檔案格式的方法,基本上跟前面幾個 obj, md2 的方法差不多,只有一些小小的變化而已。

圖片來源:http://tw.freeimages.com/photo/3d-glasses-1424688
圖片來源:http://tw.freeimages.com/photo/3d-glasses-1424688

下載需要的檔案

這裡 下載需要的檔案,要注意的是找有 3ds 格式的以及下載下來之後,texture 的圖片大小一定要是 2 的次方,否則載入 texture 的時候會出現錯誤。選桶子的原因是 texture 較為簡單,先試成功一個之後,再去載入複雜一點的模型。

載入 3DS 模型

附上載入程式碼,基本上沒有什麼不同,差別只有在於需不需要設定 texture,例如:載入 obj 跟 mtl 的時候,由於 mtl 已經有 texture 資訊,因此不需要呼叫 setTexture 方法,而 3DS 需要加入此方法,否則會看到赤裸裸的模型。另外有些模型太小,不放大會看不到 (就小小的),但是放大之後偏差跟位移也會明顯放大,各位可以試試看 …

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public ImageTargetRenderer(ImageTargets activity, SampleApplicationSession session) {
    mActivity = activity;
    vuforiaAppSession = session;
    world = new World();
    world.setAmbientLight(150, 150, 150);
    // set the following value according to your need, so the object won't be disappeared.
    world.setClippingPlanes(2.0f, 3000.0f);
    sun = new Light(world);
    sun.setIntensity(250, 250, 250);
    try{
        // 載入 texture 並且取名為 ton2.jpg,之後可以直接用 setTexture(name) 來設定 texture
        TextureManager.getInstance().addTexture("ton2.jpg",
                new Texture(mActivity.getAssets().open("ton2.jpg"))
        );
        try{
            // 載入 3ds 模型,放大 100 倍
            Object3D[] tmp = Loader.load3DS(mActivity.getAssets().open("ton3_low.3ds"), 100);
            if(tmp != null && tmp.length >= 1){
                cylinder = tmp[0];
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        cylinder.strip();
        cylinder.build();
        // 讓桶子躺下
        cylinder.rotateX(1.5f);
        // 設定 texture,讓桶子有衣服穿 XD
        cylinder.setTexture("ton2.jpg");
        cylinder.translate(0, 0, 0);
        world.addObject(cylinder);
        cam = world.getCamera();
        SimpleVector sv = new SimpleVector();
        sv.set(cylinder.getTransformedCenter());
        sv.y -= 100;
        sv.z -= 100;
        sun.setPosition(sv);
    }catch(Exception e){
        e.printStackTrace();
    }
    MemoryHelper.compact();
}

結果

載入 3ds 測試
載入 3ds 測試

Loader 部份文章就到這邊,下一篇將記錄如何遇到不同 image target 的時候可以動態載入不同的模型。

參考資料

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *