@@ -12,36 +12,63 @@ namespace HoloToolkit.Unity
1212 /// </summary>
1313 public class FixedAngularSize : MonoBehaviour
1414 {
15+ [ Tooltip ( "Off sets the scale ratio so that text does not scale down too much. (Set to zero for linear scaling)" ) ]
16+ public float OverrideSizeRatio = 0 ;
17+
1518 // The ratio between the transform's local scale and its starting
1619 // distance from the camera.
1720 private Vector3 defaultSizeRatios ;
21+ private float startingDistance ;
22+ private Vector3 startingScale ;
1823
19- void Start ( )
24+ private void Start ( )
2025 {
2126 // Calculate the XYZ ratios for the transform's localScale over its
2227 // initial distance from the camera.
23- float startingDistance = Vector3 . Distance ( Camera . main . transform . position , transform . position ) ;
24- if ( startingDistance > 0.0f )
28+ startingDistance = Vector3 . Distance ( Camera . main . transform . position , transform . position ) ;
29+ startingScale = transform . localScale ;
30+
31+ SetSizeRatio ( OverrideSizeRatio ) ;
32+ }
33+
34+ /// <summary>
35+ /// Manually update the OverrideSizeRatio during runtime or through UnityEvents in the editor
36+ /// </summary>
37+ /// <param name="ratio"> 0 - 1 : Use 0 for linear scaling</param>
38+ public void SetSizeRatio ( float ratio )
39+ {
40+ if ( ratio == 0 )
2541 {
26- defaultSizeRatios = transform . localScale / startingDistance ;
42+ if ( startingDistance > 0.0f )
43+ {
44+ // set to a linear scale ratio
45+ OverrideSizeRatio = 1 / startingDistance ;
46+ }
47+ else
48+ {
49+ // If the transform and the camera are both in the same
50+ // position (that is, the distance between them is zero),
51+ // disable this Behaviour so we don't get a DivideByZero
52+ // error later on.
53+ enabled = false ;
54+ #if UNITY_EDITOR
55+ Debug . LogWarning ( "The object and the camera are in the same position at Start(). The attached FixedAngularSize Behaviour is now disabled." ) ;
56+ #endif // UNITY_EDITOR
57+ }
2758 }
2859 else
2960 {
30- // If the transform and the camera are both in the same
31- // position (that is, the distance between them is zero),
32- // disable this Behaviour so we don't get a DivideByZero
33- // error later on.
34- enabled = false ;
35- #if UNITY_EDITOR
36- Debug . LogWarning ( "The object and the camera are in the same position at Start(). The attached FixedAngularSize Behaviour is now disabled." ) ;
37- #endif // UNITY_EDITOR
61+ OverrideSizeRatio = ratio ;
3862 }
3963 }
4064
41- void Update ( )
65+ private void Update ( )
4266 {
4367 float distanceToHologram = Vector3 . Distance ( Camera . main . transform . position , transform . position ) ;
44- transform . localScale = defaultSizeRatios * distanceToHologram ;
68+ // create an offset ratio based on the starting position. This value creates a new angle that pivots
69+ // on the starting position that is more or less drastic than the normal scale ratio.
70+ float curvedRatio = 1 - startingDistance * OverrideSizeRatio ;
71+ transform . localScale = startingScale * ( distanceToHologram * OverrideSizeRatio + curvedRatio ) ;
4572 }
4673 }
4774}
0 commit comments