- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 unity 创建一个小的测试应用程序。我正在构建应用程序的一个实例并将其设置为充当主机,然后在统一编辑器中作为客户端运行另一个实例。在当前的迭代中,我有一个按钮,按下时应该截取屏幕截图,然后将该屏幕截图发送给所有客户端。但似乎没有任何效果。我遇到的最初问题是 ReadPixels 错误:
ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame. UnityEngine.Texture2D:ReadPixels(Rect, Int32, Int32)
.
UNETChat:OnPostRender() (at Assets/Scripts/UNETChat.cs:50)
<TakeSnapshot>c__Iterator0:MoveNext() (at Assets/Scripts/UNETChat.cs:42)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
除此之外,一旦解决,我确信这里还有很多额外的工作是不合时宜的。但我才刚刚开始。感谢您的帮助!
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.NetworkSystem;
public class MyMsgType
{
public static short texture = MsgType.Highest + 1;
};
//Create a class that holds a variables to send
public class TextureMessage : MessageBase
{
public byte[] textureBytes;
public string message = "Test Received Message"; //Optional
}
public class UNETChat : Chat
{
NetworkClient myClient;
public Texture2D previewTexture;
string messageToSend = "Screen Short Image";
private void Start()
{
//if the client is also the server
if (NetworkServer.active)
{
// Register to connect event
myClient.RegisterHandler(MsgType.Connect, OnConnected);
// Register to texture receive event
myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
}
setupClient();
}
public IEnumerator TakeSnapshot(int width, int height)
{
yield return new WaitForSeconds(0.1F);
Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
texture.LoadRawTextureData(texture.GetRawTextureData());
texture.Apply();
sendTexture(texture, messageToSend);
// gameObject.renderer.material.mainTexture = TakeSnapshot;
}
public void DoSendTexture()
{
StartCoroutine(TakeSnapshot(Screen.width, Screen.height));
}
// SERVER //////////////////////////
//Call to send the Texture and a simple string message
public void sendTexture(Texture2D texture, string message)
{
TextureMessage msg = new TextureMessage();
//Convert Texture2D to byte array
msg.textureBytes = texture.GetRawTextureData();
msg.message = message;
NetworkServer.SendToAll(MyMsgType.texture, msg);
Debug.Log("Texture Sent!!");
}
// CLIENT //////////////////////////
// Create a client and connect to the server port
public void setupClient()
{
//Create new client
myClient = new NetworkClient();
//Register to connect event
myClient.RegisterHandler(MsgType.Connect, OnConnected);
//Register to texture receive event
myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);
//Connect to server
myClient.Connect("localhost", 4444);
}
//Called when texture is received
public void OnTextureReceive(NetworkMessage netMsg)
{
TextureMessage msg = netMsg.ReadMessage<TextureMessage>();
//Your Received message
string message = msg.message;
Debug.Log("Texture Messsage " + message);
//Your Received Texture2D
Texture2D receivedtexture = new Texture2D(4, 4);
receivedtexture.LoadRawTextureData(msg.textureBytes);
receivedtexture.Apply();
}
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
}
最佳答案
等到当前帧结束后再截取屏幕截图或调用 Texture2D.ReadPixels
函数。这可以通过 WaitForEndOfFrame
类来完成。请注意我是如何缓存它的,以避免每次调用 TakeSnapshot
函数时都创建新对象。
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
public IEnumerator TakeSnapshot(int width, int height)
{
yield return frameEnd;
Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
texture.LoadRawTextureData(texture.GetRawTextureData());
texture.Apply();
sendTexture(texture, messageToSend);
// gameObject.renderer.material.mainTexture = TakeSnapshot;
}
我注意到您的脚本中有 yield return new WaitForSeconds(0.1F)
,它在截屏前等待 0.2
秒。如果您必须等待 0.2
秒,则先等待 0.2
秒,然后在截屏之前等待帧结束:
WaitForSeconds waitTime = new WaitForSeconds(0.1F);
WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
public IEnumerator TakeSnapshot(int width, int height)
{
yield return waitTime;
yield return frameEnd;
Texture2D texture = new Texture2D(800, 800, TextureFormat.RGB24, true);
texture.ReadPixels(new Rect(0, 0, 800, 800), 0, 0);
texture.LoadRawTextureData(texture.GetRawTextureData());
texture.Apply();
sendTexture(texture, messageToSend);
// gameObject.renderer.material.mainTexture = TakeSnapshot;
}
关于c# - 调用 ReadPixels 以从系统帧缓冲区读取像素,而不是在绘图帧内。 UnityEngine.Texture2D :ReadPixels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46595055/
我正在尝试使用 unity 创建一个小的测试应用程序。我正在构建应用程序的一个实例并将其设置为充当主机,然后在统一编辑器中作为客户端运行另一个实例。在当前的迭代中,我有一个按钮,按下时应该截取屏幕截图
我正在尝试将图像(作为纹理)绘制到帧缓冲区,应用锐度过滤器(通过拖动 UI 上的 slider ),然后从帧缓冲区读取结果,并通过调用将数据复制到简单的 2d Canvas (不是 webgl) re
为什么我从WebGLRenderingContext.readPixels()获得的imageData颠倒了? 我尝试做以下事情: var gl = renderer.domElement.getCo
我不明白为什么我在 requestanimationframe 循环内丢失了 readpixel 值? var pixels = new Uint8Array(12*12*4); gl.clearCo
我使用 webgl_draw_buffers 扩展创建了一个帧缓冲区对象,以提供三个颜色缓冲区: colorBuffer0 = gl.createRenderbuffer(); gl.bind
当尝试使用大量数据(但屏幕上几乎没有数据)进行 HitTest 时,即使帧时间远低于 16.7 毫秒(总共 10 毫秒),读取像素也非常慢 我正在开发一个在 webgl 中运行的 datavis 平台
谁能告诉我如何在 JOGL 中从缓冲区中读取像素。请用代码说明。 最佳答案 渲染完成后,调用这个方法: public BufferedImage toImage(GL gl, int w, int h
我一直在尝试使用 WebGL 来实现裁剪和封顶。为此,我必须使用模板缓冲区来限制剪切平面的绘制,以便仅绘制对象内部的部分。 首先,我一直在尝试使用模板缓冲区。我的目标是检查 gl.clearStenc
我正在尝试使用 WebGL 来加速小型量子电路模拟中的计算,就像 Quantum Computing Playground does 那样。 。我遇到的问题是 readPixels 需要大约 10ms
我想从我的相机 View 中检索深度缓冲区以用于 3D 过滤应用程序。目前,我正在使用 glReadPixels 来获取深度组件。而不是 [0,1] 值,我需要深度缓冲区的真实值,或者在世界坐标中到相
基本上我有 200 X 200 像素的 Canvas ,一个 20 X 20 X 20 像素的体素。所以我有 100 个体素。当我在 Canvas 上绘图时,我想查看绘制了哪个像素,如果该像素位于其中
我将 id(这是一个包含在 24 位范围内的值)存储到 Float32Array(3) 中,以便稍后在 WebGL 中检索: var r = 0, g = 0, b = id; if
我正在尝试访问像素数据并将图像从游戏中的相机保存到磁盘。最初,简单的方法是使用渲染目标,然后使用RenderTarget-> ReadPixels(),但是由于ReadPixels()的 native
我已更新到 FireFox35,但以下代码不再起作用: var ctx = renderer2.getContext("experimental-webgl",{preserveDrawing
我正在使用headless-gl与 Node 一起在服务器端渲染生成的高分辨率 WebGL 图像。根据所选的分辨率,这可以正常工作。 'use strict'; const Gl = require(
我正在使用 headless-gl 在 Node.js 上运行 webGL,在服务器上动态创建图像。创建后,图像将存储在数据库 (MongoDB) 中,然后用户可以通过 API 再次访问图像。 下面是
图像采用 Base64 格式,我想比较像素关系。 在了解 GPU 的工作原理后,我认为这种类型的分析在 Canvas webgl 上下文中表现最佳,而不是像我之前使用的 2d 。我以前没有使用过 We
我试图将 RenderTexture 绘制到 Texture2D 中,目的是将其保存到磁盘。这种方法在 OSX 编辑器和 Android 上一直有效。 我在 XCode 控制台中没有看到任何错误,当我
我创建了两个 html 客户端,它们使用 websocket 服务器相互通信。一个客户端使用 Three.js 在其 Canvas 上绘制 3D 模型,并将 Canvas 上下文(即 webGl)作为
是否可以在具有多个附件的帧缓冲区上以浮点形式检索像素值? (WebGL 2) 我试过这个: var framebuffer = _gl.createFramebuffer(); _gl.bindFra
我是一名优秀的程序员,十分优秀!