Somewhat working
This commit is contained in:
1 parent
7e3d43fd19
commit
16a9f846af
28 files changed
+1124
-101
No files matched your search
@@ -0,0 +1,140 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
public class SimpleResizable : MonoBehaviour
|
||||
{
|
||||
public Vector3 PivotPosition => _pivotTransform.position; //Vector3.zero;
|
||||
|
||||
[Space(15)] public Method ScalingX;
|
||||
[Range(0, 0.5f)] public float PaddingX;
|
||||
|
||||
[Range(-0.5f, 0)] public float PaddingXMax;
|
||||
|
||||
[Space(15)] public Method ScalingY;
|
||||
[Range(0, 0.5f)] public float PaddingY;
|
||||
|
||||
[Range(-0.5f, 0)] public float PaddingYMax;
|
||||
|
||||
[Space(15)] public Method ScalingZ;
|
||||
[Range(0, 0.5f)] public float PaddingZ;
|
||||
|
||||
[Range(-0.5f, 0)] public float PaddingZMax;
|
||||
|
||||
public enum Method
|
||||
{
|
||||
Adapt,
|
||||
AdaptWithAsymmetricalPadding,
|
||||
Scale,
|
||||
None
|
||||
}
|
||||
|
||||
public Vector3 NewSize {get; set; }
|
||||
public Vector3 DefaultSize {get; private set; }
|
||||
public Mesh Mesh { get; private set; }
|
||||
|
||||
private Bounds _bounds;
|
||||
|
||||
[SerializeField] private Transform _pivotTransform;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
Mesh = GetComponent<MeshFilter>().sharedMesh;
|
||||
DefaultSize = Mesh.bounds.size;
|
||||
if(!_pivotTransform)
|
||||
_pivotTransform = transform.Find("Pivot");
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
private void OnEnable()
|
||||
{
|
||||
DefaultSize = Mesh.bounds.size;
|
||||
NewSize = DefaultSize;
|
||||
}
|
||||
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if (!_pivotTransform) return;
|
||||
|
||||
Gizmos.color = Color.red;
|
||||
float lineSize = 0.1f;
|
||||
|
||||
Vector3 startX = _pivotTransform.position + Vector3.left * lineSize * 0.5f;
|
||||
Vector3 startY = _pivotTransform.position + Vector3.down * lineSize * 0.5f;
|
||||
Vector3 startZ = _pivotTransform.position + Vector3.back * lineSize * 0.5f;
|
||||
|
||||
Gizmos.DrawRay(startX, Vector3.right * lineSize);
|
||||
Gizmos.DrawRay(startY, Vector3.up * lineSize);
|
||||
Gizmos.DrawRay(startZ, Vector3.forward * lineSize);
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
DefaultSize = Mesh.bounds.size;
|
||||
|
||||
if (GetComponent<MeshFilter>().sharedMesh == null)
|
||||
{
|
||||
// The furniture piece was not customized yet, nothing to do here
|
||||
return;
|
||||
}
|
||||
|
||||
_bounds = GetComponent<MeshFilter>().sharedMesh.bounds;
|
||||
Gizmos.matrix = transform.localToWorldMatrix;
|
||||
Vector3 newCenter = _bounds.center;
|
||||
|
||||
Gizmos.color = new Color(1, 0, 0, 0.5f);
|
||||
switch (ScalingX)
|
||||
{
|
||||
case Method.Adapt:
|
||||
Gizmos.DrawWireCube(newCenter, new Vector3(NewSize.x * PaddingX * 2, NewSize.y, NewSize.z));
|
||||
break;
|
||||
case Method.AdaptWithAsymmetricalPadding:
|
||||
Gizmos.DrawWireCube(newCenter + new Vector3(
|
||||
NewSize.x * PaddingX, 0, 0), new Vector3(0, NewSize.y, NewSize.z));
|
||||
Gizmos.DrawWireCube(newCenter + new Vector3(
|
||||
NewSize.x * PaddingXMax, 0, 0), new Vector3(0, NewSize.y, NewSize.z));
|
||||
break;
|
||||
case Method.None:
|
||||
Gizmos.DrawWireCube(newCenter, NewSize);
|
||||
break;
|
||||
}
|
||||
|
||||
Gizmos.color = new Color(0, 1, 0, 0.5f);
|
||||
switch (ScalingY)
|
||||
{
|
||||
case Method.Adapt:
|
||||
Gizmos.DrawWireCube(newCenter, new Vector3(NewSize.x, NewSize.y * PaddingY * 2, NewSize.z));
|
||||
break;
|
||||
case Method.AdaptWithAsymmetricalPadding:
|
||||
Gizmos.DrawWireCube(newCenter + new Vector3(0, NewSize.y * PaddingY, 0),
|
||||
new Vector3(NewSize.x, 0, NewSize.z));
|
||||
Gizmos.DrawWireCube(newCenter + new Vector3(0, NewSize.y * PaddingYMax, 0),
|
||||
new Vector3(NewSize.x, 0, NewSize.z));
|
||||
break;
|
||||
case Method.None:
|
||||
Gizmos.DrawWireCube(newCenter, NewSize);
|
||||
break;
|
||||
}
|
||||
|
||||
Gizmos.color = new Color(0, 0, 1, 0.5f);
|
||||
switch (ScalingZ)
|
||||
{
|
||||
case Method.Adapt:
|
||||
Gizmos.DrawWireCube(newCenter, new Vector3(NewSize.x, NewSize.y, NewSize.z * PaddingZ * 2));
|
||||
break;
|
||||
case Method.AdaptWithAsymmetricalPadding:
|
||||
Gizmos.DrawWireCube(newCenter + new Vector3(0, 0, NewSize.z * PaddingZ),
|
||||
new Vector3(NewSize.x, NewSize.y, 0));
|
||||
Gizmos.DrawWireCube(newCenter + new Vector3(0, 0, NewSize.z * PaddingZMax),
|
||||
new Vector3(NewSize.x, NewSize.y, 0));
|
||||
break;
|
||||
case Method.None:
|
||||
Gizmos.DrawWireCube(newCenter, NewSize);
|
||||
break;
|
||||
}
|
||||
|
||||
Gizmos.color = new Color(0, 1, 1, 1);
|
||||
Gizmos.DrawWireCube(newCenter, NewSize);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 39b243a9b1035c94e9f75c4e11283893
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,138 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class SimpleResizer
|
||||
{
|
||||
public void CreateResizedObject(Vector3 newSize, GameObject parent, SimpleResizable sourcePrefab)
|
||||
{
|
||||
MarcsWebLogger.Log($"Resize {newSize}");
|
||||
var prefab = MonoBehaviour.Instantiate(sourcePrefab.gameObject, Vector3.zero, Quaternion.identity);
|
||||
prefab.name = sourcePrefab.name;
|
||||
|
||||
var resizable = prefab.GetComponent<SimpleResizable>();
|
||||
resizable.NewSize = newSize;
|
||||
if (resizable == null)
|
||||
{
|
||||
Debug.LogError("Resizable component missing.");
|
||||
return;
|
||||
}
|
||||
|
||||
var resizedMesh = ProcessVertices(resizable, newSize);
|
||||
|
||||
MeshFilter mf = prefab.GetComponent<MeshFilter>();
|
||||
mf.sharedMesh = resizedMesh;
|
||||
mf.sharedMesh.RecalculateBounds();
|
||||
|
||||
// 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;
|
||||
|
||||
// cleanup
|
||||
MonoBehaviour.Destroy(resizable);
|
||||
}
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
private Mesh ProcessVertices(SimpleResizable resizable, Vector3 newSize)
|
||||
{
|
||||
Mesh originalMesh = resizable.Mesh;
|
||||
Vector3 originalBounds = originalMesh.bounds.size;
|
||||
|
||||
// Force scaling if newSize is smaller than the original mesh
|
||||
SimpleResizable.Method methodX = (originalBounds.x < newSize.x)
|
||||
? resizable.ScalingX
|
||||
: SimpleResizable.Method.Scale;
|
||||
SimpleResizable.Method methodY = (originalBounds.y < newSize.y)
|
||||
? resizable.ScalingY
|
||||
: SimpleResizable.Method.Scale;
|
||||
SimpleResizable.Method methodZ = (originalBounds.z < newSize.z)
|
||||
? resizable.ScalingZ
|
||||
: SimpleResizable.Method.Scale;
|
||||
|
||||
Vector3[] resizedVertices = originalMesh.vertices;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Mesh clonedMesh = MonoBehaviour.Instantiate(originalMesh);
|
||||
clonedMesh.vertices = resizedVertices;
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 85f4ec0cb9fef7b4e9223c280e418389
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in new issue
Block a user