GL.Viewport 视口

static function Viewport (pixelRect : Rect) : 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.

所有渲染结果被限制在参数pixelRect.矩形内。如果视口大小发生变话,在视口里的内容会伸缩适应变化。

using UnityEngine;
using System.Collections;

public class example : 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();
	}
}
// 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();
}
最后修改:2011年3月22日 Tuesday 10:07

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。