1
0
forked from cgvr/DeltaVR

generated objects are grabbable and scalable

This commit is contained in:
2026-01-13 19:22:44 +02:00
parent 1cd97e4d0a
commit 33a40c987a
4 changed files with 109 additions and 8 deletions

View File

@@ -67,17 +67,19 @@ public class ShapeDetectionMinigameController : MonoBehaviour
private void InitializeSpawnedObject(GameObject spawnedObject)
{
Rigidbody rigidbody = spawnedObject.AddComponent<Rigidbody>();
GameObject spawnedObjectParent = new GameObject("SpawnedModelParent");
spawnedObjectParent.transform.parent = modelSpawnPoint;
spawnedObjectParent.transform.position = modelSpawnPoint.transform.position;
Rigidbody rigidbody = spawnedObjectParent.AddComponent<Rigidbody>();
rigidbody.isKinematic = true;
//spawnedObject.AddComponent<NetworkObject>();
//spawnedObject.AddComponent<NetworkTransform>();
spawnedObject.transform.parent = modelSpawnPoint;
spawnedObject.transform.position = modelSpawnPoint.position;
spawnedObject.transform.parent = spawnedObjectParent.transform;
spawnedObject.transform.position = spawnedObjectParent.transform.position;
spawnedObject.tag = shapeScannerTag;
spawnedObject.SetActive(true);
spawnedObject.AddComponent<XRGrabInteractable>();
spawnedObjectParent.AddComponent<TwoHandScaleGrabInteractable>();
}
}

View File

@@ -0,0 +1,88 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class TwoHandScaleGrabInteractable : XRGrabInteractable
{
[Header("Two-Hand Scaling")]
public float minScale = 0.1f;
public float maxScale = 5f;
public float scaleLerpSpeed = 0f;
private Transform scalingTransform;
private float initialHandsDistance;
private Vector3 initialScale;
private bool twoHandsGrabbing;
void Start()
{
selectMode = InteractableSelectMode.Multiple;
useDynamicAttach = true;
reinitializeDynamicAttachEverySingleGrab = false;
scalingTransform = transform.GetChild(0);
}
protected override void OnSelectEntered(SelectEnterEventArgs args)
{
base.OnSelectEntered(args);
UpdateTwoHandState();
}
protected override void OnSelectExited(SelectExitEventArgs args)
{
Debug.Log("current local scale: " + transform.localScale);
base.OnSelectExited(args);
UpdateTwoHandState();
}
void UpdateTwoHandState()
{
if (interactorsSelecting.Count >= 2)
{
var a = interactorsSelecting[0];
var b = interactorsSelecting[1];
var pA = a.GetAttachTransform(this).position;
var pB = b.GetAttachTransform(this).position;
initialHandsDistance = Vector3.Distance(pA, pB);
initialScale = scalingTransform.localScale;
twoHandsGrabbing = initialHandsDistance > 0.0001f;
}
else
{
twoHandsGrabbing = false;
}
}
public override void ProcessInteractable(XRInteractionUpdateOrder.UpdatePhase updatePhase)
{
base.ProcessInteractable(updatePhase);
if (!twoHandsGrabbing || interactorsSelecting.Count < 2 || initialHandsDistance <= 0.0001f)
return;
var a = interactorsSelecting[0];
var b = interactorsSelecting[1];
var pA = a.GetAttachTransform(this).position;
var pB = b.GetAttachTransform(this).position;
float current = Vector3.Distance(pA, pB);
if (current <= 0.0001f)
return;
float ratio = current / initialHandsDistance;
Debug.Log("distance ratio: " + ratio);
Vector3 desired = initialScale * ratio;
float uniform = Mathf.Clamp(desired.x, minScale, maxScale);
desired = new Vector3(uniform, uniform, uniform);
if (scaleLerpSpeed > 0f)
desired = Vector3.Lerp(transform.localScale, desired, Time.deltaTime * scaleLerpSpeed);
scalingTransform.localScale = desired;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 22c2ea15df0386746b1b1bc7b58bced7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: