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. R语言包_dplyr_1

    有5个基础的函数: - filter - select - arrange - mutate - summarise - group_by (plus) 可以和databases以及data tabl ...

  2. win10: This file can't be opened

    win10打开bat脚本,不能运行,提示This file can't be opened. 解决方法如下: http://johnklann.com/these-files-cant-be-open ...

  3. e565. 关闭的时候隐藏窗口

    By default, when the close button on a frame is clicked, nothing happens. This example shows how to ...

  4. (转)LCD:LCD常用接口原理篇

    关键词:android LCD TFT TTL(RGB)  LVDS  EDP MIPI  TTL-LVDS  TTL-EDP平台信息:内核:linux2.6/linux3.0系统:android/a ...

  5. Android多任务切换与Activity启动模式SingleTask之间关系的分析

    这里会以多个场景列子进行分析,在分析之前先了解一下基本的概念. Task任务:一系列Activity的集合,这些Activity以栈的形式进行排列(后进先出). 那在什么时候系统会新建一个Task任务 ...

  6. Winform控件学习笔记【第五天】——ListView

    [第五天] 常用的基本属性: FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. GridLines:设置行和列之间是否显示网格线.( ...

  7. WAS集群:记一次Node Agent不活动问题解决过程

    之前很少接触集群,准确地说是很少接触项目现场的实施工作,或者说接触到的都是比较简单的实施工作,安装Linux.WAS.Oracle相对来说都比较简单.一直埋头干着研发的活,干着不要紧,一干就是好几年. ...

  8. MySQL 数据库定时自动备份

    创建备份目录 cd /home mkdir backup cd backup 创建备份 Shell 脚本: vim DatabaseName.sh #!/bin/bash /usr/local/mys ...

  9. php写文件操作

    function writeLog($file, $msg, $mode='a+') { $fp = fopen($file, $mode); if(flock($fp, LOCK_EX)) { fw ...

  10. tf.variable_scope

    转载:https://blog.csdn.net/gaoyueace/article/details/79079068 例如: #在名字为ae的命名空间内创建变量 with tf.variable_s ...