经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
Unity实现粒子光效导出成png序列帧
来源:jb51  时间:2019/3/18 9:36:19  对本文有异议

本文为大家分享了Unity实现粒子光效导出成png序列帧的具体代码,供大家参考,具体内容如下

这个功能并不是很实用,不过美术同学有这样的需求,那么就花了一点时间研究了下。

我们没有使用Unity的引擎,但是做特效的同学找了一批Unity的粒子特效,希望导出成png序列帧的形式,然后我们的游戏来使用。这个就相当于拿Unity做了特效编辑器的工作。这个并不是很“邪门”,因为用幻影粒子,或者3dmax,差不多也是这个思路,只不过那些软件提供了正规的导出功能,而Unity则没有。

先上代码

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. public class ParticleExporter : MonoBehaviour
  8. {
  9. // Default folder name where you want the animations to be output
  10. public string folder = "PNG_Animations";
  11. // Framerate at which you want to play the animation
  12. public int frameRate = 25; // export frame rate 导出帧率,设置Time.captureFramerate会忽略真实时间,直接使用此帧率
  13. public float frameCount = 100; // export frame count 导出帧的数目,100帧则相当于导出5秒钟的光效时间。由于导出每一帧的时间很长,所以导出时间会远远长于直观的光效播放时间
  14. public int screenWidth = 960; // not use 暂时没用,希望可以直接设置屏幕的大小(即光效画布的大小)
  15. public int screenHeight = 640;
  16. public Vector3 cameraPosition = Vector3.zero;
  17. public Vector3 cameraRotation = Vector3.zero;
  18. private string realFolder = ""; // real folder where the output files will be
  19. private float originaltimescaleTime; // track the original time scale so we can freeze the animation between frames
  20. private float currentTime = 0;
  21. private bool over = false;
  22. private int currentIndex = 0;
  23. private Camera exportCamera; // camera for export 导出光效的摄像机,使用RenderTexture
  24. public void Start()
  25. {
  26. // set frame rate
  27. Time.captureFramerate = frameRate;
  28. // Create a folder that doesn't exist yet. Append number if necessary.
  29. realFolder = Path.Combine(folder, name);
  30. // Create the folder
  31. if (!Directory.Exists(realFolder)) {
  32. Directory.CreateDirectory(realFolder);
  33. }
  34. originaltimescaleTime = Time.timeScale;
  35. GameObject goCamera = Camera.main.gameObject;
  36. if (cameraPosition != Vector3.zero) {
  37. goCamera.transform.position = cameraPosition;
  38. }
  39. if (cameraRotation != Vector3.zero) {
  40. goCamera.transform.rotation = Quaternion.Euler(cameraRotation);
  41. }
  42. GameObject go = Instantiate(goCamera) as GameObject;
  43. exportCamera = go.GetComponent<Camera>();
  44. currentTime = 0;
  45. }
  46. void Update()
  47. {
  48. currentTime += Time.deltaTime;
  49. if (!over && currentIndex >= frameCount) {
  50. over = true;
  51. Cleanup();
  52. Debug.Log("Finish");
  53. return;
  54. }
  55. // 每帧截屏
  56. StartCoroutine(CaptureFrame());
  57. }
  58. void Cleanup()
  59. {
  60. DestroyImmediate(exportCamera);
  61. DestroyImmediate(gameObject);
  62. }
  63. IEnumerator CaptureFrame()
  64. {
  65. // Stop time
  66. Time.timeScale = 0;
  67. // Yield to next frame and then start the rendering
  68. // this is important, otherwise will have error
  69. yield return new WaitForEndOfFrame();
  70. string filename = String.Format("{0}/{1:D04}.png", realFolder, ++currentIndex);
  71. Debug.Log(filename);
  72. int width = Screen.width;
  73. int height = Screen.height;
  74. //Initialize and render textures
  75. RenderTexture blackCamRenderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
  76. RenderTexture whiteCamRenderTexture = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);
  77. exportCamera.targetTexture = blackCamRenderTexture;
  78. exportCamera.backgroundColor = Color.black;
  79. exportCamera.Render();
  80. RenderTexture.active = blackCamRenderTexture;
  81. Texture2D texb = GetTex2D();
  82. //Now do it for Alpha Camera
  83. exportCamera.targetTexture = whiteCamRenderTexture;
  84. exportCamera.backgroundColor = Color.white;
  85. exportCamera.Render();
  86. RenderTexture.active = whiteCamRenderTexture;
  87. Texture2D texw = GetTex2D();
  88. // If we have both textures then create final output texture
  89. if (texw && texb) {
  90. Texture2D outputtex = new Texture2D(width, height, TextureFormat.ARGB32, false);
  91. // we need to check alpha ourselves,because particle use additive shader
  92. // Create Alpha from the difference between black and white camera renders
  93. for (int y = 0; y < outputtex.height; ++y) { // each row
  94. for (int x = 0; x < outputtex.width; ++x) { // each column
  95. float alpha;
  96. alpha = texw.GetPixel(x, y).r - texb.GetPixel(x, y).r;
  97. alpha = 1.0f - alpha;
  98. Color color;
  99. if (alpha == 0) {
  100. color = Color.clear;
  101. } else {
  102. color = texb.GetPixel(x, y);
  103. }
  104. color.a = alpha;
  105. outputtex.SetPixel(x, y, color);
  106. }
  107. }
  108. // Encode the resulting output texture to a byte array then write to the file
  109. byte[] pngShot = outputtex.EncodeToPNG();
  110. File.WriteAllBytes(filename, pngShot);
  111. // cleanup, otherwise will memory leak
  112. pngShot = null;
  113. RenderTexture.active = null;
  114. DestroyImmediate(outputtex);
  115. outputtex = null;
  116. DestroyImmediate(blackCamRenderTexture);
  117. blackCamRenderTexture = null;
  118. DestroyImmediate(whiteCamRenderTexture);
  119. whiteCamRenderTexture = null;
  120. DestroyImmediate(texb);
  121. texb = null;
  122. DestroyImmediate(texw);
  123. texb = null;
  124. System.GC.Collect();
  125. // Reset the time scale, then move on to the next frame.
  126. Time.timeScale = originaltimescaleTime;
  127. }
  128. }
  129. // Get the texture from the screen, render all or only half of the camera
  130. private Texture2D GetTex2D()
  131. {
  132. // Create a texture the size of the screen, RGB24 format
  133. int width = Screen.width;
  134. int height = Screen.height;
  135. Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
  136. // Read screen contents into the texture
  137. tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
  138. tex.Apply();
  139. return tex;
  140. }
  141. }
  142.  

