using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class zhunxing : MonoBehaviour {
public Material _mat; //材质
public Color _starColor;//颜色
const float minDistance = 1.5f; //最小距离
const float maxDistance = 8f; //最大距离
public float length = 2.5f;
float distance = minDistance;
public float increaseSpeed = 5f;
Vector2[] offset;
int n = 0;
private void Start()
{
offset = new Vector2[16]; //绘制点的坐标数组
//enabled = false;
}
private void Update()
{
if (Input.GetMouseButton(0))
{
distance += Time.deltaTime * increaseSpeed; //按下鼠标 准星慢慢变大
}
else
{
distance -= Time.deltaTime * increaseSpeed; //慢慢变小
}
if (distance < minDistance) //判断准星的范围
{
distance = minDistance;
}
else if (distance > maxDistance)
{
distance = maxDistance;
}
}
private void OnPostRender() // 在相机渲染场景之后调用。这个消息被发送到所有附加在相机的脚本。
{
DrawLine(Input.mousePosition); //Input.mousePosition 鼠标位置
}
void DrawLine(Vector2 center)
{
// GL.PushMatrix();
_mat.SetPass(0); //材质通道设为0,只有6个设置 1 - 5;
GL.LoadOrtho();//调用这个函数后,视野的锥型区域从(0,0,-1) to (1,1,100)。这个函数用来绘制2D图形
GL.Begin(GL.LINES); //开始格式 GL.LINES 线 GL.QUADS 四边行 GL.TRIANGLE_STRIP 三角形帯 GL.TRIANGLES 三角形
GL.Color(_starColor); //绘制颜色
for (int j = 0; j < 2; j++)
{
int isY = j % 2 == 0 ? 1 : -1;
for (int i = 0; i < 4; i += 2)
{
int isX = i % 4 > 1 ? 1 : -1;
offset[j * 4 + i] = new Vector2(isX * (distance + length), isY * distance);
offset[j * 4 + i + 1] = new Vector2(isX * distance, isY * distance);
}
}
for (int i = 0; i < 8; i++)
{
offset[i + 8].x = offset[i].y; //将Y的距离给X
offset[i + 8].y = offset[i].x; //将X的距离给Y
}
for (int i = 0; i < offset.Length; i +=2)
{
//鼠标位置+
Draw(center + offset[i], center + offset[i + 1]);
}
GL.End();
// GL.PopMatrix();
}
void Draw(Vector2 start, Vector2 end)
{
//除以屏幕大小,就是点在屏幕的位置
//绘制一个顶点 Screen 屏幕 屏幕高度
GL.Vertex3(start.x / Screen.width, start.y / Screen.height, 0); // 这个函数只能在GL.Begin 和GL.End 之间使用
GL.Vertex3(end.x / Screen.width, end.y / Screen.height, 0);
}
}