C#代碼建立3D模型

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   ar   使用   sp   div   

Demo 1

構建一個只包含單個三角形及紋理座標的網格

using UnityEngine;using System.Collections;public class MeshTest : MonoBehaviour{    void Start()    {        MeshFilter mf = GetComponent<MeshFilter>();        Mesh mesh = new Mesh();        mf.mesh = mesh;        mesh.vertices = new Vector3[3] { new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0) };        mesh.uv = new Vector2[3] { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1) };        mesh.triangles = new int[3] { 0, 1, 2 };    }}

可以看到只有單個三角形

從右中可以看出,共有1個三角形,3個頂點

Demo2

構建兩個三角形及紋理座標的網格

using UnityEngine;using System.Collections;public class MyMesh : MonoBehaviour{    void Start()    {        MeshFilter mf = GetComponent<MeshFilter>();        Mesh mesh = new Mesh();        mf.mesh = mesh;        //頂點[決定繪製出來圖片的scale]        mesh.vertices = new Vector3[4] { new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0) };        //三角形頂點索引的順序[3的倍數]        mesh.triangles = new int[6] { 0, 2, 1, 2, 3, 1 };        //法線        mesh.normals = new Vector3[4] { new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), };        //uv紋理座標        mesh.uv = new Vector2[4] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1) };    }}

從右中可以看出,共有2個三角形,4個頂點

設定的三角形頂點順序不同,繪製出來的效果也不相同 (mesh.triangles = new int[6] { 0, 2, 1, 0, 3, 1 };)

圖解

步驟

1、建立一個空的GameObject,綁定Mesh Filter及Mesh Renderer組件

2、建立一個Material,拖一張圖片進去,修改Shader為透明Shader

3、把2建立的Material賦給1建立的GameObject的Mesh Renderer下的Materiasl

4、這個時候預設在情境中是看不到這個GameObject渲染的

5、建立指令碼MeshTest.cs 代碼如Demo1,綁定在GameObject上,運行查看效果

6、建立指令碼MyMesh.cs   代碼如Demo2,綁定在GameObject上,運行查看效果

其它方法

建立一個Quad,不需要運行在情境中就可以看到

一些其它方法的連結

Unity3D研究院之與根據動態兩個軌跡點繪製面詳解(二十)http://www.xuanyusong.com/archives/780

<Unity3D>Unity3D中MeshRenderer的使用 http://blog.csdn.net/zuoyamin/article/details/9287507

[Unity3D學習]Mesh建立三角形、長方形 http://blog.gamerisker.com/archives/494.html

三角網格(Triangle Mesh)  http://lib.yoekey.com/?p=120

【Unity C#編程】製作星星(一)http://unity3d.9tech.cn/news/2014/0324/40085.html

C#代碼建立3D模型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.