Author Topic: NGUI and Vectrosity  (Read 4734 times)

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
NGUI and Vectrosity
« on: October 20, 2016, 06:26:05 PM »
If you are working with Vectrosity, the line drawing asset, you can use this to get started:

  1. using UnityEngine;
  2. using Vectrosity;
  3. using System.Collections.Generic;
  4.  
  5. public class LineDrawer : MonoBehaviour
  6. {
  7.     public List<Vector3> points;
  8.     private Vector3[] curvePoints = new Vector3[4];
  9.  
  10.     public GameObject sourceGO;
  11.     public GameObject targetGO;
  12.     public Color lineColor = Color.red;
  13.     private Color _setColor = Color.white;
  14.  
  15.     public Camera unityCamera;
  16.  
  17.     //create and assign a standard material, set smoothness source to "Albedo Alpha", set smoothness to 0
  18.     public Material lineMaterial;
  19.  
  20.     private Vector3 sourcePosition;
  21.     private Vector3 targetPosition;
  22.  
  23.     private VectorLine myLine;
  24.  
  25.     private void Start()
  26.     {
  27.         //set these items before any VectorLine is created
  28.  
  29.         VectorLine.SetCamera3D(unityCamera);
  30.  
  31.         //we need to set the canvas to be at the same location as the NGUI screen area
  32.         VectorLine.canvas.renderMode = RenderMode.WorldSpace;
  33.         VectorLine.canvas.transform.position = Vector3.zero;
  34.         //on UIRoot, set Constrained Scaling Style
  35.         VectorLine.canvas.scaleFactor = NGUITools.FindInParents<UIRoot>(this.gameObject).gameObject.transform.localScale.x;
  36.  
  37.  
  38.  
  39.        
  40.  
  41.         if(sourceGO == null | targetGO == null)
  42.         {
  43.             Debug.LogError("Set your source and target GOs.");
  44.             return;
  45.         }
  46.  
  47.         points =new List<Vector3> { Vector3.zero, Vector3.one };
  48.  
  49.         myLine = new VectorLine("myLine", points, 5f, LineType.Continuous);
  50.         myLine.Resize(50);
  51.  
  52.         //setting renderqueue > 3000 will draw in front of all NGUI objects
  53.         lineMaterial.renderQueue = 4000;
  54.         lineMaterial.SetColor("_Color", Color.red);
  55.         lineMaterial.SetColor("_EmissionColor", Color.red);
  56.  
  57.         myLine.material = lineMaterial;
  58.        
  59.  
  60.         myLine.Draw3DAuto();
  61.  
  62.     }
  63.  
  64.     private void Update()
  65.     {
  66.         if(sourceGO == null | targetGO == null)
  67.             return;
  68.  
  69.         if(sourceGO.transform.localPosition != sourcePosition |
  70.             targetGO.transform.localPosition != targetPosition)
  71.         {
  72.             sourcePosition = sourceGO.transform.position;
  73.             targetPosition = targetGO.transform.position;
  74.  
  75.             Vector3 pos1 = new Vector3(sourcePosition.x, sourcePosition.y, sourcePosition.z);
  76.             Vector3 pos2 = new Vector3(targetPosition.x, targetPosition.y, targetPosition.z);
  77.  
  78.             curvePoints[0] = pos1;// sourcePosition;
  79.             curvePoints[1] = (pos1 + new Vector3(0.3f, 0f, 0f));
  80.             curvePoints[2] = pos2;
  81.             curvePoints[3] = (pos2 + new Vector3(0f, 0.3f, 0f));
  82.  
  83.             myLine.MakeCurve(curvePoints);
  84.  
  85.         }
  86.  
  87.         if(_setColor != lineColor)
  88.         {
  89.             lineMaterial.renderQueue = 4000;
  90.             lineMaterial.SetColor("_Color", lineColor);
  91.             lineMaterial.SetColor("_EmissionColor", lineColor);
  92.  
  93.             myLine.material = lineMaterial;
  94.         }
  95.     }
  96. }
  97.  
  98.  

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Re: NGUI and Vectrosity
« Reply #1 on: October 22, 2016, 09:20:52 AM »
A note about renderqueue:

In my experimentation, to get proper layering/masking/clipping of vectorline objects, I think the best way to go is set each line material's renderqueue to be > any NGUI object and use UIPanel's sort order to affect layering of canvasses.  Vectrosity's default canvas sort order is 1.  NGUI panels at 0 are under lines and panels at 2 and up overlay lines. 

This may cost a few extra draw calls, as panels are "units of draw calls" but it's not enough to cause a real fps drop, from what I can see.

