FM/Assets/Test/v1.0/Render/RenderPrintingManager.cs

154 lines
5.4 KiB
C#
Raw Normal View History

2025-08-25 21:05:45 +08:00
using System;
using System.Collections;
2025-08-27 01:05:19 +08:00
using System.Collections.Generic;
2025-08-25 21:05:45 +08:00
using System.IO;
2025-08-27 01:05:19 +08:00
using HK;
2025-08-25 21:05:45 +08:00
using UnityEngine;
public class RenderPrintingManager : MonoBehaviour
{
public static RenderPrintingManager Instance;
2025-08-27 01:05:19 +08:00
[SerializeField] Camera renderCam;
[SerializeField] Transform parent;
RenderPrintingManagerOption option;
// RenderPrintingManagerModelOption modelOption;
2025-08-25 21:05:45 +08:00
private RenderTexture rendtx;
string dirPath = Path.Combine(Application.dataPath, "TestPic");
private void Awake()
{
Instance = this;
2025-08-27 01:05:19 +08:00
var path = Path.Combine(Application.streamingAssetsPath, "PrintingSavePath.txt");
if (File.Exists(path) && !string.IsNullOrEmpty(File.ReadAllText(path)))
dirPath = File.ReadAllText(path);
2025-08-25 21:05:45 +08:00
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
}
private void Update()
{
2025-08-27 01:05:19 +08:00
// if (Input.GetKeyDown(KeyCode.K))
// {
// StartCoroutine(WaitSavePreview());
// }
//
// if (Input.GetKeyDown(KeyCode.L))
// {
// StartCoroutine(WaitSavePrinting());
// }
}
2025-08-25 21:05:45 +08:00
2025-08-27 01:05:19 +08:00
public void StartRenderPrinting(GameObject preview, GameObject design, Action<byte[], List<byte[]>> callback = null)
{
previewData = new byte[] { };
printingDatas = new List<byte[]>();
StartCoroutine(WaitSaveFinish(preview, design, callback));
2025-08-25 21:05:45 +08:00
}
2025-08-27 01:05:19 +08:00
byte[] previewData;
List<byte[]> printingDatas;
IEnumerator WaitSaveFinish(GameObject preview, GameObject design, Action<byte[], List<byte[]>> callback)
2025-08-25 21:05:45 +08:00
{
2025-08-27 01:05:19 +08:00
if (preview != null)
yield return StartCoroutine(WaitSavePreview(preview));
var designGo = GameObject.Instantiate(design, parent);
this.option = designGo.GetComponent<RenderPrintingManagerOption>();
option.GetComponent<RenderPrintingManagerOptionHelper>().ShowAllPrintings();
option.GetComponent<RenderPrintingManagerOptionHelper>().HideAllPrintings();
yield return null;
for (var i = 0; i < option.objectsToRender.Count; i++)
{
foreach (var t in option.objectsToRender)
{
t.gameObject.SetActive(false);
}
option.objectsToRender[i].gameObject.SetActive(true);
yield return StartCoroutine(WaitSavePrinting(designGo));
}
GameObject.Destroy(designGo);
callback?.Invoke(previewData, printingDatas);
2025-08-25 21:05:45 +08:00
}
2025-08-27 01:05:19 +08:00
IEnumerator WaitSavePreview(GameObject preview)
2025-08-25 21:05:45 +08:00
{
2025-08-27 01:05:19 +08:00
var previewGo = GameObject.Instantiate(preview, parent);
previewGo.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
var modelOption = previewGo.GetComponent<RenderPrintingManagerOption>();
modelOption.GetComponent<RenderPrintingManagerOptionHelper>().ShowAllPreviews();
modelOption.GetComponent<RenderPrintingManagerOptionHelper>().HideAllPreviews();
yield return null;
float smallerScale = Mathf.Max(modelOption.renderPreviewSize.x / 400f, modelOption.renderPreviewSize.y / 400f);
rendtx = new RenderTexture((int)(modelOption.renderPreviewSize.x / smallerScale),
(int)(modelOption.renderPreviewSize.y / smallerScale), 24,
2025-08-25 21:05:45 +08:00
RenderTextureFormat.ARGB32);
2025-08-27 01:05:19 +08:00
modelOption.pivot.transform.localScale = Vector3.one / smallerScale;
2025-08-25 21:05:45 +08:00
renderCam.targetTexture = rendtx;
yield return null;
Texture2D tex = ToTexture2D(rendtx);
var bytes = tex.EncodeToPNG();
2025-08-27 01:05:19 +08:00
var path = Path.Combine(dirPath, $"Preview - {DateTime.Now.Ticks}.png");
File.WriteAllBytes(path, bytes);
previewData = File.ReadAllBytes(path);
2025-08-25 21:05:45 +08:00
Debug.Log("Preview finish");
rendtx.Release();
2025-08-27 01:05:19 +08:00
GameObject.Destroy(previewGo);
2025-08-25 21:05:45 +08:00
}
2025-08-27 01:05:19 +08:00
IEnumerator WaitSavePrinting(GameObject design)
2025-08-25 21:05:45 +08:00
{
2025-08-27 01:05:19 +08:00
option.pivot.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
option.pivot.transform.localScale = Vector3.one;
Vector2 printSize = option.renderPrintSize.x == 0 ? option.renderPreviewSize : option.renderPrintSize;
2025-08-25 21:05:45 +08:00
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);
}
2025-08-27 01:05:19 +08:00
private string SpecialSaveImageFile(Texture2D tex, string name)
2025-08-25 21:05:45 +08:00
{
byte[] bytes = tex.EncodeToPNG();
2025-08-27 01:05:19 +08:00
printingDatas.Add(bytes);
2025-08-25 21:05:45 +08:00
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;
}
}