123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace UnityEditor.U2D.Animation
- {
- internal class MeshPreviewTool : BaseTool
- {
- [SerializeField]
- private Material m_Material;
- private List<SpriteCache> m_Sprites;
- private IMeshPreviewBehaviour m_DefaultPreviewBehaviour = new DefaultPreviewBehaviour();
- public IMeshPreviewBehaviour previewBehaviourOverride { get; set; }
- public override IMeshPreviewBehaviour previewBehaviour
- {
- get
- {
- if (previewBehaviourOverride != null)
- return previewBehaviourOverride;
- return m_DefaultPreviewBehaviour;
- }
- }
- internal override void OnCreate()
- {
- m_Material = new Material(Shader.Find("Hidden/SkinningModule-GUITextureClip"));
- m_Material.hideFlags = HideFlags.DontSave;
- }
- internal override void OnDestroy()
- {
- base.OnDestroy();
- Debug.Assert(m_Material != null);
- DestroyImmediate(m_Material);
- }
- protected override void OnActivate()
- {
- m_Sprites = new List<SpriteCache>(skinningCache.GetSprites());
- DirtyMeshesAll();
- skinningCache.events.meshChanged.AddListener(MeshChanged);
- skinningCache.events.characterPartChanged.AddListener(CharacterPartChanged);
- skinningCache.events.skeletonPreviewPoseChanged.AddListener(SkeletonChanged);
- skinningCache.events.skeletonBindPoseChanged.AddListener(SkeletonChanged);
- skinningCache.events.skinningModeChanged.AddListener(SkinningModuleModeChanged);
- }
- protected override void OnDeactivate()
- {
- skinningCache.events.meshChanged.RemoveListener(MeshChanged);
- skinningCache.events.skeletonPreviewPoseChanged.RemoveListener(SkeletonChanged);
- skinningCache.events.skeletonBindPoseChanged.RemoveListener(SkeletonChanged);
- skinningCache.events.skinningModeChanged.RemoveListener(SkinningModuleModeChanged);
- }
- protected override void OnGUI()
- {
- Prepare();
- if (Event.current.type == EventType.Repaint)
- {
- //DrawDefaultSpriteMeshes();
- DrawSpriteMeshes();
- }
- }
- public void DrawOverlay()
- {
- if (Event.current.type != EventType.Repaint)
- return;
- if (skinningCache.mode == SkinningMode.SpriteSheet)
- {
- foreach (var sprite in m_Sprites)
- {
- if (previewBehaviour.Overlay(sprite))
- DrawSpriteMesh(sprite);
- }
- }
- else
- {
- var character = skinningCache.character;
- Debug.Assert(character != null);
- var parts = character.parts;
- foreach (var part in parts)
- {
- if (part.isVisible && previewBehaviour.Overlay(part.sprite))
- DrawSpriteMesh(part.sprite);
- }
- }
- }
- public void OverlayWireframe()
- {
- if (Event.current.type != EventType.Repaint)
- return;
- foreach (var sprite in m_Sprites)
- if (previewBehaviour.OverlayWireframe(sprite))
- DrawWireframe(sprite);
- }
- private void CharacterPartChanged(CharacterPartCache characterPart)
- {
- var meshPreview = characterPart.sprite.GetMeshPreview();
- Debug.Assert(meshPreview != null);
- meshPreview.SetSkinningDirty();
- }
- private void MeshChanged(MeshCache mesh)
- {
- var meshPreview = mesh.sprite.GetMeshPreview();
- Debug.Assert(meshPreview != null);
- meshPreview.SetMeshDirty();
- }
- private void SkeletonChanged(SkeletonCache skeleton)
- {
- DirtySkinningAll();
- }
- private void SkinningModuleModeChanged(SkinningMode mode)
- {
- DirtyMeshesAll();
- }
- private void DirtyMeshesAll()
- {
- foreach (var sprite in m_Sprites)
- {
- var meshPreview = sprite.GetMeshPreview();
- if (meshPreview != null)
- meshPreview.SetMeshDirty();
- }
- }
- private void DirtySkinningAll()
- {
- foreach (var sprite in m_Sprites)
- {
- var meshPreview = sprite.GetMeshPreview();
- Debug.Assert(meshPreview != null);
- meshPreview.SetSkinningDirty();
- }
- }
- private void Prepare()
- {
- foreach (var sprite in m_Sprites)
- {
- var meshPreview = sprite.GetMeshPreview();
- Debug.Assert(meshPreview != null);
- meshPreview.enableSkinning = true;
- meshPreview.Prepare();
- }
- }
- private void DrawDefaultSpriteMeshes()
- {
- Debug.Assert(Event.current.type == EventType.Repaint);
- if (skinningCache.mode == SkinningMode.SpriteSheet)
- {
- foreach (var sprite in m_Sprites)
- DrawDefaultSpriteMesh(sprite);
- }
- else
- {
- var character = skinningCache.character;
- Debug.Assert(character != null);
- var parts = character.parts;
- foreach (var part in parts)
- {
- if (part.isVisible)
- DrawDefaultSpriteMesh(part.sprite);
- }
- }
- }
- private void DrawDefaultSpriteMesh(SpriteCache sprite)
- {
- Debug.Assert(m_Material != null);
- var meshPreview = sprite.GetMeshPreview();
- var meshCache = sprite.GetMesh();
- var skeleton = skinningCache.GetEffectiveSkeleton(sprite);
- Debug.Assert(meshPreview != null);
- if (meshPreview.canSkin == false || skeleton.isPosePreview == false)
- {
- m_Material.mainTexture = meshCache.textureDataProvider.texture;
- m_Material.SetFloat("_Opacity", 1f);
- m_Material.SetFloat("_VertexColorBlend", 0f);
- m_Material.color = new Color(1f, 1f, 1f, 1f);
- DrawingUtility.DrawMesh(meshPreview.defaultMesh, m_Material, sprite.GetLocalToWorldMatrixFromMode());
- }
- }
- private void DrawSpriteMeshes()
- {
- Debug.Assert(Event.current.type == EventType.Repaint);
- if (skinningCache.mode == SkinningMode.SpriteSheet)
- {
- foreach (var sprite in m_Sprites)
- {
- if (previewBehaviour.Overlay(sprite))
- continue;
- DrawSpriteMesh(sprite);
- }
- }
- else
- {
- var character = skinningCache.character;
- Debug.Assert(character != null);
- var parts = character.parts;
- var selected = skinningCache.selectedSprite;
- var selectedVisible = false;
- foreach (var part in parts)
- {
- if (previewBehaviour.Overlay(part.sprite))
- continue;
- if (part.sprite == selected)
- selectedVisible = part.isVisible;
- else if (part.isVisible)
- DrawSpriteMesh(part.sprite);
- }
- if (selectedVisible && selected != null)
- DrawSpriteMesh(selected);
- }
- }
- private void DrawSpriteMesh(SpriteCache sprite)
- {
- var weightMapOpacity = previewBehaviour.GetWeightMapOpacity(sprite);
- DrawSpriteMesh(sprite, weightMapOpacity);
- if (previewBehaviour.DrawWireframe(sprite))
- DrawWireframe(sprite);
- }
- private void DrawSpriteMesh(SpriteCache sprite, float weightMapOpacity)
- {
- Debug.Assert(m_Material != null);
- var meshPreview = sprite.GetMeshPreview();
- var meshCache = sprite.GetMesh();
- Debug.Assert(meshPreview != null);
- if (meshPreview.mesh == null || meshPreview.mesh.vertexCount == 0)
- {
- DrawDefaultSpriteMesh(sprite);
- }
- else
- {
- m_Material.mainTexture = meshCache.textureDataProvider.texture;
- m_Material.SetFloat("_Opacity", 1f);
- m_Material.SetFloat("_VertexColorBlend", weightMapOpacity);
- m_Material.color = Color.white;
- DrawingUtility.DrawMesh(meshPreview.mesh, m_Material, sprite.GetLocalToWorldMatrixFromMode());
- }
- }
- private void DrawSelectedSpriteWeightMap()
- {
- var selectedSprite = skinningCache.selectedSprite;
- if (selectedSprite != null)
- {
- var opacity = GetWeightOpacityFromCurrentTool();
- if (opacity > 0f)
- DrawSpriteMesh(selectedSprite, opacity);
- }
- }
- private float GetWeightOpacityFromCurrentTool()
- {
- return IsWeightTool() ? VisibilityToolSettings.meshOpacity : 0f;
- }
- private bool IsWeightTool()
- {
- var currentTool = skinningCache.selectedTool;
- if (currentTool == skinningCache.GetTool(Tools.WeightSlider) ||
- currentTool == skinningCache.GetTool(Tools.WeightBrush) ||
- currentTool == skinningCache.GetTool(Tools.BoneInfluence) ||
- currentTool == skinningCache.GetTool(Tools.GenerateWeights))
- return true;
- return false;
- }
- private void DrawWireframe(SpriteCache sprite)
- {
- Debug.Assert(Event.current.type == EventType.Repaint);
- Debug.Assert(sprite != null);
- var meshPreview = sprite.GetMeshPreview();
- Debug.Assert(meshPreview != null);
- m_Material.mainTexture = null;
- m_Material.SetFloat("_Opacity", 0.35f);
- m_Material.SetFloat("_VertexColorBlend", 0f);
- m_Material.color = Color.white;
- GL.wireframe = true;
- DrawingUtility.DrawMesh(meshPreview.mesh, m_Material, sprite.GetLocalToWorldMatrixFromMode());
- GL.wireframe = false;
- }
- }
- }
|