准备一张贴图
 
创建材质球

球面坐标系转直角坐标系
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. 在浏览器端用H5实现图片压缩上传

    一.需求的场景: 在我们的需求中需要有一个在手机浏览器端,用户实现上传证件照片的功能,我们第一版上了一个最简版,直接让用户在本地选择图片,然后上传到公司公共的服务器上. 功能实现后我们发现一个问题,公 ...

  2. python 传参

    python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方式.这种方式相当于传值和传引用的一种综合.如果函数收到的是一个可变对象(比如字典或者列表)的引用,就能 ...

  3. JS 框架安全报告:jQuery 下载次数超过 1.2 亿次

    尽管 JavaScript 库 jQuery 仍被使用,但它已不再像以前那样流行.根据开源安全平台 Snyk 统计,目前至少十分之六的网站受到 jQuery XSS 漏洞的影响,甚至用于扩展 jQue ...

  4. 2019HDU多校Minimal Power of Prime——分段讨论&&思维

    题目 将 $n$($1 < n \leq 10^{18}$)质因数分解,求质因数幂的最小值. 分析 直接质因数分解,不太行. 可以这样想,对小区间质因数分解,n变小了,再枚举答案. 打印1-10 ...

  5. FireFox版本大于36.0,Yslow不能正常使用解决方案

    Yslow暂时不支持firefox 36.0及以上版本你可以使用它的书签版本.访问这里 http://yslow.org/mobile/把页面最后的那个 Desktop Bookmarklet Ins ...

  6. java-十五周作业

    题目1:编写一个应用程序,输入用户名和密码,访问test数据库中t_login表(字段包括id.username.password),验证登录是否成功. 题目2:在上一题基础上,当登录成功后,将t_u ...

  7. 2、python--第二天练习题

    #1.有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中. #即: {'k ...

  8. 网络和Web编程

    一.以客户端的形式同HTTP服务交互 (1)使用urllib.request模块发送HTTP GET请求 from urllib import request,parse url = 'http:// ...

  9. 牛客练习赛53 (C 富豪凯匹配串) bitset

    没想到直接拿 bitset 能过 $10^8$~ code: #include <bits/stdc++.h> #define N 1004 #define setIO(s) freope ...

  10. 18.4.1 考试解题报告 P71

    题目:https://files.cnblogs.com/files/lovewhy/problem.pdf 偷偷摘来dalao题面. P71竞赛时间:???? 年?? 月?? 日??:??-??:? ...