Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Linerender on a GUI ?

Discussion in 'Editor & General Support' started by pacodupont, Apr 7, 2015.

  1. pacodupont

    pacodupont

    Joined:
    Nov 28, 2014
    Posts:
    1
    Hello,
    i've developed a script computing the response of an electronic filter. Now i'm trying to render it through a GUI, to do so i'm using a linerenderer and the canvas system and they don't seem to work really great together. I've trouble with resolution and the line can go out of screen when you change it. Is there any better feature to render a list of points ?

    This some view of the actual rendering but if i change the resolution everything goes out:



    And this is the code rendering the line :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6.    
    7.     public void reload(){
    8.        
    9.         if (PlayerPrefs.GetString ("Type") == "OrderOne") {
    10.             f2=new OrderOne("PB",PlayerPrefs.GetFloat ("T0"), PlayerPrefs.GetFloat ("Tinf"), PlayerPrefs.GetFloat ("Omega0"));
    11.         }
    12.         if (PlayerPrefs.GetString ("Type") == "OrderTwo") {
    13.             f2=new OrderTwo("PB",  PlayerPrefs.GetFloat ( "Omega0"), PlayerPrefs.GetFloat ( "m") , PlayerPrefs.GetFloat("T0"), PlayerPrefs.GetFloat("Tmax"),PlayerPrefs.GetFloat("Tinf"));
    14.         }
    15.        
    16.         f2.autoscale (45f,20f);
    17.         lineSol.SetVertexCount (0);
    18.         lineModule.SetVertexCount (0);
    19.         linePhase.SetVertexCount (0);
    20.         echelon.SetVertexCount (0);
    21.         lineSol.SetVertexCount (f2.getSol().Count);
    22.         linePhase.SetVertexCount (f2.getOmega().Count);
    23.         lineModule.SetVertexCount (f2.getOmega().Count);
    24.         echelon.SetVertexCount (f2.getEchelon().Count);
    25.  
    26.         this.loadPosition ();              
    27.     }
    28.    
    29.     private void loadPosition(){
    30.         int i = 0;
    31.         lineSol.useWorldSpace = true;
    32.         lineModule.useWorldSpace = true ;
    33.         linePhase.useWorldSpace = true;
    34.         echelon.useWorldSpace = true;
    35.         List<float> ech = f2.getEchelon ();
    36.         while (i < f2.getT().Count) {
    37.             Vector3 pos = new Vector3 ( f2.getT () [i]+20f, f2.getSol () [i]+25f, 50f);
    38.             lineSol.SetPosition (i, pos);
    39.             i++;
    40.         }
    41.         i=0;
    42.         while (i < f2.getT().Count) {
    43.             Vector3 pos = new Vector3 ( f2.getT ()[i] + 20f , ech [i] + 25f, 50f);
    44.             echelon.SetPosition (i, pos);
    45.             i++;
    46.         }
    47.  
    48.         i = 0;
    49.         while (i < f2.getOmega().Count) {
    50.             Vector3 pos = new Vector3 (f2.getOmega () [i]+20f,  f2.getModule () [i], 50f);
    51.             lineModule.SetPosition (i, pos);
    52.             i++;
    53.         }
    54.        
    55.         i = 0;
    56.         while (i < f2.getOmega().Count) {
    57.             Vector3 pos = new Vector3 (f2.getOmega () [i]+20f, f2.getPhase () [i]-30f, 50f);
    58.             linePhase.SetPosition (i, pos);
    59.             i++;
    60.         }
    61.        
    62.     }
    63. }
    64.  
    65.  
     
    Last edited: Apr 7, 2015
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    The solution lies in mapping a unit box in 3d to the size of the viewport.

    Since you are probably rendering the line onto a plane using a perspective camera, you just need to determine the bounds of that box at the z-depth of the plane.

    One way to do this is using
    Unity - Scripting API: Camera.ViewportPointToRay

    Once you have the corners of the box in world coordinates, you just need to scale the vertices of the LineRenderer so they fit correctly in the box.

    If you drawing code assumes the line points in a 0-1 box , then 0 maps to box_left and 1 maps to box_right.


    So just box_left + box_width*line_point_vert
     
  3. Boudibou

    Boudibou

    Joined:
    Apr 14, 2015
    Posts:
    1
    Hello,
    I am pacodupont's colleague and we were wondering if there wasn't an easier way to do this..
    We use the autoscale function that we defined in our Function class which renders the line in a box (f2.autoscale(45f,20f) ) and we've been trying to set it (as well as the position of the lines) using Screen.width, camera.width, camera.pixelWidth, camera.rect, camera.pixelRect, but whenever we try to build the project in a different resolution it's complete freestyle.. Shouldn't these features be able to fix our problems??

    Ps: thank you very much for your help