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