这里对几个关键的知识点来做说明:

1、整体思路是这样的,Unity中调整好摄像机,正常播放特效,然后每帧截屏,保存成我们需要的png序列帧。这个不仅仅是特效可以这么用,其实模型也可以。比如我们需要同屏显示几百上千人,或者是无关紧要的怪物、场景物件等等,就可以使用这个导出成2d的序列帧,可以大大提高效率,使一些不可能的情况变为可能。

2、关于时间和帧率的控制。由于截屏所需要的时间远远大于帧间隔,所以光效如果是播放1秒,则导出时间可能超过一分钟。Time.captureFrameRate可以设置帧率,设置后则忽略真实时间,光效、模型会按照帧率的时间来播放。这个接口恰好就是用在视频录制上的。

3、光效画布控制。这个暂时没有找到好的方法,由于是全屏幕截屏,所以Game窗口的大小就是光效画布的大小。

4、通过调整摄像机的位置、旋转,控制光效的显示信息。

5、截屏函数就是GetTex2D()。这里面最主要的是ReadPixels函数。需要注意,CaptureFrame函数必须要以协程的方式运行,因为里面有一句yield return new WaitForEndOfFrame();如果没有这一句,会报一个错误,大概意思就是ReadPixels不在DrawFrame里面运行。

6、截屏时间消耗很大,所以需要在截屏开始使用Time.timeScale=0暂停时间运行,截屏后再恢复

7、注意截屏操作完成后清理各种资源,并进行GC。否则内存很有可能就不够用了,截100帧图片,内存很有可能就两三G了。

8、截屏的时候使用了两个RenderTexture,分别绘制白底和黑底的图片,然后根据这两张图片计算出alpha。如果不是光效其实可以不这么麻烦,直接把Camera的backgroundColor中的alpha设置为0就可以了。但是光效使用了特殊的shader,比如Additive,这里涉及到alpha blend。绘制光效时如果也这样设置的话,导出的图片没有任何东西。所以必须要有实色背景。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号