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. mac配置--ant

    每次在新的电脑安装开发工具总是免不了下载各种软件和配置环境,本文针对mac下安装ant小结一下. 安装ant的方法很多,最直接的可以到apache-ant官网http://ant.apache.org ...

  2. How to deal with "Could not find component on update server. Contact VMware Support or your system administrator." in Vmware.

    手动将vmware安装目录下的vmtools镜像文件,windows.iso文件放到虚拟机的光区里. 再进入虚拟机的系统,在系统里打开光盘进行安装

  3. USB2.0学习笔记连载(四):安装Cypress官网套件

    上一篇博客大概讲了一下USB通用驱动程序的解析.笔者使用Cypress官网给定的资料去完成USB驱动开发.官网资料地址:http://www.cypress.com/?rID=14321 下载如下图的 ...

  4. Playing FPS Games with Deep Reinforcement Learning

    论文不同点: (1)用两套网络分别实现移动和射击. (2)使用LSTM来处理不完全信息. 疑问: (1)为什么对于射击使用RNN,对导航却没有使用RNN.一般来说,当我们看见视野里面有敌人的时候,我们 ...

  5. Python模拟Linux的Crontab, 写个任务计划需求

    Python模拟Linux的Crontab, 写个任务计划需求 来具体点 需求: 执行一个程序, 程序一直是运行状态, 这里假设是一个函数 当程序运行30s的时候, 需要终止程序, 可以用python ...

  6. Linux 错误记录

    1.libmysqlclient.so.18: cannot open shared object file: No such file or directory 解决办法: [root@linux- ...

  7. (原创)Python文件与文件系统系列(3)——os.path模块

    os.path 模块实现了一些操作路径名字符串的函数,可以通过 import os.path 使用该模块,不过即使仅仅 import os 也可以使用该模块的方法. 1. abspath(path) ...

  8. Radix-64编码简介

    本文介绍Radix-64编码,PGP和S/MIME均使用了Radix-64编码技术,rfc4880的Chap 6有关于Radix-64的详细描述. Radix-64编码基于Base64编码技术,由两部 ...

  9. C# wkhtmltopdf 将html转pdf

    一.转换程序代码如下: public string HtmlToPdf(string url) { bool success = true; // string dwbh = url.Split('? ...

  10. Linux SSH实现无密码远程登录

      一.      SSH无密码远程登录原理 二.      SSH实现无密码远程登录 实现主机A 无密码远程登录主机B 主机A   IP地址:10.8.9.154 主机B   IP地址:10.8.9 ...