From 3452d559250a72dd7364ff189e13302547dc0e3c Mon Sep 17 00:00:00 2001 From: "bocska.karina" <bocska.karina@hallgato.ppke.hu> Date: Fri, 13 Sep 2024 15:16:40 +0200 Subject: [PATCH] fix: position and scale of deformAnchors --- Assets/Scenes/SampleScene.unity | 14 +-- Assets/Scrips/BlockDeform.cs | 29 +++---- Assets/Scrips/DeformAnchor.cs | 147 ++++++-------------------------- 3 files changed, 41 insertions(+), 149 deletions(-) diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c07b744..776b10c 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -18756,7 +18756,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668758913251958, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 44.7 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668758913251958, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y @@ -18784,7 +18784,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668759388320319, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 40 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668759388320319, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y @@ -18820,7 +18820,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668759906836222, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 40 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668759906836222, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y @@ -18840,7 +18840,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668759991436502, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 40 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668759991436502, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y @@ -18876,7 +18876,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668760453204319, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 44.7 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668760453204319, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y @@ -18896,7 +18896,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668760497519799, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 40 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668760497519799, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y @@ -18924,7 +18924,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 1984668760711245992, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.x - value: 40 + value: 0 objectReference: {fileID: 0} - target: {fileID: 1984668760711245992, guid: 65eb6b98091a5734ba9d9ca871cef69a, type: 3} propertyPath: m_AnchoredPosition.y diff --git a/Assets/Scrips/BlockDeform.cs b/Assets/Scrips/BlockDeform.cs index fe28cfd..922559a 100644 --- a/Assets/Scrips/BlockDeform.cs +++ b/Assets/Scrips/BlockDeform.cs @@ -11,14 +11,10 @@ public class BlockDeform : MonoBehaviour private Mesh mesh; private GameObject[] deformAnchors = new GameObject[6]; - private bool isDeforming = false; public void HandleAnchorGrabbed(Transform endPosition, Vector3 startPosition, Vector3 normal){ - // Debug.Log(startPosition.ToString()); - // Debug.Log(endPosition.position.ToString()); - // Debug.Log(this.transform.position.ToString()); - + // utility function to check if the point is on the grabbed anchor bool isPointOnPlane(Vector3 point, double epsilon){ double d = Vector3.Dot(normal, startPosition); double eq = normal.x * point.x + normal.y * point.y + normal.z * point.z - d; @@ -27,25 +23,21 @@ public class BlockDeform : MonoBehaviour else return false; } - // Debug.Log("checking vertices"); - - Vector3[] newVertices = mesh.vertices; - - for (int i = 0; i < newVertices.Length; i++) + // calculate deformation + Vector3[] deformedMeshVertices = mesh.vertices; + for (int i = 0; i < deformedMeshVertices.Length; i++) { - if (isPointOnPlane(newVertices[i] + this.transform.position, 0.1d)) { + if (isPointOnPlane(deformedMeshVertices[i] + this.transform.position, 0.1d)) { float distance = Vector3.Distance(startPosition, endPosition.position); Vector3 displacement = normal * distance; float direction = Vector3.Dot(endPosition.position - startPosition, normal); - // Debug.Log(startPosition.ToString() + " to " + endPosition.position.ToString() + " with normal: " + normal.ToString() + "results in dot product: " + direction); - newVertices[i] += displacement * (direction > 0 ? 1 : -1); + deformedMeshVertices[i] += displacement * (direction > 0 ? 1 : -1); } } - mesh.vertices = newVertices; + // set new vertices for mesh + mesh.vertices = deformedMeshVertices; mesh.RecalculateBounds(); - // DestroyImmediate(GetComponent<MeshCollider>()); - // MeshCollider collider = gameObject.AddComponent<MeshCollider>(); MeshCollider collider = GetComponent<MeshCollider>(); collider.sharedMesh = mesh; @@ -62,7 +54,7 @@ public class BlockDeform : MonoBehaviour private void UpdateDeformAnchors() { - + // collect vertices on the same plane as the anchor for (int i = 0; i < deformAnchors.Length; i++) { List<Vector3> vertices = new(); @@ -86,7 +78,6 @@ public class BlockDeform : MonoBehaviour public void HandleAnchorGrabbed(){ - isDeforming = true; DeformMesh(); } @@ -296,7 +287,7 @@ public class BlockDeform : MonoBehaviour } public void ExitGrab(){ - isDeforming = false; + } diff --git a/Assets/Scrips/DeformAnchor.cs b/Assets/Scrips/DeformAnchor.cs index bfe7063..d402f86 100644 --- a/Assets/Scrips/DeformAnchor.cs +++ b/Assets/Scrips/DeformAnchor.cs @@ -75,142 +75,43 @@ public class DeformAnchor : MonoBehaviour } public void SetMeshScale(List<Vector3> vertices) { - List<Vector3> newVertices = new(); - double epsilon = 0.1; - - foreach (var vertex in vertices) - { - Vector3 newVertex = new(0, 0, 0); - - int zeroCntr = 0; - for (int i = 0; i < 3; i++) - { - if (_normal[i] > -epsilon && _normal[i] < epsilon && zeroCntr < 2) { - newVertex[zeroCntr] = vertex[i]; - zeroCntr++; - } - } - - // Debug.Log(newVertex.ToString()); - newVertices.Add(newVertex); - } - - // newVertices.SwapAtIndices(2, 3); - // mesh.vertices = newVertices.ToArray(); - // mesh.RecalculateBounds(); - // MeshCollider collider = this.GetComponent<MeshCollider>(); - // collider.sharedMesh = mesh; - - Vector3 firstVertex = newVertices[0]; - Vector3 secondVertex = newVertices[0]; - List<Vector3> otherVertex = new(); - for (int i = 1; i < newVertices.Count; i++) + + // calculate center point for the anchor + float x = vertices[0].x; + float y = vertices[0].y; + float z = vertices[0].z; + for (int i = 1; i < vertices.Count; i++) { - float dot = Vector3.Dot(firstVertex.normalized, newVertices[i].normalized); - Debug.Log(dot); - if (dot > -1-epsilon && dot < -1+epsilon) { - Vector3 route = vertices[i] - vertices[0]; - Vector3 center = vertices[0] + route/2; - Debug.Log(center.ToString() + " real pos: " + (_blockDeform.GetPosition() + center).ToString()); - this.transform.position = _blockDeform.GetPosition() + center + _normal * _offset; - secondVertex = newVertices[i]; - } else { - otherVertex.Add(newVertices[i]); - } + x += vertices[i].x; + y += vertices[i].y; + z += vertices[i].z; } - Vector3 axis1 = firstVertex - otherVertex[0]; - Vector3 axis2 = firstVertex - otherVertex[1]; - - Debug.Log("axis1 " + axis1.ToString() + " axis2 " + axis2.ToString()); - - // float d1 = Vector3.Dot(axis1.normalized, Vector3.right); - // if ((d1 > 1-epsilon && d1 < 1+epsilon) || (d1 > -1-epsilon && d1 < -1+epsilon)) { // is the same axis - // transform.localScale = new Vector3(Math.Abs(axis1.x), Math.Abs(axis2.y), 1); - // } else if (d1 > 0-epsilon && d1 < 0+epsilon) { - // transform.localScale = new Vector3(Math.Abs(axis2.x), Math.Abs(axis1.y), 1); - // } else { - // Debug.Log("we f-ed up"); - // } - - // float x = 0, y = 0; - // if (Math.Abs(firstVertex.x - otherVertex[0].x) < epsilon){ - // x = Math.Abs(firstVertex.x - otherVertex[1].x); - // y = Math.Abs(firstVertex.y - otherVertex[0].y); - // } else { - // x = Math.Abs(firstVertex.x - otherVertex[0].x); - // y = Math.Abs(firstVertex.y - otherVertex[1].y); - // } - - transform.localScale = axis1 + axis2 + Vector3.forward; - - // this is horrible but I've been debugging this for too many hours at this point - if (transform.right == Vector3.forward || -transform.right == Vector3.forward) { - transform.localScale = new Vector3((axis1+axis2).y, (axis1 + axis2).x, 1); - } - - // transform.localScale = new Vector3(secondVertex.x - firstVertex.x, secondVertex.y - firstVertex.y, 1); - } - - public void SetMeshVertices(List<Vector3> vertices) { + Vector3 center = new(x / 4, y / 4, z / 4); + this.transform.position = _blockDeform.GetPosition() + center + _normal * _offset; - // Debug.Log("block pos: " + _blockDeform.GetPosition().ToString()); - // Debug.Log("anchor pos: " + _position.transform.position.ToString() + " normal: " + _normal.ToString()); - - - List<Vector3> newVertices = new(); + Vector3 scaleVec = vertices[0] - center; double epsilon = 0.1; - - foreach (var vertex in vertices) + int zeroCntr = 0; + Vector3 newVertex = new(0, 0, 0); + for (int i = 0; i < 3; i++) { - Vector3 newVertex = new(0, 0, 0); - - int zeroCntr = 0; - for (int i = 0; i < 3; i++) - { - if (_normal[i] > -epsilon && _normal[i] < epsilon && zeroCntr < 2) { - newVertex[zeroCntr] = vertex[i]; - zeroCntr++; - } + if (Math.Abs(_normal[i]) < epsilon && zeroCntr < 2) { + newVertex[zeroCntr] = scaleVec[i]; + zeroCntr++; } - - // Debug.Log(newVertex.ToString()); - newVertices.Add(newVertex); } + Debug.Log(scaleVec.ToString() + " reordered: " + newVertex.ToString()); - Vector3 firstVertex = newVertices[0]; - List<Vector3> customVertex = new() - { - newVertices[0] - }; - for (int i = 1; i < newVertices.Count; i++) - { - float dot = Vector3.Dot(firstVertex, newVertices[i]); - if (dot > -epsilon && dot < epsilon) { - if (customVertex.Count == 1) { - customVertex.Add(newVertices[i]); - } else { - customVertex.Add(newVertices[i]); - mesh.vertices = new Vector3[]{ customVertex[0], customVertex[1], -customVertex[0], customVertex[2] }; - break; - } - } - } - - // mesh.vertices = newVertices.ToArray(); - mesh.RecalculateBounds(); - MeshCollider collider = this.GetComponent<MeshCollider>(); - collider.sharedMesh = mesh; - + if (Math.Abs(scaleVec.x) < epsilon) { + this.transform.localScale = new Vector3(Math.Abs(newVertex.y*2), Math.Abs(newVertex.x*2), 1); + } else + this.transform.localScale = new Vector3(Math.Abs(newVertex.x*2), Math.Abs(newVertex.y*2), 1); - // Debug.Log("anchor vertex: "); - foreach (var item in mesh.vertices) - { - Debug.Log(item.ToString()); - } } + public Vector3 GetNormal() { return _normal; } -- GitLab