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

216 lines
7.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-09-04 15:34:11 +08:00
using HK.FUJIFILM;
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");
2025-08-27 13:04:14 +08:00
public string FolderPath;
2025-08-25 21:05:45 +08:00
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)
{
2025-08-27 13:04:14 +08:00
FolderPath = Path.Combine(dirPath, $"{DateTime.Now.Ticks}");
if (!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
}
2025-08-27 01:05:19 +08:00
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 13:04:14 +08:00
var path = Path.Combine(FolderPath, $"Preview - {DateTime.Now.Ticks}.png");
2025-08-27 01:05:19 +08:00
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");
2025-09-04 15:34:11 +08:00
var fileInfo = new FileInfo(uploadTmp);
var cutBG = fileInfo.Directory.FullName + $"cut bg {fileInfo.Name}";
// 褪底功能
// yield return StartCoroutine(Wait(uploadTmp, cutBG));
2025-09-04 15:34:11 +08:00
Debug.Log(cutBG);
Debug.Log("finish");
2025-08-25 21:05:45 +08:00
}
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 13:04:14 +08:00
if (!Directory.Exists(FolderPath))
2025-08-25 21:05:45 +08:00
{
2025-08-27 13:04:14 +08:00
Directory.CreateDirectory(FolderPath);
2025-08-25 21:05:45 +08:00
}
2025-08-27 13:04:14 +08:00
Debug.Log(FolderPath);
2025-08-25 21:05:45 +08:00
2025-09-04 15:34:11 +08:00
var nowTicks = DateTime.Now.Ticks;
string filePath = Path.Combine(FolderPath, $"printing-{nowTicks}");
var outputPath = $"{filePath}.png";
var changePpi = B83.Image.PNGTools.ChangePPI(bytes, 300, 300);
// File.WriteAllBytes(outputPath, changePpi);
2025-08-25 21:05:45 +08:00
PngCompressor.CompressPngAndSetDpi(changePpi, outputPath);
2025-09-04 15:34:11 +08:00
printingDatas.Add(File.ReadAllBytes(outputPath));
string tmpPath = outputPath;
// File.WriteAllBytes(tmpPath, B83.Image.PNGTools.ChangePPI(bytes, 300, 300));
2025-08-25 21:05:45 +08:00
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;
}
2025-09-04 15:34:11 +08:00
IEnumerator Wait(string filePath, string transferPath)
{
string currentDirectory = Directory.GetCurrentDirectory();
string savePath = Path.Combine(currentDirectory, "temp");
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
string fileName = "downloadedTexture.png";
string transferName = "transferTexture.png";
string path1 = Path.Combine(savePath, fileName);
File.Copy(filePath, path1, true);
string path2 = Path.Combine(savePath, transferName);
Debug.Log($"file path: {path1}");
Debug.Log($"transfer path: {path2}");
var startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = "python",
Arguments = $"cutbg.py \"{path1}\" \"{path2}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
System.Diagnostics.Process process = new System.Diagnostics.Process
{
StartInfo = startInfo
};
Debug.Log("Start");
process.Start();
process.WaitForExit();
// while (!process.HasExited)
// {
// yield return null;
// }
yield return null;
}
2025-08-25 21:05:45 +08:00
}