MegaGrab.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. // All code copyright Chris West 2011.
  2. // If you make any improvements please send them back to me at chris@west-racing.com so I can update the package.
  3. #if !UNITY_FLASH && !UNITY_METRO && !UNITY_WP8
  4. using UnityEngine;
  5. using System.IO;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. public enum IMGFormat
  10. {
  11. Tga,
  12. Jpg,
  13. }
  14. //[ExecuteInEditMode]
  15. public class MegaGrab : MonoBehaviour
  16. {
  17. public Camera SrcCamera = null; // camera to use for screenshot
  18. public KeyCode GrabKey = KeyCode.S; // Key to grab the screenshot
  19. public int ResUpscale = 1; // How much to increase the screen shot res by
  20. public float Blur = 1.0f; // Pixel oversampling
  21. public int AASamples = 8; // Anti aliasing samples
  22. public AnisotropicFiltering FilterMode = AnisotropicFiltering.ForceEnable; // Filter mode
  23. public bool UseJitter = false; // use random jitter for AA sampling
  24. public string SaveName = "Grab"; // Base name for grabs
  25. public string Format = "dddd MMM dd yyyy HH_mm_ss"; // format string for date time info
  26. public string Enviro = ""; // Enviro variable to use ie USERPROFILE
  27. public string Path = "";
  28. public bool UseDOF = false; // Use Dof grab
  29. public float focalDistance = 8.0f; // DOF focal distance
  30. public int totalSegments = 8; // How many DOF samples
  31. public float sampleRadius = 1.0f; // Amount of DOF effect
  32. //public float sampleBias = 1.0f;
  33. public bool CalcFromSize = false; // Let grab calc res from dpi and Width(in inches)
  34. public int Dpi = 300; // Number of Dots per inch required
  35. public float Width = 24.0f; // Final physical size of grab using Dpi
  36. public int NumberOfGrabs = 0; // Read only of how many grabs will happen
  37. public float EstimatedTime = 0.0f; // Guide to show how long a grab will take in Seconds
  38. public int GrabWidthWillBe = 0; // Width of final image
  39. public int GrabHeightWillBe = 0; // Height of final IMage
  40. public bool UseCoroutine = false; // Use coroutine method, needed for later versions of unity
  41. float mleft;
  42. float mright;
  43. float mtop;
  44. float mbottom;
  45. int sampcount;
  46. Vector2[] poisson;
  47. Texture2D grabtex;
  48. Color[,] accbuf;
  49. Color[,] blendbuf;
  50. byte[] output1;
  51. Color[] outputjpg;
  52. AnisotropicFiltering filtering;
  53. MGBlendTable blendtable;
  54. int DOFSamples;
  55. //float DOFJitter = 5.0f;
  56. Vector3 camfor;
  57. Vector3 campos;
  58. Matrix4x4 camtm;
  59. // Calc the camera offsets and rots in init
  60. void CalcDOFInfo(Camera camera)
  61. {
  62. camtm = camera.transform.localToWorldMatrix;
  63. campos = camera.transform.position;
  64. camfor = camera.transform.forward;
  65. }
  66. void ChangeDOFPos(int segment)
  67. {
  68. float theta = (float)segment / (float)(totalSegments) * Mathf.PI * 2.0f;
  69. float radius = sampleRadius;
  70. float uOffset = radius * Mathf.Cos(theta);
  71. float vOffset = radius * Mathf.Sin(theta);
  72. Vector3 newCameraLocation = new Vector3(uOffset, vOffset, 0.0f);
  73. Vector3 initialTargetLocation = camfor * focalDistance; //new Vector3(0.0f, 0.0f, -focalDistance);
  74. Vector3 tpos = initialTargetLocation + campos; //srcCamera.transform.TransformPoint(initialTargetLocation);
  75. SrcCamera.transform.position = camtm.MultiplyPoint3x4(newCameraLocation);
  76. SrcCamera.transform.LookAt(tpos);
  77. }
  78. static Matrix4x4 PerspectiveOffCenter(float left, float right, float bottom, float top, float near, float far)
  79. {
  80. Matrix4x4 m = Matrix4x4.identity;
  81. m[0, 0] = (2.0f * near) / (right - left);
  82. m[1, 1] = (2.0f * near) / (top - bottom);
  83. m[0, 2] = (right + left) / (right - left);
  84. m[1, 2] = (top + bottom) / (top - bottom);
  85. m[2, 2] = -(far + near) / (far - near);
  86. m[2, 3] = -(2.0f * far * near) / (far - near);
  87. m[3, 2] = -1.0f;
  88. m[3, 3] = 0.0f;
  89. return m;
  90. }
  91. Matrix4x4 CalcProjectionMatrix(float left, float right, float bottom, float top, float near, float far, float xoff, float yoff)
  92. {
  93. float scalex = (right - left) / (float)Screen.width;
  94. float scaley = (top - bottom) / (float)Screen.height;
  95. return PerspectiveOffCenter(left - xoff * scalex, right - xoff * scalex, bottom - yoff * scaley, top - yoff * scaley, near, far);
  96. }
  97. void Cleanup()
  98. {
  99. QualitySettings.anisotropicFiltering = filtering;
  100. }
  101. bool InitGrab(int width, int height, int aasamples)
  102. {
  103. blendtable = new MGBlendTable(32, 32, totalSegments, 0.4f, true);
  104. if ( ResUpscale < 1 )
  105. ResUpscale = 1;
  106. if ( AASamples < 1 )
  107. AASamples = 1;
  108. if ( SrcCamera == null )
  109. SrcCamera = Camera.main;
  110. if ( SrcCamera == null )
  111. {
  112. Debug.Log("No Camera set as source and no main camera found in the scene");
  113. return false;
  114. }
  115. CalcDOFInfo(SrcCamera);
  116. if ( OutputFormat == IMGFormat.Tga )
  117. output1 = new byte[(width * ResUpscale) * (height * ResUpscale) * 3];
  118. else
  119. outputjpg = new Color[(width * ResUpscale) * (height * ResUpscale)];
  120. if ( output1 != null || outputjpg != null )
  121. {
  122. filtering = QualitySettings.anisotropicFiltering;
  123. QualitySettings.anisotropicFiltering = FilterMode;
  124. grabtex = new Texture2D(width, height, TextureFormat.RGB24, false);
  125. if ( grabtex != null )
  126. {
  127. accbuf = new Color[width, height];
  128. blendbuf = new Color[width, height];
  129. if ( accbuf != null )
  130. {
  131. float l = (1.0f - Blur) * 0.5f;
  132. float h = 1.0f + ((Blur - 1.0f) * 0.5f);
  133. if ( UseJitter)
  134. {
  135. poisson = new Vector2[aasamples];
  136. sampcount = aasamples;
  137. for ( int i = 0; i < aasamples; i++ )
  138. {
  139. Vector2 pos = new Vector2();
  140. pos.x = Mathf.Lerp(l, h, UnityEngine.Random.value);
  141. pos.y = Mathf.Lerp(l, h, UnityEngine.Random.value);
  142. poisson[i] = pos;
  143. }
  144. }
  145. else
  146. {
  147. int samples = (int)Mathf.Sqrt((float)aasamples);
  148. if ( samples < 1 )
  149. samples = 1;
  150. sampcount = samples * samples;
  151. poisson = new Vector2[samples * samples];
  152. int i = 0;
  153. for ( int ya = 0; ya < samples; ya++ )
  154. {
  155. for ( int xa = 0; xa < samples; xa++ )
  156. {
  157. float xa1 = ((float)xa / (float)samples);
  158. float ya1 = ((float)ya / (float)samples);
  159. Vector2 pos = new Vector2();
  160. pos.x = Mathf.Lerp(l, h, xa1);
  161. pos.y = Mathf.Lerp(l, h, ya1);
  162. poisson[i++] = pos;
  163. }
  164. }
  165. }
  166. return true;
  167. }
  168. }
  169. }
  170. Debug.Log("Cant create a large enough texture, Try lower ResUpscale value");
  171. return false;
  172. }
  173. Texture2D GrabImage(int samples, float x, float y)
  174. {
  175. float ps = 1.0f / (float)ResUpscale;
  176. for ( int i = 0; i < sampcount; i++ )
  177. {
  178. float xa = poisson[i].x * ps;
  179. float ya = poisson[i].y * ps;
  180. // Move view and grab
  181. float xo = x + xa;
  182. float yo = y + ya;
  183. SrcCamera.projectionMatrix = CalcProjectionMatrix(mleft, mright, mbottom, mtop, SrcCamera.nearClipPlane, SrcCamera.farClipPlane, xo, yo);
  184. SrcCamera.Render();
  185. // Read screen contents into the texture
  186. grabtex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
  187. grabtex.Apply();
  188. if ( i == 0 )
  189. {
  190. for ( int ty = 0; ty < Screen.height; ty++ )
  191. {
  192. for ( int tx = 0; tx < Screen.width; tx++ )
  193. accbuf[tx, ty] = grabtex.GetPixel(tx, ty);
  194. }
  195. }
  196. else
  197. {
  198. for ( int ty = 0; ty < Screen.height; ty++ )
  199. {
  200. for ( int tx = 0; tx < Screen.width; tx++ )
  201. accbuf[tx, ty] += grabtex.GetPixel(tx, ty);
  202. }
  203. }
  204. }
  205. for ( int ty = 0; ty < Screen.height; ty++ )
  206. {
  207. for ( int tx = 0; tx < Screen.width; tx++ )
  208. grabtex.SetPixel(tx, ty, accbuf[tx, ty] / sampcount);
  209. }
  210. grabtex.Apply();
  211. return grabtex;
  212. }
  213. void GrabAA(float x, float y)
  214. {
  215. float ps = 1.0f / (float)ResUpscale;
  216. for ( int ty = 0; ty < Screen.height; ty++ )
  217. {
  218. for ( int tx = 0; tx < Screen.width; tx++ )
  219. accbuf[tx, ty] = Color.black;
  220. }
  221. for ( int i = 0; i < sampcount; i++ )
  222. {
  223. float xa = poisson[i].x * ps;
  224. float ya = poisson[i].y * ps;
  225. // Move view and grab
  226. float xo = x + xa;
  227. float yo = y + ya;
  228. SrcCamera.projectionMatrix = CalcProjectionMatrix(mleft, mright, mbottom, mtop, SrcCamera.nearClipPlane, SrcCamera.farClipPlane, xo, yo);
  229. SrcCamera.Render();
  230. // Read screen contents into the texture
  231. grabtex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
  232. grabtex.Apply();
  233. for ( int ty = 0; ty < Screen.height; ty++ )
  234. {
  235. for ( int tx = 0; tx < Screen.width; tx++ )
  236. accbuf[tx, ty] += grabtex.GetPixel(tx, ty);
  237. }
  238. }
  239. for ( int ty = 0; ty < Screen.height; ty++ )
  240. {
  241. for ( int tx = 0; tx < Screen.width; tx++ )
  242. accbuf[tx, ty] = accbuf[tx, ty] / sampcount;
  243. }
  244. }
  245. // return accbuf here
  246. Texture2D GrabImageDOF(int samples, float x, float y)
  247. {
  248. //float ps = 1.0f / (float)ResUpscale;
  249. for ( int ty = 0; ty < Screen.height; ty++ )
  250. {
  251. for ( int tx = 0; tx < Screen.width; tx++ )
  252. blendbuf[tx, ty] = Color.black;
  253. }
  254. for ( int d = 0; d < totalSegments; d++ )
  255. {
  256. ChangeDOFPos(d);
  257. //SrcCamera.transform.localToWorldMatrix = ChangeCameraDOF(d);
  258. GrabAA(x, y);
  259. // Blend image
  260. blendtable.BlendImages(blendbuf, accbuf, Screen.width, Screen.height, d);
  261. }
  262. return grabtex;
  263. }
  264. void DoGrabTGA()
  265. {
  266. if ( InitGrab(Screen.width, Screen.height, AASamples) )
  267. {
  268. mtop = SrcCamera.nearClipPlane * Mathf.Tan(SrcCamera.fieldOfView * 0.5f * Mathf.Deg2Rad);
  269. mbottom = -mtop;
  270. mleft = mbottom * SrcCamera.aspect;
  271. mright = mtop * SrcCamera.aspect;
  272. //float mWidth = SrcCamera.nearClipPlane / SrcCamera.projectionMatrix.m00;
  273. //float mHeight = SrcCamera.nearClipPlane / SrcCamera.projectionMatrix.m11;
  274. //float spox = mWidth / (Screen.width * ResUpscale * 1.0f);
  275. //float spoy = mHeight / (Screen.height * ResUpscale * 1.0f);
  276. int width = Screen.width;
  277. int height = Screen.height;
  278. if ( AASamples < 1 )
  279. AASamples = 1;
  280. //int total = ResUpscale * ResUpscale;
  281. int count = 0;
  282. for ( int y = 0; y < ResUpscale; y++ )
  283. {
  284. float yo = (float)y / (float)ResUpscale;
  285. for ( int x = 0; x < ResUpscale; x++ )
  286. {
  287. //MyDebug.Log("Doing Grab " + count + " of " + total);
  288. count++;
  289. float xo = (float)x / (float)ResUpscale;
  290. Texture2D tex;
  291. if ( UseDOF )
  292. {
  293. tex = GrabImageDOF(AASamples, xo, yo);
  294. for ( int h = 0; h < Screen.height; h++ )
  295. {
  296. int index = ((ResUpscale - y) + (h * ResUpscale) - 1) * (width * ResUpscale);
  297. for ( int w = 0; w < Screen.width; w++ )
  298. {
  299. Color col = blendbuf[w, h]; //tex.GetPixel(w, h);
  300. int ix = (index + ((ResUpscale - x) + (w * ResUpscale) - 1)) * 3;
  301. output1[ix + 0] = (byte)(col.b * 255.0f);
  302. output1[ix + 1] = (byte)(col.g * 255.0f);
  303. output1[ix + 2] = (byte)(col.r * 255.0f);
  304. }
  305. }
  306. }
  307. else
  308. {
  309. tex = GrabImage(AASamples, xo, yo);
  310. for ( int h = 0; h < Screen.height; h++ )
  311. {
  312. int index = ((ResUpscale - y) + (h * ResUpscale) - 1) * (width * ResUpscale);
  313. for ( int w = 0; w < Screen.width; w++ )
  314. {
  315. Color col = tex.GetPixel(w, h);
  316. int ix = (index + ((ResUpscale - x) + (w * ResUpscale) - 1)) * 3;
  317. output1[ix + 0] = (byte)(col.b * 255.0f);
  318. output1[ix + 1] = (byte)(col.g * 255.0f);
  319. output1[ix + 2] = (byte)(col.r * 255.0f);
  320. }
  321. }
  322. }
  323. }
  324. }
  325. string epath = "";
  326. if ( Enviro != null && Enviro.Length > 0 )
  327. epath = System.Environment.GetEnvironmentVariable(Enviro);
  328. //string fname = epath + Path + SaveName + " " + (width * ResUpscale) + "x" + (height * ResUpscale) + " " + System.DateTime.Now.ToString(Format);
  329. // Save big version
  330. //if ( uploadGrabs )
  331. //{
  332. // string fname = SaveName + " " + (width * ResUpscale) + "x" + (height * ResUpscale) + " " + System.DateTime.Now.ToString(Format);
  333. // UploadTGA(fname + ".tga", (width * ResUpscale), (height * ResUpscale), output1);
  334. //}
  335. //else
  336. //{
  337. string fname = epath + Path + SaveName + " " + (width * ResUpscale) + "x" + (height * ResUpscale) + " " + System.DateTime.Now.ToString(Format);
  338. SaveTGA(fname + ".tga", (width * ResUpscale), (height * ResUpscale), output1);
  339. //}
  340. //SaveTGA(fname + ".tga", (width * ResUpscale), (height * ResUpscale), output1);
  341. //SrcCamera.camera.worldToCameraMatrix = cameraMat;
  342. //SrcCamera.ResetWorldToCameraMatrix();
  343. SrcCamera.ResetProjectionMatrix();
  344. Cleanup();
  345. }
  346. }
  347. public IMGFormat OutputFormat = IMGFormat.Jpg;
  348. public float Quality = 75.0f;
  349. void DoGrabJPG()
  350. {
  351. if ( InitGrab(Screen.width, Screen.height, AASamples) )
  352. {
  353. mtop = SrcCamera.nearClipPlane * Mathf.Tan(SrcCamera.fieldOfView * 0.5f * Mathf.Deg2Rad);
  354. mbottom = -mtop;
  355. mleft = mbottom * SrcCamera.aspect;
  356. mright = mtop * SrcCamera.aspect;
  357. //float mWidth = SrcCamera.nearClipPlane / SrcCamera.projectionMatrix.m00;
  358. //float mHeight = SrcCamera.nearClipPlane / SrcCamera.projectionMatrix.m11;
  359. //float spox = mWidth / (Screen.width * ResUpscale * 1.0f);
  360. //float spoy = mHeight / (Screen.height * ResUpscale * 1.0f);
  361. int width = Screen.width;
  362. int height = Screen.height;
  363. if ( AASamples < 1 )
  364. AASamples = 1;
  365. //int total = ResUpscale * ResUpscale;
  366. int count = 0;
  367. for ( int y = 0; y < ResUpscale; y++ )
  368. {
  369. float yo = (float)y / (float)ResUpscale;
  370. for ( int x = 0; x < ResUpscale; x++ )
  371. {
  372. //MyDebug.Log("Doing Grab " + count + " of " + total);
  373. count++;
  374. float xo = (float)x / (float)ResUpscale;
  375. Texture2D tex;
  376. if ( UseDOF )
  377. {
  378. tex = GrabImageDOF(AASamples, xo, yo);
  379. for ( int h = 0; h < Screen.height; h++ )
  380. {
  381. int index = ((ResUpscale - y) + (h * ResUpscale) - 1) * (width * ResUpscale);
  382. for ( int w = 0; w < Screen.width; w++ )
  383. {
  384. Color col = blendbuf[w, h]; //tex.GetPixel(w, h);
  385. int ix = (index + ((ResUpscale - x) + (w * ResUpscale) - 1));
  386. outputjpg[ix] = col;
  387. }
  388. }
  389. }
  390. else
  391. {
  392. tex = GrabImage(AASamples, xo, yo);
  393. for ( int h = 0; h < Screen.height; h++ )
  394. {
  395. int index = ((ResUpscale - y) + (h * ResUpscale) - 1) * (width * ResUpscale);
  396. for ( int w = 0; w < Screen.width; w++ )
  397. {
  398. Color col = tex.GetPixel(w, h);
  399. int ix = (index + ((ResUpscale - x) + (w * ResUpscale) - 1));
  400. outputjpg[ix] = col;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. string epath = "";
  407. if ( Enviro != null && Enviro.Length > 0 )
  408. epath = System.Environment.GetEnvironmentVariable(Enviro);
  409. //string fname = epath + Path + SaveName + " " + (width * ResUpscale) + "x" + (height * ResUpscale) + " " + System.DateTime.Now.ToString(Format);
  410. // Save big version
  411. if ( uploadGrabs )
  412. {
  413. string fname = SaveName + " " + (width * ResUpscale) + "x" + (height * ResUpscale) + " " + System.DateTime.Now.ToString(Format);
  414. UploadJPG(fname + ".jpg", (width * ResUpscale), (height * ResUpscale), outputjpg);
  415. }
  416. else
  417. {
  418. string fname = epath + Path + SaveName + " " + (width * ResUpscale) + "x" + (height * ResUpscale) + " " + System.DateTime.Now.ToString(Format);
  419. SaveJPG(fname + ".jpg", (width * ResUpscale), (height * ResUpscale), outputjpg);
  420. }
  421. //SrcCamera.camera.worldToCameraMatrix = cameraMat;
  422. //SrcCamera.ResetWorldToCameraMatrix();
  423. SrcCamera.ResetProjectionMatrix();
  424. Cleanup();
  425. }
  426. }
  427. public bool uploadGrabs = false;
  428. void SaveJPG(string filename, int width, int height, Color[] pixels)
  429. {
  430. FileStream fs = new FileStream(filename, FileMode.Create);
  431. if ( fs != null )
  432. {
  433. BinaryWriter bw = new BinaryWriter(fs);
  434. if ( bw != null )
  435. {
  436. Quality = Mathf.Clamp(Quality, 0.0f, 100.0f);
  437. JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality);
  438. NewEncoder.doEncoding();
  439. byte[] TexData = NewEncoder.GetBytes();
  440. bw.Write(TexData);
  441. bw.Close();
  442. }
  443. fs.Close();
  444. }
  445. }
  446. void UploadJPG(string filename, int width, int height, Color[] pixels)
  447. {
  448. Quality = Mathf.Clamp(Quality, 0.0f, 100.0f);
  449. JPGEncoder NewEncoder = new JPGEncoder(pixels, width, height, Quality);
  450. NewEncoder.doEncoding();
  451. byte[] TexData = NewEncoder.GetBytes();
  452. UploadFile(TexData, m_URL, filename);
  453. }
  454. #if false
  455. void UploadTGA(string filename, int width, int height, byte[] pixels)
  456. {
  457. //byte[] data = new byte[output1.Length * 4];
  458. //Buffer.BlockCopy(output1, 0, data, 0, data.Length);
  459. UploadFile(pixels, m_URL, filename);
  460. }
  461. #endif
  462. void SaveTGA(string filename, int width, int height, byte[] pixels)
  463. {
  464. FileStream fs = new FileStream(filename, FileMode.Create);
  465. if ( fs != null )
  466. {
  467. BinaryWriter bw = new BinaryWriter(fs);
  468. if ( bw != null )
  469. {
  470. bw.Write((short)0);
  471. bw.Write((byte)2);
  472. bw.Write((int)0);
  473. bw.Write((int)0);
  474. bw.Write((byte)0);
  475. bw.Write((short)width);
  476. bw.Write((short)height);
  477. bw.Write((byte)24);
  478. bw.Write((byte)0);
  479. for ( int h = 0; h < pixels.Length; h++ )
  480. bw.Write(pixels[h]);
  481. bw.Close();
  482. }
  483. fs.Close();
  484. }
  485. }
  486. void CalcUpscale()
  487. {
  488. float w = Width / ((float)Screen.width / (float)Dpi); // * Width;
  489. ResUpscale = (int)(w);
  490. GrabWidthWillBe = Screen.width * ResUpscale;
  491. GrabHeightWillBe = Screen.height * ResUpscale;
  492. }
  493. void CalcEstimate()
  494. {
  495. NumberOfGrabs = ResUpscale * ResUpscale * AASamples;
  496. if ( UseDOF )
  497. {
  498. NumberOfGrabs *= totalSegments;
  499. }
  500. EstimatedTime = NumberOfGrabs * 0.41f;
  501. }
  502. IEnumerator GrabCoroutine()
  503. {
  504. yield return new WaitForEndOfFrame();
  505. if ( OutputFormat == IMGFormat.Tga )
  506. DoGrabTGA();
  507. else
  508. DoGrabJPG();
  509. yield return null;
  510. }
  511. void LateUpdate()
  512. {
  513. if ( Input.GetKeyDown(GrabKey) )
  514. {
  515. #if UNITY_IPHONE
  516. Path = Application.persistentDataPath + "/";
  517. #endif
  518. //StartCoroutine(GrabCoroutine());
  519. if ( CalcFromSize )
  520. CalcUpscale();
  521. CalcEstimate();
  522. if ( UseCoroutine )
  523. {
  524. StartCoroutine(GrabCoroutine());
  525. }
  526. else
  527. {
  528. float t = Time.realtimeSinceStartup;
  529. //DoGrabTGA();
  530. if ( OutputFormat == IMGFormat.Tga )
  531. DoGrabTGA();
  532. else
  533. DoGrabJPG();
  534. float time = Time.realtimeSinceStartup - t;
  535. Debug.Log("Took " + time.ToString("0.00000000") + "s");
  536. }
  537. }
  538. }
  539. void OnDrawGizmos()
  540. {
  541. if ( CalcFromSize )
  542. CalcUpscale();
  543. CalcEstimate();
  544. }
  545. //using UnityEngine;
  546. //using System.Collections;
  547. //public class FileUpload : MonoBehaviour
  548. //{
  549. public string m_URL = "http://www.west-racing.com/uploadtest1.php";
  550. IEnumerator UploadFileCo(byte[] data, string uploadURL, string filename)
  551. {
  552. #if !UNITY_2019 && !UNITY_2018_3 && !UNITY_2020
  553. WWWForm postForm = new WWWForm();
  554. // version 1
  555. //postForm.AddBinaryData("theFile",localFile.bytes);
  556. Debug.Log("uploading " + filename);
  557. // version 2
  558. postForm.AddField("action", "Upload Image");
  559. postForm.AddBinaryData("theFile", data, filename, "images/jpg"); //text/plain");
  560. Debug.Log("url " + uploadURL);
  561. WWW upload = new WWW(uploadURL, postForm);
  562. yield return upload;
  563. //if ( upload.error == null )
  564. //Debug.Log("upload done :" + upload.text);
  565. //else
  566. //Debug.Log("Error during upload: " + upload.error);
  567. #else
  568. yield return null;
  569. #endif
  570. }
  571. void UploadFile(byte[] data, string uploadURL, string filename)
  572. {
  573. Debug.Log("Start upload");
  574. //StartCoroutine(UploadFileCo(data, uploadURL, filename));
  575. StartCoroutine(UploadLevel(data, uploadURL, filename));
  576. Debug.Log("len " + data.Length);
  577. }
  578. IEnumerator UploadLevel(byte[] data, string uploadURL, string filename)
  579. {
  580. #if !UNITY_2019 && !UNITY_2018_3 && !UNITY_2020
  581. WWWForm form = new WWWForm();
  582. form.AddField("action", "level upload");
  583. form.AddField("file", "file");
  584. form.AddBinaryData("file", data, filename, "images/jpg");
  585. Debug.Log("url " + uploadURL);
  586. WWW w = new WWW(uploadURL, form);
  587. yield return w;
  588. if ( w.error != null )
  589. {
  590. print("error");
  591. print(w.error);
  592. }
  593. else
  594. {
  595. if ( w.uploadProgress == 1 && w.isDone )
  596. {
  597. yield return new WaitForSeconds(5);
  598. }
  599. }
  600. #else
  601. yield return null;
  602. #endif
  603. }
  604. #if false
  605. <?
  606. if ( isset ($_POST['action']) ) {
  607. if($_POST['action'] == "Upload Image") {
  608. unset($imagename);
  609. if(!isset($_FILES) && isset($HTTP_POST_FILES)) $_FILES = $HTTP_POST_FILES;
  610. if(!isset($_FILES['fileUpload'])) $error["image_file"] = "An image was not found.";
  611. $imagename = basename($_FILES['fileUpload']['name']);
  612. if(empty($imagename)) $error["imagename"] = "The name of the image was not found.";
  613. if(empty($error)) {
  614. $newimage = "images/" . $imagename;
  615. //echo $newimage;
  616. $result = @move_uploaded_file($_FILES['fileUpload']['tmp_name'], $newimage);
  617. if ( empty($result) ) $error["result"] = "There was an error moving the uploaded file.";
  618. }
  619. }
  620. } else {
  621. echo "no form data found";
  622. }
  623. ?>
  624. #endif
  625. }
  626. // Example php code to take uploaded jog images
  627. #if false
  628. // Php Code, upload this to your server
  629. <?php
  630. //check if something its being sent to this script
  631. if ($_POST)
  632. {
  633. //check if theres a field called action in the sent data
  634. if ( isset ($_POST['action']) )
  635. {
  636. //if it indeed theres an field called action. check if its value its level upload
  637. if($_POST['action'] === 'level upload')
  638. {
  639. if(!isset($_FILES) && isset($HTTP_POST_FILES))
  640. {
  641. $_FILES = $HTTP_POST_FILES;
  642. }
  643. if ($_FILES['file']['error'] === UPLOAD_ERR_OK)
  644. {
  645. //check if the file has a name, in this script it has to have a name to be stored, the file name is sent by unity
  646. if ($_FILES['file']['name'] !== "")
  647. {
  648. //this checks the file mime type, to filter the kind of files you want to accept, this script is configured to accept only jpg files
  649. if ($_FILES['file']['type'] === 'images/jpg')
  650. {
  651. $uploadfile = $_FILES['file']['name'];
  652. $newimage = "images/" . $uploadfile;
  653. move_uploaded_file($_FILES['file']['tmp_name'], $newimage);
  654. }
  655. }
  656. }
  657. }
  658. }
  659. }
  660. ?>
  661. #endif
  662. #endif