Files
XRLib/Assets/Scripts/SHI/modal/ISOP/ISOPModelView.cs
2025-11-27 13:41:37 +09:00

664 lines
23 KiB
C#

#nullable enable
using System;
using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using GLTFast;
using UnityEngine;
using UnityEngine.UIElements;
using UVC.UI.List.Tree;
using UVC.Util;
namespace SHI.Modal.ISOP
{
/// <summary>
/// glTF 모델을 비동기로 로드해 전용 카메라로 오프스크린 렌더링하고, 결과를 UIElements의 VisualElement에 출력하는 뷰입니다.
/// 마우스 조작(이동/확대/회전), 항목 하이라이트, 와이어프레임 토글 등을 제공합니다.
/// </summary>
[UxmlElement]
public partial class ISOPModelView : VisualElement
{
/// <summary>
/// 뷰 내부에서 항목이 선택될 때 발생합니다.
/// </summary>
public Action<TreeListItemData>? OnItemSelected;
/// <summary>
/// 모델 뷰가 확장 요청될 때 발생합니다.
/// </summary>
public Action? OnExpand;
// 리소스 경로 상수
private const string UXML_PATH = "SHI/Modal/ISOP/ISOPModelView"; // Resources 폴더 기준 경로
// 설정
private Color cameraBackgroundColor = new Color(1f, 1f, 1f, 1f);
private int modelLayer = 6;
private bool createDefaultLight = true;
// 마우스 조작 설정
private float panSpeed = 1.0f;
private float rotateDegPerPixel = 0.2f;
private float zoomSpeed = 5f;
// 와이어프레임
private bool wireframeMode = true;
private Camera? _viewCamera;
private RenderTexture? _rt;
private Material? _wireframeMat;
private bool _wireframeApplied;
// Orbit controls state
private Vector3 _orbitTarget;
private float _orbitDistance = 5f;
private float _yaw = 0f;
private float _pitch = 20f;
// Drag state
private bool _mmbDragging;
private bool _rmbDragging;
private Vector3 _mmbLastPos;
private Vector3 _rmbStartPos;
private float _yawStart;
private float _pitchStart;
private Quaternion _modelStartRot;
private Vector3 _modelStartPos;
private Vector3 _rmbPivot;
private readonly Dictionary<int, GameObject> _idToObject = new Dictionary<int, GameObject>();
private readonly Dictionary<Renderer, UnityEngine.Material[]> _originalSharedByRenderer = new Dictionary<Renderer, UnityEngine.Material[]>();
private GameObject? _root;
private int? _focusedId;
// UI Element for rendering
private VisualElement? _renderContainer;
private Button? _expandBtn;
private int itemIdSeed = 1;
public ISOPModelView()
{
// UXML 로드
var visualTree = Resources.Load<VisualTreeAsset>(UXML_PATH);
if (visualTree != null)
{
visualTree.CloneTree(this);
}
else
{
Debug.LogError($"Failed to load UXML at path: {UXML_PATH}");
}
// 렌더링 컨테이너 생성
_renderContainer = this.Q<VisualElement>("render-container");
_expandBtn = this.Q<Button>("expand-btn");
if(_expandBtn != null)
{
_expandBtn.clicked += () =>
{
OnExpand?.Invoke();
};
}
// 마우스 이벤트 등록
RegisterCallback<MouseDownEvent>(OnMouseDown);
RegisterCallback<MouseUpEvent>(OnMouseUp);
RegisterCallback<MouseMoveEvent>(OnMouseMove);
RegisterCallback<WheelEvent>(OnWheel);
RegisterCallback<GeometryChangedEvent>(OnGeometryChanged);
}
/// <summary>
/// 주어진 경로의 glTF 모델을 비동기로 로드하고, UI 트리에 사용할 계층 항목을 생성합니다.
/// </summary>
public async UniTask<IEnumerable<TreeListItemData>> LoadModelAsync(string path, CancellationToken ct)
{
Debug.Log($"ISOPModelView.LoadModelAsync: {path}");
Dispose();
await UniTask.DelayFrame(1);
EnsureCameraAndTargetTexture();
var items = new List<TreeListItemData>();
var gltf = new GltfImport();
var success = await gltf.Load(path, new ImportSettings(), ct);
if (!success)
{
Debug.LogError($"glTFast Load failed: {path}");
return items;
}
_root = new GameObject("ISOPModelViewRoot");
_root.layer = modelLayer;
var sceneOk = await gltf.InstantiateMainSceneAsync(_root.transform);
if (!sceneOk)
{
Debug.LogError("InstantiateMainSceneAsync failed");
return items;
}
SetLayerRecursive(_root, modelLayer);
if (_root != null)
{
for (int i = 0; i < _root.transform.childCount; i++)
{
var child = _root.transform.GetChild(i);
var topItem = BuildItemRecursive(child, _root.transform);
if (topItem != null)
{
items.Add(topItem);
}
}
}
var bounds = CalculateBounds(_root);
FrameToBounds(bounds);
TryLoadWireframeMaterial();
if (wireframeMode && _wireframeMat != null)
{
ApplyWireframeMaterialToRoot();
}
return items;
}
private TreeListItemData BuildItemRecursive(Transform node, Transform root)
{
var data = new TreeListItemData
{
id = itemIdSeed++,
name = node.name,
isExpanded = true
};
_idToObject[data.id] = node.gameObject;
CacheOriginalMaterials(node.gameObject);
for (int i = 0; i < node.childCount; i++)
{
var child = node.GetChild(i);
var childData = BuildItemRecursive(child, root);
data.Add(childData);
}
return data;
}
private void TryLoadWireframeMaterial()
{
if (_wireframeMat == null)
{
_wireframeMat = Resources.Load<Material>("SHI/Shader/BasicWireframe");
if (_wireframeMat == null)
{
Debug.LogWarning("BasicWireframe material not found at Resources/SHI/Shader/BasicWireframe.");
}
}
}
private void ApplyWireframeMaterialToRoot()
{
if (_root == null || _wireframeMat == null) return;
var rends = _root.GetComponentsInChildren<Renderer>(true);
foreach (var r in rends)
{
if (r == null) continue;
var count = Mathf.Max(1, r.sharedMaterials.Length);
var arr = new Material[count];
for (int i = 0; i < count; i++) arr[i] = _wireframeMat;
r.sharedMaterials = arr;
}
_wireframeApplied = true;
}
private void RestoreAllOriginalMaterials()
{
foreach (var kv in _originalSharedByRenderer)
{
var r = kv.Key;
if (r == null) continue;
var originals = kv.Value;
r.sharedMaterials = originals;
}
_wireframeApplied = false;
}
private void EnsureCameraAndTargetTexture()
{
if (_viewCamera == null)
{
var rig = new GameObject("ISOPModelViewRig");
rig.layer = modelLayer;
rig.transform.SetParent(null, false);
var camGo = new GameObject("ISOPModelViewCamera");
camGo.layer = modelLayer;
camGo.transform.SetParent(rig.transform, false);
_viewCamera = camGo.AddComponent<UnityEngine.Camera>();
_viewCamera.clearFlags = CameraClearFlags.SolidColor;
_viewCamera.backgroundColor = cameraBackgroundColor;
_viewCamera.nearClipPlane = 0.01f;
_viewCamera.farClipPlane = 5000f;
_viewCamera.cullingMask = (modelLayer >= 0 && modelLayer <= 31) ? (1 << modelLayer) : ~0;
_viewCamera.targetDisplay = 1;
_viewCamera.enabled = false;
}
if (createDefaultLight && _viewCamera.transform.parent != null && _viewCamera.transform.parent.Find("ISOPModelViewLight") == null)
{
var lightGo = new GameObject("ISOPModelViewLight");
lightGo.layer = modelLayer;
lightGo.transform.SetParent(_viewCamera.transform.parent, false);
lightGo.transform.localPosition = Vector3.zero;
lightGo.transform.localRotation = Quaternion.Euler(50f, -30f, 0f);
var light = lightGo.AddComponent<Light>();
light.type = LightType.Directional;
light.intensity = 1.1f;
light.shadows = LightShadows.Soft;
}
EnsureRenderTargetSize();
}
private void EnsureRenderTargetSize()
{
if (_viewCamera == null || _renderContainer == null) return;
int w = Mathf.Max(64, Mathf.RoundToInt(_renderContainer.resolvedStyle.width));
int h = Mathf.Max(64, Mathf.RoundToInt(_renderContainer.resolvedStyle.height));
if (_rt == null || _rt.width != w || _rt.height != h)
{
if (_rt != null)
{
if (_viewCamera.targetTexture == _rt) _viewCamera.targetTexture = null;
_rt.Release();
UnityEngine.Object.Destroy(_rt);
}
_rt = new RenderTexture(w, h, 24, RenderTextureFormat.ARGB32)
{
name = "ISOPModelViewRT",
antiAliasing = 2
};
_viewCamera.targetTexture = _rt;
_viewCamera.enabled = true;
// UIElements에 렌더텍스처 적용
if (_renderContainer != null)
{
_renderContainer.style.backgroundImage = new StyleBackground(Background.FromRenderTexture(_rt));
}
}
else
{
if (_viewCamera.targetTexture != _rt)
{
_viewCamera.targetTexture = _rt;
}
if (!_viewCamera.enabled) _viewCamera.enabled = true;
}
}
private void OnGeometryChanged(GeometryChangedEvent evt)
{
EnsureRenderTargetSize();
}
/// <summary>
/// 와이어프레임 모드를 토글합니다.
/// </summary>
public void SetWireframe(bool on)
{
wireframeMode = on;
TryLoadWireframeMaterial();
if (_wireframeMat != null)
{
if (on) ApplyWireframeMaterialToRoot(); else RestoreAllOriginalMaterials();
}
}
/// <summary>
/// 지정한 항목을 포커스(하이라이트)합니다.
/// </summary>
public void FocusItem(TreeListItemData data)
{
if (data == null) return;
FocusItemById(data.id);
}
public void FocusItemById(int id)
{
Debug.Log($"ISOPModelView.FocusItemById: id={id}");
_focusedId = id;
if (_idToObject.TryGetValue(id, out var go))
{
Highlight(go, true);
Debug.Log($"ISOPModelView.FocusItemById: {go.name}");
_orbitTarget = go.transform.position;
}
}
public void UnfocusItem()
{
if (_focusedId.HasValue && _idToObject.TryGetValue(_focusedId.Value, out var go))
{
Highlight(go, false);
}
_focusedId = null;
}
public void Export(int id)
{
Debug.Log($"ISOPModelView.Export: id={id}");
if (_idToObject.TryGetValue(id, out var go))
{
Debug.Log($"Exporting object: {go.name}");
UVC.GLTF.GLTFExporter.ExportNodeByExplorer(go);
}
}
public void SetVisibility(int id, bool on)
{
Debug.Log($"ISOPModelView.SetVisibility: id={id} on={on}");
if (_idToObject.TryGetValue(id, out var go))
{
go.SetActive(on);
}
}
private static readonly string[] ColorProps = new[] {
"_WireframeColor", "_WireColor",
"_BaseColor", "_Color", "_LineColor", "_TintColor"
};
private bool TrySetColor(Material mat, Color c)
{
for (int i = 0; i < ColorProps.Length; i++)
{
var prop = ColorProps[i];
if (mat != null && mat.HasProperty(prop)) { mat.SetColor(prop, c); return true; }
}
try { mat.color = c; return true; } catch { /* ignore */ }
return false;
}
private bool TryGetColor(Material mat, out Color c)
{
for (int i = 0; i < ColorProps.Length; i++)
{
var prop = ColorProps[i];
if (mat != null && mat.HasProperty(prop)) { c = mat.GetColor(prop); return true; }
}
try { c = mat.color; return true; } catch { /* ignore */ }
c = ColorUtil.FromHex("#888888"); return false;
}
private bool TryGetDefaultWireColor(out Color c)
{
if (_wireframeMat != null && TryGetColor(_wireframeMat, out c)) return true;
c = ColorUtil.FromHex("#888888"); return false;
}
private void Highlight(GameObject go, bool on)
{
var rends = go.GetComponentsInChildren<Renderer>(true);
if (rends == null || rends.Length == 0) return;
for (int i = 0; i < rends.Length; i++)
{
var r = rends[i];
if (r == null) continue;
var mats = r.materials;
for (int m = 0; m < mats.Length; m++)
{
if (on)
{
TrySetColor(mats[m], ColorUtil.FromHex("#888814"));
}
else
{
bool matIsWire = mats[m] != null && (mats[m].HasProperty("_WireframeColor") || mats[m].HasProperty("_WireColor"));
if (_wireframeApplied || matIsWire)
{
Color baseWire;
if (TryGetDefaultWireColor(out baseWire))
TrySetColor(mats[m], baseWire);
else
TrySetColor(mats[m], ColorUtil.FromHex("#888888"));
}
else
{
Color orig;
UnityEngine.Material[] originals;
if (_originalSharedByRenderer.TryGetValue(r, out originals)
&& m < originals.Length && originals[m] != null
&& TryGetColor(originals[m], out orig))
{
TrySetColor(mats[m], orig);
}
else
{
TrySetColor(mats[m], ColorUtil.FromHex("#888888"));
}
}
}
}
r.materials = mats;
}
}
public void RaiseSelected(TreeListItemData data)
{
OnItemSelected?.Invoke(data);
}
// 마우스 이벤트 처리
private void OnMouseDown(MouseDownEvent evt)
{
if (evt.button == 2) // Middle mouse button
{
_mmbDragging = true;
_mmbLastPos = evt.mousePosition;
evt.StopPropagation();
}
else if (evt.button == 1) // Right mouse button
{
_rmbDragging = true;
_rmbStartPos = evt.mousePosition;
_yawStart = _yaw;
_pitchStart = _pitch;
if (_root != null)
{
_modelStartRot = _root.transform.rotation;
_modelStartPos = _root.transform.position;
}
_rmbPivot = _orbitTarget;
evt.StopPropagation();
}
}
private void OnMouseUp(MouseUpEvent evt)
{
if (evt.button == 2)
{
_mmbDragging = false;
evt.StopPropagation();
}
else if (evt.button == 1)
{
_rmbDragging = false;
evt.StopPropagation();
}
}
private void OnMouseMove(MouseMoveEvent evt)
{
if (_viewCamera == null) return;
// 가운데 버튼: 모델 이동
if (_mmbDragging && _root != null)
{
Vector3 cur = evt.mousePosition;
Vector2 dp = (Vector2)(cur - _mmbLastPos);
_mmbLastPos = cur;
float wPix = Mathf.Max(1f, _renderContainer?.resolvedStyle.width ?? 1f);
float hPix = Mathf.Max(1f, _renderContainer?.resolvedStyle.height ?? 1f);
float halfV = Mathf.Tan(_viewCamera.fieldOfView * Mathf.Deg2Rad * 0.5f) * _orbitDistance;
float halfH = halfV * _viewCamera.aspect;
float worldPerPixelX = (halfH * 2f) / wPix * panSpeed;
float worldPerPixelY = (halfV * 2f) / hPix * panSpeed;
Vector3 deltaModel = _viewCamera.transform.right * (dp.x * worldPerPixelX) + _viewCamera.transform.up * (-dp.y * worldPerPixelY);
_root.transform.position += deltaModel;
_orbitTarget += deltaModel;
evt.StopPropagation();
}
// 오른쪽 버튼: 모델 회전
if (_rmbDragging && _root != null)
{
Vector3 cur = evt.mousePosition;
Vector2 dpAbs = (Vector2)(cur - _rmbStartPos);
float yaw = -dpAbs.x * rotateDegPerPixel;
float pitch = -dpAbs.y * rotateDegPerPixel;
Quaternion yawQ = Quaternion.AngleAxis(yaw, _viewCamera.transform.up);
Quaternion pitchQ = Quaternion.AngleAxis(pitch, _viewCamera.transform.right);
Quaternion r = yawQ * pitchQ;
Vector3 startVec = _modelStartPos - _rmbPivot;
_root.transform.position = _rmbPivot + r * startVec;
_root.transform.rotation = r * _modelStartRot;
evt.StopPropagation();
}
}
private void OnWheel(WheelEvent evt)
{
if (_viewCamera == null || _root == null) return;
float scroll = -evt.delta.y * 0.001f;
if (Mathf.Abs(scroll) > 1e-5f)
{
var forward = _viewCamera.transform.forward;
Vector3 deltaZ = forward * (-scroll * zoomSpeed);
_root.transform.position += deltaZ;
_orbitTarget += deltaZ;
evt.StopPropagation();
}
}
public void Dispose()
{
foreach (var kv in _originalSharedByRenderer)
{
var r = kv.Key;
if (r == null) continue;
var originals = kv.Value;
var mats = r.materials;
for (int m = 0; m < mats.Length; m++)
{
if (mats[m] != null) UnityEngine.Object.Destroy(mats[m]);
}
r.materials = originals;
}
_originalSharedByRenderer.Clear();
_idToObject.Clear();
if (_root != null) UnityEngine.Object.Destroy(_root);
_root = null;
_focusedId = null;
_wireframeApplied = false;
if (_viewCamera != null)
{
if (_rt != null && _viewCamera.targetTexture == _rt)
{
_viewCamera.targetTexture = null;
}
_viewCamera.enabled = false;
}
if (_rt != null)
{
_rt.Release();
UnityEngine.Object.Destroy(_rt);
_rt = null;
}
}
// 유틸리티 메서드
private static void SetLayerRecursive(GameObject go, int layer)
{
if (layer < 0 || layer > 31) return;
go.layer = layer;
foreach (Transform c in go.transform) SetLayerRecursive(c.gameObject, layer);
}
private static Bounds CalculateBounds(GameObject root)
{
var rends = root.GetComponentsInChildren<Renderer>(true);
var has = false;
var bounds = new Bounds(root.transform.position, Vector3.zero);
foreach (var r in rends)
{
if (r == null) continue;
if (!has) { bounds = r.bounds; has = true; }
else bounds.Encapsulate(r.bounds);
}
if (!has) bounds = new Bounds(root.transform.position, Vector3.one);
return bounds;
}
private void FrameToBounds(Bounds b)
{
if (_viewCamera == null) return;
var center = b.center;
var extents = b.extents;
float radius = Mathf.Max(extents.x, Mathf.Max(extents.y, Mathf.Max(extents.z, 0.001f)));
float fovRad = _viewCamera.fieldOfView * Mathf.Deg2Rad;
float dist = radius / Mathf.Sin(fovRad * 0.5f);
dist = Mathf.Clamp(dist, 1f, 1e4f);
_viewCamera.transform.position = center + new Vector3(1, 0.5f, 1).normalized * dist;
_viewCamera.transform.LookAt(center);
_orbitTarget = center;
_orbitDistance = Vector3.Distance(_viewCamera.transform.position, _orbitTarget);
var dir = (_viewCamera.transform.position - _orbitTarget).normalized;
_yaw = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg;
_pitch = Mathf.Asin(dir.y) * Mathf.Rad2Deg;
}
private string BuildFullPath(Transform node, Transform root)
{
var stack = new Stack<string>();
var current = node;
while (current != null && current != root)
{
stack.Push(current.name);
current = current.parent;
}
return "/" + string.Join('/', stack);
}
private void CacheOriginalMaterials(GameObject go)
{
var rends = go.GetComponentsInChildren<Renderer>(true);
if (rends.Length == 0) return;
for (int i = 0; i < rends.Length; i++)
{
var r = rends[i];
if (r == null) continue;
if (_originalSharedByRenderer.ContainsKey(r)) continue;
_originalSharedByRenderer[r] = r.sharedMaterials;
}
}
}
}