public static FrustumCorners New() { var fc = new FrustumCorners { nearCorners = new Vector3[4], farCorners = new Vector3[4] }; return(fc); }
/// <summary> /// 创建两个摄像机的裁面顶点数组 /// </summary> private void InitFrustumCorners() { mainCamera_fcs = new FrustumCorners[lodLevel]; lightCamera_fcs = new FrustumCorners[lodLevel]; for (var i = 0; i < lodLevel; i++) { mainCamera_fcs[i] = FrustumCorners.New(); lightCamera_fcs[i] = FrustumCorners.New(); } }
protected override void AddVerticesAndIndices() { int baseIndex = _vertices.Count; FrustumCorners corners = _frustum.GetCorners(); _vertices.Add(new VertexPositionNormalTexture(corners.NearTopLeft, Vector3.One * 0.5f, Vector2.One * 0.5f)); _vertices.Add(new VertexPositionNormalTexture(corners.NearTopRight, Vector3.One * 0.5f, Vector2.One * 0.5f)); _vertices.Add(new VertexPositionNormalTexture(corners.NearBottomRight, Vector3.One * 0.5f, Vector2.One * 0.5f)); _vertices.Add(new VertexPositionNormalTexture(corners.NearBottomLeft, Vector3.One * 0.5f, Vector2.One * 0.5f)); _vertices.Add(new VertexPositionNormalTexture(corners.FarTopLeft, Vector3.Zero, Vector2.Zero)); _vertices.Add(new VertexPositionNormalTexture(corners.FarTopRight, Vector3.Zero, Vector2.Zero)); _vertices.Add(new VertexPositionNormalTexture(corners.FarBottomRight, Vector3.Zero, Vector2.Zero)); _vertices.Add(new VertexPositionNormalTexture(corners.FarBottomLeft, Vector3.Zero, Vector2.Zero)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 2)); }
protected override void AddVerticesAndIndices() { ushort baseIndex = checked ((ushort)_vertices.Count); FrustumCorners corners = _frustum.GetCorners(); _vertices.Add(new WireframeVertex(corners.NearTopLeft, Color)); _vertices.Add(new WireframeVertex(corners.NearTopRight, Color)); _vertices.Add(new WireframeVertex(corners.NearBottomRight, Color)); _vertices.Add(new WireframeVertex(corners.NearBottomLeft, Color)); _vertices.Add(new WireframeVertex(corners.FarTopLeft, Color)); _vertices.Add(new WireframeVertex(corners.FarTopRight, Color)); _vertices.Add(new WireframeVertex(corners.FarBottomRight, Color)); _vertices.Add(new WireframeVertex(corners.FarBottomLeft, Color)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 0)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 3)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 4)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 7)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 5)); _indices.Add((ushort)(baseIndex + 1)); _indices.Add((ushort)(baseIndex + 2)); _indices.Add((ushort)(baseIndex + 6)); _indices.Add((ushort)(baseIndex + 2)); }
void OnDrawGizmos() { if (dirLightCamera == null) { return; } /* * Gizmos.color = Color.blue; * for (int i = 0; i < 4; i++) * { * Gizmos.DrawSphere(mainCamera_fcs.nearCorners[i], 0.1f); * Gizmos.DrawSphere(mainCamera_fcs.farCorners[i], 0.1f); * } * * Gizmos.color = Color.red; * for (int i = 0; i < 4; i++) * { * Gizmos.DrawSphere(lightCamera_fcs.nearCorners[i], 0.1f); * Gizmos.DrawSphere(lightCamera_fcs.farCorners[i], 0.1f); * } * * Gizmos.color = Color.red; * Gizmos.DrawLine(lightCamera_fcs.nearCorners[0], lightCamera_fcs.nearCorners[1]); * Gizmos.DrawLine(lightCamera_fcs.nearCorners[1], lightCamera_fcs.nearCorners[2]); * Gizmos.DrawLine(lightCamera_fcs.nearCorners[2], lightCamera_fcs.nearCorners[3]); * Gizmos.DrawLine(lightCamera_fcs.nearCorners[3], lightCamera_fcs.nearCorners[0]); * * Gizmos.color = Color.green; * Gizmos.DrawLine(lightCamera_fcs.farCorners[0], lightCamera_fcs.farCorners[1]); * Gizmos.DrawLine(lightCamera_fcs.farCorners[1], lightCamera_fcs.farCorners[2]); * Gizmos.DrawLine(lightCamera_fcs.farCorners[2], lightCamera_fcs.farCorners[3]); * Gizmos.DrawLine(lightCamera_fcs.farCorners[3], lightCamera_fcs.farCorners[0]); * * Gizmos.DrawLine(lightCamera_fcs.nearCorners[0], lightCamera_fcs.farCorners[0]); * Gizmos.DrawLine(lightCamera_fcs.nearCorners[1], lightCamera_fcs.farCorners[1]); * Gizmos.DrawLine(lightCamera_fcs.nearCorners[2], lightCamera_fcs.farCorners[2]); * Gizmos.DrawLine(lightCamera_fcs.nearCorners[3], lightCamera_fcs.farCorners[3]); */ FrustumCorners[] fcs = new FrustumCorners[4]; for (int k = 0; k < 4; k++) { Gizmos.color = Color.white; Gizmos.DrawLine(mainCamera_Splits_fcs[k].nearCorners[1], mainCamera_Splits_fcs[k].nearCorners[2]); fcs[k].nearCorners = new Vector3[4]; fcs[k].farCorners = new Vector3[4]; for (int i = 0; i < 4; i++) { fcs[k].nearCorners[i] = dirLightCameraSplits[k].transform.TransformPoint(lightCamera_Splits_fcs[k].nearCorners[i]); fcs[k].farCorners[i] = dirLightCameraSplits[k].transform.TransformPoint(lightCamera_Splits_fcs[k].farCorners[i]); } Gizmos.color = Color.red; Gizmos.DrawLine(fcs[k].nearCorners[0], fcs[k].nearCorners[1]); Gizmos.DrawLine(fcs[k].nearCorners[1], fcs[k].nearCorners[2]); Gizmos.DrawLine(fcs[k].nearCorners[2], fcs[k].nearCorners[3]); Gizmos.DrawLine(fcs[k].nearCorners[3], fcs[k].nearCorners[0]); Gizmos.color = Color.green; Gizmos.DrawLine(fcs[k].farCorners[0], fcs[k].farCorners[1]); Gizmos.DrawLine(fcs[k].farCorners[1], fcs[k].farCorners[2]); Gizmos.DrawLine(fcs[k].farCorners[2], fcs[k].farCorners[3]); Gizmos.DrawLine(fcs[k].farCorners[3], fcs[k].farCorners[0]); Gizmos.DrawLine(fcs[k].nearCorners[0], fcs[k].farCorners[0]); Gizmos.DrawLine(fcs[k].nearCorners[1], fcs[k].farCorners[1]); Gizmos.DrawLine(fcs[k].nearCorners[2], fcs[k].farCorners[2]); Gizmos.DrawLine(fcs[k].nearCorners[3], fcs[k].farCorners[3]); } }