Unity基于URP管线利用GL.wireframe快捷绘制物体双面线框

使用循环正反面都分别绘制了一遍,感觉应该有一次就把正反面都绘制完成的办法。主要是GL这块还没完全看明白,不着急,慢慢来。

代码参考:

using UnityEngine;
using UnityEngine.Rendering;

public class GLWireFrame : MonoBehaviour
{
	[SerializeField]
	MeshFilter filter;
	[SerializeField]
	Material matFront;
	[SerializeField]
	Material matBack;

	Vector3[] verts;
	int[] tris;
	private void Start()
	{
		verts = filter.mesh.vertices;
		tris = filter.mesh.triangles;
	}

	void OnEnable()
	{
		RenderPipelineManager.endCameraRendering += EndCameraRendering;
	}

	void OnDisable()
	{
		RenderPipelineManager.endCameraRendering -= EndCameraRendering;
	}

	void EndCameraRendering(ScriptableRenderContext context, Camera camera)
	{
		GL.MultMatrix(transform.localToWorldMatrix);
		GL.wireframe = true;

		GL.Begin(GL.TRIANGLES);

		matBack.SetPass(0);

		for (int i = 0; i < tris.Length; i += 3)
		{
			GL.Vertex(verts[tris[i]]);
			GL.Vertex(verts[tris[i + 2]]);
			GL.Vertex(verts[tris[i + 1]]);
		}

		GL.End();

		matFront.SetPass(0);
		GL.Begin(GL.TRIANGLES);

		for (int i = 0; i < tris.Length; i+=3)
		{
			GL.Vertex(verts[tris[i]]);
			GL.Vertex(verts[tris[i +1]]);
			GL.Vertex(verts[tris[i +2]]);
		}

		GL.End();
		GL.wireframe = false;
	}
}

你可能感兴趣的:(Unity,GL,Unity)