using UnityEngine;
using System.Collections;
using System.IO;
using Unity.Collections;
using UnityEngine.Rendering;
///
/// Sauvegarde deux vues personnalisées pour le PDF.
///
/// Bouton 1 → SaveView1() → pdfview1.jpg (remplace la vue scène principale)
/// Bouton 2 → SaveView2() → pdfview2.jpg (vue supplémentaire dans le PDF)
///
/// Si les fichiers existent lors de la génération PDF, PDFCreation les utilise
/// au lieu de faire une capture automatique.
///
/// Dossier : Application.persistentDataPath/PDFViews/
///
public class PDFViewSaver : MonoBehaviour
{
// ─── Constantes ──────────────────────────────────────────────────────────
private const string FOLDER = "PDFViews";
private const int CAPTURE_W = 1280;
private const int CAPTURE_H = 750;
private const int JPEG_QUALITY = 85;
// ─── Chemin des fichiers (statique — accessible par PDFCreation) ─────────
// Sous Windows : dossier fixe "F:/PDF CLIENTS/Views" ; sinon repli sur persistentDataPath/PDFViews.
public static string BaseFolder =>
(Application.platform == RuntimePlatform.WindowsPlayer ||
Application.platform == RuntimePlatform.WindowsEditor)
? @"F:/PDF CLIENTS/Views"
: Path.Combine(Application.persistentDataPath, FOLDER);
public static string GetViewPath(int index) =>
Path.Combine(BaseFolder, $"pdfview{index}.jpg");
public static bool ViewExists(int index) => File.Exists(GetViewPath(index));
// ─── Boutons ─────────────────────────────────────────────────────────────
/// Bouton 1 — Sauvegarde la vue actuelle comme pdfview1.jpg
public void SaveView1() => StartCoroutine(SaveViewCoroutine(1));
/// Bouton 2 — Sauvegarde la vue actuelle comme pdfview2.jpg
public void SaveView2() => StartCoroutine(SaveViewCoroutine(2));
// ─── Interne ─────────────────────────────────────────────────────────────
private RenderTexture _captureRT;
private IEnumerator SaveViewCoroutine(int index)
{
WaitCircle.Setting(true, $"Sauvegarde vue PDF {index}...");
bool wasHD = _G.HD;
if (!wasHD) DASH.HDsetBTNcolor(true);
yield return new WaitForSeconds(0.5f);
Camera cam = Camera.main;
if (cam == null)
{
Debug.LogError("[PDFViewSaver] Camera.main introuvable");
WaitCircle.Setting(false, "");
yield break;
}
// Capture dans une RenderTexture
var rt = new RenderTexture(CAPTURE_W, CAPTURE_H, 24);
var savedRT = cam.targetTexture;
cam.targetTexture = rt;
cam.Render();
cam.targetTexture = savedRT;
// Flip vertical (même convention que PDFCreation.CaptureToRenderTexture)
_captureRT = new RenderTexture(CAPTURE_W, CAPTURE_H, 24);
Graphics.Blit(rt, _captureRT, new Vector2(1f, -1f), new Vector2(0f, 1f));
rt.Release();
Destroy(rt);
if (!wasHD) DASH.HDsetBTNcolor(false);
// Lecture GPU asynchrone → sauvegarde
AsyncGPUReadback.Request(_captureRT, 0, TextureFormat.RGBA32,
req => OnReadbackDone(req, index));
}
private void OnReadbackDone(AsyncGPUReadbackRequest req, int index)
{
var format = _captureRT.graphicsFormat;
uint width = (uint)_captureRT.width;
uint height = (uint)_captureRT.height;
_captureRT.Release();
Destroy(_captureRT);
_captureRT = null;
if (req.hasError)
{
Debug.LogError($"[PDFViewSaver] Erreur GPU readback (vue {index})");
WaitCircle.Setting(false, "");
return;
}
// Second flip (identique à PDFCreation.RequestFlip)
NativeArray raw = req.GetData();
int w = (int)width;
int h = (int)height;
var tex = new Texture2D(w, h, TextureFormat.RGBA32, false);
var processed = tex.GetRawTextureData();
for (int i = 0; i < raw.Length; i += 4)
{
int pixel = i / 4;
int x = pixel % w;
int y = pixel / w;
int flippedIdx = (x + (h - 1 - y) * w) * 4;
processed[i] = raw[flippedIdx];
processed[i + 1] = raw[flippedIdx + 1];
processed[i + 2] = raw[flippedIdx + 2];
processed[i + 3] = raw[flippedIdx + 3];
}
// Encoder en JPEG
byte[] jpg = ImageConversion.EncodeArrayToJPG(
processed.ToArray(), format, width, height, 0, JPEG_QUALITY);
// Créer le dossier et sauvegarder
string dir = BaseFolder;
Directory.CreateDirectory(dir);
string path = GetViewPath(index);
File.WriteAllBytes(path, jpg);
Destroy(tex);
Debug.Log($"✅ [PDFViewSaver] Vue {index} sauvegardée ({jpg.Length / 1024} KB)\n→ {path}");
WaitCircle.Setting(false, "");
}
}