服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity实现粒子光效导出成png序列帧

Unity实现粒子光效导出成png序列帧

2022-07-13 09:31langresser C#

这篇文章主要为大家详细介绍了Unity实现粒子光效导出成png序列帧,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

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

先上代码

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

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

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。绘制光效时如果也这样设置的话,导出的图片没有任何东西。所以必须要有实色背景。

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

原文链接:https://blog.csdn.net/langresser_king/article/details/44036983

延伸 · 阅读

精彩推荐
  • C#C#实现Word转为PDF的方法

    C#实现Word转为PDF的方法

    今天小编就为大家分享一篇关于C#实现Word转为PDF的方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看...

    chenqiangdage7202022-03-08
  • C#C#窗体控件DataGridView常用设置

    C#窗体控件DataGridView常用设置

    这篇文章主要为大家详细介绍了C#窗体控件DataGridView常用10项设置,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    云梦鸿5392022-01-19
  • C#UGUI轮播图组件实现方法详解

    UGUI轮播图组件实现方法详解

    这篇文章主要为大家详细介绍了UGUI轮播图组件的实现方法,支持自动轮播、手势切换等功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    阿循3912022-07-09
  • C#C# List引用类型克隆的3种方法

    C# List引用类型克隆的3种方法

    这篇文章主要给大家介绍了关于C# List引用类型克隆的3种方法,包括反射、序列化(依赖Newtonsoft.Json) 以及序列化(BinaryFormatter)的实现方法,需要的朋友可...

    陈国良9752022-03-09
  • C#C#通过NPOI操作Excel的实例代码

    C#通过NPOI操作Excel的实例代码

    C#操作Excel的方法有很多种,本文介绍了C#通过NPOI操作Excel,具有一定的参考价值,有兴趣的可以了解一下。...

    独钓寒江雪丶6362021-12-18
  • C#C#代码实现对AES加密解密

    C#代码实现对AES加密解密

    这篇文章主要介绍了C#代码实现对AES加密解密的相关资料,AES是一个新的可以用于保护电子数据的加密算法,需要的朋友可以参考下...

    C#教程网7222021-11-05
  • C#C#数据结构之顺序表(SeqList)实例详解

    C#数据结构之顺序表(SeqList)实例详解

    这篇文章主要介绍了C#数据结构之顺序表(SeqList)实现方法,结合实例形式较为详细的分析了顺序表的定义、原理与具体实现技巧,具有一定参考借鉴价值,需要...

    Jimmy.Yang10012021-11-04
  • C#基于Json序列化和反序列化通用的封装完整代码

    基于Json序列化和反序列化通用的封装完整代码

    JSON 是存储和交换文本信息的语法。类似 XML。JSON 比 XML 更小、更快,更易解析。下面通过实例代码给大家分享Json序列化和反序列化通用的封装,需要的的...

    LowKeyC4562022-01-17