简体   繁体   English

渲染网格以将renderTexture转换为Texture2D

[英]Render mesh to renderTexture convert to Texture2D

I am trying to dynamically create a billboard from a given mesh. 我正在尝试根据给定的网格动态创建广告牌。 I am trying to do this with Graphics.DrawMeshNow to the active rendertexture, then convert the renderTexture to a texture 2d. 我正在尝试使用Graphics.DrawMeshNow到活动的rendertexture,然后将renderTexture转换为纹理2d。 This is my understanding of how RenderTexture through code works, that the next draw call is rendered to the active rt. 这是我对RenderTexture通过代码的工作方式的理解,即下一个绘制调用将呈现给活动rt。 for ReadPixel, it reads the pixels of the active rt. 对于ReadPixel,它将读取活动rt的像素。 I have also tried to attach the renderTexture to the billboard, skipping the texture2d but with no luck. 我也尝试将renderTexture附加到广告牌,跳过texture2d但没有运气。

var renderTexture = RenderTexture.GetTemporary(width,height,24);
var billboardTexture = new Texture2D(256, 256,TextureFormat.RGB24,false);
Graphics.SetRenderTarget(renderTexture);
Graphics.DrawMeshNow(quad, CurrentActiveGameObject.transform.localPosition,Quaternion.identity);
billboardTexture.ReadPixels(new Rect(0,0,width,height), 0,0);
billboardTexture.Apply();
billboardTexture.name = name + "_billboardTexture";
primMeshRenderer.material.mainTexture = billboardTexture;

When it gets to the last line of this code block, the billboardTexture is null. 当到达此代码块的最后一行时,billboardTexture为null。 1) What have I missed, how can I draw a mesh to a renderTexture? 1)我错过了什么,如何绘制网格到renderTexture? 2) can I only use renderTexture with a camera? 2)我只能在相机上使用renderTexture吗? 3) how can I test this? 3)我该如何测试? How can I test the DrawMeshNow is drawing to the renderTexture? 如何测试DrawMeshNow正在绘制到renderTexture? I've step through the code but that gave me no clues as I wasn't too sure on what the objects should have contained. 我已经遍历了代码,但是由于我不太确定对象应该包含什么内容,因此没有任何线索。 In my scene the mesh appears, which is suggesting its rendering to the camera still and not the RenderTexture. 在我的场景中,出现了网格,这表明仍在相机上渲染它,而不是在RenderTexture上。

Thanks. 谢谢。

Forgot to mention this is a editor script and is executed by a menu/shortcut. 忘记提及这是一个编辑器脚本,由菜单/快捷方式执行。

Edit Question, Do I need to wait for endofFrame after DrawMeshNow for the ReadPixels to work? 编辑问题,是否需要在DrawMeshNow之后等待endofFrame才能使ReadPixels正常工作?

The documents to me suggest that you should have been able to readPixel from a Temporary renderTexture, ReadPixel . 给我的文档建议您应该能够从Temporary renderTexture ReadPixel读取readPixel But maybe a temp renderTexture is never 'active'? 但是也许临时的renderTexture永远不会“活跃”? Since I was never able to readPixel from the Temporary rendertexture, in the end I created a 2nd camera, assigned the camera with a renderTexture object and made it active, I then readPixel from that renderTexture. 由于我永远无法从Temporary rendertexture中读取像素,因此最终我创建了第二台摄像机,为该摄像机分配了renderTexture对象并将其激活,然后从该renderTexture中读取像素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM