准备一张贴图
 
创建材质球

球面坐标系转直角坐标系
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;
} }

unity三维地球模型生成的更多相关文章

  1. OpenGL ES学习笔记(二)——平滑着色、自适应宽高及三维图像生成

    首先申明下,本文为笔者学习<OpenGL ES应用开发实践指南(Android卷)>的笔记,涉及的代码均出自原书,如有需要,请到原书指定源码地址下载. <Android学习笔记--O ...

  2. Unity不同平台生成中预处理的注意点

    http://blog.csdn.net/pandawuwyj/article/details/7959335 Unity3D的项目,这周吃亏在宏上了.大背景是项目需要在Unity中用Hudson自动 ...

  3. Unity 三维软件单位导入资源单位比例

    三维软件 内部米制尺寸/m 默认设置导入unity中的尺寸/m 与unity单位比例 Maya 1 100 1:100 3DS MAX 1 0.01 100:1 Cinema 4D 1 100 1:1 ...

  4. unity 打包Apk生成签名证书keystore

    进行Android项目开发中想要将androidapp导出为apk的时候需要选择一个数字证书,即keystore文件(android.keystore),它用来对我们的APP进行签名,是导出APP的一 ...

  5. Unity AssetBundle的生成、加载和热更新

    当前使用的是unity2018.2.6版本. 生成AssetBundle 这个版本生成AssetBundle有两种方式,一种是在资源的Inspector面板下边配置AssetBundle名称,然后调用 ...

  6. unity下载资源存储-生成md5

    IEnumerator GetText() { using (UnityWebRequest request = UnityWebRequest.Get("localhost:80/txt/ ...

  7. unity中动态生成网格

    以下是绘制正方形面片的一个例子,方便之后查阅: 效果如图所示: 红轴为x方向,蓝轴为z方向. 代码如下: using System.Collections; using System.Collecti ...

  8. Unity TextMeshPro 一键生成工具

    本文参考了这片博客文章,在此基础上进行优化和改进: https://blog.csdn.net/akof1314/article/details/80868869 先截张效果图: TextMeshPr ...

  9. 关于微软企业库中依赖注入容器Unity两种生成对象的实现u

    http://www.byywee.com/page/M0/S261/261037.html

随机推荐

  1. 跨域问题——学习ing

    问题 跨域:我写了一个页面,在js中写了请求,这个请求的url跟我这个页面不在一个域名,那么这个请求就是跨域请求. 跨域会怎么样:没见过,可能就不让你请求呗,为了安全考虑之类的.(涉及浏览器的同源策略 ...

  2. (二)AppScan使用教程

    1.新建扫描:一般选择 常规扫描 2.选择扫描的平台:web或app 3.扫描配置向导 ①配置URL和服务器 ②配置登录管理 在扫描的过程中,可能会不小心碰到退出按钮导致Appscan注销.因此,要登 ...

  3. SQLCommand命令、DbTransaction事务

    一.SqlDataReader SqlConnection conn = new SqlConnection("server=10.126.64.11;user=it_oper;pwd=IT ...

  4. Python+request 获取响应(elapsed)和响应时间(timeout)《七》

    requests发请求时,接口的响应时间,也是我们需要关注的一个点,如果响应时间太长,也是不合理的.如果服务端没及时响应,也不能一直等着,可以设置一个timeout超时的时间 具体的实现如下: 超时( ...

  5. csv和xlsx区别

    CSV是文本文件,用记事本就能打开.XLS 是二进制的文件只有用 EXCEL 才能打开: CSV 文件格式只能保存活动工作表中的单元格所显示的文本和数值.数据列以逗号分隔,每一行数据都以回车符结束.如 ...

  6. Codeforces Round #587 (Div. 3) C题 【判断两个矩形是否完全覆盖一个矩形问题】 {补题 [差点上分系列]}

    C. White Sheet There is a white sheet of paper lying on a rectangle table. The sheet is a rectangle ...

  7. UDP广播,组播服务器

    广播 #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/ ...

  8. [Luogu] 矩阵加速(数列)

    题面:https://www.luogu.org/problemnew/show/P1939 题解:https://www.zybuluo.com/wsndy-xx/note/1153810

  9. 异步时钟FIFO(一)

    FIFO一般用于通过两个不同时钟域的数据传输.一个水池有进和出两个通道,由于进出口水流不一致所以需要水池加以缓冲.堆栈也是相当于水池的作用.如果输入端不是连续的数据流,可以通过堆栈来调节使数据以稳定的 ...

  10. JAVA基础知识|HTTP协议-两个特性

    一.无连接 无连接:服务器与浏览器之间的一次连接只处理一个http请求,请求处理结束后,连接断开.下一次请求再重新建立连接. 然而随着互联网的发展,一台服务器同一时间处理的请求越来越多,如果依然采用原 ...