3D學習筆記

# 3D

## Google 關鍵詞

### 關鍵詞

- OpenGL
- Unity3D
- EasyAR
- Android

### 搜索關鍵字

- OpenGL ES 2.0 for Android
- OpenGL ES應用開發實踐指南


## 安卓與EasyAR交互顯示Unity3D

- [EasyAR4.0使用說明--02--平面圖像跟蹤](https://www.bilibili.com/video/BV1VZ4y147kj/)
- [配置EasyAR Sense Unity Plugin](https://help.easyar.cn/EasyAR%20Sense/v4/GettingStartedUnity/Setting-up-EasyAR-Unity-Package.html)
- [上傳圖片,檢測可識別度](https://www.easyar.cn/targetcode.html)
- [Unity加載網絡圖片爲Image貼圖,並保存本地。](https://blog.csdn.net/weixin_42540271/article/details/95337041)
- [Unity3d EasyAR開發案例系列教程](https://www.cnblogs.com/qq764424567/p/9178125.html)

- 前提條件
  - Unity 2019.3.8f1
  - Android Studio 3.6.3

- 操作步驟如下
  - 下載Unity後導入EasyAR Sense Unity Plugin
  - 拖拉 EasyAR_ImageTracker-1 和 ImageTarget 到 Hierachy 裏。
  - 拖拉一個Cube在ImageTarget下爲一個子對象
  - 新建腳本 first_run 拖拉到 Cube上
  - 腳本如下:

    ```
        using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;

        public class first_run : MonoBehaviour
        {
            // Start is called before the first frame update
            void Start()
            {
                
            }

            // Update is called once per frame
            void Update()
            {
                //    Vector3 v = transform.position;
                //    v.z -= 0.1f;
                //    transform.position = v;
                if (Input.GetMouseButtonDown(0))
                {//判斷是否是點擊事件
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    RaycastHit hitInfo;
                    if (Physics.Raycast(ray, out hitInfo))
                    {
                        //如果是一根手指觸摸屏幕而且是剛開始觸摸屏幕
                        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began)
                        {
                            if (Input.GetTouch(0).tapCount == 1)
                            {//判斷點擊的次數
                                //Destroy(hitInfo.collider.gameObject);//銷燬場景中的模型
                                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                                string name = jo.Call<string>("getName", "成功調用android方法");
                                setNum(name);

                            }

                        }
                    }
                }

            }

            void setNum(string num)
            {
                AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                string name = jo.Call<string>("getName1", num);
            }


        }
    ```
  - 在Unity裏配置 [Preferences]裏配置
    - External Tools: Visual Studio
    - Android JDk: 1.8
    - Android SDK
  - 在Unity菜單: File > Build Settings裏設置
    - Android 勾選 Export Project
    - 點擊 Player Settings 按鈕
      - 點選Tab: Player
        - 修改 Company Name: sletech
        - 修改 Product Name: itasktour
        - 修改 Default Icon
        - 修改 Other Settings
          - 修改identification > Package Name: com.sletech.itasktour
  - 點選導出: Export 導出Android 工程
  - Android Studio 新建一個工程
    - 修改配置文件 build.gradle : 

    ```
        android {
        ............

            defaultConfig {
                minSdkVersion 21

            ............

            sourceSets {
                main {
                    jniLibs.srcDirs = ['libs']
                }
            }
            defaultConfig {
                ndk {
                    abiFilters 'armeabi-v7a'
                }
            }
        ............    
        }
    ```
  - Android Studio 菜單選擇 File > New > Import Module :: Source Directory 選擇之前導出的工程下的目錄: unityLibrary
    - 修改配置文件 build.gradle : 

    ```
        android {
        ............

            defaultConfig {
                minSdkVersion 21
                targetSdkVersion 29
                ndk {
                    abiFilters 'armeabi-v7a'
                }

            ............

            packagingOptions {
                doNotStrip '*/armeabi-v7a/*.so'
                    }
        ............    
        }
    ```
  - 從工程中複製res目錄下的mipmap目錄和values目錄下的strings.xml到module下的res目錄下
  - 修改Module裏的AndroidManifest.xml文件以下內容

      ```
        <application android:label="@string/app_name">
        <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:hardwareAccelerated="false">
        </activity>
      ```
  - 修改文件:com.unity3d.player.UnityPlayerActivity

      ```
            @Override public boolean onKeyDown(int keyCode, KeyEvent event)   {
        //        return mUnityPlayer.injectEvent(event);

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    onDestroy();
                }
                return true;
            }
            public String getName(final String str) {
        //        runOnUiThread(new Runnable() {
        //            @Override
        //            public void run() {
        //                Toast.makeText(UnityPlayerActivity.this, str, Toast.LENGTH_SHORT).show();
        //            }
        //        });
                return "我是甩鍋男Android中的數據" + (++num);
            }

            public String getName1(final String str) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(UnityPlayerActivity.this, str, Toast.LENGTH_SHORT).show();
                    }
                });
                return "";
            }
      ```

  - 修改項目工程裏的文件:

      ```
            package com.sletech.itasktour;

            import androidx.appcompat.app.AppCompatActivity;

            import android.content.Intent;
            import android.os.Bundle;
            import android.view.View;

            import com.unity3d.player.UnityPlayerActivity;

            public class MainActivity extends AppCompatActivity {

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            startActivity(new Intent(MainActivity.this, UnityPlayerActivity.class));
                        }
                    });
                }
            }
      ```

  - 當修改了Unity裏的內容或者腳本需要替換以下內容
    - unityLibrary/libs
    - unityLibrary/src/main/assets
    - unityLibrary/src/main/jniLibs/armeabi-v7a

## 參考

- Math
    - [OpenGL 的空間變換(上):矩陣在空間幾何中的應用](https://www.cnblogs.com/ojo-blogs/p/6754589.html)
    - [齊次座標](https://zh.wikipedia.org/wiki/%E9%BD%90%E6%AC%A1%E5%9D%90%E6%A0%87)

- OpenGL
    - [安卓 OpenGL ES 2.0 完全入門(一):基本概念和 hello world](https://blog.piasy.com/2016/06/07/Open-gl-es-android-2-part-1/index.html)
    - [歡迎來到OpenGL的世界](https://learnopengl-cn.github.io/)
       - [Welcome to OpenGL](https://learnopengl.com/)
       - [Learn C++](https://www.learncpp.com/)
    - [《OpenGL ES 2.0 for Android》讀書筆記](https://juejin.im/post/5aefdb2c51882522835e6542)
    - [OpenGL ES](https://developer.android.com/guide/topics/graphics/opengl)
    - [Book: OpenGL ES 2.0 for Android](https://www.amazon.com/OpenGL-Android-Quick-Start-Pragmatic-Programmers/dp/1937785343)
    - [OpenGL ES應用開發實踐指南](https://github.com/Canber/OpenGL-ES-for-android)
    - [OpenGL-ES-2.0-for-Android](https://github.com/wxdut/OpenGL-ES-2.0-for-Android)
    - [Learn OpenGL ES](http://www.learnopengles.com/)

- Unity
    - [Unity](https://unity.com/)
    - [Unity Cn](https://unity.com/cn)
    - [Unity中文](https://www.unity.cn/)
    - [Unity User Manual](https://docs.unity3d.com/Manual/UnityManual.html)
    - [John Lemon's Haunted Jaunt 項目教程 (適用於3D初學者)](https://learn.unity.com/project/john-lemon-s-haunted-jaunt-3d-beginner-1)
    - [Unity3D從安裝到運行第一個程序](https://blog.csdn.net/q943520218/article/details/100939733?utm_source=distribute.pc_relevant.none-task)
    - [Unity Technologies China](https://github.com/unity-cn)
    - [史上最全Unity3D教程](https://www.bilibili.com/video/BV12s411g7gU?p=120)
    - [unity3d從入門到大佬最全教程[特效實戰篇]](https://www.bilibili.com/video/BV1z441157Lf/?spm_id_from=333.788.videocard.1)


    - [Unity官方實例教程 Roll-a-Ball](https://www.jianshu.com/p/6e4b0435e30e)
    - [Unity官方實例教程 Space Shooter](https://www.jianshu.com/p/8cc3a2109d3b)
    - [Unity3D官方教程](https://www.w3cschool.cn/unity3d_jc/)
    - [[Unity3D] 2D像素遊戲(一) Hello Unity!](https://blog.csdn.net/qq_37398834/article/details/81948376?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task)
    - [Vuforia for Unity 入門教程](https://blog.csdn.net/slj2017/article/details/79730378)
    - [Vuforia Developer Library](https://library.vuforia.com/getting-started/overview.html)

- 3D免費模型
    - [專業人士的3D模型](https://www.turbosquid.com/zh_cn/)
    - [CG 3D網](https://www.cg99.cn/)
    - [知末網3d模型](https://3d.znzmo.com/3dmoxing.html?keyword=%E5%AE%9D%E7%AE%B1&st=0)
    - [哪個網站有免費的 3D 模型素材?](https://www.zhihu.com/question/19959438)
    - [3D俠](https://www.3dxia.com/)
    - [Free3D](https://free3d.com/zh/)
    - [Sketchfab - Publish & find 3D models online](https://sketchfab.com/)
    - [Free 3D Models](https://www.turbosquid.com/Search/3D-Models/free/)

- Android 3D
    - [Android與Unity交互研究](https://blog.csdn.net/crazy1235/article/details/46733221)
    - [實現Unity和Android進行交互](https://www.jianshu.com/p/7e46fe7485bb)
    - [Unity3D與Android間的相互調用](https://github.com/DingMouRen/UnityDemo)

- EasyAR
    - [EasyAR官網](https://www.easyar.cn/)
    - [概覽 — EasyAR Sense 4.0.0 文檔](https://help.easyar.cn/EasyAR%20Sense/v4/ApiReference/Overview.html)
    - [使用手冊 — EasyAR Sense 4.0.0 文檔](https://help.easyar.cn/EasyAR%20Sense/v4/Guides/Guides.html)
    - [重磅 | 完備的EasyAR學習路線,最詳細的資源整理](https://mp.weixin.qq.com/s/8YcKuDLuyIcwyj0GcyEoqQ)
    - [EasyAR4.0報錯VIOCameraDevice不可用]
       - [Motion Tracking支持的設備](https://help.easyar.cn/EasyAR%20Sense/v4/Guides/EasyAR-Motion-Tracking-Supported-Devices.html)
    - [EasyAR SpatialMap空間地圖開發](https://forum.easyar.cn/?p=149)
    - [EasyAR UnityPlugin 視頻教程合集](https://forum.easyar.cn/?p=42)
    - [EasyAR4.0使用說明--07--稀疏空間地圖](https://www.bilibili.com/video/BV1Xg4y1z7d8/)
    - [EasyAR4.0使用說明--08--稠密空間地圖](https://www.bilibili.com/video/BV1sf4y1S7sp)
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章