Legacy Documentation: Version 4.6.2
Language: English
  • C#
  • JS
  • Boo

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

GL.Viewport

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

public static function Viewport(pixelRect: Rect): void;
public static void Viewport(Rect pixelRect);
public static def Viewport(pixelRect as Rect) as void

Description

Set the rendering viewport.

All rendering is constrained to be inside the passed pixelRect.
If the Viewport is modified, all the rendered content inside of it gets stretched.

	// 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
	var mat : Material;
	private var stretch : boolean = false;

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

function OnPostRender() { if (!mat) { Debug.LogError("Please Assign a material on the inspector"); return; } GL.PushMatrix(); mat.SetPass(0); GL.LoadPixelMatrix(); if(stretch) { GL.Viewport(Rect(0,0,Screen.width,Screen.height)); } else { GL.Viewport(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(); }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Material mat;
    private 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();
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public mat as Material

	private stretch as bool = false

	def Update() as void:
		if Input.GetKeyDown(KeyCode.Space):
			if stretch:
				stretch = false
			else:
				stretch = true

	def OnPostRender() as void:
		if not mat:
			Debug.LogError('Please Assign a material on the inspector')
			return
		GL.PushMatrix()
		mat.SetPass(0)
		GL.LoadPixelMatrix()
		if stretch:
			GL.Viewport(Rect(0, 0, Screen.width, Screen.height))
		else:
			GL.Viewport(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()