using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class LatLonGridGL : MonoBehaviour {
/// <summary>
/// 地球半径
/// </summary>
public float R = ;
/// <summary>
/// 纬线圈数(不包括南极点和北极点)
/// </summary>
public int latNum = ;
/// <summary>
/// 经线数
/// </summary>
public int lonNum = ;
/// <summary>
/// 一条纬线圈分段
/// </summary>
public int latSegment = ;
/// <summary>
/// 一条经线(半圆)分段
/// </summary>
public int lonSegment = ;
/// <summary>
/// 网格颜色
/// </summary>
public Color color = Color.white;
/// <summary>
/// 显示度数
/// </summary>
private Object latlonText3D;
private List<List<Vector3>> latLines = new List<List<Vector3>>();
private List<List<Vector3>> lonLines = new List<List<Vector3>>();
/// <summary>
/// 里面包含了shader
/// </summary>
static Material lineMaterial; void Start()
{
float latSpan = Mathf.PI / (latNum + );//纬线间隔度数
float lonSpan = Mathf.PI * / lonNum;//经线间隔度数
float anglePerLatSeg = Mathf.PI * / latSegment;//一条纬线每一段对应的度数
float anglePerLonSeg = Mathf.PI / lonSegment;//一条经线每一段对应的度数
latlonText3D = Resources.Load("LatLonText3D", typeof(GameObject));
// 纬度度数
for (int r = ; r < latNum + ; r++)
{
GameObject obj = Instantiate(latlonText3D, this.transform) as GameObject;
obj.transform.position = new Vector3(R * Mathf.Sin(latSpan * r), R * Mathf.Cos(latSpan * r), );
obj.GetComponent<TextMesh>().text = (int)(Mathf.Rad2Deg * (latSpan * r)) - + "°";
obj.GetComponent<TextMesh>().fontSize = ;
obj.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
obj.GetComponent<TextMesh>().color = Color.red;
}
//经度度数
for (int c = ; c < lonNum; c++)
{
GameObject obj = Instantiate(latlonText3D, this.transform) as GameObject;
obj.transform.position = new Vector3(R * Mathf.Cos(lonSpan * c), , R * Mathf.Sin(lonSpan * c));
obj.GetComponent<TextMesh>().text = (int)(Mathf.Rad2Deg * (lonSpan * c)) + "°";
obj.GetComponent<TextMesh>().fontSize = ;
obj.transform.localScale = new Vector3(0.01f, 0.01f, 0.01f);
obj.GetComponent<TextMesh>().color = Color.yellow;
} // 绘制纬线圈
for (int r = ; r < latNum; r++)
{
//顶点
List<Vector3> vertices = new List<Vector3>();
for (int n = ; n < latSegment+; n++)
{
Vector3 v;
v.x = R * Mathf.Sin(latSpan * (r + )) * Mathf.Cos(anglePerLatSeg * n);
v.y = R * Mathf.Cos(latSpan * (r + ));
v.z = R * Mathf.Sin(latSpan * (r + )) * Mathf.Sin(anglePerLatSeg * n);
vertices.Add(v);
}
latLines.Add(vertices);
} // 绘制经线圈
for (int c = ; c < lonNum; c++)
{
//顶点
List<Vector3> vertices = new List<Vector3>();
for (int n = ; n < lonSegment+; n++)
{
Vector3 v;
v.x = R * Mathf.Sin(anglePerLonSeg * n) * Mathf.Cos(lonSpan * c);
v.y = R * Mathf.Cos(anglePerLonSeg * n);
v.z = R * Mathf.Sin(anglePerLonSeg * n) * Mathf.Sin(lonSpan * c);
vertices.Add(v);
}
lonLines.Add(vertices);
} } public void OnRenderObject()
{
CreateLineMaterial();
// Apply the line material
lineMaterial.SetPass(); GL.PushMatrix();
// Set transformation matrix for drawing to
// match our transform
GL.MultMatrix(transform.localToWorldMatrix);
// Draw lines
foreach (List<Vector3> vertices in latLines)
{
GL.Begin(GL.LINE_STRIP);
GL.Color(color);
//GL.Color(new Color(0, 0.5f, 1, 0.5F));
foreach (Vector3 ver in vertices)
{
GL.Vertex3(ver.x, ver.y, ver.z);
}
GL.End();
}
foreach (List<Vector3> vertices in lonLines)
{
GL.Begin(GL.LINE_STRIP);
GL.Color(color);
//GL.Color(new Color(0, 0.5f, 1, 1.0F));
foreach (Vector3 ver in vertices)
{
GL.Vertex3(ver.x, ver.y, ver.z);
}
GL.End();
}
/*for (int i = 0; i < lineCount; ++i)
{
float a = i / (float)lineCount;
float angle = a * Mathf.PI * 2;
// Vertex colors change from red to green
GL.Color(new Color(a, 1 - a, 0, 0.8F));
// One vertex at transform position
//GL.Vertex3(0, 0, 0);
// Another vertex at edge of circle
GL.Vertex3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0);
}*/
GL.PopMatrix();
} void OnPostRender()
{
// Set your materials
GL.PushMatrix();
// yourMaterial.SetPass( );
// Draw your stuff
GL.PopMatrix(); } static void CreateLineMaterial()
{
if (!lineMaterial)
{
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
lineMaterial = new Material(shader);
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
// Turn on alpha blending
lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
// Turn backface culling off
lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
// Turn off depth writes
lineMaterial.SetInt("_ZWrite", );
}
} }

