348 lines
12 KiB
C#
348 lines
12 KiB
C#
|
using Thousandto.Core.Asset;
|
|||
|
using Thousandto.Core.Base;
|
|||
|
using Thousandto.Editor.Excel;
|
|||
|
using Thousandto.Launcher.ExternalLibs;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.IO;
|
|||
|
using System.Text;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using UnityEditor;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
namespace Thousandto.Editor.ModelEditor
|
|||
|
{
|
|||
|
public static class ModelEditorShowModel
|
|||
|
{
|
|||
|
private static GameObject _rootGo = null;
|
|||
|
private static GameObject _rootModel = null;
|
|||
|
private static Material _material = null;
|
|||
|
private static List<GameObject> _vfxList = new List<GameObject>();
|
|||
|
|
|||
|
private static Camera _camera = null;
|
|||
|
private static RenderTexture _cameraTarget = null;
|
|||
|
|
|||
|
private static float _cameraHeight = 0f;
|
|||
|
private static float _cameraDis = 10f;
|
|||
|
private static float _minCameraDIs = 0f;
|
|||
|
private static float _maxCameraDIs = 10f;
|
|||
|
|
|||
|
private static AnimListBaseScript _animation = null;
|
|||
|
private static AnimationPlayer _animPlayer = null;
|
|||
|
private static List<string> _animList = new List<string>();
|
|||
|
|
|||
|
private static Texture _normalMainTex = null;
|
|||
|
private static Texture _normalMaskTex = null;
|
|||
|
|
|||
|
public static RenderTexture CameraTargetTex
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _cameraTarget;
|
|||
|
}
|
|||
|
}
|
|||
|
public static List<string> AnimList
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return _animList;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void Init(Vector2 size)
|
|||
|
{
|
|||
|
UnInit();
|
|||
|
|
|||
|
_rootGo = new GameObject("[ModelEditorModelRoot]");
|
|||
|
GameObject cameraGo = new GameObject("[ModelEditorCamera]");
|
|||
|
UnityUtils.Reset(cameraGo.transform);
|
|||
|
_camera = UnityUtils.RequireComponent<Camera>(cameraGo);
|
|||
|
_camera.farClipPlane = 500;
|
|||
|
_camera.clearFlags = CameraClearFlags.SolidColor;
|
|||
|
_camera.backgroundColor = new Color(0.176f, 0.176f, 0.176f, 1f);
|
|||
|
_camera.enabled = true;
|
|||
|
|
|||
|
_camera.transform.position = new Vector3(0f, _cameraHeight, _cameraDis);
|
|||
|
_camera.transform.forward = new Vector3(0f, 0f, -1f);
|
|||
|
_cameraTarget = RenderTexture.GetTemporary((int)size.x * 2, (int)size.y * 2, 32, RenderTextureFormat.RGB565);
|
|||
|
_cameraTarget.name = "RenderTexture81";
|
|||
|
_cameraTarget.anisoLevel = 1;
|
|||
|
_camera.targetTexture = _cameraTarget;
|
|||
|
}
|
|||
|
public static void UnInit()
|
|||
|
{
|
|||
|
DestoryModel();
|
|||
|
if (_camera != null)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_camera.gameObject);
|
|||
|
}
|
|||
|
_camera = null;
|
|||
|
|
|||
|
if (_cameraTarget != null)
|
|||
|
{
|
|||
|
RenderTexture.ReleaseTemporary(_cameraTarget);
|
|||
|
}
|
|||
|
_cameraTarget = null;
|
|||
|
|
|||
|
if(_rootGo != null)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_rootGo);
|
|||
|
}
|
|||
|
_rootGo = null;
|
|||
|
}
|
|||
|
|
|||
|
public static void SetModel(ModelData modelCfg)
|
|||
|
{
|
|||
|
DestoryModel();
|
|||
|
|
|||
|
var go = ResourcesEx.Load(AssetUtils.GetModelAssetPath(modelCfg.ModelTypeCode, modelCfg.ModelID)) as GameObject;
|
|||
|
if (go == null)
|
|||
|
return;
|
|||
|
_rootModel = GameObject.Instantiate(go) as GameObject;
|
|||
|
var render = FindSelfAndChildCommpent<Renderer>(_rootModel);
|
|||
|
if(render == null)
|
|||
|
{
|
|||
|
DestoryModel();
|
|||
|
return;
|
|||
|
}
|
|||
|
_material = render.material;
|
|||
|
_rootModel.transform.parent = _rootGo.transform;
|
|||
|
_normalMainTex = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GetAssetPath(_material.mainTexture));
|
|||
|
|
|||
|
var tex = _material.GetTexture(ShaderPropertyNameDefine.CN_SPN_MASKTEX);
|
|||
|
_normalMaskTex = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GetAssetPath(tex));
|
|||
|
UnityUtils.Reset(_rootModel.transform);
|
|||
|
|
|||
|
var collider = FindSelfAndChildCommpent<CapsuleCollider>(_rootModel);
|
|||
|
if(collider != null)
|
|||
|
{
|
|||
|
_cameraHeight = collider.height / 2;
|
|||
|
_cameraDis = collider.height;
|
|||
|
_camera.transform.position = new Vector3(0f, _cameraHeight, _cameraDis);
|
|||
|
|
|||
|
_rootGo.transform.position = new Vector3(0f, _cameraHeight, 0f);
|
|||
|
_rootGo.transform.eulerAngles = Vector3.zero;
|
|||
|
_rootModel.transform.localPosition = new Vector3(0f, -_cameraHeight, 0f);
|
|||
|
_maxCameraDIs = _cameraDis * 5;
|
|||
|
}
|
|||
|
|
|||
|
_animList.Clear();
|
|||
|
_animPlayer = null;
|
|||
|
var animList = FindSelfAndChildCommpent<AnimListScript>(_rootModel);
|
|||
|
if (animList != null)
|
|||
|
{
|
|||
|
_animation = UnityUtils.RequireComponent<AnimListBaseScript>(animList.gameObject);
|
|||
|
_animation.AddClips(AnimInfo.GetClips(_animation.AnimAssetPathDic.Values));
|
|||
|
|
|||
|
var clipList = _animation.AnimList;
|
|||
|
for (int i = 0; i < clipList.Count; ++i)
|
|||
|
{
|
|||
|
_animList.Add(clipList[i].name);
|
|||
|
}
|
|||
|
_animPlayer = new AnimationPlayer(_animation);
|
|||
|
PlayAnim("idle");
|
|||
|
}
|
|||
|
LoadVfx(modelCfg);
|
|||
|
SetupShader(modelCfg);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
public static void Update(float dt)
|
|||
|
{
|
|||
|
if(_animPlayer != null)
|
|||
|
{
|
|||
|
_animPlayer.Update(dt);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void LoadVfx(ModelData modelCfg)
|
|||
|
{
|
|||
|
for (int i = 0; i < _vfxList.Count; ++i)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_vfxList[i]);
|
|||
|
}
|
|||
|
_vfxList.Clear();
|
|||
|
|
|||
|
for (int i = 0; i < modelCfg.VfxList.Count; ++i)
|
|||
|
{
|
|||
|
var vfxCfg = modelCfg.VfxList[i];
|
|||
|
if (vfxCfg.VfxID <= 0)
|
|||
|
continue;
|
|||
|
var go = ResourcesEx.Load(AssetUtils.GetModelAssetPath(vfxCfg.VfxModeType, vfxCfg.VfxID)) as GameObject;
|
|||
|
if (go == null)
|
|||
|
continue;
|
|||
|
var vfx = GameObject.Instantiate(go) as GameObject;
|
|||
|
var slot = SlotUtils.GetSlotTransform(_rootModel.transform, (Slot)vfxCfg.VfxSlot);
|
|||
|
vfx.transform.parent = slot;
|
|||
|
UnityUtils.Reset(vfx.transform);
|
|||
|
_vfxList.Add(vfx);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void SetOutLine(ModelData modelCfg)
|
|||
|
{
|
|||
|
if(_material != null)
|
|||
|
{
|
|||
|
//_material.SetColor(ShaderPropertyNameDefine.CN_SPN_OUTLINECOLOR, modelCfg.OutLineColor);
|
|||
|
//_material.SetFloat(ShaderPropertyNameDefine.CN_SPN_OUTLINE, modelCfg.OutLineSize);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void SetMainTex(ModelData modelCfg)
|
|||
|
{
|
|||
|
if (_material != null)
|
|||
|
{
|
|||
|
Texture2D tex = null;
|
|||
|
if (!string.IsNullOrEmpty(modelCfg.MainTexture))
|
|||
|
{
|
|||
|
var path = Application.dataPath + "/GameAssets/Resources/Texture/Shader/" + modelCfg.MainTexture + ".tga";
|
|||
|
if (!File.Exists(path))
|
|||
|
{
|
|||
|
path = Application.dataPath + "/GameAssets/Resources/Texture/Shader/" + modelCfg.MainTexture + ".png";
|
|||
|
if (!File.Exists(path))
|
|||
|
{
|
|||
|
path = Application.dataPath + "/GameAssets/Resources/Texture/Shader/" + modelCfg.MainTexture + ".jpg";
|
|||
|
}
|
|||
|
}
|
|||
|
path = path.Replace('\\', '/');
|
|||
|
path = path.Substring(path.IndexOf("Assets"));
|
|||
|
tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
|
|||
|
}
|
|||
|
if(tex != null)
|
|||
|
{
|
|||
|
_material.mainTexture = tex;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_material.mainTexture = _normalMainTex;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void SetMaskTex(ModelData modelCfg)
|
|||
|
{
|
|||
|
if (_material != null)
|
|||
|
{
|
|||
|
Texture2D tex = null;
|
|||
|
if (!string.IsNullOrEmpty(modelCfg.MaskTexture))
|
|||
|
{
|
|||
|
var path = Application.dataPath + "/GameAssets/Resources/Texture/Shader/" + modelCfg.MaskTexture + ".tga";
|
|||
|
if (!File.Exists(path))
|
|||
|
{
|
|||
|
path = Application.dataPath + "/GameAssets/Resources/Texture/Shader/" + modelCfg.MaskTexture + ".png";
|
|||
|
if (!File.Exists(path))
|
|||
|
{
|
|||
|
path = Application.dataPath + "/GameAssets/Resources/Texture/Shader/" + modelCfg.MaskTexture + ".jpg";
|
|||
|
}
|
|||
|
}
|
|||
|
path = path.Replace('\\', '/');
|
|||
|
path = path.Substring(path.IndexOf("Assets"));
|
|||
|
tex = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
|
|||
|
}
|
|||
|
if (tex != null)
|
|||
|
{
|
|||
|
_material.SetTexture(ShaderPropertyNameDefine.CN_SPN_MASKTEX, tex);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
_material.SetTexture(ShaderPropertyNameDefine.CN_SPN_MASKTEX, _normalMaskTex);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void SetupShader(ModelData modelCfg)
|
|||
|
{
|
|||
|
if (_material != null)
|
|||
|
{
|
|||
|
if(modelCfg.ShaderParam != null)
|
|||
|
{
|
|||
|
if(_material.shader.name != modelCfg.ShaderParam.ShaderName)
|
|||
|
{
|
|||
|
//重新设置shader
|
|||
|
_material.shader = Shader.Find(modelCfg.ShaderParam.ShaderName);
|
|||
|
}
|
|||
|
modelCfg.ShaderParam.SetUpShader(_material);
|
|||
|
}
|
|||
|
SetOutLine(modelCfg);
|
|||
|
SetMainTex(modelCfg);
|
|||
|
SetMaskTex(modelCfg);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void PlayAnim(string animName)
|
|||
|
{
|
|||
|
if (_animPlayer == null)
|
|||
|
return;
|
|||
|
_animPlayer.Play(animName, AnimationPartType.AllBody, WrapMode.Loop);
|
|||
|
}
|
|||
|
|
|||
|
public static void OnDrag(Vector2 dt)
|
|||
|
{
|
|||
|
if (_rootGo == null)
|
|||
|
return;
|
|||
|
var elurAngle = _rootGo.transform.eulerAngles;
|
|||
|
elurAngle.y -= dt.x;
|
|||
|
_rootGo.transform.eulerAngles = elurAngle;
|
|||
|
}
|
|||
|
|
|||
|
public static void OnScrollWheel(float dt)
|
|||
|
{
|
|||
|
_cameraDis += dt * 0.1f;
|
|||
|
if(_cameraDis < _minCameraDIs)
|
|||
|
{
|
|||
|
_cameraDis = _minCameraDIs;
|
|||
|
}
|
|||
|
if(_cameraDis > _maxCameraDIs)
|
|||
|
{
|
|||
|
_cameraDis = _maxCameraDIs;
|
|||
|
}
|
|||
|
_camera.transform.position = new Vector3(0f, _cameraHeight, _cameraDis);
|
|||
|
}
|
|||
|
private static T FindSelfAndChildCommpent<T>(GameObject go) where T : Component
|
|||
|
{
|
|||
|
T result = null;
|
|||
|
result = go.GetComponent<T>();
|
|||
|
if (result == null)
|
|||
|
{
|
|||
|
for (int i = 0; i < go.transform.childCount; ++i)
|
|||
|
{
|
|||
|
var childGo = go.transform.GetChild(i).gameObject;
|
|||
|
result = childGo.GetComponent<T>();
|
|||
|
if (result != null)
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
return result;
|
|||
|
}
|
|||
|
public static void DestoryModel()
|
|||
|
{
|
|||
|
if (_rootModel != null)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_rootModel);
|
|||
|
}
|
|||
|
_rootModel = null;
|
|||
|
if (_material != null)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_material);
|
|||
|
}
|
|||
|
_material = null;
|
|||
|
|
|||
|
for (int i = 0; i < _vfxList.Count; ++i)
|
|||
|
{
|
|||
|
GameObject.DestroyImmediate(_vfxList[i]);
|
|||
|
}
|
|||
|
_vfxList.Clear();
|
|||
|
}
|
|||
|
|
|||
|
public static void Render()
|
|||
|
{
|
|||
|
if(_camera != null)
|
|||
|
{
|
|||
|
_camera.Render();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|