Advertisement
KaiClavier

STMOutlineGenerator.cs

Jun 30th, 2021 (edited)
1,515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /*
  5.  
  6. Make SURE that outlineParent is a gameObject that's is above STM in the hierarchy for UI elements.
  7. Script isn't perfect yet, sometimes gameobjects don't get deleted when they should, but it seems to work fine in Play mode.
  8.  
  9. */
  10.  
  11. [ExecuteInEditMode]
  12. public class STMOutlineGenerator : MonoBehaviour {
  13.  
  14.     public SuperTextMesh superTextMesh;
  15.     [Tooltip("Make sure this is ABOVE your STM in the hierarchy for UI objects!!")]
  16.     public Transform outlineParent;
  17.     [Range(0,16)]
  18.     public int detailLevel = 8;
  19.     public float size = 0.05f;
  20.     public Color32 color;
  21.     [Tooltip("Offset text with this")]
  22.     public Vector3 offset = new Vector3(0f,0f,0.01f);
  23.     public bool updateEveryFrame = true;
  24.     //public float distance = 0.001f;
  25.  
  26.     private Mesh sharedOutlineMesh = null;
  27.     private GameObject[] outlineObjects = new GameObject[0];
  28.  
  29.     [System.Serializable]
  30.     public class OutlineRenderer
  31.     {
  32.         public Transform transform;
  33.         public Vector3 offset;
  34.         public GameObject gameObject;
  35.         public MeshFilter meshFilter;
  36.         public MeshRenderer meshRenderer;
  37.         public CanvasRenderer canvasRenderer;
  38.     }
  39.     private OutlineRenderer[] allRenderers = new OutlineRenderer[0];
  40.  
  41.     private OutlineRenderer newRenderer;
  42.  
  43.     public void Reset()
  44.     {
  45.         superTextMesh = GetComponent<SuperTextMesh>();
  46.        
  47.     }
  48.     public void OnEnable()
  49.     {
  50.         if(Application.isPlaying)
  51.         {
  52.             superTextMesh.OnRebuildEvent += GenerateOutlines;
  53.         }
  54.     }
  55.     public void OnDisable()
  56.     {
  57.         if(Application.isPlaying)
  58.         {
  59.             superTextMesh.OnRebuildEvent -= GenerateOutlines;
  60.         }
  61.     }
  62.     private bool validate;
  63.     public void OnValidate()
  64.     {
  65.         validate = true;
  66.     }
  67.     public void Update()
  68.     {  
  69.         if(!Application.isPlaying && validate)
  70.         {
  71.             validate = false;
  72.             GenerateOutlines();
  73.         }
  74.     }
  75.     public void LateUpdate()
  76.     {
  77.         if(Application.isPlaying && updateEveryFrame)
  78.         {
  79.             RefreshOutlines();
  80.         }
  81.     }
  82.     public void GenerateOutlines(Vector3[] verts, Vector3[] middles, Vector3[] positions)
  83.     {
  84.         GenerateOutlines();
  85.     }
  86.     public void RefreshOutlines()
  87.     {  
  88.         for(int i=0; i<detailLevel; i++)
  89.         {
  90.             if(superTextMesh.uiMode)
  91.             {
  92.                 allRenderers[i].canvasRenderer.SetMesh(superTextMesh.textMesh);
  93.             }
  94.             else
  95.             {
  96.                 CloneTextMesh();
  97.                 allRenderers[i].meshFilter.sharedMesh = sharedOutlineMesh;
  98.             }
  99.         }
  100.     }
  101.     private void CloneTextMesh()
  102.     {
  103.         sharedOutlineMesh = new Mesh();
  104.         if(superTextMesh.textMesh == null)
  105.             return;
  106.        
  107.         sharedOutlineMesh.vertices = superTextMesh.textMesh.vertices;
  108.         //sharedOutlineMesh.triangles = superTextMesh.textMesh.triangles;
  109.         sharedOutlineMesh.normals = superTextMesh.textMesh.normals;
  110.         sharedOutlineMesh.uv = superTextMesh.textMesh.uv;
  111.         sharedOutlineMesh.uv2 = superTextMesh.textMesh.uv2;
  112.         sharedOutlineMesh.subMeshCount = superTextMesh.textMesh.subMeshCount;
  113.  
  114.         //use submeshes instead of setting triangles for entire mesh:
  115.         for(int i=0, iL=sharedOutlineMesh.subMeshCount; i<iL; i++)
  116.         {
  117.             sharedOutlineMesh.SetTriangles(superTextMesh.textMesh.GetTriangles(i), i); //apply to mesh
  118.         }
  119.  
  120.         //colors
  121.         colors = superTextMesh.textMesh.colors32;
  122.         for(int j=0; j<colors.Length; j++)
  123.         {
  124.             colors[j] = color; //assign outline color
  125.         }
  126.         sharedOutlineMesh.colors32 = colors;
  127.     }
  128.  
  129.     private Color32[] colors;
  130.     public void GenerateOutlines()
  131.     {
  132.         if(superTextMesh != null && outlineParent != null)
  133.         {
  134.             sharedOutlineMesh = null; //clear last mesh
  135.  
  136.             for(int i=0; i<outlineObjects.Length; i++)
  137.             {
  138.                 if(Application.isPlaying)
  139.                 {
  140.                     Destroy(outlineObjects[i]);
  141.                 }
  142.                 else
  143.                 {
  144.                     DestroyImmediate(outlineObjects[i]);
  145.                 }
  146.             }
  147.             foreach(Transform child in outlineParent)
  148.             {
  149.                 //destroy all
  150.                 if(Application.isPlaying)
  151.                 {
  152.                     Destroy(child.gameObject);
  153.                 }
  154.                 else
  155.                 {
  156.                     DestroyImmediate(child.gameObject);
  157.                 }
  158.             }
  159.  
  160.             //recreate all
  161.             allRenderers = new OutlineRenderer[detailLevel];
  162.             outlineObjects = new GameObject[detailLevel];
  163.             for(int i=0; i<allRenderers.Length; i++)
  164.             {
  165.                 //create outline gameobjects
  166.                 newRenderer = new OutlineRenderer();
  167.                 newRenderer.gameObject = new GameObject();
  168.                 outlineObjects[i] = newRenderer.gameObject;
  169.                 newRenderer.transform = newRenderer.gameObject.transform;
  170.                 newRenderer.transform.name = "Outline";
  171.                 newRenderer.transform.SetParent(outlineParent); //parent to STM
  172.                
  173.  
  174.                 //UI text
  175.                 if(superTextMesh.uiMode && superTextMesh.c.GetMaterial() != null)
  176.                 {
  177.                     newRenderer.canvasRenderer = newRenderer.gameObject.AddComponent<CanvasRenderer>();
  178.                     newRenderer.canvasRenderer.SetMesh(superTextMesh.textMesh);
  179.                     if(superTextMesh.c.GetMaterial().HasProperty("_MainTex"))
  180.                     {
  181.                         newRenderer.canvasRenderer.SetTexture(superTextMesh.c.GetMaterial().GetTexture("_MainTex"));
  182.                        
  183.                         newRenderer.canvasRenderer.materialCount = 1;
  184.                         newRenderer.canvasRenderer.SetMaterial(superTextMesh.c.GetMaterial(), 0);
  185.                     }
  186.                     newRenderer.canvasRenderer.SetColor(color);
  187.                 }
  188.                 //regular text
  189.                 else if(superTextMesh.r.sharedMaterials != null)
  190.                 {
  191.                    
  192.                     newRenderer.meshFilter = newRenderer.gameObject.AddComponent<MeshFilter>();
  193.                     newRenderer.meshRenderer = newRenderer.gameObject.AddComponent<MeshRenderer>();
  194.                    
  195.  
  196.                     if(sharedOutlineMesh == null)
  197.                     {
  198.                         CloneTextMesh();
  199.                     }
  200.  
  201.                     newRenderer.meshFilter.sharedMesh = sharedOutlineMesh;
  202.                     newRenderer.meshRenderer.sharedMaterials = superTextMesh.r.sharedMaterials;
  203.  
  204.        
  205.                 }
  206.                 //give it an offset... for now uhh
  207.                 newRenderer.offset.x = (superTextMesh.t.position.x + Mathf.Cos(Mathf.PI * 2f * ((float)i/detailLevel)) * size) + offset.x;
  208.                 newRenderer.offset.y = (superTextMesh.t.position.y + Mathf.Sin(Mathf.PI * 2f * ((float)i/detailLevel)) * size) + offset.y;
  209.                 newRenderer.offset.z = (superTextMesh.t.position.z) + offset.z;
  210.  
  211.                 newRenderer.transform.position = newRenderer.offset;
  212.  
  213.                 //assign to array
  214.                 allRenderers[i] = newRenderer;
  215.             }
  216.         }
  217.     }
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement