使用GL线:
我建议使用GL API绘制线条。在屏幕上,线条的粗细将始终为1px,并且无法更改它。也不会有阴影。
GL方法调用会立即执行,因此您需要确保在相机渲染后再调用它们。
将脚本附加到相机并使用Camera.OnPostRender()可以很好地在游戏窗口中进行渲染。要使其显示在编辑器中,可以使用MonoBehaviour.OnDrawGizmos()。
这是使用GL API划分界限的准系统代码:
public Material lineMat = new Material("Shader \"Lines/Colored Blended\" {" + "SubShader { Pass { " + " Blend SrcAlpha OneMinusSrcAlpha " + " ZWrite Off Cull Off Fog { Mode Off } " + " BindChannels {" + " Bind \"vertex\", vertex Bind \"color\", color }" + "} } }");
void OnPostRender() {
GL.Begin(GL.LINES);
lineMat.SetPass(0);
GL.Color(new Color(0f, 0f, 0f, 1f));
GL.Vertex3(0f, 0f, 0f);
GL.Vertex3(1f, 1f, 1f);
GL.End();
}
这是将所有给定点附加到主要点的完整脚本。在代码的注释中有一些说明,以正确进行设置以及正在发生的事情。
如果在更改连接线的颜色时遇到问题,请确保在线材上使用考虑了顶点颜色的着色器,例如Unlit/Color
。
using UnityEngine;
using System.Collections;
// Put this script on a Camera
public class DrawLines : MonoBehaviour {
// Fill/drag these in from the editor
// Choose the Unlit/Color shader in the Material Settings
// You can change that color, to change the color of the connecting lines
public Material lineMat;
public GameObject mainPoint;
public GameObject[] points;
// Connect all of the `points` to the `mainPoint`
void DrawConnectingLines() {
if(mainPoint && points.Length > 0) {
// Loop through each point to connect to the mainPoint
foreach(GameObject point in points) {
Vector3 mainPointPos = mainPoint.transform.position;
Vector3 pointPos = point.transform.position;
GL.Begin(GL.LINES);
lineMat.SetPass(0);
GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));
GL.Vertex3(mainPointPos.x, mainPointPos.y, mainPointPos.z);
GL.Vertex3(pointPos.x, pointPos.y, pointPos.z);
GL.End();
}
}
}
// To show the lines in the game window whne it is running
void OnPostRender() {
DrawConnectingLines();
}
// To show the lines in the editor
void OnDrawGizmos() {
DrawConnectingLines();
}
}
在阴影还请注意:我能否利用几何着色器,使阴影,但由于GL电话立即运行,他们是不是在正常渲染管线和AutoLight.cginc
并Lighting.cginc
不会拿起ShadowCaster
通。
阴影和半径线
如果需要更改线条粗细并希望具有逼真的阴影。只需使用圆柱网格并缩放高度即可。
这是一个脚本,它将使圆柱体将每个点连接到主要点。将其放在一个空的游戏对象上并填写参数。它将容纳所有其他连接对象。
using UnityEngine;
using System.Collections;
public class ConnectPointsWithCylinderMesh : MonoBehaviour {
// Material used for the connecting lines
public Material lineMat;
public float radius = 0.05f;
// Connect all of the `points` to the `mainPoint`
public GameObject mainPoint;
public GameObject[] points;
// Fill in this with the default Unity Cylinder mesh
// We will account for the cylinder pivot/origin being in the middle.
public Mesh cylinderMesh;
GameObject[] ringGameObjects;
// Use this for initialization
void Start () {
this.ringGameObjects = new GameObject[points.Length];
//this.connectingRings = new ProceduralRing[points.Length];
for(int i = 0; i < points.Length; i++) {
// Make a gameobject that we will put the ring on
// And then put it as a child on the gameobject that has this Command and Control script
this.ringGameObjects[i] = new GameObject();
this.ringGameObjects[i].name = "Connecting ring #" + i;
this.ringGameObjects[i].transform.parent = this.gameObject.transform;
// We make a offset gameobject to counteract the default cylindermesh pivot/origin being in the middle
GameObject ringOffsetCylinderMeshObject = new GameObject();
ringOffsetCylinderMeshObject.transform.parent = this.ringGameObjects[i].transform;
// Offset the cylinder so that the pivot/origin is at the bottom in relation to the outer ring gameobject.
ringOffsetCylinderMeshObject.transform.localPosition = new Vector3(0f, 1f, 0f);
// Set the radius
ringOffsetCylinderMeshObject.transform.localScale = new Vector3(radius, 1f, radius);
// Create the the Mesh and renderer to show the connecting ring
MeshFilter ringMesh = ringOffsetCylinderMeshObject.AddComponent<MeshFilter>();
ringMesh.mesh = this.cylinderMesh;
MeshRenderer ringRenderer = ringOffsetCylinderMeshObject.AddComponent<MeshRenderer>();
ringRenderer.material = lineMat;
}
}
// Update is called once per frame
void Update () {
for(int i = 0; i < points.Length; i++) {
// Move the ring to the point
this.ringGameObjects[i].transform.position = this.points[i].transform.position;
// Match the scale to the distance
float cylinderDistance = 0.5f*Vector3.Distance(this.points[i].transform.position, this.mainPoint.transform.position);
this.ringGameObjects[i].transform.localScale = new Vector3(this.ringGameObjects[i].transform.localScale.x, cylinderDistance, this.ringGameObjects[i].transform.localScale.z);
// Make the cylinder look at the main point.
// Since the cylinder is pointing up(y) and the forward is z, we need to offset by 90 degrees.
this.ringGameObjects[i].transform.LookAt(this.mainPoint.transform, Vector3.up);
this.ringGameObjects[i].transform.rotation *= Quaternion.Euler(90, 0, 0);
}
}
}