<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>mtl &#8211; 記下來</title>
	<atom:link href="https://noter.tw/tag/mtl/feed/" rel="self" type="application/rss+xml" />
	<link>https://noter.tw</link>
	<description>一路上踩到的坑、遇到的問題，一點一滴記下來，希望能幫助到需要的人~</description>
	<lastBuildDate>Mon, 05 Jul 2021 07:29:43 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.6.3</generator>

<image>
	<url>https://noter.tw/wp-content/uploads/cropped-old-1130742_1920-1-32x32.jpg</url>
	<title>mtl &#8211; 記下來</title>
	<link>https://noter.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Android Vuforia with jPCT-AE (2) – 載入 obj 測試</title>
		<link>https://noter.tw/3688/android-vuforia-with-jpct-ae-2-%e8%bc%89%e5%85%a5-obj-%e6%b8%ac%e8%a9%a6/</link>
					<comments>https://noter.tw/3688/android-vuforia-with-jpct-ae-2-%e8%bc%89%e5%85%a5-obj-%e6%b8%ac%e8%a9%a6/#comments</comments>
		
		<dc:creator><![CDATA[黃小蛙]]></dc:creator>
		<pubDate>Wed, 06 Jul 2016 00:24:55 +0000</pubDate>
				<category><![CDATA[手機 App]]></category>
		<category><![CDATA[jPCT-AE]]></category>
		<category><![CDATA[mtl]]></category>
		<category><![CDATA[texture]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[AR]]></category>
		<category><![CDATA[Vuforia]]></category>
		<category><![CDATA[.obj]]></category>
		<guid isPermaLink="false">https://wazai.net/?p=3688</guid>

					<description><![CDATA[<p>上一篇&#160;Android Vuforia with jPCT-AE (1) – 基本範例 我們已經把 Vuforia 與 jPCT-AE 的環境建立出來，也可以正常顯示 Vuforia + j&#46;&#46;&#46;</p>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3688/android-vuforia-with-jpct-ae-2-%e8%bc%89%e5%85%a5-obj-%e6%b8%ac%e8%a9%a6/" data-wpel-link="internal">Android Vuforia with jPCT-AE (2) – 載入 obj 測試</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<p>上一篇&nbsp;<a rel="noreferrer noopener" href="https://noter.tw/3639/android-vuforia-with-jpct-ae-1-%e5%9f%ba%e6%9c%ac%e7%af%84%e4%be%8b/" target="_blank" data-wpel-link="internal">Android Vuforia with jPCT-AE (1) – 基本範例</a> 我們已經把 Vuforia 與 jPCT-AE 的環境建立出來，也可以正常顯示 Vuforia + jPCT-AE 的範例─一個顏色有點昏暗的桶子，這篇繼續記錄怎麼直接載入一般常見的 .obj 檔案。 </p>



<span id="more-3688"></span>



<h2 class="para wp-block-heading">囉哩巴唆</h2>



<p>透過 <a href="https://noter.tw/3639/android-vuforia-with-jpct-ae-1-%e5%9f%ba%e6%9c%ac%e7%af%84%e4%be%8b/" target="_blank" rel="noreferrer noopener" data-wpel-link="internal">Android Vuforia with jPCT-AE (1) – 基本範例</a> 是直接在 ImageTargetRenderer.java 的建構式中，將要畫出物件的座標以及一些參數設定好之後，透過 jPCT-AE 畫出來，聽起來不怎麼有彈性，加上一些複雜的模型要透過自己訂定座標點的方式畫出來，貌似非常不科學啊！因此，這篇要記錄如何透過 Loader 類別來載入 .obj 的檔案，而不是在程式中，自己把座標一一描繪出來。</p>



<h2 class="para wp-block-heading">到 TD3M 下載需要的檔案</h2>



<p>這篇小蛙使用&nbsp;<a href="http://tf3dm.com/3d-model/bedside-table-4-skins-96709.html" target="_blank" rel="noreferrer noopener nofollow external" data-wpel-link="external" class="wpel-icon-right">Bedside Table (4 skins) @ TF3DM<span class="wpel-icon wpel-image wpel-icon-6"></span></a> 當作範例，因為這個範例非常符合 jPCT-AE 載入的規則，後面會再提到這是什麼意思，總之先把這個範例下載下來，下載完解壓縮之後會看到如下圖</p>



<div class="wp-block-image"><figure class="aligncenter"><a href="https://sshfs.crazyall.net/wazai/2016/07/table.png" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/table.png" alt="Bedside table 3d model" class="wp-image-3732" title="Android Vuforia with jPCT-AE (2) – 載入 obj 測試"></a><figcaption>Bedside table 3d model</figcaption></figure></div>



<p>將這三個檔案通通複製到 Android Studio 專案中的 assets 目錄下</p>



<div class="wp-block-image"><figure class="aligncenter"><a href="https://sshfs.crazyall.net/wazai/2016/07/as_project.png" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/as_project.png" alt="將模型放入 assets 目錄下" class="wp-image-3731" title="Android Vuforia with jPCT-AE (2) – 載入 obj 測試"></a><figcaption>將模型放入 assets 目錄下</figcaption></figure></div>



<p>小蛙測試的時候用了很多模型，造成 assets 目錄下面亂七八糟，因此又在 assets 下新增了 obj、mtl、texture 等目錄，分別把上述「Bedside Table D.obj」、「Bedside_Table_D.mtl」、「Bedside_Table_D_default_1_1.png」依類別放入。</p>



<h2 class="para wp-block-heading">讓 jPCT-AE 載入 obj, mtl, texture</h2>



<p>建議可以先把 jPCT-AE 的 ImageTargetRenderer.java 的建構式注解起來，到時候如果改壞了，還可以還原回去，然後把以下建構式的程式碼貼上</p>



<pre class="wp-block-preformatted withcode">public ImageTargetRenderer(ImageTargets activity, SampleApplicationSession session) {
     mActivity = activity;
     vuforiaAppSession = session;
     world = new World();
     // 如果亮度太暗或是亮度怪怪的，可以調整這裡
     world.setAmbientLight(150, 150, 150);
     world.setClippingPlanes(2.0f, 3000.0f);
     sun = new Light(world);
     // 如果亮度太暗或是亮度怪怪的，可以調整這裡
     sun.setIntensity(250, 250, 250);
     try{
         // 載入材質，第一個參數是 texture 名稱，這邊使用原始檔名
         // 因為當載入 mtl 時，mtl 的檔名會自動對應到該 texture
         TextureManager.getInstance().addTexture(
                 "Bedside_Table_D_default_1_1.png",
                 new Texture(mActivity.getAssets().open("texture/Bedside_Table_D_default_1_1.png"))
         );
         // loadOBJ 參數分別是：.obj, .mtl, 縮放大小
         Object3D[] tmp = Loader.loadOBJ(
                 mActivity.getAssets().open("obj/Bedside Table D.obj"),
                 mActivity.getAssets().open("mtl/Bedside_Table_D.mtl"),
                 5
         );
         if(tmp != null &amp;&amp; tmp.length >= 1){
             cylinder = tmp[0];
         }
         cylinder.strip();
         cylinder.build();
         // 位移
         cylinder.translate(-110, -160, 0);
         // x 軸旋轉，不然載入的櫃子可能是趴著或是躺著的
         cylinder.rotateX(-1.5f);
         // 如果有 mtl 就可以不用 setTexture，mtl 內有 texture 名稱會自動載入
         //cylinder.setTexture("Bedside_Table_D_default_1_1.png");
         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();
 }</pre>



<p></p>



<p>執行看看是不是可以正常載入一個櫃子了呢？</p>



<div class="wp-block-image"><figure class="aligncenter"><a href="https://sshfs.crazyall.net/wazai/2016/07/obj.png" target="_blank" rel="noopener nofollow external noreferrer" data-wpel-link="external"><img decoding="async" src="https://sshfs.crazyall.net/wazai/2016/07/obj-500x599.png" alt="載入 .obj 測試" class="wp-image-3760" title="Android Vuforia with jPCT-AE (2) – 載入 obj 測試"></a><figcaption>載入 .obj 測試</figcaption></figure></div>



<h2 class="wp-block-heading">遭遇問題</h2>



<p>小蛙之前弄了半天，遇到一拖拉庫的錯誤，有些事情後來才釐清到底為什麼會發生錯誤，在 texture 的部份，<strong>圖片大小必須要是 2 的次方</strong>，像是：128, 256, 512, 1024, 2048 &#8230; 等，只要邊長不是 2 的次方，就會得到圖片大小錯誤的訊息，外加因為載不到圖片隨之而來的 javaNullPointer Exception。</p>



<p>render 出來沒有東西，這個要仔細看，有時候是<strong>模型太小</strong>，小蛙有一次 render 出來的東西，必須要放大 30000 倍，才符合使用大小 &#8230; 有時候是座標軸不對導致 redner 在 image target 下面 (被紙張遮住了)，有時候是沒有 mtl 也沒有材質，就會 render 出空的東西 (沒有東西) &#8230; 等，有時候是 mtl 裡面的 texture 有問題，像是根本沒有 texture 資訊，或是 texture 資訊還是錯誤的檔案路徑(如：C:\xxx\rrr\ccc\xx.png 但是實際上根本沒有這種路徑，此時就要手動修正他)，有時候其實根本也不知道為什麼就是沒有東西。</p>



<p>到 TD3M 多下載幾個範例檔試試看，再慢慢釐清為什麼不能載入的原因，這個範例應該是不需要做什麼調整就可以直接載入，因此使用這個當作範例。</p>



<p><strong>Android Vuforia 系列：</strong></p>



<ul class="my-li bg-darkblue wp-block-list"><li><a href="https://noter.tw/3639/android-vuforia-with-jpct-ae-1-%e5%9f%ba%e6%9c%ac%e7%af%84%e4%be%8b/" data-wpel-link="internal">Android Vuforia with jPCT-AE (1) – 基本範例</a></li><li><a href="https://noter.tw/3688/android-vuforia-with-jpct-ae-2-%e8%bc%89%e5%85%a5-obj-%e6%b8%ac%e8%a9%a6/" data-wpel-link="internal">Android Vuforia with jPCT-AE (2) – 載入 obj 測試</a></li><li><a href="https://noter.tw/3735/android-vuforia-with-jpct-ae-3-%e8%bc%89%e5%85%a5-md2-%e6%b8%ac%e8%a9%a6/" data-wpel-link="internal">Android Vuforia with jPCT-AE (3) – 載入 md2 測試</a></li><li><a href="https://noter.tw/3737/android-vuforia-with-jpct-ae-4-%e8%bc%89%e5%85%a5-3ds-%e6%b8%ac%e8%a9%a6/" data-wpel-link="internal">Android Vuforia with jPCT-AE (4) – 載入 3DS 測試</a></li><li><a href="https://noter.tw/3739/android-vuforia-with-jpct-ae-5-%e5%a4%9a%e9%87%8d%e6%a8%a1%e5%9e%8b%e8%bc%89%e5%85%a5%ef%bc%8c%e4%bb%a5-obj-%e7%82%ba%e4%be%8b/" data-wpel-link="internal">Android Vuforia with jPCT-AE (5) – 多重模型載入，以 obj 為例</a></li></ul>



<h2 class="para wp-block-heading">參考資料</h2>



<ul class="my-li bg-darkblue wp-block-list"><li><a rel="noreferrer noopener nofollow external" aria-label="Bedside Table (4 skins) @ TF3DM (在新分頁中開啟)" href="http://tf3dm.com/3d-model/bedside-table-4-skins-96709.html" target="_blank" data-wpel-link="external" class="wpel-icon-right">Bedside Table (4 skins) @ TF3DM<span class="wpel-icon wpel-image wpel-icon-6"></span></a></li></ul>
<p>這篇文章 <a rel="nofollow" href="https://noter.tw/3688/android-vuforia-with-jpct-ae-2-%e8%bc%89%e5%85%a5-obj-%e6%b8%ac%e8%a9%a6/" data-wpel-link="internal">Android Vuforia with jPCT-AE (2) – 載入 obj 測試</a> 最早出現於 <a rel="nofollow" href="https://noter.tw" data-wpel-link="internal">記下來</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://noter.tw/3688/android-vuforia-with-jpct-ae-2-%e8%bc%89%e5%85%a5-obj-%e6%b8%ac%e8%a9%a6/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