Unity使用OpenGL绘制经纬线圈的更多相关文章

  1. Unity使用OpenGL绘制线段

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class ShowGrid ...

  2. Opengl绘制我们的小屋(二)第一人称漫游

    这章我们先讲第一人称漫游的实现.在openTK里,我们用函数Matrix4.LookAt(caram.Eye,caram.Target,Vector3.UnitY)来放置摄像机,其中三个参数分别与摄像 ...

  3. [Modern OpenGL系列(三)]用OpenGL绘制一个三角形

    本文已同步发表在CSDN:http://blog.csdn.net/wenxin2011/article/details/51347008 在上一篇文章中已经介绍了OpenGL窗口的创建.本文接着说如 ...

  4. opengl绘制正弦曲线

    利用opengl绘制正弦曲线 ,见代码: #include <windows.h> //#include <GLUT/glut.h> #include <GL/glut. ...

  5. OpenGL绘制自由落体小球

    OpenGL绘制自由落体小球 一.    程序运行的软硬件环境 本次设计在window10系统下进行,运用C++进行编写,在CodeBlocks环境下使用OpenGL进行设计. 所需环境配置分为2部分 ...

  6. OpenGL绘制简单场景,实现旋转缩放平移和灯光效果

    本项目实现了用OpenGL绘制一个简单场景,包括正方体.球体和网格,实现了物体的旋转.缩放.平移和灯光效果.附有项目完整代码.有具体凝视.适合刚開始学习的人熟悉opengl使用. 开发情况 开发环境V ...

  7. OpenGl 绘制一个立方体

    OpenGl 绘制一个立方体 为了绘制六个正方形,我们为每个正方形指定四个顶点,最终我们需要指定6*4=24个顶点.但是我们知道,一个立方体其实总共只有八个顶点,要指定24次,就意味着每个顶点其实重复 ...

  8. OPENGL绘制文字

    OPENGL没有提供直接绘制文字的功能,需要借助于操作系统. 用OPENGL绘制文字比较常见的方法是利用显示列表.创建一系列显示列表,每个字符对应一个列表编号.例如,'A'对应列表编号1000+'A' ...

  9. 第一课、OpenGL绘制直线等等

    第一课.OpenGL绘制直线等等 分类: [开发技术]OpenGL 2012-01-18 14:59 5217人阅读 评论(0) 收藏 举报 buffer图形c // //  main.c //  o ...

随机推荐

  1. windows下 删除指定文件夹里面一周前的所有文件和文件夹的bat

    forfiles /p "指定文件夹路径" /m * /s /d -7 /c "cmd /c if @isdir==TRUE (rd /q @path) else del ...

  2. Python之2维list转置、旋转及其简单应用

    给一个矩阵,顺时针旋转顺序输出其元素,例如: 对于矩阵: [ 1, 2, 3 ] [ 4, 5, 6 ] [ 7, 8, 9 ] 输出为: 1,2,3,6,9,8,7,4,5 def transpos ...

  3. 第一个shell程序

    前言:我为什么又来学习shell呢?因为这个轻量级的编程小脚本语言能够帮我处理一些基于linux的复杂手工工作.真是一言难尽,学会一门又来一门!! 看了2天这个教程,试着写了一个小脚本,没啥技术含量, ...

  4. Unity---------Particle Effect详情

    Effects:效果/特效. Particle System:粒子系统.可用于创建烟雾.气流.火焰.涟漪等效果. 在Unity3D 3.5版本之后退出了新的shuriken粒子系统:   添加组件之后 ...

  5. 使用selenium遇到java.lang.NoSuchMethodError: org.apache.xpath.XPathContext,排查

    初试selenium webdriver,运行小程序,抛如下错误:   java.lang.NoSuchMethodError: org.apache.xpath.XPathContext.<i ...

  6. jquery 实现 Json 的一些转换方法

    有一个json 字符串 1)要判断该字符串是否是 json 格式 方法:将其转换成json对象,如果报异常,则不是,否则就是json格式 function isJsonFormat(str) { tr ...

  7. 设置时间同步(Linux,Solaris)

    经过网上各种搜索,将LINUX平台及solaris平台的时间同步整理如下: 主机情况:应用:2台LINUX服务器 redhat 5.5 内网数据库:2台Solaris服务器 Solaris 10 内网 ...

  8. 正则表达式awk

    以冒号: 为分隔符打印出来:打印第一段$1:  -F 分隔符 [root@localhost awk]# awk -F ':' '{print $1}' test.txt root bin daemo ...

  9. 相对和绝对路径/cd命令/创建和删除目录mkdir/rmdir/rm命令

    2.6 相对和绝对路径 2.7 cd命令 2.8 创建和删除目录mkdir/rmdir 2.9 rm命令 绝对路径:从根开始的路径:文件所在的路径: 相对路径:相对于当前目录而言的路径:上一级或者下一 ...

  10. Link1123:转换到COFF期间失败:文件无效或损坏

        当在编译VS项目时,出现如下错误:         这个错误,表明在连接阶段出错.COFF为Common Object File Format,通用对象文件格式,它的出现为混合语言编程带来方便 ...