101 lines
3.0 KiB
C#
101 lines
3.0 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.IO;
|
||
|
using UnityEngine;
|
||
|
|
||
|
public class RenderPrintingManager : MonoBehaviour
|
||
|
{
|
||
|
public static RenderPrintingManager Instance;
|
||
|
public Vector2 renderPrintSize;
|
||
|
|
||
|
public Transform pivot;
|
||
|
|
||
|
public Camera renderCam;
|
||
|
private RenderTexture rendtx;
|
||
|
string dirPath = Path.Combine(Application.dataPath, "TestPic");
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
Instance = this;
|
||
|
if (!Directory.Exists(dirPath))
|
||
|
{
|
||
|
Directory.CreateDirectory(dirPath);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private void Update()
|
||
|
{
|
||
|
if (Input.GetKeyDown(KeyCode.K))
|
||
|
{
|
||
|
StartCoroutine(WaitSavePreview());
|
||
|
}
|
||
|
|
||
|
if (Input.GetKeyDown(KeyCode.L))
|
||
|
{
|
||
|
StartCoroutine(WaitSavePrinting());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void StartRenderPrinting()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
IEnumerator WaitSavePreview()
|
||
|
{
|
||
|
float smallerScale = Mathf.Max(renderPrintSize.x / 400f, renderPrintSize.y / 400f);
|
||
|
rendtx = new RenderTexture((int)(renderPrintSize.x / smallerScale), (int)(renderPrintSize.y / smallerScale), 24,
|
||
|
RenderTextureFormat.ARGB32);
|
||
|
pivot.transform.localScale = Vector3.one / smallerScale;
|
||
|
|
||
|
renderCam.targetTexture = rendtx;
|
||
|
yield return null;
|
||
|
Texture2D tex = ToTexture2D(rendtx);
|
||
|
var bytes = tex.EncodeToPNG();
|
||
|
File.WriteAllBytes(Path.Combine(dirPath, $"Preview - {DateTime.Now.Ticks}.png"), bytes);
|
||
|
Debug.Log("Preview finish");
|
||
|
rendtx.Release();
|
||
|
}
|
||
|
|
||
|
IEnumerator WaitSavePrinting()
|
||
|
{
|
||
|
pivot.transform.localScale = Vector3.one;
|
||
|
Vector2 printSize = renderPrintSize;
|
||
|
rendtx = new RenderTexture((int)printSize.x, (int)printSize.y, 24, RenderTextureFormat.ARGB32);
|
||
|
renderCam.targetTexture = rendtx;
|
||
|
yield return null;
|
||
|
Texture2D print_tex = ToTexture2D(rendtx);
|
||
|
string uploadTmp = SpecialSaveImageFile(print_tex, "printing");
|
||
|
Debug.Log(uploadTmp);
|
||
|
}
|
||
|
|
||
|
public string SpecialSaveImageFile(Texture2D tex, string name)
|
||
|
{
|
||
|
byte[] bytes = tex.EncodeToPNG();
|
||
|
|
||
|
|
||
|
if (!Directory.Exists(dirPath))
|
||
|
{
|
||
|
Directory.CreateDirectory(dirPath);
|
||
|
}
|
||
|
|
||
|
Debug.Log(dirPath);
|
||
|
|
||
|
string filePath = Path.Combine(dirPath, $"printing-{DateTime.Now.Ticks}");
|
||
|
File.WriteAllBytes(filePath + ".png", B83.Image.PNGTools.ChangePPI(bytes, 300, 300));
|
||
|
|
||
|
string tmpPath = dirPath + DateTime.Now.ToString("yyyyMMHHmmss") + ".png";
|
||
|
File.WriteAllBytes(tmpPath, B83.Image.PNGTools.ChangePPI(bytes, 300, 300));
|
||
|
return tmpPath;
|
||
|
}
|
||
|
|
||
|
Texture2D ToTexture2D(RenderTexture rTex)
|
||
|
{
|
||
|
RenderTexture currentActiveRT = RenderTexture.active;
|
||
|
Texture2D tex = new Texture2D(rTex.width, rTex.height, TextureFormat.ARGB32, false);
|
||
|
RenderTexture.active = rTex;
|
||
|
tex.ReadPixels(new Rect(0, 0, rTex.width, rTex.height), 0, 0);
|
||
|
tex.Apply();
|
||
|
RenderTexture.active = currentActiveRT;
|
||
|
return tex;
|
||
|
}
|
||
|
}
|