Hello everyone.

The Mixed Reality Forums here are no longer being used or maintained.

There are a few other places we would like to direct you to for support, both from Microsoft and from the community.

The first way we want to connect with you is our mixed reality developer program, which you can sign up for at https://aka.ms/IWantMR.

For technical questions, please use Stack Overflow, and tag your questions using either hololens or windows-mixed-reality.

If you want to join in discussions, please do so in the HoloDevelopers Slack, which you can join by going to https://aka.ms/holodevelopers, or in our Microsoft Tech Communities forums at https://techcommunity.microsoft.com/t5/mixed-reality/ct-p/MicrosoftMixedReality.

And always feel free to hit us up on Twitter @MxdRealityDev.
Options

Line renderer not positioned correctly

Hi,

I have a project where I'm using a line renderer to draw a curve between two objects, and it works fine in unity- see attached photo. However when I build and deploy to hololens it's like the line positions haven't updated and the line isn't drawn where it's meant to be.

Any ideas why this would happen?

Cheers

Answers

  • Options

    Photo didn't attach, here it is :)

  • Options

    Hello, I use LineRenderer in the Hololens and the position is fine. Can you add more informations on the code you are using?

  • Options

    @Frimy This is the script that I'm using. At the moment I can change the positions in the inspector in unity so that the line draws in the right position in hololens, but it only draws a straight line now, not a curve like it's meant to.

    public class Bezier : MonoBehaviour
    {

    public Transform[] controlPoints;       //array containing start point for line, end point, and middle point to interpolate around
    public LineRenderer lineRenderer;       //lineRenderer draws the line from a set of points given to it
    private GameObject cube;                 //used as a temporary middle point between hives
    public int hiveFrom;
    public int hiveTo;
    
    //variables from the original script
    private int curveCount = 0;     
    private int layerOrder = 0;
    private int SEGMENT_COUNT = 100;
    private int[,] hiveDrift;
    private string hivetostring;
    private string hivefromstring;
    
    
    void Start()
    {
        cube= GameObject.CreatePrimitive(PrimitiveType.Cube);   //initialise the gameobject as a cube
    
        if (!lineRenderer)
        {
            lineRenderer = GetComponent<LineRenderer>();    //get the lineRenderer if there isn't one
        }
    
        lineRenderer.sortingLayerID = layerOrder;
    
        curveCount = (int)controlPoints.Length / 3;
    
        var ds = new DataService("BeeData.db");         //new data service object which connects to the BeeData database
         DBScript db = new DBScript();
         hiveDrift = new int[4, 12];
         hiveDrift = db.returnHiveDriftComplete(ds);
    
        hivetostring = "Hive " + hiveTo;
        hivefromstring = "Hive " + hiveFrom;
    
        if(hiveTo>4)
        {
            hivetostring = "FeederStation " + (hiveTo - 4);
        }
    
        if (hiveFrom > 4)
        {
            hivefromstring = "FeederStation " + (hiveFrom - 4);
        }
    }
    
    
    void Update()
    {
    
        //get the x distance between the two hives
        float dist = controlPoints[0].position.x - controlPoints[2].position.x;
        float dist2 = controlPoints[2].position.x - controlPoints[0].position.x;
    
        float height = 0f;
    
        //changes the height of the middle point depending on how close the hives are, in a simple way
        if (dist <= dist2)
        {
            height = dist2;
        }
        else
        {
            height = dist;
        }
    
        if (height <= 5)
        {
            height = 3f;
        }
        else if (height < 10 && height > 5)
        {
            height = 4f;
        }
        else
        {
            height = 5f;
        }
    
    
        Vector3 Coord = new Vector3(0f, height, 0f);       // new vector which will be the middle points position
    
        //works out the middle place between the x and z values
        if (controlPoints[0].position.x < controlPoints[2].position.x)
        {
            Coord.x = ((controlPoints[2].position.x - controlPoints[0].position.x) / 2) + controlPoints[0].position.x;
        }
        else
        {
            Coord.x = ((controlPoints[0].position.x - controlPoints[2].position.x) / 2) + controlPoints[2].position.x;
        }
    
    
        if (controlPoints[0].position.z < controlPoints[2].position.z)
        {
            Coord.z = ((controlPoints[2].position.z - controlPoints[0].position.z) / 2) + controlPoints[0].position.z;
        }
        else
        {
            Coord.z = ((controlPoints[0].position.z - controlPoints[2].position.z) / 2) + controlPoints[2].position.z;
        }
    
        cube.transform.position = Coord;    //sets the position of the middle point
    
        controlPoints[1] = cube.transform;      //saves the middle point to the controlPoints array
        cube.active = false;            //set the cube to not be active so that it doesnt appear on the screen when app is running
    
    
    
        //If there are bees drifting between the hives, draw the curve
        if (hiveDrift[hiveFrom - 1, hiveTo - 1] != 0)
        {
            int numBees = hiveDrift[hiveFrom - 1, hiveTo - 1];
    
           if (numBees <= 5)
            {
                lineRenderer.SetWidth(.01f, .01f);
            }
            else if (numBees > 5 && numBees <= 10)
            {
                lineRenderer.SetWidth(.02f, .02f);
            }
            else if (numBees > 10 && numBees <= 20)
            {
                lineRenderer.SetWidth(.045f, .045f);
            }
            else
            {
                lineRenderer.SetWidth(.05f, .05f);
            }
    
            DrawCurve();
        }
    }
    
    
    //Draws the curve, calls CalculateQuadraticBezierPoint to get each new point on the line and draw it
    void DrawCurve()
    {
        for (int j = 0; j < curveCount; j++)
        {
            for (int i = 1; i <= SEGMENT_COUNT; i++)
            {
                float t = i / (float)SEGMENT_COUNT;
                int nodeIndex = j * 3;
                Vector3 pixel = CalculateQuadraticBezierPoint(t, controlPoints[nodeIndex].position, controlPoints[nodeIndex + 1].position, controlPoints[nodeIndex + 2].position);
                lineRenderer.SetVertexCount(((j * SEGMENT_COUNT) + i));
                lineRenderer.SetPosition((j * SEGMENT_COUNT) + (i - 1), pixel);
            }
    
        }
    }
    
    //applies a quadratic bezier equation to the three points given and returns the new point
    Vector3 CalculateQuadraticBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
    {
        float u = 1 - t;
        float tt = t * t;
        float uu = u * u;
        float uuu = uu * u;
        float ttt = tt * t;
    
        Vector3 p = uu * p0;
        p += 2 * u * t * p1;
        p += tt * p2;
    
        return p;
    }
    

    }

Sign In or Register to comment.