diff --git a/Assets/Samples/XR Interaction Toolkit/2.4.3/Starter Assets/XRI Default Input Actions.inputactions b/Assets/Samples/XR Interaction Toolkit/2.4.3/Starter Assets/XRI Default Input Actions.inputactions index 191f40d4dfc357da778d46de4e52b0824fa9f5bc..16ee2ded7bb74069cb929ff2d912a4927648a7af 100644 --- a/Assets/Samples/XR Interaction Toolkit/2.4.3/Starter Assets/XRI Default Input Actions.inputactions +++ b/Assets/Samples/XR Interaction Toolkit/2.4.3/Starter Assets/XRI Default Input Actions.inputactions @@ -1419,22 +1419,13 @@ "initialStateCheck": true }, { - "name": "CreateMesh", + "name": "SpawnBlock", "type": "Button", "id": "75035d52-623c-4514-bc79-25c62c88c921", "expectedControlType": "Button", "processors": "", "interactions": "", "initialStateCheck": false - }, - { - "name": "Custom Mesh", - "type": "Button", - "id": "ca5a54af-440a-460c-90ee-acc8ac1aab3c", - "expectedControlType": "Button", - "processors": "", - "interactions": "", - "initialStateCheck": false } ], "bindings": [ @@ -1588,18 +1579,7 @@ "interactions": "", "processors": "", "groups": "Generic XR Controller", - "action": "CreateMesh", - "isComposite": false, - "isPartOfComposite": false - }, - { - "name": "", - "id": "70ffdb01-2580-42ce-9ba0-0a94c26a7996", - "path": "<XRController>{RightHand}/{TriggerButton}", - "interactions": "", - "processors": "", - "groups": "Generic XR Controller", - "action": "Custom Mesh", + "action": "SpawnBlock", "isComposite": false, "isPartOfComposite": false } diff --git a/Assets/Scenes/SampleScene.unity b/Assets/Scenes/SampleScene.unity index c07b7440721cf6df6cca97ca421259567186924d..b730aa65ae0f573736c6fd214d89dd647fe63fe3 100644 --- a/Assets/Scenes/SampleScene.unity +++ b/Assets/Scenes/SampleScene.unity @@ -4471,9 +4471,8 @@ GameObject: m_Component: - component: {fileID: 426651920} - component: {fileID: 426651919} - - component: {fileID: 426651921} m_Layer: 0 - m_Name: Create Mesh Handler + m_Name: Spawn Block Handler m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -4509,21 +4508,6 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!114 &426651921 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 426651918} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: f578074ddc67d67499cfd53336b86530, type: 3} - m_Name: - m_EditorClassIdentifier: - spawnPrefab: {fileID: 0} - inputTransform: {fileID: 1044421913} - triggerEventAction: {fileID: -8447281263088936906, guid: c348712bda248c246b8c49b3db54643f, type: 3} --- !u!1001 &427008753 PrefabInstance: m_ObjectHideFlags: 0 @@ -18756,7 +18740,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 +18768,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 +18804,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 +18824,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 +18860,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 +18880,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 +18908,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 6f530ecc84606173ce9b68170109b5f3e58fe441..c62d80f203df66f6e8338689adcf6fa53431e5fb 100644 --- a/Assets/Scrips/BlockDeform.cs +++ b/Assets/Scrips/BlockDeform.cs @@ -23,19 +23,29 @@ public class BlockDeform : MonoBehaviour public void HandleAnchorGrabbed(Transform endPosition, Vector3 startPosition, DeformAnchor anchor) { - Vector3 normal = anchor.GetNormal(); + Vector3 normal = this.transform.rotation.normalized * anchor.GetNormal(); // calculate deformation Vector3[] deformedMeshVertices = _mesh.vertices; for (int i = 0; i < deformedMeshVertices.Length; i++) { - if (IsPointOnPlane(deformedMeshVertices[i] + this.transform.position, normal, startPosition, 0.1d)) + Vector3 vertex = (this.transform.rotation.normalized * deformedMeshVertices[i]) + this.transform.position; + if (IsPointOnPlane(vertex, normal, startPosition, 0.1d)) { float distance = Vector3.Distance(startPosition, endPosition.position); - Vector3 displacement = normal * distance; + + // Vector3 displacement = endPosition.position - startPosition; + // deformedMeshVertices[i] += displacement; + // Debug.Log("startpos: " + startPosition + " normal: " + normal + " vertex: " + vertex); + + Vector3 displacement = anchor.GetNormal() * distance; float direction = Vector3.Dot(endPosition.position - startPosition, normal); deformedMeshVertices[i] += displacement * (direction > 0 ? 1 : -1); } + else + { + // Debug.LogError("startpos: " + startPosition + " normal: " + normal + " vertex: " + vertex); + } } // check if it's a valid grab @@ -69,21 +79,25 @@ public class BlockDeform : MonoBehaviour { List<Vector3> vertices = new(); - Vector3 anchorNormal = _deformAnchors[i].GetNormal(); + Vector3 anchorNormal = this.transform.rotation.normalized * _deformAnchors[i].GetNormal(); Vector3 anchorPosition = _deformAnchors[i].GetPosition(); foreach (Vector3 vertex in _mesh.vertices) { - if (IsPointOnPlane(vertex + transform.position, anchorNormal, anchorPosition, 0.1d)) + Vector3 rotatedVert = this.transform.rotation.normalized * vertex; + if (IsPointOnPlane(rotatedVert + this.transform.position, anchorNormal, anchorPosition, 0.1d)) { - if (vertices.IndexOf(vertex) == -1) + if (vertices.IndexOf(rotatedVert) == -1) { - vertices.Add(vertex); + vertices.Add(rotatedVert); } } } - _deformAnchors[i].SetMeshScale(vertices); + if (vertices.Count >= 4) + _deformAnchors[i].SetMeshScale(vertices); + else + Debug.LogError("Couldn't find fitting vertices to update the deform anchors."); } } @@ -130,6 +144,11 @@ public class BlockDeform : MonoBehaviour return transform.position; } + public Quaternion GetRotation() + { + return transform.rotation; + } + public void EnableTransparency() { diff --git a/Assets/Scrips/DeformAnchor.cs b/Assets/Scrips/DeformAnchor.cs index 9c5897762c4f29d5d67a7456dc57a209fd17982c..f91cf4b08e6c285fccbc5ac587ebb9633cdcd0fa 100644 --- a/Assets/Scrips/DeformAnchor.cs +++ b/Assets/Scrips/DeformAnchor.cs @@ -16,7 +16,8 @@ public class DeformAnchor : MonoBehaviour private XRGrabInteractable grabInteractable; private Mesh mesh; - public void SetupAnchor(BlockDeform blockDeform, Vector3 normal, Vector3 bounds, float offset) { + public void SetupAnchor(BlockDeform blockDeform, Vector3 normal, Vector3 bounds, float offset) + { this._blockDeform = blockDeform; this._normal = normal; this._offset = offset; @@ -33,7 +34,8 @@ public class DeformAnchor : MonoBehaviour _position.transform.position = transform.position + (-_normal * _offset); } - private void Awake() { + private void Awake() + { grabInteractable = GetComponent<XRGrabInteractable>(); grabInteractable.selectEntered.AddListener(HandleSelectEnter); grabInteractable.selectExited.AddListener(HandleSelectExit); @@ -70,62 +72,66 @@ public class DeformAnchor : MonoBehaviour // Update is called once per frame void Update() { - + } - public void SetMeshScale(List<Vector3> vertices) { - + public void SetMeshScale(List<Vector3> vertices) + { + // calculate center point for the anchor - float x = vertices[0].x; - float y = vertices[0].y; - float z = vertices[0].z; + Vector3 center = vertices[0]; for (int i = 1; i < vertices.Count; i++) { - x += vertices[i].x; - y += vertices[i].y; - z += vertices[i].z; + center += vertices[i]; } - Vector3 center = new(x / 4, y / 4, z / 4); - this.transform.position = _blockDeform.GetPosition() + center + _normal * _offset; + center /= 4; + this.transform.position = _blockDeform.GetPosition() + center + _blockDeform.GetRotation().normalized * _normal * _offset; - Vector3 scaleVec = vertices[0] - center; + // calculate scale multiplier + Vector3 scaleVec = Quaternion.Inverse(_blockDeform.GetRotation().normalized) * (vertices[0] - center); double epsilon = 0.1; int zeroCntr = 0; Vector3 newVertex = new(0, 0, 0); for (int i = 0; i < 3; i++) { - if (Math.Abs(_normal[i]) < epsilon && zeroCntr < 2) { + if (Math.Abs(_normal[i]) < epsilon && zeroCntr < 2) + { newVertex[zeroCntr] = scaleVec[i]; zeroCntr++; } } - Debug.Log(scaleVec.ToString() + " reordered: " + newVertex.ToString()); + // Debug.Log(scaleVec.ToString() + " reordered: " + newVertex.ToString()); - 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); + 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); } - public Vector3 GetNormal() { + public Vector3 GetNormal() + { return _normal; } - public Vector3 GetPosition() { + public Vector3 GetPosition() + { return _position.transform.position; } - public void ResetPosition() { - this.transform.position = _previousPosition + _normal * _offset; + public void ResetPosition() + { + this.transform.position = _previousPosition + _blockDeform.GetRotation().normalized * _normal * _offset; } - private void OnDrawGizmosSelected() { + private void OnDrawGizmosSelected() + { Gizmos.color = Color.yellow; - Gizmos.DrawLine(transform.position, transform.position + _normal); - + Gizmos.DrawLine(transform.position, transform.position + (_blockDeform.GetRotation().normalized * _normal)); } } diff --git a/Assets/Scrips/TriggerEvent.cs b/Assets/Scrips/SpawnBlockEvent.cs similarity index 90% rename from Assets/Scrips/TriggerEvent.cs rename to Assets/Scrips/SpawnBlockEvent.cs index ca44cb517e87f24610927ba85c4f3c1ce1b8fa71..150ebd7aac73d4b6148cd1f2bdbd47de613e6617 100644 --- a/Assets/Scrips/TriggerEvent.cs +++ b/Assets/Scrips/SpawnBlockEvent.cs @@ -5,25 +5,28 @@ using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UI; -public class TriggerEvent : MonoBehaviour +public class SpawnBlockEvent : MonoBehaviour { public GameObject spawnPrefab; public Transform inputTransform; public InputActionReference triggerEventAction; - private void Awake() { + private void Awake() + { triggerEventAction.action.Enable(); triggerEventAction.action.performed += SpawnMesh; InputSystem.onDeviceChange += OnDeviceChange; } - private void OnDestroy() { + private void OnDestroy() + { triggerEventAction.action.Disable(); triggerEventAction.action.performed -= SpawnMesh; InputSystem.onDeviceChange -= OnDeviceChange; } - private void SpawnMesh(InputAction.CallbackContext context){ + private void SpawnMesh(InputAction.CallbackContext context) + { if (Physics.Raycast(inputTransform.position, inputTransform.forward, out RaycastHit hit)) { @@ -38,8 +41,10 @@ public class TriggerEvent : MonoBehaviour } } - private void OnDeviceChange(InputDevice device, InputDeviceChange change){ - switch(change){ + private void OnDeviceChange(InputDevice device, InputDeviceChange change) + { + switch (change) + { case InputDeviceChange.Disconnected: triggerEventAction.action.Disable(); triggerEventAction.action.performed -= SpawnMesh; @@ -64,6 +69,6 @@ public class TriggerEvent : MonoBehaviour // Update is called once per frame void Update() { - + } } diff --git a/Assets/Scrips/TriggerEvent.cs.meta b/Assets/Scrips/SpawnBlockEvent.cs.meta similarity index 100% rename from Assets/Scrips/TriggerEvent.cs.meta rename to Assets/Scrips/SpawnBlockEvent.cs.meta diff --git a/Assets/Shaders/DeformTextureMat.mat b/Assets/Shaders/DeformTextureMat.mat index 85f74f45c5766671b39964eb97d8c49e0a42addf..a0a8098ca91305b34f53bbf592a60105cbf9ec71 100644 --- a/Assets/Shaders/DeformTextureMat.mat +++ b/Assets/Shaders/DeformTextureMat.mat @@ -16,7 +16,7 @@ Material: m_LightmapFlags: 2 m_EnableInstancingVariants: 0 m_DoubleSidedGI: 0 - m_CustomRenderQueue: -1 + m_CustomRenderQueue: 3050 stringTagMap: {} disabledShaderPasses: [] m_LockedProperties: @@ -42,7 +42,7 @@ Material: m_Ints: [] m_Floats: - _QueueControl: 0 - - _QueueOffset: 0 + - _QueueOffset: 50 m_Colors: - _Color: {r: 0, g: 0.53119797, b: 0.9150943, a: 0} m_BuildTextureStacks: []