Advertisement
Guest User

qwerty

a guest
Nov 13th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.91 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using System.IO;
  6. using BayatGames.SaveGamePro;
  7. using UnityEngine.UI;
  8. using System.Drawing;
  9.  
  10. public class ScreenShootBase : MonoBehaviour
  11. {
  12. // [SerializeField]
  13. // private UIColorManager uiColorManager;
  14.  
  15. [SerializeField]
  16. private GeometryBuilder geometryBuilder;
  17.  
  18. [SerializeField]
  19. private float previewScale;
  20.  
  21. [SerializeField]
  22. private float mainScale;
  23.  
  24. [Range(0, 100)]
  25. [SerializeField]
  26. private int offsetX = 5;
  27.  
  28. [Range(-100,0)]
  29. [SerializeField]
  30. private float offsetY = -50;
  31.  
  32. [SerializeField]
  33. ImageFormat format = ImageFormat.PPM;
  34.  
  35. private bool captureScreenShoot = false;
  36.  
  37. private Rect previewRect, mainRect;
  38. private RenderTexture previewRenderTexture, mainRenderTexture;
  39. private Texture2D previewScreenShot, mainScreenShoot;
  40. [SerializeField]
  41. private Camera camera;
  42.  
  43. private float minX, maxX, minY, maxY;
  44. private float width, height;
  45.  
  46. public bool CaptureScreenShoot => captureScreenShoot;
  47. public float Scale { get => previewScale; set => previewScale = value; }
  48.  
  49. public static Action<bool> TakeScreenshot;
  50.  
  51. private void OnDestroy()
  52. {
  53. VectorElementController.FilledShape -= OnFilledShape;
  54. GeometryBuilder.LoadImageDone -= OnLoadImageDone;
  55. }
  56.  
  57. private void Start()
  58. {
  59. GeometryBuilder.LoadImageDone += OnLoadImageDone;
  60. }
  61.  
  62. /// <summary>
  63. /// <para> bounds = { minX, maxX, minY, maxY } </para>
  64. /// </summary>
  65. ///
  66. /// <param name="cameraPosition"></param>
  67. /// <param name="cameraOrthographicSize"></param>
  68. /// <param name="bounds"></param>
  69. public void Initialize(Vector3 cameraPosition, float[] bounds)
  70. {
  71. SetBounds(bounds);
  72. CalculateImageDimension();
  73. SetCamera(cameraPosition);
  74.  
  75.  
  76. DetermineRenderTexturePreviewImage(previewScale, ref previewRect,ref previewRenderTexture,ref previewScreenShot);
  77. DetermineRenderTextureNormalImage(mainScale,ref mainRect,ref mainRenderTexture, ref mainScreenShoot);
  78.  
  79. }
  80.  
  81.  
  82. private void OnLoadImageDone()
  83. {
  84. CreateScreenShoot();
  85. geometryBuilder.ColorImage();
  86. CreateBMP();
  87. }
  88.  
  89. private void OnFilledShape(int colorID)
  90. {
  91. // captureScreenShoot is needed because when color changed then invokes event we actually don't need.
  92. // Change color here is necessary to unselect elements which we haven't colored yet. We don't want to take a screenshoot with uncolored elements
  93. captureScreenShoot = true;
  94. // int currentColor = UIColorManager.CurrentColor;
  95. // uiColorManager.UnSelectColor();
  96.  
  97. CreateScreenShoot();
  98.  
  99. // uiColorManager.SelectColor(currentColor);
  100. captureScreenShoot = false;
  101. }
  102.  
  103. //private void DetermineRenderTexture()
  104. //{
  105. // float offsetY = -50;
  106. // float topImageWorldPointY = camera.WorldToScreenPoint(new Vector3(0,maxY,0)).y;
  107.  
  108. // int cameraRectWidth = (int)camera.pixelRect.width;
  109. // int cameraRectHeight = (int) camera.pixelRect.height;
  110.  
  111. // float imageAndRectDiff = cameraRectHeight - topImageWorldPointY + offsetY;
  112.  
  113. // rect = new Rect(0, imageAndRectDiff, cameraRectWidth, cameraRectHeight - Mathf.Abs(imageAndRectDiff));
  114. // renderTexture = new RenderTexture(cameraRectWidth, cameraRectHeight, 32);
  115. // screenShot = new Texture2D(cameraRectWidth , ((int)rect.height), TextureFormat.RGBA32, false);
  116. //}
  117.  
  118. private void DetermineRenderTexturePreviewImage(float scaleFactor, ref Rect rect, ref RenderTexture renderTexture, ref Texture2D screenshoTexture2D)
  119. {
  120. int cameraRectWidth = (int)(scaleFactor * camera.pixelRect.width);
  121. int cameraRectHeight = (int)(scaleFactor * camera.pixelRect.height);
  122. // #if UNITY_EDITOR// - tak powinno byc kiedy tworzymy builda na windowsa
  123. #if !UNITY_EDITOR || !UNITY_STANDALONE// ! - test w edytorze
  124.  
  125. float bottomImageWordPointY = scaleFactor * camera.WorldToScreenPoint(new Vector3(0, minY, 0)).y;
  126. float imageAndRectDiff = cameraRectHeight - Mathf.Abs(cameraRectHeight - bottomImageWordPointY ) + offsetY;
  127.  
  128. rect = new Rect(0, imageAndRectDiff, cameraRectWidth, cameraRectHeight - imageAndRectDiff);
  129. renderTexture = new RenderTexture(cameraRectWidth, cameraRectHeight, 32);
  130. screenshoTexture2D = new Texture2D(cameraRectWidth, ((int)previewRect.height), TextureFormat.RGBA32, false);
  131.  
  132. #else
  133. float topImageWorldPointY = scaleFactor * camera.WorldToScreenPoint(new Vector3(0, maxY, 0)).y;
  134. float imageAndRectDiff = cameraRectHeight - topImageWorldPointY + offsetY;
  135.  
  136. rect = new Rect(0, imageAndRectDiff, cameraRectWidth, cameraRectHeight - Mathf.Abs(imageAndRectDiff));
  137. renderTexture = new RenderTexture(cameraRectWidth, cameraRectHeight, 32);
  138. screenshoTexture2D = new Texture2D(cameraRectWidth, ( (int) previewRect.height ), TextureFormat.RGBA32, false);
  139. #endif
  140.  
  141. }
  142.  
  143.  
  144.  
  145. private void DetermineRenderTextureNormalImage(float scaleFactor, ref Rect rect, ref RenderTexture renderTexture, ref Texture2D screenshoTexture2D)
  146. {
  147. int cameraRectWidth = (int)(scaleFactor * camera.pixelRect.width);
  148. int cameraRectHeight = (int)(scaleFactor * camera.pixelRect.height);
  149.  
  150. // #if UNITY_EDITOR// - tak powinno byc kiedy tworzymy builda na windowsa
  151. #if !UNITY_EDITOR || !UNITY_STANDALONE// ! - test w edytorze
  152.  
  153. float bottomImageWordPointY = scaleFactor * camera.WorldToScreenPoint(new Vector3(0, minY, 0)).y;
  154. float imageAndRectDiff = cameraRectHeight - Mathf.Abs(cameraRectHeight - bottomImageWordPointY ) + offsetY;
  155.  
  156. rect = new Rect(0, imageAndRectDiff, cameraRectWidth, cameraRectHeight - imageAndRectDiff);
  157. renderTexture = new RenderTexture(cameraRectWidth, cameraRectHeight, 32);
  158. screenshoTexture2D = new Texture2D(cameraRectWidth, ((int)rect.height), TextureFormat.RGBA32, false);
  159.  
  160. #else
  161. // float topImageWorldPointY = scaleFactor * camera.WorldToScreenPoint(new Vector3(0, maxY, 0)).y;
  162. float topImageWorldPointY = camera.WorldToScreenPoint(new Vector3(0, maxY, 0)).y * scaleFactor;
  163. // float topImageWorldPointY = camera.WorldToScreenPoint(new Vector3(0, maxY, 0)).y;
  164. float imageAndRectDiff = cameraRectHeight - topImageWorldPointY;// + offsetY;
  165.  
  166. rect = new Rect(0, imageAndRectDiff, cameraRectWidth, cameraRectHeight - Mathf.Abs(imageAndRectDiff));
  167. renderTexture = new RenderTexture(cameraRectWidth, cameraRectHeight, 32);
  168. screenshoTexture2D = new Texture2D(cameraRectWidth, ((int)rect.height), TextureFormat.RGBA32, false);
  169. #endif
  170.  
  171. }
  172.  
  173.  
  174.  
  175. private void SetCamera(Vector3 cameraPosition)
  176. {
  177. AdjustCameraOrthographicSize();
  178.  
  179. // On Android devices cutting a screenshot is starting from the bottom but in pc from the top
  180. #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE // to jest ok, nie ruszac
  181. // transform.position = new Vector3(cameraPosition.x, -(Mathf.Abs(minY) - Mathf.Abs(minY/2)), 0);
  182. float bottomPoint = transform.InverseTransformPoint(camera.ViewportToWorldPoint(new Vector3(0, 0, 0))).y;
  183. // transform.position = new Vector3(cameraPosition.x, (Mathf.Abs(bottomPoint) - Mathf.Abs(minY)) - 2, 0);
  184. transform.position = new Vector3(cameraPosition.x, (Mathf.Abs(bottomPoint) - Mathf.Abs(minY)) , 0);
  185.  
  186. #else
  187. //transform.position = new Vector3(cameraPosition.x, -(Mathf.Abs(maxY + maxY/2)),0);
  188. float topPoint = transform.InverseTransformPoint(camera.ViewportToWorldPoint(new Vector3(0, 1, 0))).y;
  189. transform.position = new Vector3(cameraPosition.x, - (topPoint-maxY) + 2, 0);
  190.  
  191.  
  192. #endif
  193. }
  194.  
  195. private void AdjustCameraOrthographicSize()
  196. {
  197. // camera.orthographicSize = 80;
  198.  
  199. var widthSize = width * Screen.height / Screen.width * 0.5f;// + offsetX;
  200. var heightSize = height / 2;// + offsetX;
  201.  
  202. camera.orthographicSize = widthSize > heightSize ? widthSize : heightSize;
  203.  
  204.  
  205. //float screenRatio = (float)Screen.width / (float)Screen.height;
  206. //float targetRatio = width / height;
  207.  
  208. //if (screenRatio >= targetRatio)
  209. //{
  210. // camera.orthographicSize = height / 2;
  211. //}
  212. //else
  213. //{
  214. // float differenceInSize = targetRatio / screenRatio;
  215. // camera.orthographicSize = height / 2 * differenceInSize;
  216. //}
  217.  
  218. // camera.orthographicSize += 20;
  219. }
  220.  
  221. private void SetBounds(float[] bounds)
  222. {
  223. minX = bounds[0];//* scale;
  224. maxX = bounds[1];//* scale;
  225. minY = bounds[2];//* scale;
  226. maxY = bounds[3]; //* scale;
  227.  
  228. //minX = bounds[0]* mainScale;
  229. //maxX = bounds[1]* mainScale;
  230. //minY = bounds[2]* mainScale;
  231. //maxY = bounds[3]* mainScale;
  232. }
  233.  
  234. private void CalculateImageDimension()
  235. {
  236. width = Mathf.Abs(maxX) + Mathf.Abs(minX);
  237. height = Mathf.Abs(maxY) + Mathf.Abs(minY);
  238. }
  239.  
  240. public GameObject outlineContainer;
  241.  
  242. [ContextMenu("Create Bitmap")]
  243. public void CreateBMP()
  244. {
  245. TakeScreenshot?.Invoke(false);
  246.  
  247. outlineContainer.SetActive(false);
  248.  
  249. camera.targetTexture = mainRenderTexture;
  250. camera.Render();
  251.  
  252. // read pixels will read from the currently active render texture so make our offscreen
  253. // render texture active and then read the pixels
  254. RenderTexture.active = mainRenderTexture;
  255. mainScreenShoot.ReadPixels(mainRect, 0, 0);
  256.  
  257. // reset active camera texture and render texture
  258. camera.targetTexture = null;
  259. RenderTexture.active = null;
  260.  
  261. // DataStorageBase.CreateDirectory(DataStorageBase.MiniaturesDataPath);
  262. // string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage);// + DataStorageBase.MiniaturesSavingExtension);
  263. // string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage + ".png");
  264. string filename = DataStorageBase.ChosenImage.Replace(".svg", "Bitmap.svg");
  265. //DataStorageBase.ChosenImage;
  266. SaveTexture(filename, mainScreenShoot, format);
  267.  
  268. outlineContainer.SetActive(true);
  269. TakeScreenshot?.Invoke(true);
  270. }
  271.  
  272.  
  273. [ContextMenu("Create SS")]
  274. public void CreateScreenShoot()
  275. {
  276. if (DataStorageBase.errorExists == true)
  277. {
  278. return;
  279. }
  280. TakeScreenshot?.Invoke(false);
  281.  
  282.  
  283. camera.targetTexture = previewRenderTexture;
  284. camera.Render();
  285.  
  286. // read pixels will read from the currently active render texture so make our offscreen
  287. // render texture active and then read the pixels
  288. RenderTexture.active = previewRenderTexture;
  289. previewScreenShot.ReadPixels(previewRect, 0, 0);
  290.  
  291. // reset active camera texture and render texture
  292. camera.targetTexture = null;
  293. RenderTexture.active = null;
  294.  
  295. // DataStorageBase.CreateDirectory(DataStorageBase.MiniaturesDataPath);
  296. // string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage);// + DataStorageBase.MiniaturesSavingExtension);
  297. // string filename = Path.Combine(DataStorageBase.MiniaturesDataPath, DataStorageBase.ChosenImage + ".png");
  298. string filename = DataStorageBase.ChosenImage;
  299. SaveTexture(filename,previewScreenShot, format);
  300.  
  301. TakeScreenshot?.Invoke(true);
  302. }
  303.  
  304. //public static void SaveTextureInMiniatureDirectory(string filename, Texture2D screenShot, ImageFormat format)
  305. //{
  306. // filename = Path.Combine(DataStorageBase.MiniaturesDataPath, filename + DataStorageBase.MiniaturesSavingExtension);
  307. // SaveTexture(filename, screenShot, format);
  308. //}
  309.  
  310. private static void SaveTexture(string filename, Texture2D screenShot, ImageFormat format)
  311. {
  312. // pull in our file header/data bytes for the specified image format (has to be done from main thread)
  313. byte[] fileHeader = null;
  314. byte[] fileData = null;
  315. if (format == ImageFormat.RAW)
  316. {
  317. fileData = screenShot.GetRawTextureData();
  318. }
  319. else if (format == ImageFormat.PNG)
  320. {
  321. fileData = screenShot.EncodeToPNG();
  322. }
  323. else if (format == ImageFormat.JPG)
  324. {
  325. fileData = screenShot.EncodeToJPG();
  326. }
  327. else // ppm
  328. {
  329. // create a file header for ppm formatted file
  330. string headerStr = string.Format("P6\n{0} {1}\n255\n", screenShot.width, screenShot.height);
  331. fileHeader = System.Text.Encoding.ASCII.GetBytes(headerStr);
  332. fileData = screenShot.GetRawTextureData();
  333. }
  334.  
  335. int x =filename.LastIndexOf("\\");
  336. string name = filename.Substring(x+1);
  337. filename = Path.Combine(DataStorageBase.MiniatureDirectory, name.Replace("svg", "png"));//DataStorageBase.MiniaturesSavingExtension);
  338.  
  339. // create new thread to save the image to file (only operation that can be done in background)
  340. new System.Threading.Thread(() =>
  341. {
  342. // create file and write optional header with image bytes
  343. var f = System.IO.File.Create(filename);
  344. if (fileHeader != null)
  345. f.Write(fileHeader, 0, fileHeader.Length);
  346. f.Write(fileData, 0, fileData.Length);
  347. f.Close();
  348. }).Start();
  349.  
  350.  
  351. }
  352. }
  353.  
  354. // configure with raw, jpg, png, or ppm (simple raw format)
  355. public enum ImageFormat
  356. {
  357. RAW,
  358. JPG,
  359. PNG,
  360. PPM
  361. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement