Tanoda
LeapBlendShapeData.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using Leap.Unity.Query;
14
16
17 public partial class LeapGraphic {
18
25 public void SetBlendShapeAmount(float amount) {
26 getFeatureDataOrThrow<LeapBlendShapeData>().amount = amount;
27 }
28
35 public float GetBlendShapeAmount() {
36 return getFeatureDataOrThrow<LeapBlendShapeData>().amount;
37 }
38 }
39
40 [LeapGraphicTag("Blend Shape")]
41 [Serializable]
43
44 [Range(0, 1)]
45 [SerializeField]
46 private float _amount = 0;
47
49 [SerializeField]
50 private BlendShapeType _type = BlendShapeType.Scale;
51
53 [MinValue(0)]
54 [SerializeField]
55 private float _scale = 1.1f;
56
58 [SerializeField]
59 private Vector3 _translation = new Vector3(0, 0, 0.1f);
60
62 [SerializeField]
63 private Vector3 _rotation = new Vector3(0, 0, 5);
64
66 [SerializeField]
67 private Transform _transform = null;
68
74 public float amount {
75 get {
76 return _amount;
77 }
78 set {
79 if (value != _amount) {
81 _amount = value;
82 }
83 }
84 }
85
92 public bool TryGetBlendShape(List<Vector3> blendVerts) {
93 if (!(graphic is LeapMeshGraphicBase)) {
94 return false;
95 }
96
97 var meshGraphic = graphic as LeapMeshGraphicBase;
98
99 var mesh = meshGraphic.mesh;
100
101 if (mesh == null) {
102 return false;
103 }
104
105 if (_type == BlendShapeType.Mesh) {
106 if (mesh.blendShapeCount <= 0) {
107 return false;
108 }
109
110 Vector3[] deltaVerts = new Vector3[mesh.vertexCount];
111 Vector3[] deltaNormals = new Vector3[mesh.vertexCount];
112 Vector3[] deltaTangents = new Vector3[mesh.vertexCount];
113 mesh.GetBlendShapeFrameVertices(0, 0, deltaVerts, deltaNormals, deltaTangents);
114
115 mesh.vertices.Query().Zip(deltaVerts.Query(), (a, b) => a + b).FillList(blendVerts);
116 return true;
117 } else {
118 Matrix4x4 transformation;
119
120 switch (_type) {
121 case BlendShapeType.Translation:
122 transformation = Matrix4x4.TRS(_translation, Quaternion.identity, Vector3.one);
123 break;
124 case BlendShapeType.Rotation:
125 transformation = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(_rotation), Vector3.one);
126 break;
127 case BlendShapeType.Scale:
128 transformation = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one * _scale);
129 break;
130 case BlendShapeType.Transform:
131 if (_transform == null) {
132 return false;
133 }
134 transformation = graphic.transform.worldToLocalMatrix * _transform.localToWorldMatrix;
135 break;
136 default:
137 throw new InvalidOperationException();
138 }
139
140 mesh.vertices.Query().Select(transformation.MultiplyPoint).FillList(blendVerts);
141 return true;
142 }
143 }
144
145 public enum BlendShapeType {
146 Translation,
147 Rotation,
148 Scale,
149 Transform,
150 Mesh
151 }
152 }
153}
bool TryGetBlendShape(List< Vector3 > blendVerts)
Returns the blended vertices based on the current mesh this blend shape is attached to....
float amount
Gets or sets the amount of strength for this blend shape. The value should be in the 0-1 range,...
float GetBlendShapeAmount()
Helper method to get the blend shape amount for a blend shape data object attached to this graphic....
void SetBlendShapeAmount(float amount)
Helper method to set the blend shape amount for a blend shape data object attached to this graphic....
This class is a base class for all graphics that can be represented by a mesh object.
Mesh mesh
Returns the mesh that represents this graphic. It can have any topology, any number of uv channels,...