2
0

SquishyToy.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR;
  5. using Valve.VR.InteractionSystem;
  6. namespace Valve.VR.InteractionSystem.Sample
  7. {
  8. public class SquishyToy : MonoBehaviour
  9. {
  10. public Interactable interactable;
  11. public new SkinnedMeshRenderer renderer;
  12. public bool affectMaterial = true;
  13. public SteamVR_Action_Single gripSqueeze = SteamVR_Input.GetAction<SteamVR_Action_Single>("Squeeze");
  14. public SteamVR_Action_Single pinchSqueeze = SteamVR_Input.GetAction<SteamVR_Action_Single>("Squeeze");
  15. private new Rigidbody rigidbody;
  16. private void Start()
  17. {
  18. if (rigidbody == null)
  19. rigidbody = GetComponent<Rigidbody>();
  20. if (interactable == null)
  21. interactable = GetComponent<Interactable>();
  22. if (renderer == null)
  23. renderer = GetComponent<SkinnedMeshRenderer>();
  24. }
  25. private void Update()
  26. {
  27. float grip = 0;
  28. float pinch = 0;
  29. if (interactable.attachedToHand)
  30. {
  31. grip = gripSqueeze.GetAxis(interactable.attachedToHand.handType);
  32. pinch = pinchSqueeze.GetAxis(interactable.attachedToHand.handType);
  33. }
  34. renderer.SetBlendShapeWeight(0, Mathf.Lerp(renderer.GetBlendShapeWeight(0), grip * 100, Time.deltaTime * 10));
  35. if (renderer.sharedMesh.blendShapeCount > 1) // make sure there's a pinch blend shape
  36. renderer.SetBlendShapeWeight(1, Mathf.Lerp(renderer.GetBlendShapeWeight(1), pinch * 100, Time.deltaTime * 10));
  37. if (affectMaterial)
  38. {
  39. renderer.material.SetFloat("_Deform", Mathf.Pow(grip * 1f, 0.5f));
  40. if (renderer.material.HasProperty("_PinchDeform"))
  41. {
  42. renderer.material.SetFloat("_PinchDeform", Mathf.Pow(pinch * 1f, 0.5f));
  43. }
  44. }
  45. }
  46. }
  47. }