sdas2021

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: NGUI and Vectrosity
« Reply #2 on: July 16, 2017, 08:04:38 AM »
  1. using UnityEngine;
  2. using Vectrosity;
  3. using System.Collections.Generic;
  4.  
  5. public class LineDraw : MonoBehaviour
  6. {
  7.         public List<Vector3> points;
  8.         private Vector3[] curvePoints = new Vector3[4];
  9.  
  10.         public GameObject sourceGO;
  11.         public GameObject targetGO;
  12.         public Color lineColor = Color.red;
  13.         private Color _setColor = Color.white;
  14.  
  15.         public Camera unityCamera;
  16.  
  17.         //create and assign a standard material, set smoothness source to "Albedo Alpha", set smoothness to 0
  18.         public Material lineMaterial;
  19.  
  20.         private Vector3 sourcePosition;
  21.         private Vector3 targetPosition;
  22.  
  23.         private VectorLine myLine;
  24.  
  25.         private void Start()
  26.         {
  27.                 //set these items before any VectorLine is created
  28.  
  29.                 VectorLine.SetCamera3D(unityCamera);
  30.  
  31.                 //we need to set the canvas to be at the same location as the NGUI screen area
  32.                 VectorLine.canvas.renderMode = RenderMode.WorldSpace;
  33.                 //Change#1: Shifting canvas to sourceGO position
  34.                 VectorLine.canvas.transform.position = sourceGO.transform.position;
  35.                 //on UIRoot, set Constrained Scaling Style
  36.                 VectorLine.canvas.scaleFactor = NGUITools.FindInParents<UIRoot>(this.gameObject).gameObject.transform.localScale.x;
  37.  
  38.  
  39.  
  40.  
  41.  
  42.                 if(sourceGO == null | targetGO == null)
  43.                 {
  44.                         Debug.LogError("Set your source and target GOs.");
  45.                         return;
  46.                 }
  47.                 //Change#2: Changed to match sourceGO and targetGO positions
  48.                 points =new List<Vector3> { sourceGO.transform.position, targetGO.transform.position };
  49.  
  50.                 myLine = new VectorLine("myLine", points, 5f, LineType.Continuous);
  51.                 //Change#3: Changed to draw straight line
  52. //              myLine.Resize(50);
  53.  
  54.                 //setting renderqueue > 3000 will draw in front of all NGUI objects
  55.                 lineMaterial.renderQueue = 4000;
  56.                 lineMaterial.SetColor("_Color", Color.red);
  57.                 lineMaterial.SetColor("_EmissionColor", Color.red);
  58.  
  59.                 myLine.material = lineMaterial;
  60.  
  61.  
  62.                 myLine.Draw3DAuto();
  63.  
  64.         }
  65.  
  66.         private void Update()
  67.         {
  68.                 if(sourceGO == null | targetGO == null)
  69.                         return;
  70.  
  71.                 if(sourceGO.transform.localPosition != sourcePosition |
  72.                         targetGO.transform.localPosition != targetPosition)
  73.                 {
  74.                         sourcePosition = sourceGO.transform.position;
  75.                         targetPosition = targetGO.transform.position;
  76.  
  77.                         Vector3 pos1 = new Vector3(sourcePosition.x, sourcePosition.y, sourcePosition.z);
  78.                         Vector3 pos2 = new Vector3(targetPosition.x, targetPosition.y, targetPosition.z);
  79.  
  80.                         curvePoints[0] = pos1;// sourcePosition;
  81.                         curvePoints[1] = (pos1 + new Vector3(0.3f, 0f, 0f));
  82.                         curvePoints[2] = pos2;
  83.                         curvePoints[3] = (pos2 + new Vector3(0f, 0.3f, 0f));
  84.  
  85.                         myLine.MakeCurve(curvePoints);
  86.  
  87.                 }
  88.  
  89.                 if(_setColor != lineColor)
  90.                 {
  91.                         lineMaterial.renderQueue = 4000;
  92.                         lineMaterial.SetColor("_Color", lineColor);
  93.                         lineMaterial.SetColor("_EmissionColor", lineColor);
  94.  
  95.                         myLine.material = lineMaterial;
  96.                 }
  97.         }
  98. }
  99.  

I am using the code given by Zyxil at http://www.tasharen.com/forum/index.php?topic=14921.msg65089#msg65089

Have changed the code to put start and end position at sourceGO and targetGO, and draw a straight line.
Issue: As you can see in the attachment that in the scene view, line is drawn correctly. But in Game view the scale of the line is off. How can I correct it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI and Vectrosity
« Reply #3 on: July 22, 2017, 02:24:19 PM »
My guess is that your line is drawn by a different camera than your UI. Check its layer.

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Re: NGUI and Vectrosity
« Reply #4 on: July 23, 2017, 07:45:35 PM »
Yup, check the layers. 

I think the problem is with moving the canvas location.  I had to try many, many different ways of locating things to get Vectrosity and NGUI to line up properly.  They may be seen by the same UI camera, but not seeing them properly.

Note that NGUI and Vectrosity use different rendering methods with Vectrosity on Unity canvasses.  When running the game, be sure your scene view is in 3d mode and move your scene view around to note parallax changes between the different rendering methods. 

Zyxil

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 4
  • Posts: 37
    • View Profile
Re: NGUI and Vectrosity
« Reply #5 on: July 23, 2017, 08:28:59 PM »
Some more notes, as I haven't messed with this in awhile:

- Learn the Vectrosity documentation.  The product has a ton of features and different ways to use them.  The Coding.pdf, in particular, is extremely useful.

- I don't remember the reason, but I think creating the points list at the beginning with v3.zero and v3.one was necessary.  When you set the array a few lines down (in my code) is when the actual positions of source and target are set.  Since you just want straight lines, use a 2 element array and don't call Resize or MakeCurve.  The 2nd and 4th elements are for the curve control points.