15 #region public members 16 [Tooltip(
"The object take up this percent vertically in our view (not technically a percent use 0.5 for 50%)")]
17 public float TargetViewPercentV = 0.5f;
19 [Tooltip(
"If the object is closer than MinDistance, the distance used is clamped here")]
20 public float MinDistance = 0.5f;
22 [Tooltip(
"If the object is farther than MaxDistance, the distance used is clamped here")]
23 public float MaxDistance = 3.5f;
25 [Tooltip(
"Minimum scale value possible (world space scale)")]
26 public float MinScale = 0.01f;
28 [Tooltip(
"Maximum scale value possible (world space scale)")]
29 public float MaxScale = 100f;
31 [Tooltip(
"Used for dead zone for scaling")]
32 public float ScaleBuffer = 0.01f;
34 [Tooltip(
"If you don't trust or don't like the auto size calculation, specify a manual size here. 0 is ignored")]
35 public float ManualObjectSize = 0;
48 public float CurrentScalePercent
52 return objectScalePercent;
58 public float CurrentDistancePercent
62 return objectDistancePercent;
67 #region private members 68 private float fovScalar = 1f;
69 private float objectSize = 1f;
70 private float objectScalePercent = 1f;
71 private float objectDistancePercent = 1f;
76 float baseSize = CalculateObjectSize();
80 objectSize = baseSize;
84 Debug.LogWarning(
"ConstantViewSize: Object base size calculate was 0, defaulting to 1");
96 private float CalculateObjectSize()
98 if (ManualObjectSize > 0)
100 return ManualObjectSize;
103 Vector3 rootScale = transform.root.localScale;
104 transform.root.localScale = Vector3.one;
108 Bounds combinedBounds =
new Bounds(transform.position, Vector3.zero);
109 Renderer[] renderers = this.GetComponentsInChildren<Renderer>();
111 foreach (Renderer render
in renderers)
113 combinedBounds.Encapsulate(render.bounds);
116 maxSize = combinedBounds.extents.magnitude;
118 transform.root.localScale = rootScale;
126 float lastScalePct = objectScalePercent;
127 AdjustSizeForView(solverHandler.TransformTarget);
128 float scaleDiff = (objectScalePercent - lastScalePct) / solverHandler.DeltaTime;
130 if (scaleDiff > ScaleBuffer)
134 else if (scaleDiff < -ScaleBuffer)
153 float sinfov = Mathf.Sin(camFOVrad * 0.5f);
154 float scalar = 2f * TargetViewPercentV * sinfov / objectSize;
159 private void AdjustSizeForView(Transform targTransform)
161 if (targTransform != null)
164 fovScalar = GetFOVScalar();
167 solverHandler.AltScale.SetGoal(this.transform.localScale);
171 float scalePower = 1f;
173 Vector3 targetPosition = targTransform.position;
174 float distance = Mathf.Clamp(Vector3.Distance(transform.position, targetPosition), MinDistance, MaxDistance);
175 float scale = Mathf.Clamp(fovScalar * Mathf.Pow(distance, scalePower), MinScale, MaxScale);
176 GoalScale = Vector3.one * scale;
179 objectDistancePercent = Mathf.InverseLerp(MinDistance, MaxDistance, distance);
180 objectScalePercent = Mathf.InverseLerp(MinScale, MaxScale, scale);
182 UpdateWorkingScaleToGoal();