返回介绍

GL.Viewport 视口

发布于 2019-12-18 15:37:44 字数 3875 浏览 1249 评论 0 收藏 0

JavaScript => public static function Viewport(pixelRect: Rect): void;
C# => public static void Viewport(Rect pixelRect);

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矩形内。如果视口大小发生变话,在视口里的内容会伸缩适应变化。

JavaScript:

	// 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();
	}

C#:

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();
    }
}

GL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文