Search Unity

Vuforia AR crashing when simple line renderers

Discussion in 'AR' started by keithroyce, Jan 10, 2019.

  1. keithroyce

    keithroyce

    Joined:
    Mar 2, 2018
    Posts:
    17
    Hi everyone

    Im running into a problem with Vuforia AR. I have used some scripts that use line renderers that I have found on the forums to create a graph on a world space UI. Whenever I load my AR scene in the editor it works fine, but when i build it to my mobile - it crashes. I tested my other scenes without these line renderers and it works fine. So i can understand that it may be the script that is crashing the app, but why? ive looked over the scripts and they seem fairly light weight.

    I have attached the scripts below. Please, can any of you veterans and pros out there find out why it doesnt work?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. namespace EasyCurvedLine
    6. {
    7.     [RequireComponent(typeof(LineRenderer))]
    8.     public class CurvedLineRenderer : MonoBehaviour
    9.     {
    10.         public float lineSegmentSize = 0.15f;
    11.         public float lineWidth = 0.1f;
    12.         [Tooltip("Enable this to set a custom width for the line end")]
    13.         public bool useCustomEndWidth = false;
    14.         [Tooltip("Custom width for the line end")]
    15.         public float endWidth = 0.1f;
    16.         [Header("Gizmos")]
    17.         public bool showGizmos = true;
    18.         public float gizmoSize = 0.1f;
    19.         public Color gizmoColor = new Color(1, 0, 0, 0.5f);
    20.  
    21.         private CurvedLinePoint[] linePoints = new CurvedLinePoint[0];
    22.         private Vector3[] linePositions = new Vector3[0];
    23.         private Vector3[] linePositionsOld = new Vector3[0];
    24.  
    25.         // Update is called once per frame
    26.         public void Update()
    27.         {
    28.             GetPoints();
    29.             SetPointsToLine();
    30.         }
    31.  
    32.         private void GetPoints()
    33.         {
    34.             //find curved points in children
    35.             linePoints = this.GetComponentsInChildren<CurvedLinePoint>();
    36.  
    37.             //add positions
    38.             linePositions = new Vector3[linePoints.Length];
    39.             for (int i = 0; i < linePoints.Length; i++)
    40.             {
    41.                 linePositions[i] = linePoints[i].transform.position;
    42.             }
    43.         }
    44.  
    45.         private void SetPointsToLine()
    46.         {
    47.             //create old positions if they dont match
    48.             if (linePositionsOld.Length != linePositions.Length)
    49.             {
    50.                 linePositionsOld = new Vector3[linePositions.Length];
    51.             }
    52.  
    53.             //check if line points have moved
    54.             bool moved = false;
    55.             for (int i = 0; i < linePositions.Length; i++)
    56.             {
    57.                 //compare
    58.                 if (linePositions[i] != linePositionsOld[i])
    59.                 {
    60.                     moved = true;
    61.                 }
    62.             }
    63.  
    64.             //update if moved
    65.             if (moved == true)
    66.             {
    67.                 LineRenderer line = this.GetComponent<LineRenderer>();
    68.  
    69.                 //get smoothed values
    70.                 Vector3[] smoothedPoints = LineSmoother.SmoothLine(linePositions, lineSegmentSize);
    71.  
    72.                 //set line settings
    73.                 line.positionCount = smoothedPoints.Length;
    74.                 line.SetPositions(smoothedPoints);
    75.                 line.startWidth = lineWidth;
    76.                 line.endWidth = useCustomEndWidth ? endWidth : lineWidth;
    77.             }
    78.         }
    79.  
    80.         private void OnDrawGizmosSelected()
    81.         {
    82.             Update();
    83.         }
    84.  
    85.         private void OnDrawGizmos()
    86.         {
    87.             if (linePoints.Length == 0)
    88.             {
    89.                 GetPoints();
    90.             }
    91.  
    92.             //settings for gizmos
    93.             foreach (CurvedLinePoint linePoint in linePoints)
    94.             {
    95.                 linePoint.showGizmo = showGizmos;
    96.                 linePoint.gizmoSize = gizmoSize;
    97.                 linePoint.gizmoColor = gizmoColor;
    98.             }
    99.         }
    100.     }
    101. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. namespace EasyCurvedLine
    5. {
    6.     public class CurvedLinePoint : MonoBehaviour
    7.     {
    8.         [HideInInspector] public bool showGizmo = true;
    9.         [HideInInspector] public float gizmoSize = 0.1f;
    10.         [HideInInspector] public Color gizmoColor = new Color(1, 0, 0, 0.5f);
    11.  
    12.         private void OnDrawGizmos()
    13.         {
    14.             if (showGizmo == true)
    15.             {
    16.                 Gizmos.color = gizmoColor;
    17.  
    18.                 Gizmos.DrawSphere(this.transform.position, gizmoSize);
    19.             }
    20.         }
    21.  
    22.         /// <summary>
    23.         /// Update the parent line when this point is moved
    24.         /// </summary>
    25.         private void OnDrawGizmosSelected()
    26.         {
    27.             CurvedLineRenderer curvedLine = this.transform.parent.GetComponent<CurvedLineRenderer>();
    28.  
    29.             if (curvedLine != null)
    30.             {
    31.                 curvedLine.Update();
    32.             }
    33.         }
    34.     }
    35. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class LineSmoother : MonoBehaviour
    6. {
    7.     public static Vector3[] SmoothLine( Vector3[] inputPoints, float segmentSize )
    8.     {
    9.         //create curves
    10.         AnimationCurve curveX = new AnimationCurve();
    11.         AnimationCurve curveY = new AnimationCurve();
    12.         AnimationCurve curveZ = new AnimationCurve();
    13.  
    14.         //create keyframe sets
    15.         Keyframe[] keysX = new Keyframe[inputPoints.Length];
    16.         Keyframe[] keysY = new Keyframe[inputPoints.Length];
    17.         Keyframe[] keysZ = new Keyframe[inputPoints.Length];
    18.  
    19.         //set keyframes
    20.         for( int i = 0; i < inputPoints.Length; i++ )
    21.         {
    22.             keysX[i] = new Keyframe( i, inputPoints[i].x );
    23.             keysY[i] = new Keyframe( i, inputPoints[i].y );
    24.             keysZ[i] = new Keyframe( i, inputPoints[i].z );
    25.         }
    26.  
    27.         //apply keyframes to curves
    28.         curveX.keys = keysX;
    29.         curveY.keys = keysY;
    30.         curveZ.keys = keysZ;
    31.  
    32.         //smooth curve tangents
    33.         for( int i = 0; i < inputPoints.Length; i++ )
    34.         {
    35.             curveX.SmoothTangents( i, 0 );
    36.             curveY.SmoothTangents( i, 0 );
    37.             curveZ.SmoothTangents( i, 0 );
    38.         }
    39.  
    40.         //list to write smoothed values to
    41.         List<Vector3> lineSegments = new List<Vector3>();
    42.  
    43.         //find segments in each section
    44.         for( int i = 0; i < inputPoints.Length; i++ )
    45.         {
    46.             //add first point
    47.             lineSegments.Add( inputPoints[i] );
    48.  
    49.             //make sure within range of array
    50.             if( i+1 < inputPoints.Length )
    51.             {
    52.                 //find distance to next point
    53.                 float distanceToNext = Vector3.Distance(inputPoints[i], inputPoints[i+1]);
    54.  
    55.                 //number of segments
    56.                 int segments = (int)(distanceToNext / segmentSize);
    57.  
    58.                 //add segments
    59.                 for( int s = 1; s < segments; s++ )
    60.                 {
    61.                     //interpolated time on curve
    62.                     float time = ((float)s/(float)segments) + (float)i;
    63.  
    64.                     //sample curves to find smoothed position
    65.                     Vector3 newSegment = new Vector3( curveX.Evaluate(time), curveY.Evaluate(time), curveZ.Evaluate(time) );
    66.  
    67.                     //add to list
    68.                     lineSegments.Add( newSegment );
    69.                 }
    70.             }
    71.         }
    72.  
    73.         return lineSegments.ToArray();
    74.     }
    75.  
    76. }
     
  2. rob_ice

    rob_ice

    Joined:
    Nov 11, 2016
    Posts:
    112
    Can you get any xcode or logcat logs to see what the crash log is? That will give us some clues
     
  3. keithroyce

    keithroyce

    Joined:
    Mar 2, 2018
    Posts:
    17
    All good, found a solution. Turns out i was overloading the app by rendering too many at once - so found a way to do it more economically.