65 lines
4.0 KiB
C#
65 lines
4.0 KiB
C#
#if UNITY_EDITOR
|
|
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEditor;
|
|
using UnityEditor.SceneManagement;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
using Object=UnityEngine.Object;
|
|
|
|
/// <summary>Isolated static or sampled-pose views; preserves all open scene contents.</summary>
|
|
public static class AuditCharacterVisual
|
|
{
|
|
public static string Capture(string prefabPath,string outputDirectory,float height=.55f,string clipPath="",float phase=0)
|
|
{
|
|
string dir=Path.GetFullPath(Path.Combine(Application.dataPath,"../..",outputDirectory));Directory.CreateDirectory(dir);
|
|
var scene=EditorSceneManager.NewPreviewScene();
|
|
try
|
|
{
|
|
var prefab=AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
|
|
if(prefab==null)throw new Exception("Missing visual prefab: "+prefabPath);
|
|
var unit=(GameObject)PrefabUtility.InstantiatePrefab(prefab,scene);
|
|
var animator=unit.GetComponentInChildren<Animator>();if(animator!=null)animator.enabled=false;
|
|
if(animator==null)
|
|
{
|
|
var filters=unit.GetComponentsInChildren<MeshFilter>();
|
|
var points=filters.SelectMany(m=>m.sharedMesh.vertices.Select(v=>m.transform.TransformPoint(v))).ToArray();
|
|
if(points.Length==0)throw new Exception("No static geometry");
|
|
float minY=points.Min(v=>v.y),actualHeight=points.Max(v=>v.y)-minY;
|
|
int triangles=filters.Sum(m=>m.sharedMesh.triangles.Length/3);
|
|
if(Mathf.Abs(minY)>.002f || Mathf.Abs(actualHeight-height)>.002f)throw new Exception("Static height/bottom invalid");
|
|
if(unit.GetComponentsInChildren<Renderer>().Any(r=>r.sharedMaterial==null || r.sharedMaterial.shader.name!="Test/XPbr"))throw new Exception("Static XPbr material missing");
|
|
File.WriteAllText(Path.Combine(dir,"static_verification.txt"),$"height={actualHeight:F6} bottom={minY:F6} triangles={triangles} XPbr=True; facing verified in front/side/back views");
|
|
}
|
|
if(!string.IsNullOrEmpty(clipPath))
|
|
{
|
|
var clip=AssetDatabase.LoadAssetAtPath<AnimationClip>(clipPath);
|
|
if(clip==null || animator==null)throw new Exception("Missing clip/animator");
|
|
clip.SampleAnimation(animator.gameObject,clip.length*phase);
|
|
}
|
|
var light=new GameObject("AuditLight").AddComponent<Light>();SceneManager.MoveGameObjectToScene(light.gameObject,scene);
|
|
light.type=LightType.Directional;light.intensity=1.4f;light.transform.rotation=Quaternion.Euler(40,-30,0);
|
|
var camera=new GameObject("AuditCamera").AddComponent<Camera>();SceneManager.MoveGameObjectToScene(camera.gameObject,scene);camera.scene=scene;
|
|
camera.clearFlags=CameraClearFlags.SolidColor;camera.backgroundColor=new Color(.56f,.64f,.7f);
|
|
camera.orthographic=true;camera.orthographicSize=height*.68f;camera.nearClipPlane=.01f;camera.farClipPlane=10;
|
|
var rt=new RenderTexture(640,640,24);var tex=new Texture2D(640,640,TextureFormat.RGB24,false);var previous=RenderTexture.active;
|
|
try
|
|
{
|
|
var directions=new[]{new Vector3(0,.22f,-1),new Vector3(1,.22f,0),new Vector3(0,.22f,1),new Vector3(.7f,.3f,-1)};
|
|
string[] names={"front","side","back","perspective"};
|
|
for(int i=0;i<directions.Length;i++)
|
|
{
|
|
var center=Vector3.up*height*.5f;camera.transform.position=center+directions[i]*height*3;
|
|
camera.transform.LookAt(center);camera.targetTexture=rt;camera.Render();RenderTexture.active=rt;
|
|
tex.ReadPixels(new Rect(0,0,640,640),0,0);tex.Apply();File.WriteAllBytes(Path.Combine(dir,names[i]+".png"),tex.EncodeToPNG());
|
|
}
|
|
}
|
|
finally {RenderTexture.active=previous;camera.targetTexture=null;rt.Release();Object.DestroyImmediate(rt);Object.DestroyImmediate(tex);}
|
|
}
|
|
finally {EditorSceneManager.ClosePreviewScene(scene);}
|
|
return dir;
|
|
}
|
|
}
|
|
#endif
|