unity三維地球模型生成


准備一張貼圖
 
創建材質球
 
球面坐標系轉直角坐標系
x=rsinθcosφ.
y=rsinθsinφ.
z=rcosθ.
效果如下
 
腳本如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//創建球形mesh
public class BallCreater : MonoBehaviour {
    public Material ballMat;//材質
	void Start () {
        createBall("mEarth",new Vector3(0,0,0),100.0f,181,88);
    }

    void Update () {
		
	}
    /// <summary>
    /// 創建球形mesh
    /// </summary>
    /// <param name="meshName">名稱</param>
    /// <param name="center">球心</param>
    /// <param name="radius">半徑</param>
    /// <param name="longPart">經線數</param>
    /// <param name="latPart">緯線數</param>
    /// <returns></returns>
    private GameObject createBall(string meshName,Vector3 center, float radius, int longPart, int latPart)
    {
        GameObject meshObject = new GameObject(meshName);

        int verticeNum = longPart * latPart+2* longPart;//經線數*緯線數+極點處多個重合點 (首尾經線重合)
        Vector3[] vertices = new Vector3[verticeNum];//頂點數組 
        Vector3[] normals = new Vector3[verticeNum];//頂點法線數組
        Vector2[] uvs = new Vector2[verticeNum];//uv數組
        int[] triangles = new int[(longPart-1)* latPart * 3 * 2];//三角集合數組,保存頂點索引 

        float degreesToRadians = Mathf.PI / 180.0f; //弧度轉換
        float deltaLong = 360.0f /(longPart-1);//經度每份對應度數
        float deltaLat = 180.0f/ (latPart+2);//緯度每份對應度數
        //極點放置多個頂點,同位置不同uv
        for (int i = 0; i < longPart; i++)
        {
            vertices[i] = new Vector3(0, 0, 1) * radius;
            normals[i] = vertices[0].normalized;
            uvs[i] = new Vector2((float)i/(float)longPart,0);
        } 
        int k = 0; 
        for (int i = 0; i < longPart-1; i++)
        {
            triangles[k++] = i; 
            triangles[k++] = i+ longPart;
            triangles[k++] = i + longPart+1;
        }  
        for (int tempLat = 0; tempLat < latPart; tempLat++) 
        { 
            float tempAngle1 = ((tempLat+1)* deltaLat) * degreesToRadians; 
            for (int tempLong = 0; tempLong < longPart; tempLong++) 
            {
                float tempAngle2 = (tempLong*deltaLong) * degreesToRadians; 
                int tempIndex = tempLong+ tempLat* longPart+ longPart; 
                vertices[tempIndex] = new Vector3(Mathf.Sin(tempAngle1) * Mathf.Cos(tempAngle2), Mathf.Sin(tempAngle1) * Mathf.Sin(tempAngle2), Mathf.Cos(tempAngle1)) * radius;
                normals[tempIndex] = vertices[tempIndex].normalized;
                uvs[tempIndex] = new Vector2((float)tempLong / (float)longPart, (float)tempLat / (float)latPart);
                if (tempLat!= latPart-1)
                {
                    if (tempLong != longPart-1)
                    {
                        triangles[k++] = tempLong + tempLat * longPart + longPart;
                        triangles[k++] = tempLong + tempLat * longPart + 2 * longPart;
                        triangles[k++] = tempLong + tempLat * longPart + longPart + 1;

                        triangles[k++] = tempLong + tempLat * longPart + 2 * longPart;
                        triangles[k++] = tempLong + tempLat * longPart + 1 + 2 * longPart;
                        triangles[k++] = tempLong + tempLat * longPart + 1 + longPart;

                    } 
                }  
            } 
        }
        //極點放置多個頂點,同位置不同uv
        for (int i = 0; i < longPart; i++)
        {
            vertices[verticeNum - 1-i] = new Vector3(0, 0, -1) * radius;
            normals[verticeNum - 1-i] = vertices[verticeNum - 1].normalized;
            uvs[verticeNum - 1-i] = new Vector2(1.0f-(float)i / (float)longPart, 1.0f); 
        } 
        for (int i = 0; i < longPart-1; i++)
        {
            triangles[k++] = verticeNum - 1-i; 
            triangles[k++] = verticeNum - 1-i- longPart;
            triangles[k++] = verticeNum - 2 - i- longPart;
        } 

        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.normals = normals;
        mesh.uv = uvs;
        mesh.RecalculateBounds();
        mesh.RecalculateNormals();
        meshObject.AddComponent<MeshFilter>();
        meshObject.AddComponent<MeshRenderer>();
        meshObject.GetComponent<MeshFilter>().mesh = mesh;
        meshObject.GetComponent<MeshRenderer>().material = ballMat;
        meshObject.transform.position += center;
        return meshObject;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM