From 9c388a78b5812154ea7f33d92e829a096d9c7a38 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 11 Nov 2020 19:20:00 +0900 Subject: [PATCH] =?UTF-8?q?=E3=82=B9=E3=82=AF=E3=83=AA=E3=83=BC=E3=83=B3?= =?UTF-8?q?=E3=82=B7=E3=83=A7=E3=83=83=E3=83=88=E3=82=92=E4=BD=9C=E6=88=90?= =?UTF-8?q?=E3=81=99=E3=82=8B=E3=83=9C=E3=82=BF=E3=83=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UniVRM/Editor/Meta/VRMMetaObjectEditor.cs | 73 ++++++++++++++++++- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/Assets/VRM/UniVRM/Editor/Meta/VRMMetaObjectEditor.cs b/Assets/VRM/UniVRM/Editor/Meta/VRMMetaObjectEditor.cs index c806df61a..d2bc00c11 100644 --- a/Assets/VRM/UniVRM/Editor/Meta/VRMMetaObjectEditor.cs +++ b/Assets/VRM/UniVRM/Editor/Meta/VRMMetaObjectEditor.cs @@ -1,6 +1,7 @@ using UnityEditor; using UnityEngine; using MeshUtility.M17N; +using System.IO; namespace VRM { @@ -172,12 +173,25 @@ namespace VRM // texture EditorGUILayout.BeginHorizontal(); { + // 左側 EditorGUILayout.BeginVertical(); - GUI.enabled = false; - EditorGUILayout.PropertyField(m_exporterVersion); - GUI.enabled = true; - EditorGUILayout.PropertyField(m_thumbnail); + { + GUI.enabled = false; + EditorGUILayout.PropertyField(m_exporterVersion); + GUI.enabled = true; + EditorGUILayout.PropertyField(m_thumbnail); + + if (Camera.main) + { + EditorGUILayout.HelpBox("Camera.main で画像を Render します。", MessageType.Info); + if (GUILayout.Button("スクリーンショット")) + { + TakeScreenShot(); + } + } + } EditorGUILayout.EndVertical(); + // 右側 m_thumbnail.objectReferenceValue = TextureField("", (Texture2D)m_thumbnail.objectReferenceValue, 100); } EditorGUILayout.EndHorizontal(); @@ -218,6 +232,57 @@ namespace VRM serializedObject.ApplyModifiedProperties(); } + void TakeScreenShot() + { + var dst = SaveDialog(m_target, "png"); + if (string.IsNullOrEmpty(dst)) + { + return; + } + + var backup = RenderTexture.active; + var backup2 = Camera.main.targetTexture; + var rt = new RenderTexture(1024, 1024, 16, RenderTextureFormat.ARGB32); + var tex = new Texture2D(rt.width, rt.height, TextureFormat.RGB24, false); + try + { + RenderTexture.active = rt; + Camera.main.targetTexture = rt; + Camera.main.Render(); + tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0); + tex.Apply(); + File.WriteAllBytes(dst, tex.EncodeToJPG()); + var assetPath = MeshUtility.UnityPath.FromFullpath(dst); + EditorApplication.delayCall += () => + { + assetPath.ImportAsset(); + m_target.Thumbnail = assetPath.LoadAsset(); + }; + } + finally + { + RenderTexture.active = backup; + Camera.main.targetTexture = backup2; + Texture2D.DestroyImmediate(tex); + RenderTexture.DestroyImmediate(rt); + } + } + + static string SaveDialog(VRMMetaObject meta, string ext) + { + var directory = Application.dataPath; + var assetPath = AssetDatabase.GetAssetPath(meta); + if (!string.IsNullOrEmpty(assetPath)) + { + directory = Path.Combine(directory + "/..", Path.GetDirectoryName(assetPath)); + } + return EditorUtility.SaveFilePanel( + "Save thumbnail", + directory, + $"thumbnail.{ext}", + ext); + } + static (Rect, Rect) FixedRight(Rect r, int width) { if (width > r.width)