VR/Assets/Scripts/SimpleResizer/SimpleResizer.cs

164 lines
5.3 KiB
C#
Executable File

using UnityEngine;
public class SimpleResizer
{
public void CreateResizedObject(Vector3 newSize, GameObject parent, SimpleResizable sourcePrefab)
{
MarcsWebLogger.Log($"CRO: 1");
var prefab = MonoBehaviour.Instantiate(sourcePrefab.gameObject, Vector3.zero, Quaternion.identity);
MarcsWebLogger.Log($"CRO: 2");
prefab.name = sourcePrefab.name;
var resizable = prefab.GetComponent<SimpleResizable>();
resizable.NewSize = newSize;
MarcsWebLogger.Log($"CRO: 3");
if (resizable == null)
{
Debug.LogError("Resizable component missing.");
return;
}
MarcsWebLogger.Log($"CRO: 4");
var resizedMesh = ProcessVertices(resizable, newSize);
MarcsWebLogger.Log($"CRO: 5");
MeshFilter mf = prefab.GetComponent<MeshFilter>();
mf.sharedMesh = resizedMesh;
MarcsWebLogger.Log($"CRO: 6");
mf.sharedMesh.RecalculateBounds();
MarcsWebLogger.Log($"CRO: 7");
// child it after creation so the bounds math plays nicely
prefab.transform.parent = parent.transform;
prefab.transform.localPosition = Vector3.zero;
prefab.transform.localRotation = Quaternion.identity;
MarcsWebLogger.Log($"CRO: 8");
// cleanup
MonoBehaviour.Destroy(resizable);
MarcsWebLogger.Log($"CRO: 9");
}
#region PRIVATE METHODS
private Mesh ProcessVertices(SimpleResizable resizable, Vector3 newSize)
{
MarcsWebLogger.Log($"PV: 1a");
Mesh originalMesh = resizable.Mesh;
MarcsWebLogger.Log($"PV: 1b");
Vector3 originalBounds = originalMesh.bounds.size;
MarcsWebLogger.Log($"PV: 1c");
// Force scaling if newSize is smaller than the original mesh
SimpleResizable.Method methodX = (originalBounds.x < newSize.x)
? resizable.ScalingX
: SimpleResizable.Method.Scale;
MarcsWebLogger.Log($"PV: 1d");
SimpleResizable.Method methodY = (originalBounds.y < newSize.y)
? resizable.ScalingY
: SimpleResizable.Method.Scale;
MarcsWebLogger.Log($"PV: 1e");
SimpleResizable.Method methodZ = (originalBounds.z < newSize.z)
? resizable.ScalingZ
: SimpleResizable.Method.Scale;
MarcsWebLogger.Log($"PV: 1f");
Vector3[] resizedVertices = originalMesh.vertices;
MarcsWebLogger.Log($"PV: 2");
float pivotX = (1 / resizable.DefaultSize.x) * resizable.PivotPosition.x;
float pivotY = (1 / resizable.DefaultSize.y) * resizable.PivotPosition.y;
float pivotZ = (1 / resizable.DefaultSize.z) * resizable.PivotPosition.z;
for (int i = 0; i < resizedVertices.Length; i++)
{
Vector3 vertexPosition = resizedVertices[i];
vertexPosition.x = CalculateNewVertexPosition(
methodX,
vertexPosition.x,
originalBounds.x,
newSize.x,
resizable.PaddingX,
resizable.PaddingXMax,
pivotX);
vertexPosition.y = CalculateNewVertexPosition(
methodY,
vertexPosition.y,
originalBounds.y,
newSize.y,
resizable.PaddingY,
resizable.PaddingYMax,
pivotY);
vertexPosition.z = CalculateNewVertexPosition(
methodZ,
vertexPosition.z,
originalBounds.z,
newSize.z,
resizable.PaddingZ,
resizable.PaddingZMax,
pivotZ);
resizedVertices[i] = vertexPosition;
}
MarcsWebLogger.Log($"PV: 4");
Mesh clonedMesh = MonoBehaviour.Instantiate(originalMesh);
clonedMesh.vertices = resizedVertices;
MarcsWebLogger.Log($"PV: 5");
return clonedMesh;
}
private float CalculateNewVertexPosition(
SimpleResizable.Method resizeMethod,
float currentPosition,
float currentSize,
float newSize,
float padding,
float paddingMax,
float pivot)
{
float resizedRatio = currentSize / 2
* (newSize / 2 * (1 / (currentSize / 2)))
- currentSize / 2;
switch (resizeMethod)
{
case SimpleResizable.Method.Adapt:
if (Mathf.Abs(currentPosition) >= padding)
currentPosition = resizedRatio * Mathf.Sign(currentPosition) + currentPosition;
break;
case SimpleResizable.Method.AdaptWithAsymmetricalPadding:
if (currentPosition >= padding)
currentPosition = resizedRatio * Mathf.Sign(currentPosition) + currentPosition;
if (currentPosition <= paddingMax)
currentPosition = resizedRatio * Mathf.Sign(currentPosition) + currentPosition;
break;
case SimpleResizable.Method.Scale:
currentPosition = newSize / (currentSize / currentPosition);
break;
case SimpleResizable.Method.None:
break;
}
float pivotPos = newSize * (-pivot);
currentPosition += pivotPos;
return currentPosition;
}
#endregion
}