Skip to content

Commit 8ef75dc

Browse files
committed
Merge branch 'unstable' of https://github.com/Banbury/UnitySpritesAndBones into unstable
2 parents e49dbf8 + ca73bb8 commit 8ef75dc

File tree

2 files changed

+262
-0
lines changed

2 files changed

+262
-0
lines changed
Binary file not shown.
Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
using UnityEngine;
2+
using System;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
/*
8+
The MIT License (MIT)
9+
10+
Copyright (c) 2014 Brad Nelson and Play-Em Inc.
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a copy
13+
of this software and associated documentation files (the "Software"), to deal
14+
in the Software without restriction, including without limitation the rights
15+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
copies of the Software, and to permit persons to whom the Software is
17+
furnished to do so, subject to the following conditions:
18+
19+
The above copyright notice and this permission notice shall be included in
20+
all copies or substantial portions of the Software.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+
THE SOFTWARE.
29+
*/
30+
31+
// AnimationToPNG is based on Twinfox and bitbutter's Render Particle to Animated Texture Scripts, this script will render out an animation that is played when "Play" is pressed in the editor.
32+
33+
/* Basically this is a script you can attach to any gameobject in the scene, but you have to reference a Black Camera and White Camera both of which should be set to orthographic, but this script should have it covered. There is a prefab called AnimationToPNG which should include everything needed to run the script.
34+
35+
If you have Unity Pro, you can use Render Texture, which can accurately render the transparent background for your animations easily in full resolution of the camera. Just check the box for the variable "useRenderTexture" to use RenderTextures instead. If you are using Unity Free, then leave this unchecked and you will have a split area using half of the screen width to render the animations.
36+
37+
You can change the "animationName" to a string of your choice for a prefix for the output file names, if it is left empty then no filename will be added.
38+
39+
The destination folder is relative to the Project Folder root, so you can change the string to a folder name of your choice and it will be created. If it already exists, it will simply create a new folder with a number incremented as to how many of those named folders exist.
40+
41+
Choose how many frames per second the animation will run by changing the "frameRate" variable, and how many frames of the animation you wish to capture by changing the "framesToCapture" variable.
42+
43+
Once "Play" is pressed in the Unity Editor, it should output all the animation frames to PNGs output in the folder you have chosen, and will stop capturing after the number of frames you wish to capture is completed. */
44+
45+
public class AnimationToPNG : MonoBehaviour {
46+
47+
// Animation Name to be the prefix for the output filenames
48+
public string animationName = "";
49+
50+
// Default folder name where you want the animations to be output
51+
public string folder = "PNG_Animations";
52+
53+
// Framerate at which you want to play the animation
54+
public int frameRate = 25;
55+
56+
// How many frames you want to capture during the animation
57+
public int framesToCapture = 25;
58+
59+
// White Camera
60+
public Camera whiteCam;
61+
62+
// Black Camera
63+
public Camera blackCam;
64+
65+
// Pixels to World Unit size
66+
public float pixelsToWorldUnit = 74.48275862068966f;
67+
68+
// If you have Unity Pro you can use a RenderTexture which will render the full camera width, otherwise it will only render half
69+
public bool useRenderTexture = false;
70+
71+
private int videoframe = 0; // how many frames we've rendered
72+
73+
private float originaltimescaleTime; // track the original time scale so we can freeze the animation between frames
74+
75+
private string realFolder = ""; // real folder where the output files will be
76+
77+
private bool done = false; // is the capturing finished?
78+
79+
private bool readyToCapture = false; // Make sure all the camera setup is complete before capturing
80+
81+
private float cameraSize; // Size of the orthographic camera established from the current screen resolution and the pixels to world unit
82+
83+
private Texture2D texb; // black camera texture
84+
85+
private Texture2D texw; // white camera texture
86+
87+
private Texture2D outputtex; // final output texture
88+
89+
private RenderTexture blackCamRenderTexture; // black camera render texure
90+
91+
private RenderTexture whiteCamRenderTexture; // white camera render texure
92+
93+
// Get the texture from the screen, render all or only half of the camera
94+
public Texture2D GetTex2D( bool renderAll ) {
95+
// Create a texture the size of the screen, RGB24 format
96+
int width = Screen.width;
97+
int height = Screen.height;
98+
if (!renderAll){
99+
width = width / 2;
100+
}
101+
102+
Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
103+
// Read screen contents into the texture
104+
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
105+
tex.Apply();
106+
return tex;
107+
}
108+
109+
public void Start () {
110+
// Set the playback framerate!
111+
// (real time doesn't influence time anymore)
112+
Time.captureFramerate = frameRate;
113+
114+
// Create a folder that doesn't exist yet. Append number if necessary.
115+
realFolder = folder;
116+
int count = 1;
117+
while (System.IO.Directory.Exists(realFolder)) {
118+
realFolder = folder + count;
119+
count++;
120+
}
121+
// Create the folder
122+
System.IO.Directory.CreateDirectory(realFolder);
123+
124+
originaltimescaleTime = Time.timeScale;
125+
126+
// Force orthographic camera to render out sprites per pixel size designated by pixels to world unit
127+
cameraSize = Screen.width / ((( Screen.width / Screen.height ) * 2 ) * pixelsToWorldUnit );
128+
blackCam.orthographic = true;
129+
blackCam.orthographicSize = cameraSize;
130+
131+
whiteCam.orthographic = true;
132+
whiteCam.orthographicSize = cameraSize;
133+
134+
// If not using a Render Texture then set the cameras to split the screen to ensure we have an accurate image with alpha
135+
if (!useRenderTexture){
136+
// Change the camera rects to have split on screen to capture the animation properly
137+
blackCam.rect = new Rect(0.0f, 0.0f, 0.5f, 1.0f);
138+
139+
whiteCam.rect = new Rect(0.5f, 0.0f, 0.5f, 1.0f);
140+
}
141+
// Cameras are set ready to capture!
142+
readyToCapture = true;
143+
}
144+
145+
void Update() {
146+
// If the capturing is not done and the cameras are set, then Capture the animation
147+
if(!done && readyToCapture){
148+
StartCoroutine(Capture());
149+
}
150+
}
151+
152+
void LateUpdate(){
153+
// When we are all done capturing, clean up all the textures and RenderTextures from the scene
154+
if (done){
155+
156+
DestroyImmediate(texb);
157+
DestroyImmediate(texw);
158+
DestroyImmediate(outputtex);
159+
160+
if (useRenderTexture){
161+
//Clean Up
162+
whiteCam.targetTexture = null;
163+
RenderTexture.active = null;
164+
DestroyImmediate(whiteCamRenderTexture);
165+
166+
blackCam.targetTexture = null;
167+
RenderTexture.active = null;
168+
DestroyImmediate(blackCamRenderTexture);
169+
}
170+
}
171+
}
172+
173+
IEnumerator Capture () {
174+
if(videoframe < framesToCapture) {
175+
// name is "realFolder/animationName0000.png"
176+
// string name = realFolder + "/" + animationName + Time.frameCount.ToString("0000") + ".png";
177+
string name = String.Format("{0}/" + animationName + "{1:D04}.png", realFolder, Time.frameCount);
178+
179+
// Stop time
180+
Time.timeScale = 0;
181+
// Yield to next frame and then start the rendering
182+
yield return new WaitForEndOfFrame();
183+
184+
// If we are using a render texture to make the animation frames then set up the camera render textures
185+
if (useRenderTexture){
186+
//Initialize and render textures
187+
blackCamRenderTexture = new RenderTexture(Screen.width,Screen.height,24, RenderTextureFormat.ARGB32);
188+
whiteCamRenderTexture = new RenderTexture(Screen.width,Screen.height,24, RenderTextureFormat.ARGB32);
189+
190+
blackCam.targetTexture = blackCamRenderTexture;
191+
blackCam.Render();
192+
RenderTexture.active = blackCamRenderTexture;
193+
texb = GetTex2D(true);
194+
195+
//Now do it for Alpha Camera
196+
whiteCam.targetTexture = whiteCamRenderTexture;
197+
whiteCam.Render();
198+
RenderTexture.active = whiteCamRenderTexture;
199+
texw = GetTex2D(true);
200+
}
201+
// If not using render textures then simply get the images from both cameras
202+
else{
203+
// store 'black background' image
204+
texb = GetTex2D(true);
205+
206+
// store 'white background' image
207+
texw = GetTex2D(false);
208+
}
209+
210+
// If we have both textures then create final output texture
211+
if (texw && texb){
212+
213+
int width = Screen.width;
214+
int height = Screen.height;
215+
216+
// If we are not using a render texture then the width will only be half the screen
217+
if (!useRenderTexture){
218+
width = width / 2;
219+
}
220+
outputtex = new Texture2D(width, height, TextureFormat.ARGB32, false);
221+
222+
// Create Alpha from the difference between black and white camera renders
223+
for (int y = 0; y < outputtex.height; ++y) { // each row
224+
for (int x = 0; x < outputtex.width; ++x) { // each column
225+
float alpha;
226+
if (useRenderTexture){
227+
alpha = (float)(texw.GetPixel(x, y).r - texb.GetPixel(x, y).r);
228+
}
229+
else {
230+
alpha = (float)(texb.GetPixel(x + width, y).r - texb.GetPixel(x, y).r);
231+
}
232+
alpha = 1.0f - alpha;
233+
Color color;
234+
if(alpha == 0) {
235+
color = Color.clear;
236+
}
237+
else {
238+
color = texb.GetPixel(x, y) / alpha;
239+
}
240+
color.a = alpha;
241+
outputtex.SetPixel(x, y, color);
242+
}
243+
}
244+
245+
// Encode the resulting output texture to a byte array then write to the file
246+
byte[] pngShot = outputtex.EncodeToPNG();
247+
File.WriteAllBytes(name, pngShot);
248+
249+
// Reset the time scale, then move on to the next frame.
250+
Time.timeScale = originaltimescaleTime;
251+
videoframe++;
252+
}
253+
254+
// Debug.Log("Frame " + name + " " + videoframe);
255+
}
256+
else {
257+
Debug.Log("Complete! "+videoframe+" videoframes rendered (0 indexed)");
258+
done = true;
259+
}
260+
}
261+
}
262+

0 commit comments

Comments
 (0)