unity3D技术之WaitForEndOfFrame 等待帧结束
2015-04-30 16:45
537 查看
Inherits from
YieldInstruction
Waits until the end of the frame after all cameras and GUI is rendered,
just before displaying the frame on screen.
等待直到所有的摄像机和
GUI
被渲染完成后,在该帧显示在屏幕之前。
You can use it to read the display into a texture, encode it as an image
file (see
Texture2D.ReadPixels
and
Texture2D.EncodeToPNG
) and send it
somewhere.
你可以使用它读取显示到纹理
【
狗刨学习网
】,编码它为一个图片文件(参见
Texture2D.ReadPixels
和)并且发送到任意地方。
C#
JavaScript
yield new WaitForEndOfFrame ();
// Saves screenshot as PNG file.
//
保持屏幕为
PNG
文件
import System.IO;
// Take a shot immediately
//
立即截屏
function Start() {
UploadPNG();
}
function UploadPNG() {
// We should only read the screen bufferafter rendering is
complete
//
等待完成后读取屏幕缓存
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
//
创建一个屏幕大小的图片,
RGB24
格式
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24,
false );
// Read screen contents into the texture
//
到图片读取屏幕内容
tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
//
编码图片为
PNG
格式
var bytes = tex.EncodeToPNG();
Destroy( tex );
//
For
testing
purposes,
also
write
to
a
file
in
the
project
folder
//
出于测试目的,也在工程文件夹保存一个文件
// File.WriteAllBytes(Application.dataPath +
"/../SavedScreen.png", bytes);
// Create a Web Form
//
创建一个
Web
表单
var form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("fileUpload",bytes);
// Upload to a cgi script
//
上传到一个
cgi
脚本
var w = WWW("http://localhost/cgi-bin/env.cgi?post", form);
yield w;
if (w.error != null)
print(w.error);
else
print("Finished Uploading Screenshot");
}
// Shows alpha channel contents in the game view.
// Requires Unity Pro as this script uses GL class.
//
在游戏视图中显示
alpha
通道的内容,需要
Unity
Pro
专业版,这个脚本使用
了
GL
类。
private var mat : Material;
// After the frame is completely rendered, we'll
// draw a full screen quad that extracts the alpha channel.
//
在该帧完全渲染之后,我们将绘制一个提取了
alpha
通道的全屏矩形
function Start() {
while (true) {
yield WaitForEndOfFrame();
if( !mat ) {
mat = new Material( "Shader \"Hidden/Alpha\" {"
+
"SubShader {" +
" Pass {" +
" ZTest Always Cull Off ZWrite Off" +
" Blend DstAlpha Zero" +
" Color (1,1,1,1)" +
" }" +
"}" +
"}"
);
}
GL.PushMatrix ();
GL.LoadOrtho ();
for (var i = 0; i < mat.passCount; ++i) {
mat.SetPass (i);
GL.Begin( GL.QUADS );
GL.Vertex3( 0, 0, 0.1 );
GL.Vertex3( 1, 0, 0.1 );
GL.Vertex3( 1, 1, 0.1 );
GL.Vertex3( 0, 1, 0.1 );
GL.End();
}
GL.PopMatrix ();
}
YieldInstruction
Waits until the end of the frame after all cameras and GUI is rendered,
just before displaying the frame on screen.
等待直到所有的摄像机和
GUI
被渲染完成后,在该帧显示在屏幕之前。
You can use it to read the display into a texture, encode it as an image
file (see
Texture2D.ReadPixels
and
Texture2D.EncodeToPNG
) and send it
somewhere.
你可以使用它读取显示到纹理
【
狗刨学习网
】,编码它为一个图片文件(参见
Texture2D.ReadPixels
和)并且发送到任意地方。
C#
JavaScript
yield new WaitForEndOfFrame ();
// Saves screenshot as PNG file.
//
保持屏幕为
PNG
文件
import System.IO;
// Take a shot immediately
//
立即截屏
function Start() {
UploadPNG();
}
function UploadPNG() {
// We should only read the screen bufferafter rendering is
complete
//
等待完成后读取屏幕缓存
yield WaitForEndOfFrame();
// Create a texture the size of the screen, RGB24 format
//
创建一个屏幕大小的图片,
RGB24
格式
var width = Screen.width;
var height = Screen.height;
var tex = new Texture2D( width, height, TextureFormat.RGB24,
false );
// Read screen contents into the texture
//
到图片读取屏幕内容
tex.ReadPixels( Rect(0, 0, width, height), 0, 0 );
tex.Apply();
// Encode texture into PNG
//
编码图片为
PNG
格式
var bytes = tex.EncodeToPNG();
Destroy( tex );
//
For
testing
purposes,
also
write
to
a
file
in
the
project
folder
//
出于测试目的,也在工程文件夹保存一个文件
// File.WriteAllBytes(Application.dataPath +
"/../SavedScreen.png", bytes);
// Create a Web Form
//
创建一个
Web
表单
var form = new WWWForm();
form.AddField("frameCount", Time.frameCount.ToString());
form.AddBinaryData("fileUpload",bytes);
// Upload to a cgi script
//
上传到一个
cgi
脚本
var w = WWW("http://localhost/cgi-bin/env.cgi?post", form);
yield w;
if (w.error != null)
print(w.error);
else
print("Finished Uploading Screenshot");
}
// Shows alpha channel contents in the game view.
// Requires Unity Pro as this script uses GL class.
//
在游戏视图中显示
alpha
通道的内容,需要
Unity
Pro
专业版,这个脚本使用
了
GL
类。
private var mat : Material;
// After the frame is completely rendered, we'll
// draw a full screen quad that extracts the alpha channel.
//
在该帧完全渲染之后,我们将绘制一个提取了
alpha
通道的全屏矩形
function Start() {
while (true) {
yield WaitForEndOfFrame();
if( !mat ) {
mat = new Material( "Shader \"Hidden/Alpha\" {"
+
"SubShader {" +
" Pass {" +
" ZTest Always Cull Off ZWrite Off" +
" Blend DstAlpha Zero" +
" Color (1,1,1,1)" +
" }" +
"}" +
"}"
);
}
GL.PushMatrix ();
GL.LoadOrtho ();
for (var i = 0; i < mat.passCount; ++i) {
mat.SetPass (i);
GL.Begin( GL.QUADS );
GL.Vertex3( 0, 0, 0.1 );
GL.Vertex3( 1, 0, 0.1 );
GL.Vertex3( 1, 1, 0.1 );
GL.Vertex3( 0, 1, 0.1 );
GL.End();
}
GL.PopMatrix ();
}
相关文章推荐
- Unity3D 中 脚本(MonoBehaviour) 生命周期WaitForEndOfFrame需要注意的地方
- WaitForEndOfFrame
- 如何调用一个程序,等待运行结束,并知晓程序运行成功与否:ShellExecuteEx;WaitForSingleObject;GetExitCodeProcess(C代码)
- 如何调用一个程序,等待运行结束,并知晓程序运行成功与否:ShellExecuteEx;WaitForSingleObject;GetExitCodeProcess(C代码)
- 等待线程结束的高手--WaitForSingleObject
- Windows和Linux上分别怎样实现等待某个线程结束?---用WaitForSingleObject和pthread_join
- WaitForSingleObject 示例之一: 等待另一个进程的结束
- Win32多线程之等待一个线程的结束(WaitForSingleObject)
- 主线程利用MsgWaitForMultipleObjects等待子线程结束时,同时处理子线程发送的窗口消息
- fatal error C1010: unexpected end of file while looking for precompiled header directive
- 【VS开发】WaitForSingleObject 和 WaitForMultipleObjects函数 (让线程挂起等待事件)
- delphi 中使用WaitForMultipleObjects等待线程执行,再执行后续代码
- 添加Basic excel后出现Error 2 error C1010: unexpected end of file while looking for precompiled
- Windows下pthread多线程使用(5):WaitForAnEnd
- fatal error C1010: unexpected end of file while looking for precompiled header directive的处理
- fatal error C1010: unexpected end of file while looking for precompiled header directive
- [LeetCode] Search for a Range (sorted integers array,find start & end position of a target number)
- fatal error C1010: unexpected end of file while looking for precompiled header directive
- 开启后需要等待APB1时钟与RTC时钟同步才能读写寄存器 RTC_WaitForSynchro
- 等待子进程结束wait()和waitpid()