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. install ubuntu on Android mobile phone

    Android 是基于Linux内核的开源操作系统,主要用在移动设备上.当然同样是基于Linux内核的操作系统,现在支持的Android的智能手机理论来说都能运行基于Linux的操作系统,比如现在流行 ...

  2. 【转】在android程序中使用配置文件properties

    在android程序中使用配置文件来管理一些程序的配置信息其实非常简单 在这里我们主要就是用到Properties这个类直接给函数给大家 这个都挺好理解的 读写函数分别如下: //读取配置文件 pub ...

  3. IDEA 在某个工程下一个module如何使用另一个module中的资源文件(.xml .prop等)

    问题如题,经google,解决方案有四种,选择了比较直观有效的一种罗列如下: 因为项目采用maven管理,所以我们可以在module2下的pom.xml制定<resources>的路径,让 ...

  4. 对微软Microsoft Dynamics CRM 的认识

    MS CRM的认识 技术层面: MS CRM使用了当前最为流行的Web Service作为数据交互的手段,这给我们的二次开发和系统级的集成带来了无可比拟的方便性.易用性.我们不用关心如何去访问CRM数 ...

  5. InteliJ Idea pom.xml不自动提示的解决

    file–>Settings–>Build,Execute…–>Build Tools–>Maven–>Repositories 会看到类似表格的画面,内容是你的mave ...

  6. (转)DSound开发常用的几个结构

    WAVEFORMATEX WAVEFORMATEX { WORD wFormatTag; WORD nChannels; DWORD nSamplesPerSec; DWORD nAvgBytesPe ...

  7. 【Hibernate步步为营】--最后的集合映射

    上篇文章具体讨论了组合对象映射的原理.它事实上指的是怎样将对象模型中的组合关系映射到关系模型中,它是通过使用Hibernate提供的<component>标签来实现的,并须要在该标签中加入 ...

  8. jq dom不存在时绑定事件

    $( "a.offsite" ).live( "click", function() { alert( "Goodbye!" ); // j ...

  9. C# byte[]保存成文件

    string path = Server.MapPath(@"\a.pdf"); FileStream fs = new FileStream(path, FileMode.Cre ...

  10. Xshell的常用命令

    常用的命令: suse linux 常用命令 (1) 命令ls——列出文件 ls  显示当前目录文件 ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件 ls a* 列出当 ...