GL.Viewport

Unity · Версия 2020.3 LTS · Scripting · GL

GL.Viewport

Объявление

public static void Viewport(Rect pixelRect);

Описание

Установите область просмотра рендеринга.

Вся обработка ограничивается передачей pixelRect. При изменении области просмотра все визуализируемое содержимое внутри нее растягивается.

using UnityEngine;

public class Example : MonoBehaviour
{
    // Draw a red Quad that covers all the view port, and the when space is pressed
    // the viewport gets expanded to the whole screen and stretch the contents inside
    Material mat;
    bool stretch = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (stretch)
            {
                stretch = false;
            }
            else
            {
                stretch = true;
            }
        }
    }

    void OnPostRender()
    {
        if (!mat)
        {
            Debug.LogError("Please Assign a material on the inspector");
            return;
        }
        GL.PushMatrix();
        mat.SetPass(0);
        GL.LoadPixelMatrix();
        if (stretch)
        {
            GL.Viewport(new Rect(0, 0, Screen.width, Screen.height));
        }
        else
        {
            GL.Viewport(new Rect(0, 0, Screen.width / 2, Screen.height));
        }
        GL.Color(Color.red);
        GL.Begin(GL.QUADS);
        GL.Vertex3(0, 0, 0);
        GL.Vertex3(0, Screen.height, 0);
        GL.Vertex3(Screen.width, Screen.height, 0);
        GL.Vertex3(Screen.width, 0, 0);
        GL.Color(Color.yellow);
        GL.End();
        GL.Begin(GL.TRIANGLES);
        GL.Vertex3(Screen.width / 2, Screen.height / 4, 1);
        GL.Vertex3(Screen.width / 4, Screen.height / 2, 1);
        GL.Vertex3(Screen.width - Screen.width / 4, Screen.height / 2, 1);
        GL.End();
        GL.PopMatrix();
    }
}

Обновлено 17 ноября 2026 г.