Skip to content
Snippets Groups Projects
Commit afaeedc2 authored by mhby1g21's avatar mhby1g21
Browse files

added scale to target dimensions functionality

parent d88c7af1
No related branches found
No related tags found
2 merge requests!7added #if UNITY_EDITOR #endif directives to all editor related scripts,!6added subjective and objective evaluation scenes, with improvements on meshdimension script and xr origin prefab
...@@ -142,6 +142,37 @@ public static class MeshDimensionCalculatorLogic ...@@ -142,6 +142,37 @@ public static class MeshDimensionCalculatorLogic
targetObject.transform.localScale = Vector3.one; targetObject.transform.localScale = Vector3.one;
Debug.Log("Target object scale has been reset to (1, 1, 1)."); Debug.Log("Target object scale has been reset to (1, 1, 1).");
} }
public static Vector3 CalculateScaleFactorsForTargetDimensions(Vector3 currentDimensions, Vector3 targetDimensions)
{
if (currentDimensions == Vector3.zero)
{
Debug.LogError("Current dimensions are zero. Please calculate dimensions first!");
return Vector3.one;
}
return new Vector3(
targetDimensions.x / currentDimensions.x,
targetDimensions.y / currentDimensions.y,
targetDimensions.z / currentDimensions.z
);
}
public static void ScaleToTargetDimensions(GameObject targetObject, Vector3 currentDimensions, Vector3 targetDimensions)
{
if (targetObject == null)
{
Debug.LogError("Target object is not assigned!");
return;
}
Vector3 scaleFactors = CalculateScaleFactorsForTargetDimensions(currentDimensions, targetDimensions);
Undo.RecordObject(targetObject.transform, "Scale To Target Dimensions");
Vector3 currentScale = targetObject.transform.localScale;
Vector3 newScale = Vector3.Scale(currentScale, scaleFactors);
targetObject.transform.localScale = newScale;
Debug.Log($"Scaled object to target dimensions. New scale: {newScale}");
}
} }
// MonoBehaviour Component // MonoBehaviour Component
...@@ -150,6 +181,7 @@ public class MeshDimensionCalculator : MonoBehaviour ...@@ -150,6 +181,7 @@ public class MeshDimensionCalculator : MonoBehaviour
public GameObject targetObject; public GameObject targetObject;
private DimensionResults results; private DimensionResults results;
[SerializeField] private Vector3 targetDimensions = Vector3.one;
[SerializeField] private Vector3 scaleFactors = Vector3.one; [SerializeField] private Vector3 scaleFactors = Vector3.one;
[SerializeField] private float uniformScaleFactor = 1f; [SerializeField] private float uniformScaleFactor = 1f;
...@@ -187,6 +219,17 @@ public class MeshDimensionCalculator : MonoBehaviour ...@@ -187,6 +219,17 @@ public class MeshDimensionCalculator : MonoBehaviour
{ {
MeshDimensionCalculatorLogic.ResetScale(targetObject); MeshDimensionCalculatorLogic.ResetScale(targetObject);
} }
public void ScaleToTargetDimensions()
{
if (results == null || results.dimensions == Vector3.zero)
{
Debug.LogWarning("Please calculate dimensions first!");
return;
}
MeshDimensionCalculatorLogic.ScaleToTargetDimensions(targetObject, results.dimensions, targetDimensions);
}
} }
// Inspector UI for the MonoBehaviour component // Inspector UI for the MonoBehaviour component
...@@ -194,12 +237,14 @@ public class MeshDimensionCalculator : MonoBehaviour ...@@ -194,12 +237,14 @@ public class MeshDimensionCalculator : MonoBehaviour
public class MeshDimensionCalculatorEditor : Editor public class MeshDimensionCalculatorEditor : Editor
{ {
SerializedProperty targetObjectProperty; SerializedProperty targetObjectProperty;
SerializedProperty targetDimensionsProperty;
SerializedProperty scaleFactorsProperty; SerializedProperty scaleFactorsProperty;
SerializedProperty uniformScaleFactorProperty; SerializedProperty uniformScaleFactorProperty;
private void OnEnable() private void OnEnable()
{ {
targetObjectProperty = serializedObject.FindProperty("targetObject"); targetObjectProperty = serializedObject.FindProperty("targetObject");
targetDimensionsProperty = serializedObject.FindProperty("targetDimensions");
scaleFactorsProperty = serializedObject.FindProperty("scaleFactors"); scaleFactorsProperty = serializedObject.FindProperty("scaleFactors");
uniformScaleFactorProperty = serializedObject.FindProperty("uniformScaleFactor"); uniformScaleFactorProperty = serializedObject.FindProperty("uniformScaleFactor");
} }
...@@ -227,6 +272,15 @@ public class MeshDimensionCalculatorEditor : Editor ...@@ -227,6 +272,15 @@ public class MeshDimensionCalculatorEditor : Editor
calculator.ResetFloorToZero(); calculator.ResetFloorToZero();
} }
EditorGUILayout.Space();
EditorGUILayout.LabelField("Target Dimensions", EditorStyles.boldLabel);
EditorGUILayout.PropertyField(targetDimensionsProperty, new GUIContent("Target Dimensions (X, Y, Z)"));
if (GUILayout.Button("Scale To Target Dimensions"))
{
calculator.ScaleToTargetDimensions();
}
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUILayout.LabelField("Scaling Options", EditorStyles.boldLabel); EditorGUILayout.LabelField("Scaling Options", EditorStyles.boldLabel);
...@@ -260,6 +314,7 @@ public class MeshDimensionCalculatorWindow : EditorWindow ...@@ -260,6 +314,7 @@ public class MeshDimensionCalculatorWindow : EditorWindow
{ {
private GameObject targetObject; private GameObject targetObject;
private DimensionResults results; private DimensionResults results;
private Vector3 targetDimensions = Vector3.one;
private Vector3 scaleFactors = Vector3.one; private Vector3 scaleFactors = Vector3.one;
private float uniformScaleFactor = 1f; private float uniformScaleFactor = 1f;
...@@ -301,6 +356,22 @@ public class MeshDimensionCalculatorWindow : EditorWindow ...@@ -301,6 +356,22 @@ public class MeshDimensionCalculatorWindow : EditorWindow
} }
} }
EditorGUILayout.Space();
EditorGUILayout.LabelField("Target Dimensions", EditorStyles.boldLabel);
targetDimensions = EditorGUILayout.Vector3Field("Target Dimensions (X, Y, Z)", targetDimensions);
if (GUILayout.Button("Scale To Target Dimensions"))
{
if (results != null)
{
MeshDimensionCalculatorLogic.ScaleToTargetDimensions(targetObject, results.dimensions, targetDimensions);
}
else
{
EditorUtility.DisplayDialog("Error", "Please calculate dimensions first!", "OK");
}
}
EditorGUILayout.Space(); EditorGUILayout.Space();
EditorGUILayout.LabelField("Scaling Options", EditorStyles.boldLabel); EditorGUILayout.LabelField("Scaling Options", EditorStyles.boldLabel);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment