123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Ximmerse.XR.Utils
- {
- /// <summary>
- /// GL draw helper.
- /// </summary>
- public class RxDraw : MonoBehaviour
- {
- public enum RxDrawAlignment
- {
- LeftBottom = 0,
- Center = 1,
- RightTop = 2,
- }
- public enum RxCameraSpaceType
- {
- ViewPort = 0,
- ScreenSpace = 1,
- WorldSpace = 2,
- }
- private struct RxDrawRequest
- {
- internal PEDrawType drawType;
- internal Vector3 position;
- internal Vector3 position2;
- internal Quaternion rotation;
- internal float scale;
- internal Color color;
- internal Color wireColor;
- internal bool noDepth;
- internal Vector3 v3Size;
- internal float endTime;
- internal Material customMaterial;
- internal int customMaterialPass;
- internal Vector2Int scaleInt;
- internal Mesh meshReference;
- internal RxDrawAlignment alignment;
- internal Camera targetCamera;
- internal RxCameraSpaceType GUISpaceType;
- /// <summary>
- /// UI Verts : data structure wrap for UI/Text drawing
- /// </summary>
- internal UIVertex[] uiVerts;
- }
- private enum PEDrawType
- {
- Line,
- Sphere,
- WireSphere,
- WiredSphere,
- Cube,
- WireCube,
- WiredCube,
- Cuboid,
- WireCuboid,
- WiredCuboid,
- Quad,
- WireQuad,
- WiredQuad,
- TranslateGizmos,
- RotationGizmos,
- ScalingGizmos,
- Plane,
- WirePlane,
- WiredPlane,
- Grids,
- Arrow,
- Cone,
- Cylinder,
- Pyramid,
- Mesh,
- /// <summary>
- /// Draw a human skeleton, where original model height = 1m.
- /// </summary>
- HumanSkeleton,
- /// <summary>
- /// Draw a human skeleton line, where original model height = 1m
- /// </summary>
- HumanSkeletonLine,
- /// <summary>
- /// Draw a human skeleton, where original model width = 1m
- /// </summary>
- DogSkeleton,
- /// <summary>
- /// Draw a human skeleton line, where original model width = 1m
- /// </summary>
- DogSkeletonLine,
- MeshWireFrame,
- /// <summary>
- /// Draws a GUI circle.
- /// </summary>
- GUICircle,
- /// <summary>
- /// Draws full screen rect.
- /// </summary>
- ScreenRect,
- /// <summary>
- /// Draws 3d text mesh
- /// </summary>
- TextMesh3D,
- WireCone,
- }
- static List<RxDrawRequest> sDrawRequests = new List<RxDrawRequest>();
- static bool m_DepthRendering = false;
- public static bool DepthRendering
- {
- get
- {
- return m_DepthRendering;
- }
- set
- {
- m_DepthRendering = value;
- }
- }
- static TextGenerator sTextGen = null;
- static TextGenerator TextGen
- {
- get
- {
- if (sTextGen == null)
- sTextGen = new TextGenerator();
- return sTextGen;
- }
- }
- static List<UIVertex> sTextVertices = new List<UIVertex>();
- static List<Vector3> sTextMeshVertices = new List<Vector3>();
- static List<Vector3> sTextMeshNormals = new List<Vector3>();
- static List<Vector4> sTextMeshTangents = new List<Vector4>();
- static List<int> sTextMeshTris = new List<int>();
- static List<Vector2> sTextMeshUVs = new List<Vector2>();
- static List<Color> sTextMeshColor = new List<Color>();
- static Material matUI = null;
- static Material MaterialUI
- {
- get
- {
- if (matUI == null)
- matUI = new Material(Shader.Find("UI/Default"));
- return matUI;
- }
- }
- static Mesh sTextMesh = null;
- static Mesh TextMesh
- {
- get
- {
- if (sTextMesh == null)
- {
- sTextMesh = new Mesh();
- sTextMesh.name = "PEDrawTextMesh";
- TextMesh.MarkDynamic();
- }
- return sTextMesh;
- }
- }
- Coroutine clearQueueCoroutine = null;
- private void OnEnable()
- {
- clearQueueCoroutine = StartCoroutine(ClearQueue());
- }
- private void OnDisable()
- {
- if(clearQueueCoroutine != null)
- {
- StopCoroutine(clearQueueCoroutine);
- clearQueueCoroutine = null;
- }
- }
- /// <summary>
- /// Sets the custom material for previous draw command.
- /// </summary>
- /// <param name="CustomMaterial">Custom material.</param>
- /// <param name="Pass">Pass.</param>
- public static void SetCustomMaterial(Material CustomMaterial, int Pass = 0)
- {
- if (sDrawRequests.Count == 0)
- {
- Debug.Log("Draw command queue is empty, can't set custom material.");
- return;
- }
- var drawCommand = sDrawRequests[sDrawRequests.Count - 1];
- drawCommand.customMaterial = CustomMaterial;
- drawCommand.customMaterialPass = Pass;
- sDrawRequests[sDrawRequests.Count - 1] = drawCommand;
- }
- /// <summary>
- /// Draws a line.
- /// </summary>
- /// <param name="start">Start.</param>
- /// <param name="end">End.</param>
- /// <param name="color">Color.</param>
- public static void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawLine(start, end, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Line,
- position = start,
- position2 = end,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a line.
- /// </summary>
- /// <param name="start">Start.</param>
- /// <param name="end">End.</param>
- /// <param name="color">Color.</param>
- public static void DrawLine(Vector3 start, Vector3 end, Color color, float lineWidth, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawLine(start, end, lineWidth, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Line,
- position = start,
- position2 = end,
- color = color,
- scale = lineWidth,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a ray.
- /// </summary>
- /// <param name="start">Start.</param>
- /// <param name="direction">Direction.</param>
- /// <param name="color">Color.</param>
- public static void DrawRay(Vector3 start, Vector3 direction, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawLine(start, start + direction, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Line,
- position = start,
- position2 = start + direction,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a ray.
- /// </summary>
- /// <param name="start">Start.</param>
- /// <param name="direction">Direction.</param>
- /// <param name="color">Color.</param>
- public static void DrawRay(Vector3 start, Vector3 direction, Color color, float lineWidth, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawLine(start, start + direction, lineWidth, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Line,
- position = start,
- position2 = start + direction,
- color = color,
- scale = lineWidth,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a sphere.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawSphere(Vector3 center, float radius, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawSphere(center, Quaternion.identity, radius, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Sphere,
- position = center,
- scale = radius,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wire sphere.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWireSphere(Vector3 center, float radius, Color wireColor, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWireSphere(center, Quaternion.identity, radius, wireColor);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WireSphere,
- position = center,
- scale = radius,
- wireColor = wireColor,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a sphere with wired.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWiredSphere(Vector3 center, float radius, Color color, Color wireColor, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWiredSphere(center, Quaternion.identity, radius, color, wireColor);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WiredSphere,
- position = center,
- scale = radius,
- color = color,
- wireColor = wireColor,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws an arrow.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawArrow(Vector3 center, Quaternion rotation, float tipPivot, float shaftWidth, float tipWidth, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawArrow(center, rotation * Vector3.forward, tipPivot, shaftWidth, tipWidth, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Arrow,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(tipPivot, shaftWidth, tipWidth),
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws mesh directly
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawMesh(Mesh m, Vector3 center, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawMesh(m, center, rotation, scale, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Mesh,
- position = center,
- rotation = rotation,
- v3Size = scale,
- color = color,
- endTime = Time.time + duration,
- meshReference = m,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws mesh wire frame
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawMeshWireframe(Mesh m, Vector3 center, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDrawExt.DrawMeshWireFrame(center, rotation, scale, m, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.MeshWireFrame,
- position = center,
- rotation = rotation,
- v3Size = scale,
- color = color,
- endTime = Time.time + duration,
- meshReference = m,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a cube.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawCube(Vector3 center, Quaternion rotation, float size, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawCube(center, rotation, size, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Cube,
- position = center,
- rotation = rotation,
- scale = size,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a cone.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawCone(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawCone(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Cone,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height),
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wire cone.
- /// </summary>
- public static void DrawWireCone(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWireCone(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WireCone,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height),
- wireColor = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a cylinder.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawCylinder(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawCylinder(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Cylinder,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height),
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a pyramid.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawPyramid(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawPyramid(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Pyramid,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height),
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wire cube.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWireCube(Vector3 center, Quaternion rotation, float size, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWireCube(center, rotation, size, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WireCube,
- position = center,
- rotation = rotation,
- scale = size,
- wireColor = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wired cube.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWiredCube(Vector3 center, Quaternion rotation, float size, Color color, Color wireColor, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWiredCube(center, rotation, size, color, wireColor);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WiredCube,
- position = center,
- rotation = rotation,
- scale = size,
- wireColor = wireColor,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a cuboid.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawCuboid(Vector3 center, Quaternion rotation, Vector3 size, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawCuboid(center, rotation, size, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Cuboid,
- position = center,
- rotation = rotation,
- v3Size = size,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wire Cuboid.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWireCuboid(Vector3 center, Quaternion rotation, Vector3 size, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWireCuboid(center, rotation, size, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WireCuboid,
- position = center,
- rotation = rotation,
- v3Size = size,
- wireColor = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wired Cuboid.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWiredCuboid(Vector3 center, Quaternion rotation, Vector3 size, Color color, Color wireColor, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWiredCuboid(center, rotation, size, color, wireColor);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WiredCuboid,
- position = center,
- rotation = rotation,
- v3Size = size,
- wireColor = wireColor,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a quad.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawQuad(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawQuad(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Quad,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height, 0),
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a quad of wire.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWireQuad(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWireQuad(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WireQuad,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height, 0),
- wireColor = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wired and filled quad.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWiredQuad(Vector3 center, Quaternion rotation, float width, float height, Color color, Color wireColor, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWiredQuad(center, rotation, width, height, color, wireColor);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WiredQuad,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height, 0),
- color = color,
- wireColor = wireColor,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a plane.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawPlane(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawPlane(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Plane,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height, 0),
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a plane of wire.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWirePlane(Vector3 center, Quaternion rotation, float width, float height, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWirePlane(center, rotation, width, height, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WirePlane,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height, 0),
- wireColor = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a wired and filled plane.
- /// </summary>
- /// <param name="center">Center.</param>
- /// <param name="radius">Radius.</param>
- /// <param name="color">Color.</param>
- public static void DrawWiredPlane(Vector3 center, Quaternion rotation, float width, float height, Color color, Color wireColor, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawWiredPlane(center, rotation, width, height, color, wireColor);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.WiredPlane,
- position = center,
- rotation = rotation,
- v3Size = new Vector3(width, height, 0),
- color = color,
- wireColor = wireColor,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws the translate(positional) gizmos.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawTranslateGizmos(Vector3 position, Quaternion rotation, float scale, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawTranslateGizmo(position, rotation, scale);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.TranslateGizmos,
- position = position,
- rotation = rotation,
- scale = scale,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a 3D text mesh.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- /// <param name="text">Text.</param>
- /// <param name="Duration">Duration.</param>
- /// <param name="color">Color.</param>
- /// <param name="font">Font.If null, the default arial is used.</param>
- /// <param name="Style">Style.</param>
- public static void Text3D(Vector3 position, Quaternion rotation, float scale, string text, Color color, float Duration = 0, Font font = null, FontStyle style = default(FontStyle))
- {
- Font drawFont = font != null ? font : Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
- Material drawMaterial = drawFont.material;
- TextGenerationSettings setting = new TextGenerationSettings();
- setting.textAnchor = TextAnchor.MiddleCenter;
- setting.color = color;
- setting.generationExtents = Vector2.zero;
- setting.pivot = new Vector2(0.5f, 0.5f);
- setting.richText = false;
- setting.font = drawFont;
- setting.fontStyle = style;
- setting.fontSize = 1;
- setting.horizontalOverflow = HorizontalWrapMode.Overflow;
- setting.verticalOverflow = VerticalWrapMode.Overflow;
- setting.alignByGeometry = true;
- setting.lineSpacing = 1;
- setting.scaleFactor = 120;
- setting.resizeTextForBestFit = false;
- setting.resizeTextMaxSize = 1;
- setting.resizeTextMinSize = 1;
- TextGen.Invalidate();
- TextGen.Populate(text, setting);
- sTextVertices.Clear();
- // TextGen.GetVertices(sTextVertices);
- UIVertex[] _uiVerts = TextGen.GetVerticesArray();
- float rescaleFactor = scale * 0.01f;
- RxDrawRequest drawTextReq = new RxDrawRequest()
- {
- drawType = PEDrawType.TextMesh3D,
- uiVerts = _uiVerts,
- position = position,
- rotation = rotation,
- scale = rescaleFactor,
- color = color,
- customMaterial = drawMaterial,
- customMaterialPass = 0,
- endTime = Time.time + Duration,
- };
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UIVerticesToTextMesh(new List<UIVertex>(_uiVerts), TextMesh);
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.CustomMaterial = drawMaterial;
- UltiDraw.Begin();
- UltiDraw.DrawMesh(TextMesh, position, rotation, Vector3.one * rescaleFactor, setting.color);
- UltiDraw.End();
- UltiDraw.CustomMaterial = null;
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(drawTextReq);
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- ///// <summary>
- ///// Draws text using default setting:
- ///// alignment = middle center,
- ///// rich text = true,
- ///// Best fit = false,
- ///// Vertical and horizontal wrap mode = Overflow
- ///// </summary>
- ///// <param name="position">Position.</param>
- ///// <param name="rotation">Rotation.</param>
- ///// <param name="scale">Scale.</param>
- ///// <param name="text">Text.</param>
- ///// <param name="setting">Setting.</param>
- ///// <param name="duration">Duration.</param>
- //[System.Obsolete ("Deprecated method, call PEDraw.Text3D()")]
- //public static void DrawText(Vector3 position, Quaternion rotation, Vector3 scale, string text, Font font, FontStyle fontStyle, Color color, float duration = 0)
- //{
- // TextGenerationSettings setting = new TextGenerationSettings();
- // setting.textAnchor = TextAnchor.MiddleCenter;
- // setting.color = color;
- // setting.generationExtents = Vector2.zero;
- // setting.pivot = new Vector2(0.5f, 0.5f);
- // setting.richText = false;
- // setting.font = font;
- // setting.fontStyle = fontStyle;
- // setting.fontSize = 1;
- // setting.horizontalOverflow = HorizontalWrapMode.Overflow;
- // setting.verticalOverflow = VerticalWrapMode.Overflow;
- // setting.alignByGeometry = true;
- // setting.lineSpacing = 1;
- // setting.scaleFactor = 120;
- // setting.resizeTextForBestFit = false;
- // setting.resizeTextMaxSize = 1;
- // setting.resizeTextMinSize = 1;
- // DrawText(position, rotation, scale, text, setting, duration);
- //}
- ///// <summary>
- ///// Draws text using custom setting.
- ///// </summary>
- ///// <param name="position">Position.</param>
- ///// <param name="rotation">Rotation.</param>
- ///// <param name="scale">Scale.</param>
- ///// <param name="text">Text.</param>
- ///// <param name="setting">Setting.</param>
- ///// <param name="color">Color.</param>
- ///// <param name="duration">Duration.</param>
- //[System.Obsolete ("Deprecated method, call PEDraw.Text3D()")]
- //public static void DrawText(Vector3 position, Quaternion rotation, Vector3 scale, string text, TextGenerationSettings setting, float duration = 0)
- //{
- // TextGen.Invalidate();
- // TextGen.Populate(text, setting);
- // sTextVertices.Clear();
- // TextGen.GetVertices(sTextVertices);
- // //Convert UIVertices to Mesh:
- // if (sTextVertices.Count == 0)
- // return;
- // //UIVertice to text mesh:
- // if ((sTextVertices.Count % 4) != 0)
- // {
- // Debug.LogErrorFormat("Error drawing text:{0}. Reason : UIVertices to mesh error: {1} not multiplier of 4 !", text, sTextVertices.Count);
- // return;
- // }
- // if (setting.font == null || setting.font.material == null)
- // {
- // Debug.LogError("Font's invalid !");
- // return;
- // }
- // //Fill up text mesh:
- // UIVerticesToTextMesh(sTextVertices, TextMesh);
- // //For editor call, direct draw:
- // if (Application.isEditor)
- // {
- // UltiDraw.SetDepthRendering(DepthRendering);
- // UltiDraw.CustomMaterial = setting.font.material;
- // UltiDraw.Begin();
- // UltiDraw.DrawMesh(TextMesh, position, rotation, scale * 0.01f, setting.color);
- // UltiDraw.End();
- // UltiDraw.CustomMaterial = null;
- // }
- // if (!Application.isPlaying) return;
- // sDrawRequests.Add(new PEDrawRequest()
- // {
- // drawType = PEDrawType.Mesh,
- // position = position,
- // rotation = rotation,
- // v3Size = scale * 0.01f,
- // endTime = Time.time + duration,
- // meshReference = TextMesh,
- // color = setting.color,
- // customMaterial = setting.font.material,
- // }
- // );
- // if (singleton == null)
- // {
- // InitializeBehavior();
- // }
- //}
- /// <summary>
- /// Convert UI vertices to mesh
- /// </summary>
- /// <param name="verticesLst">Vertices lst.</param>
- /// <param name="TextMesh">Text mesh.</param>
- static void UIVerticesToTextMesh(List<UIVertex> verticesLst, Mesh TextMesh)
- {
- int charCount = (verticesLst.Count / 4);
- sTextMeshVertices.Clear();
- sTextMeshVertices.Capacity = verticesLst.Count;
- sTextMeshTris.Clear();
- sTextMeshTris.Capacity = charCount * 2 * 3;
- sTextMeshUVs.Clear();
- sTextMeshUVs.Capacity = verticesLst.Count;
- sTextMeshColor.Clear();
- sTextMeshColor.Capacity = verticesLst.Count;
- sTextMeshNormals.Clear();
- sTextMeshNormals.Capacity = verticesLst.Count;
- sTextMeshTangents.Clear();
- sTextMeshTangents.Capacity = verticesLst.Count;
- for (int c = 0; c < charCount; c++)
- {
- var c1 = verticesLst[c * 4];
- var c2 = verticesLst[c * 4 + 1];
- var c3 = verticesLst[c * 4 + 2];
- var c4 = verticesLst[c * 4 + 3];
- Vector3 v1 = c1.position;
- Vector3 v2 = c2.position;
- Vector3 v3 = c3.position;
- Vector3 v4 = c4.position;
- sTextMeshVertices.Add(v1);
- sTextMeshVertices.Add(v2);
- sTextMeshVertices.Add(v3);
- sTextMeshVertices.Add(v4);
- int t1 = sTextMeshVertices.Count - 4;
- int t2 = sTextMeshVertices.Count - 3;
- int t3 = sTextMeshVertices.Count - 2;
- int t4 = sTextMeshVertices.Count - 1;
- sTextMeshTris.Add(t1);
- sTextMeshTris.Add(t2);
- sTextMeshTris.Add(t3);
- sTextMeshTris.Add(t1);
- sTextMeshTris.Add(t3);
- sTextMeshTris.Add(t4);
- sTextMeshUVs.Add(c1.uv0);
- sTextMeshUVs.Add(c2.uv0);
- sTextMeshUVs.Add(c3.uv0);
- sTextMeshUVs.Add(c4.uv0);
- sTextMeshColor.Add(c1.color);
- sTextMeshColor.Add(c2.color);
- sTextMeshColor.Add(c3.color);
- sTextMeshColor.Add(c4.color);
- sTextMeshNormals.Add(c1.normal);
- sTextMeshNormals.Add(c2.normal);
- sTextMeshNormals.Add(c3.normal);
- sTextMeshNormals.Add(c4.normal);
- sTextMeshTangents.Add(c1.tangent);
- sTextMeshTangents.Add(c2.tangent);
- sTextMeshTangents.Add(c3.tangent);
- sTextMeshTangents.Add(c4.tangent);
- }
- TextMesh.Clear();
- TextMesh.SetVertices(sTextMeshVertices);
- TextMesh.SetUVs(0, sTextMeshUVs);
- TextMesh.SetColors(sTextMeshColor);
- TextMesh.SetNormals(sTextMeshNormals);
- TextMesh.SetTangents(sTextMeshTangents);
- TextMesh.SetTriangles(sTextMeshTris, 0);
- // TextMesh.UploadMeshData (false);
- }
- /// <summary>
- /// Draws the rotation gizmos.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawRotationGizmos(Vector3 position, Quaternion rotation, float scale, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawRotateGizmo(position, rotation, scale);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.RotationGizmos,
- position = position,
- rotation = rotation,
- scale = scale,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws the grid gizmos.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawGrids(Vector3 position, Quaternion rotation, int gridX, int gridY,
- float gridXSize, float gridYSize, Color gridColor, float duration = 0, RxDrawAlignment drawAlignment = RxDrawAlignment.LeftBottom)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- switch (drawAlignment)
- {
- case RxDrawAlignment.Center:
- UltiDraw.DrawGrid(position, rotation, gridX, gridY, gridXSize, gridYSize, color: gridColor);
- break;
- case RxDrawAlignment.LeftBottom:
- UltiDraw.DrawGridLBOrigin(position, rotation, gridX, gridY, gridXSize, gridYSize, color: gridColor);
- break;
- case RxDrawAlignment.RightTop:
- UltiDraw.DrawGridRTOrigin(position, rotation, gridX, gridY, gridXSize, gridYSize, color: gridColor);
- break;
- }
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.Grids,
- position = position,
- rotation = rotation,
- scaleInt = new Vector2Int(gridX, gridY),
- v3Size = new Vector3(gridXSize, gridYSize, 0),
- color = gridColor,
- endTime = Time.time + duration,
- alignment = drawAlignment,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a human skeleton.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawHumanSkeleton(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDrawExt.DrawHumanSkeleton(position, rotation, scale, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.HumanSkeleton,
- position = position,
- rotation = rotation,
- v3Size = scale,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a human skeleton line.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawHumanSkeletonLine(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDrawExt.DrawHumanSkeletonLine(position, rotation, scale, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.HumanSkeletonLine,
- position = position,
- rotation = rotation,
- v3Size = scale,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a GUI circle at the screen point.
- /// If drawSpace = ViewPort, Position and Radius is measured in screen coordination.
- /// Else If drawSpace = ScreenSpace, Screen point and Radius is measured in pixel coordination.
- /// Else If drawSpace = WorldSpace, Screen point and Radius is measured in 3D global coordination.
- /// </summary>
- /// <param name="ScreenPoint">Screen point.</param>
- /// <param name="Radius">Radius.</param>
- /// <param name="color">Color.</param>
- /// <param name="cameraTarget">Camera target.</param>
- /// <param name="duration">Duration.</param>
- public static void DrawGUICircle(Vector3 Position, float Radius, Color color, Camera cameraTarget = null, float duration = 0)
- {
- if (!Application.isPlaying)
- {
- return;
- }
- var drawReq = new RxDrawRequest()
- {
- drawType = PEDrawType.GUICircle,
- position = Position,
- scale = Radius,
- color = color,
- endTime = Time.time + duration,
- targetCamera = cameraTarget,
- GUISpaceType = RxCameraSpaceType.ScreenSpace,
- };
- ConvertPixelSpace(ref drawReq);
- sDrawRequests.Add(drawReq);
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a dog skeleton.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawDogSkeleton(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDrawExt.DrawDogSkeleton(position, rotation, scale, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.DogSkeleton,
- position = position,
- rotation = rotation,
- v3Size = scale,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a dog skeleton line.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawDogSkeletonLine(Vector3 position, Quaternion rotation, Vector3 scale, Color color, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDrawExt.DrawDogSkeletonLine(position, rotation, scale, color);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.DogSkeletonLine,
- position = position,
- rotation = rotation,
- v3Size = scale,
- color = color,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a scale gizmos.
- /// </summary>
- /// <param name="position">Position.</param>
- /// <param name="rotation">Rotation.</param>
- /// <param name="scale">Scale.</param>
- public static void DrawScaleGizmos(Vector3 position, Quaternion rotation, float scale, float duration = 0)
- {
- //For editor call, direct draw:
- if (Application.isEditor)
- {
- UltiDraw.SetDepthRendering(DepthRendering);
- UltiDraw.Begin();
- UltiDraw.DrawScaleGizmo(position, rotation, scale);
- UltiDraw.End();
- }
- if (!Application.isPlaying) return;
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.ScalingGizmos,
- position = position,
- rotation = rotation,
- scale = scale,
- endTime = Time.time + duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- /// <summary>
- /// Draws a screen rect, when scale = 1, the rect fully covers screen viewport.
- /// </summary>
- /// <param name="color">Color.</param>
- /// <param name="scale">Scale.</param>
- /// <param name="duration">Duration.</param>
- public static void DrawScreenRect (Color Color, float Scale = 1, float Duration = 0)
- {
- sDrawRequests.Add(new RxDrawRequest()
- {
- drawType = PEDrawType.ScreenRect,
- scale = Scale,
- color = Color,
- endTime = Time.time + Duration,
- }
- );
- if (singleton == null)
- {
- InitializeBehavior();
- }
- }
- static RxDraw singleton;
- /// <summary>
- /// Initializes the behavior.
- /// </summary>
- static void InitializeBehavior()
- {
- if (singleton == null)
- {
- singleton = FindObjectOfType<RxDraw>();
- if (!singleton)
- {
- var go = new GameObject("RxDraw");
- singleton = go.AddComponent<RxDraw>();
- //go.hideFlags = HideFlags.HideAndDontSave;
- if (Application.isPlaying)
- {
- DontDestroyOnLoad(go);
- }
- }
- }
- }
- void OnRenderObject()
- {
- if (sDrawRequests.Count > 0)
- {
- UltiDraw.Begin();
- // UltiDraw.SetCurvature(0f);
- UltiDraw.SetDepthRendering(DepthRendering);
- for (int i = sDrawRequests.Count - 1; i >= 0; i--)
- {
- var dReq = sDrawRequests[i];
- // Debug.Log ("Draw: " + dReq.drawType.ToString());
- if (dReq.customMaterial != null)
- {
- UltiDraw.CustomMaterial = dReq.customMaterial;
- UltiDraw.CustomMaterialPass = dReq.customMaterialPass;
- }
- switch (dReq.drawType)
- {
- case PEDrawType.Line:
- if (dReq.scale <= 0)
- {
- UltiDraw.DrawLine(dReq.position, dReq.position2, dReq.color);
- }
- else
- {
- UltiDraw.DrawLine(dReq.position, dReq.position2, dReq.scale, dReq.color);
- }
- break;
- case PEDrawType.Sphere:
- UltiDraw.DrawSphere(dReq.position, Quaternion.identity, dReq.scale, dReq.color);
- break;
- case PEDrawType.WireSphere:
- UltiDraw.DrawWireSphere(dReq.position, Quaternion.identity, dReq.scale, dReq.wireColor);
- break;
- case PEDrawType.WiredSphere:
- UltiDraw.DrawWiredSphere(dReq.position, Quaternion.identity, dReq.scale, dReq.color, dReq.wireColor);
- break;
- case PEDrawType.Cube:
- UltiDraw.DrawCube(dReq.position, dReq.rotation, dReq.scale, dReq.color);
- break;
- case PEDrawType.WireCube:
- UltiDraw.DrawWireCube(dReq.position, dReq.rotation, dReq.scale, dReq.wireColor);
- break;
- case PEDrawType.WiredCube:
- UltiDraw.DrawWiredCube(dReq.position, dReq.rotation, dReq.scale, dReq.color, dReq.wireColor);
- break;
- case PEDrawType.Cuboid:
- UltiDraw.DrawCuboid(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
- break;
- case PEDrawType.WireCuboid:
- UltiDraw.DrawWireCuboid(dReq.position, dReq.rotation, dReq.v3Size, dReq.wireColor);
- break;
- case PEDrawType.WiredCuboid:
- UltiDraw.DrawWiredCuboid(dReq.position, dReq.rotation, dReq.v3Size, dReq.color, dReq.wireColor);
- break;
- case PEDrawType.Quad:
- UltiDraw.DrawQuad(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case PEDrawType.WireQuad:
- UltiDraw.DrawWireQuad(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.wireColor);
- break;
- case PEDrawType.WiredQuad:
- UltiDraw.DrawWiredQuad(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color, dReq.wireColor);
- break;
- case PEDrawType.Plane:
- UltiDraw.DrawPlane(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case PEDrawType.WirePlane:
- UltiDraw.DrawWirePlane(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.wireColor);
- break;
- case PEDrawType.WiredPlane:
- UltiDraw.DrawWiredPlane(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color, dReq.wireColor);
- break;
- case PEDrawType.TranslateGizmos:
- UltiDraw.DrawTranslateGizmo(dReq.position, dReq.rotation, dReq.scale);
- break;
- case PEDrawType.RotationGizmos:
- UltiDraw.DrawRotateGizmo(dReq.position, dReq.rotation, dReq.scale);
- break;
- case PEDrawType.ScalingGizmos:
- UltiDraw.DrawScaleGizmo(dReq.position, dReq.rotation, dReq.scale);
- break;
- case PEDrawType.Grids:
- switch (dReq.alignment)
- {
- case RxDrawAlignment.Center:
- UltiDraw.DrawGrid(dReq.position, dReq.rotation, dReq.scaleInt.x, dReq.scaleInt.y, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case RxDrawAlignment.LeftBottom:
- UltiDraw.DrawGridLBOrigin(dReq.position, dReq.rotation, dReq.scaleInt.x, dReq.scaleInt.y, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case RxDrawAlignment.RightTop:
- UltiDraw.DrawGridRTOrigin(dReq.position, dReq.rotation, dReq.scaleInt.x, dReq.scaleInt.y, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- }
- break;
- case PEDrawType.Arrow:
- UltiDraw.DrawArrow(dReq.position, dReq.rotation * Vector3.forward, dReq.v3Size.x, dReq.v3Size.y, dReq.v3Size.z, dReq.color);
- break;
- case PEDrawType.Cone:
- UltiDraw.DrawCone(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case PEDrawType.Cylinder:
- UltiDraw.DrawCylinder(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case PEDrawType.Pyramid:
- UltiDraw.DrawPyramid(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.color);
- break;
- case PEDrawType.Mesh:
- UltiDraw.DrawMesh(dReq.meshReference, dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
- break;
- case PEDrawType.MeshWireFrame:
- UltiDrawExt.DrawMeshWireFrame(dReq.position, dReq.rotation, dReq.v3Size, dReq.meshReference, dReq.color);
- break;
- case PEDrawType.HumanSkeleton:
- UltiDrawExt.DrawHumanSkeleton(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
- break;
- case PEDrawType.HumanSkeletonLine:
- UltiDrawExt.DrawHumanSkeletonLine(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
- break;
- case PEDrawType.DogSkeleton:
- UltiDrawExt.DrawDogSkeleton(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
- break;
- case PEDrawType.DogSkeletonLine:
- UltiDrawExt.DrawDogSkeletonLine(dReq.position, dReq.rotation, dReq.v3Size, dReq.color);
- break;
- case PEDrawType.GUICircle:
- UltiDraw.DrawGUICircle((Vector2)dReq.position, dReq.scale, dReq.color, dReq.targetCamera);
- break;
- case PEDrawType.ScreenRect:
- GL.PushMatrix();
- MaterialUI.SetPass(0);
- MaterialUI.color = dReq.color;
- GL.LoadOrtho();
- GL.Begin(GL.TRIANGLE_STRIP);
- if (dReq.scale != 1)
- {
- GL.Vertex(new Vector3(0.5f - 0.5f * dReq.scale, 0.5f * dReq.scale + 0.5f, 0));
- GL.Vertex(new Vector3(0.5f * dReq.scale + 0.5f, 0.5f * dReq.scale + 0.5f, 0));
- GL.Vertex(new Vector3(0.5f - 0.5f * dReq.scale, 0.5f - 0.5f * dReq.scale, 0));
- GL.Vertex(new Vector3(0.5f * dReq.scale + 0.5f, 0.5f - 0.5f * dReq.scale, 0));
- }
- else
- {
- GL.Vertex(new Vector3(0, 1, 0));
- GL.Vertex(new Vector3(1, 1, 0));
- GL.Vertex(new Vector3(0, 0, 0));
- GL.Vertex(new Vector3(1, 0, 0));
- }
- GL.End();
- GL.PopMatrix();
- break;
- case PEDrawType.TextMesh3D:
- GL.PushMatrix();
- GL.MultMatrix(Matrix4x4.TRS(dReq.position, dReq.rotation, dReq.scale * Vector3.one));
- dReq.customMaterial.SetPass(dReq.customMaterialPass);
- var vLst = dReq.uiVerts;
- int charCount = (vLst.Length / 4); //每个character占据4个vertices
- GL.Begin(GL.TRIANGLES);
- //draw characters by characters
- for (int c = 0; c < charCount; c++)
- {
- //draw triangle first
- {
- var ct11 = vLst[c * 4];
- var ct12 = vLst[c * 4 + 1];
- var ct13 = vLst[c * 4 + 2];
- GL.Color(dReq.color);GL.MultiTexCoord(0,ct11.uv0);GL.Vertex(ct11.position);
- GL.Color(dReq.color);GL.MultiTexCoord(0,ct12.uv0);GL.Vertex(ct12.position);
- GL.Color(dReq.color);GL.MultiTexCoord(0,ct13.uv0);GL.Vertex(ct13.position);
- }
- //draw triangle second
- {
- var ct21 = vLst[c * 4];
- var ct22 = vLst[c * 4 + 2];
- var ct23 = vLst[c * 4 + 3];
- GL.Color(dReq.color);GL.TexCoord(ct21.uv0);GL.Vertex(ct21.position);
- GL.Color(dReq.color);GL.TexCoord(ct22.uv0);GL.Vertex(ct22.position);
- GL.Color(dReq.color);GL.TexCoord(ct23.uv0);GL.Vertex(ct23.position);
- }
- }
- GL.End();
- GL.PopMatrix();
- break;
- case PEDrawType.WireCone:
- UltiDraw.DrawWireCone(dReq.position, dReq.rotation, dReq.v3Size.x, dReq.v3Size.y, dReq.wireColor);
- break;
- default:
- break;
- }
- UltiDraw.CustomMaterial = null;//Clear after draw
- }
- UltiDraw.End();
- }
- }
- /// <summary>
- /// Converts the position from screen normalize space to pixel space.
- /// </summary>
- /// <param name="drawRequest">Draw request.</param>
- static void ConvertPixelSpace(ref RxDrawRequest drawRequest)
- {
- var _Camera = drawRequest.targetCamera ?? Camera.main;
- if (_Camera)
- {
- if (drawRequest.GUISpaceType == RxCameraSpaceType.ScreenSpace)
- {
- drawRequest.position = _Camera.ScreenToViewportPoint(drawRequest.position);
- }
- else if (drawRequest.GUISpaceType == RxCameraSpaceType.WorldSpace)
- {
- drawRequest.position = _Camera.WorldToViewportPoint(drawRequest.position);
- }
- }
- }
- void OnDestroy()
- {
- sDrawRequests.Clear();
- if (singleton)
- {
- if (Application.isEditor && Application.isPlaying == false)
- {
- DestroyImmediate(singleton.gameObject);
- }
- else
- {
- Destroy(singleton.gameObject);
- }
- }
- singleton = null;
- }
- IEnumerator ClearQueue ()
- {
- var waitEndFrame = new WaitForEndOfFrame();
- while (true)
- {
- yield return waitEndFrame;
- if (sDrawRequests.Count > 0)
- {
- for (int i = sDrawRequests.Count - 1; i >= 0; i--)
- {
- if (sDrawRequests[i].endTime < Time.time)
- {
- sDrawRequests.RemoveAt(i);
- }
- }
- }
- }
- }
- }
- }
|