t1 / TFDContents / Assets / KinectScripts / AvatarScaler.cs @ 9
이력 | 보기 | 이력해설 | 다운로드 (27.7 KB)
1 | 3 | KTH | using UnityEngine; |
---|---|---|---|
2 | using System.Collections; |
||
3 | |||
4 | /// <summary> |
||
5 | /// Avatar scaler is the component that scales avatar's body, according to body measures of the user. |
||
6 | /// </summary> |
||
7 | [RequireComponent(typeof(Animator))] |
||
8 | public class AvatarScaler : MonoBehaviour |
||
9 | { |
||
10 | [Tooltip("Index of the player, tracked by this component. 0 means the 1st player, 1 - the 2nd one, 2 - the 3rd one, etc.")] |
||
11 | public int playerIndex = 0; |
||
12 | |||
13 | [Tooltip("Whether the avatar is facing the player or not.")] |
||
14 | public bool mirroredAvatar = false; |
||
15 | |||
16 | [Tooltip("Body scale factor (incl. arms and legs) that may be used for fine tuning of model-scale.")] |
||
17 | [Range(0.0f, 2.0f)] |
||
18 | public float bodyScaleFactor = 1.0f; |
||
19 | |||
20 | [Tooltip("Body width scale factor that may be used for fine tuning of model-width scale.")] |
||
21 | [Range(0.0f, 2.0f)] |
||
22 | public float bodyWidthFactor = 1.0f; |
||
23 | |||
24 | [Tooltip("Additional scale factor for arms that may be used for fine tuning of model arm-scale.")] |
||
25 | [Range(0.0f, 2.0f)] |
||
26 | public float armScaleFactor = 1.0f; |
||
27 | |||
28 | [Tooltip("Additional scale factor for legs that may be used for fine tuning of model leg-scale.")] |
||
29 | [Range(0.0f, 2.0f)] |
||
30 | public float legScaleFactor = 1.0f; |
||
31 | |||
32 | [Tooltip("Whether the scale is updated continuously or just after the calibration pose.")] |
||
33 | public bool continuousScaling = true; |
||
34 | |||
35 | [Tooltip("Scale smoothing factor used in case of continuous scaling.")] |
||
36 | public float smoothFactor = 5f; |
||
37 | |||
38 | [Tooltip("Camera that may be used to overlay the model over the background.")] |
||
39 | public Camera foregroundCamera; |
||
40 | |||
41 | // [Tooltip("Whether to put the clothing model hip and shoulder joints where the user joints are.")] |
||
42 | // public bool fixModelHipsAndShoulders = false; |
||
43 | |||
44 | [Tooltip("GUI-Text to display the avatar-scaler debug messages.")] |
||
45 | public GUIText debugText; |
||
46 | |||
47 | // used by category selector |
||
48 | [System.NonSerialized] |
||
49 | public long currentUserId = 0; |
||
50 | |||
51 | // used by category selector |
||
52 | [System.NonSerialized] |
||
53 | public bool scalerInited = false; |
||
54 | |||
55 | // class references |
||
56 | private KinectManager kinectManager = null; |
||
57 | private AvatarController avtController = null; |
||
58 | |||
59 | // model transforms for scaling |
||
60 | private Transform bodyScaleTransform; |
||
61 | //private Transform bodyHipsTransform; |
||
62 | |||
63 | private Transform leftShoulderScaleTransform; |
||
64 | private Transform leftElbowScaleTransform; |
||
65 | private Transform rightShoulderScaleTransform; |
||
66 | private Transform rightElbowScaleTransform; |
||
67 | private Transform leftHipScaleTransform; |
||
68 | private Transform leftKneeScaleTransform; |
||
69 | private Transform rightHipScaleTransform; |
||
70 | private Transform rightKneeScaleTransform; |
||
71 | |||
72 | private Vector3 modelBodyScale = Vector3.one; |
||
73 | private Vector3 modelLeftShoulderScale = Vector3.one; |
||
74 | private Vector3 modelLeftElbowScale = Vector3.one; |
||
75 | private Vector3 modelRightShoulderScale = Vector3.one; |
||
76 | private Vector3 modelRightElbowScale = Vector3.one; |
||
77 | private Vector3 modelLeftHipScale = Vector3.one; |
||
78 | private Vector3 modelLeftKneeScale = Vector3.one; |
||
79 | private Vector3 modelRightHipScale = Vector3.one; |
||
80 | private Vector3 modelRightKneeScale = Vector3.one; |
||
81 | |||
82 | // model bone sizes and original scales |
||
83 | private float modelBodyHeight = 0f; |
||
84 | private float modelBodyWidth = 0f; |
||
85 | private float modelLeftUpperArmLength = 0f; |
||
86 | private float modelLeftLowerArmLength = 0f; |
||
87 | private float modelRightUpperArmLength = 0f; |
||
88 | private float modelRightLowerArmLength = 0f; |
||
89 | private float modelLeftUpperLegLength = 0f; |
||
90 | private float modelLeftLowerLegLength = 0f; |
||
91 | private float modelRightUpperLegLength = 0f; |
||
92 | private float modelRightLowerLegLength = 0f; |
||
93 | |||
94 | // user bone sizes |
||
95 | private float userBodyHeight = 0f; |
||
96 | private float userBodyWidth = 0f; |
||
97 | private float leftUpperArmLength = 0f; |
||
98 | private float leftLowerArmLength = 0f; |
||
99 | private float rightUpperArmLength = 0f; |
||
100 | private float rightLowerArmLength = 0f; |
||
101 | private float leftUpperLegLength = 0f; |
||
102 | private float leftLowerLegLength = 0f; |
||
103 | private float rightUpperLegLength = 0f; |
||
104 | private float rightLowerLegLength = 0f; |
||
105 | |||
106 | // user bone scale factors |
||
107 | private float fScaleBodyHeight = 0f; |
||
108 | private float fScaleBodyWidth = 0f; |
||
109 | private float fScaleLeftUpperArm = 0f; |
||
110 | private float fScaleLeftLowerArm = 0f; |
||
111 | private float fScaleRightUpperArm = 0f; |
||
112 | private float fScaleRightLowerArm = 0f; |
||
113 | private float fScaleLeftUpperLeg = 0f; |
||
114 | private float fScaleLeftLowerLeg = 0f; |
||
115 | private float fScaleRightUpperLeg = 0f; |
||
116 | private float fScaleRightLowerLeg = 0f; |
||
117 | |||
118 | |||
119 | public void Start () |
||
120 | { |
||
121 | // get references to other components |
||
122 | kinectManager = KinectManager.Instance; |
||
123 | avtController = gameObject.GetComponent<AvatarController>(); |
||
124 | |||
125 | // get model transforms |
||
126 | Animator animatorComponent = GetComponent<Animator>(); |
||
127 | AvatarController avatarController = GetComponent<AvatarController>(); |
||
128 | |||
129 | // use the root transform for body scale |
||
130 | bodyScaleTransform = transform; |
||
131 | |||
132 | if (animatorComponent && animatorComponent.GetBoneTransform (HumanBodyBones.Hips)) |
||
133 | { |
||
134 | //bodyHipsTransform = animatorComponent.GetBoneTransform (HumanBodyBones.Hips); |
||
135 | |||
136 | leftShoulderScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.LeftUpperArm); |
||
137 | leftElbowScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.LeftLowerArm); |
||
138 | rightShoulderScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.RightUpperArm); |
||
139 | rightElbowScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.RightLowerArm); |
||
140 | |||
141 | leftHipScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.LeftUpperLeg); |
||
142 | leftKneeScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.LeftLowerLeg); |
||
143 | rightHipScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.RightUpperLeg); |
||
144 | rightKneeScaleTransform = animatorComponent.GetBoneTransform (HumanBodyBones.RightLowerLeg); |
||
145 | } |
||
146 | else if (avatarController) |
||
147 | { |
||
148 | //bodyHipsTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.SpineBase, false)); |
||
149 | |||
150 | leftShoulderScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.ShoulderLeft, false)); |
||
151 | leftElbowScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.ElbowLeft, false)); |
||
152 | rightShoulderScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.ShoulderRight, false)); |
||
153 | rightElbowScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.ElbowRight, false)); |
||
154 | |||
155 | leftHipScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.HipLeft, false)); |
||
156 | leftKneeScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.KneeLeft, false)); |
||
157 | rightHipScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.HipRight, false)); |
||
158 | rightKneeScaleTransform = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.KneeRight, false)); |
||
159 | } |
||
160 | else |
||
161 | { |
||
162 | // needed transforms could not be found |
||
163 | return; |
||
164 | } |
||
165 | |||
166 | // get model bone scales |
||
167 | modelBodyScale = bodyScaleTransform ? bodyScaleTransform.localScale : Vector3.one; |
||
168 | |||
169 | modelLeftShoulderScale = leftShoulderScaleTransform ? leftShoulderScaleTransform.localScale : Vector3.one; |
||
170 | modelLeftElbowScale = leftElbowScaleTransform ? leftElbowScaleTransform.localScale : Vector3.one; |
||
171 | modelRightShoulderScale = rightShoulderScaleTransform ? rightShoulderScaleTransform.localScale : Vector3.one; |
||
172 | modelRightElbowScale = rightElbowScaleTransform ? rightElbowScaleTransform.localScale : Vector3.one; |
||
173 | |||
174 | modelLeftHipScale = leftHipScaleTransform ? leftHipScaleTransform.localScale : Vector3.one; |
||
175 | modelLeftKneeScale = leftKneeScaleTransform ? leftKneeScaleTransform.localScale : Vector3.one; |
||
176 | modelRightHipScale = rightHipScaleTransform ? rightHipScaleTransform.localScale : Vector3.one; |
||
177 | modelRightKneeScale = rightKneeScaleTransform ? rightKneeScaleTransform.localScale : Vector3.one; |
||
178 | |||
179 | if (animatorComponent && animatorComponent.GetBoneTransform (HumanBodyBones.Hips)) |
||
180 | { |
||
181 | GetModelBodyHeight(animatorComponent, ref modelBodyHeight, ref modelBodyWidth); |
||
182 | //Debug.Log (string.Format("MW: {0:F3}, MH: {1:F3}", modelBodyWidth, modelBodyHeight)); |
||
183 | |||
184 | GetModelBoneLength(animatorComponent, HumanBodyBones.LeftUpperArm, HumanBodyBones.LeftLowerArm, ref modelLeftUpperArmLength); |
||
185 | GetModelBoneLength(animatorComponent, HumanBodyBones.LeftLowerArm, HumanBodyBones.LeftHand, ref modelLeftLowerArmLength); |
||
186 | GetModelBoneLength(animatorComponent, HumanBodyBones.RightUpperArm, HumanBodyBones.RightLowerArm, ref modelRightUpperArmLength); |
||
187 | GetModelBoneLength(animatorComponent, HumanBodyBones.RightLowerArm, HumanBodyBones.RightHand, ref modelRightLowerArmLength); |
||
188 | |||
189 | GetModelBoneLength(animatorComponent, HumanBodyBones.LeftUpperLeg, HumanBodyBones.LeftLowerLeg, ref modelLeftUpperLegLength); |
||
190 | GetModelBoneLength(animatorComponent, HumanBodyBones.LeftLowerLeg, HumanBodyBones.LeftFoot, ref modelLeftLowerLegLength); |
||
191 | GetModelBoneLength(animatorComponent, HumanBodyBones.RightUpperLeg, HumanBodyBones.RightLowerLeg, ref modelRightUpperLegLength); |
||
192 | GetModelBoneLength(animatorComponent, HumanBodyBones.RightLowerLeg, HumanBodyBones.RightFoot, ref modelRightLowerLegLength); |
||
193 | |||
194 | scalerInited = true; |
||
195 | } |
||
196 | else if (avatarController) |
||
197 | { |
||
198 | GetModelBodyHeight(avatarController, ref modelBodyHeight, ref modelBodyWidth); |
||
199 | //Debug.Log (string.Format("MW: {0:F3}, MH: {1:F3}", modelBodyWidth, modelBodyHeight)); |
||
200 | |||
201 | GetModelBoneLength(avatarController, KinectInterop.JointType.ShoulderLeft, KinectInterop.JointType.ElbowLeft, ref modelLeftUpperArmLength); |
||
202 | GetModelBoneLength(avatarController, KinectInterop.JointType.ElbowLeft, KinectInterop.JointType.WristLeft, ref modelLeftLowerArmLength); |
||
203 | GetModelBoneLength(avatarController, KinectInterop.JointType.ShoulderRight, KinectInterop.JointType.ElbowRight, ref modelRightUpperArmLength); |
||
204 | GetModelBoneLength(avatarController, KinectInterop.JointType.ElbowRight, KinectInterop.JointType.WristRight, ref modelRightLowerArmLength); |
||
205 | |||
206 | GetModelBoneLength(avatarController, KinectInterop.JointType.HipLeft, KinectInterop.JointType.KneeLeft, ref modelLeftUpperLegLength); |
||
207 | GetModelBoneLength(avatarController, KinectInterop.JointType.KneeLeft, KinectInterop.JointType.AnkleLeft, ref modelLeftLowerLegLength); |
||
208 | GetModelBoneLength(avatarController, KinectInterop.JointType.HipRight, KinectInterop.JointType.KneeRight, ref modelRightUpperLegLength); |
||
209 | GetModelBoneLength(avatarController, KinectInterop.JointType.KneeRight, KinectInterop.JointType.AnkleRight, ref modelRightLowerLegLength); |
||
210 | |||
211 | scalerInited = true; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | void Update () |
||
216 | { |
||
217 | if (scalerInited && kinectManager && kinectManager.IsInitialized()) |
||
218 | { |
||
219 | long userId = kinectManager.GetUserIdByIndex(playerIndex); |
||
220 | |||
221 | if (userId != currentUserId) |
||
222 | { |
||
223 | currentUserId = userId; |
||
224 | |||
225 | if (userId != 0) |
||
226 | { |
||
227 | GetUserBodySize (true, true, true); |
||
228 | |||
229 | // if (fixModelHipsAndShoulders) |
||
230 | // FixJointsBeforeScale(); |
||
231 | ScaleAvatar(0f); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | if(continuousScaling) |
||
237 | { |
||
238 | GetUserBodySize(true, true, true); |
||
239 | ScaleAvatar(smoothFactor); |
||
240 | } |
||
241 | } |
||
242 | |||
243 | // gets the the actual sizes of the user bones |
||
244 | public void GetUserBodySize(bool bBody, bool bArms, bool bLegs) |
||
245 | { |
||
246 | //KinectManager kinectManager = KinectManager.Instance; |
||
247 | if(kinectManager == null) |
||
248 | return; |
||
249 | |||
250 | if(bBody) |
||
251 | { |
||
252 | GetUserBodyHeight(kinectManager, bodyScaleFactor, bodyWidthFactor, ref userBodyHeight, ref userBodyWidth); |
||
253 | } |
||
254 | |||
255 | if(bArms) |
||
256 | { |
||
257 | GetUserBoneLength(kinectManager, KinectInterop.JointType.ShoulderLeft, KinectInterop.JointType.ElbowLeft, armScaleFactor, ref leftUpperArmLength); |
||
258 | GetUserBoneLength(kinectManager, KinectInterop.JointType.ElbowLeft, KinectInterop.JointType.WristLeft, armScaleFactor, ref leftLowerArmLength); |
||
259 | GetUserBoneLength(kinectManager, KinectInterop.JointType.ShoulderRight, KinectInterop.JointType.ElbowRight, armScaleFactor, ref rightUpperArmLength); |
||
260 | GetUserBoneLength(kinectManager, KinectInterop.JointType.ElbowRight, KinectInterop.JointType.WristRight, armScaleFactor, ref rightLowerArmLength); |
||
261 | |||
262 | EqualizeBoneLength(ref leftUpperArmLength, ref rightUpperArmLength); |
||
263 | EqualizeBoneLength(ref leftLowerArmLength, ref rightLowerArmLength); |
||
264 | } |
||
265 | |||
266 | if(bLegs) |
||
267 | { |
||
268 | GetUserBoneLength(kinectManager, KinectInterop.JointType.HipLeft, KinectInterop.JointType.KneeLeft, legScaleFactor, ref leftUpperLegLength); |
||
269 | GetUserBoneLength(kinectManager, KinectInterop.JointType.KneeLeft, KinectInterop.JointType.AnkleLeft, legScaleFactor, ref leftLowerLegLength); |
||
270 | GetUserBoneLength(kinectManager, KinectInterop.JointType.HipRight, KinectInterop.JointType.KneeRight, legScaleFactor, ref rightUpperLegLength); |
||
271 | GetUserBoneLength(kinectManager, KinectInterop.JointType.KneeRight, KinectInterop.JointType.AnkleRight, legScaleFactor, ref rightLowerLegLength); |
||
272 | |||
273 | EqualizeBoneLength(ref leftUpperLegLength, ref rightUpperLegLength); |
||
274 | EqualizeBoneLength(ref leftLowerLegLength, ref rightLowerLegLength); |
||
275 | } |
||
276 | } |
||
277 | |||
278 | // scales the avatar as needed |
||
279 | public void ScaleAvatar(float fSmooth) |
||
280 | { |
||
281 | // KinectManager manager = KinectManager.Instance; |
||
282 | // if(!manager) |
||
283 | // return; |
||
284 | // |
||
285 | // if(fSmooth != 0f && manager.IsUserTurnedAround(currentUserId)) |
||
286 | // return; |
||
287 | |||
288 | // scale body |
||
289 | if (bodyScaleFactor > 0f) |
||
290 | { |
||
291 | // SetupBoneScale(bodyScaleTransform, modelBodyScale, modelBodyHeight, |
||
292 | // userBodyHeight, 0f, fSmooth, ref fScaleBodyHeight); |
||
293 | SetupBodyScale(bodyScaleTransform, modelBodyScale, modelBodyHeight, modelBodyWidth, userBodyHeight, userBodyWidth, |
||
294 | fSmooth, ref fScaleBodyHeight, ref fScaleBodyWidth); |
||
295 | |||
296 | if (avtController) |
||
297 | { |
||
298 | // recalibrate avatar position due to transform scale change |
||
299 | avtController.offsetCalibrated = false; |
||
300 | } |
||
301 | } |
||
302 | |||
303 | // scale arms |
||
304 | if (armScaleFactor > 0f) |
||
305 | { |
||
306 | float fLeftUpperArmLength = !mirroredAvatar ? leftUpperArmLength : rightUpperArmLength; |
||
307 | SetupBoneScale(leftShoulderScaleTransform, modelLeftShoulderScale, modelLeftUpperArmLength, |
||
308 | fLeftUpperArmLength, fScaleBodyHeight, fSmooth, ref fScaleLeftUpperArm); |
||
309 | |||
310 | float fLeftLowerArmLength = !mirroredAvatar ? leftLowerArmLength : rightLowerArmLength; |
||
311 | SetupBoneScale(leftElbowScaleTransform, modelLeftElbowScale, modelLeftLowerArmLength, |
||
312 | fLeftLowerArmLength, fScaleLeftUpperArm, fSmooth, ref fScaleLeftLowerArm); |
||
313 | |||
314 | float fRightUpperArmLength = !mirroredAvatar ? rightUpperArmLength : leftUpperArmLength; |
||
315 | SetupBoneScale(rightShoulderScaleTransform, modelRightShoulderScale, modelRightUpperArmLength, |
||
316 | fRightUpperArmLength, fScaleBodyHeight, fSmooth, ref fScaleRightUpperArm); |
||
317 | |||
318 | float fRightLowerArmLength = !mirroredAvatar ? rightLowerArmLength : leftLowerArmLength; |
||
319 | SetupBoneScale(rightElbowScaleTransform, modelRightElbowScale, modelLeftLowerArmLength, |
||
320 | fRightLowerArmLength, fScaleRightUpperArm, fSmooth, ref fScaleRightLowerArm); |
||
321 | } |
||
322 | |||
323 | // scale legs |
||
324 | if (legScaleFactor > 0) |
||
325 | { |
||
326 | float fLeftUpperLegLength = !mirroredAvatar ? leftUpperLegLength : rightUpperLegLength; |
||
327 | SetupBoneScale(leftHipScaleTransform, modelLeftHipScale, modelLeftUpperLegLength, |
||
328 | fLeftUpperLegLength, fScaleBodyHeight, fSmooth, ref fScaleLeftUpperLeg); |
||
329 | |||
330 | float fLeftLowerLegLength = !mirroredAvatar ? leftLowerLegLength : rightLowerLegLength; |
||
331 | SetupBoneScale(leftKneeScaleTransform, modelLeftKneeScale, modelLeftLowerLegLength, |
||
332 | fLeftLowerLegLength, fScaleLeftUpperLeg, fSmooth, ref fScaleLeftLowerLeg); |
||
333 | |||
334 | float fRightUpperLegLength = !mirroredAvatar ? rightUpperLegLength : leftUpperLegLength; |
||
335 | SetupBoneScale(rightHipScaleTransform, modelRightHipScale, modelRightUpperLegLength, |
||
336 | fRightUpperLegLength, fScaleBodyHeight, fSmooth, ref fScaleRightUpperLeg); |
||
337 | |||
338 | float fRightLowerLegLength = !mirroredAvatar ? rightLowerLegLength : leftLowerLegLength; |
||
339 | SetupBoneScale(rightKneeScaleTransform, modelRightKneeScale, modelRightLowerLegLength, |
||
340 | fRightLowerLegLength, fScaleRightUpperLeg, fSmooth, ref fScaleRightLowerLeg); |
||
341 | } |
||
342 | |||
343 | if(debugText != null) |
||
344 | { |
||
345 | string sDebug = string.Format("BW: {0:F2}/{1:F3}, BH: {2:F2}/{3:F3}\nLUA: {4:F3}, LLA: {5:F3}; RUA: {6:F3}, RLA: {7:F3}\nLUL: {8:F3}, LLL: {9:F3}; RUL: {10:F3}, RLL: {11:F3}", |
||
346 | userBodyWidth, fScaleBodyWidth, userBodyHeight, fScaleBodyHeight, |
||
347 | fScaleLeftUpperArm, fScaleLeftLowerArm, |
||
348 | fScaleRightUpperArm, fScaleRightLowerArm, |
||
349 | fScaleLeftUpperLeg, fScaleLeftLowerLeg, |
||
350 | fScaleRightUpperLeg, fScaleRightLowerLeg); |
||
351 | debugText.text = sDebug; |
||
352 | } |
||
353 | |||
354 | } |
||
355 | |||
356 | private bool GetModelBodyHeight(Animator animatorComponent, ref float height, ref float width) |
||
357 | { |
||
358 | height = 0f; |
||
359 | |||
360 | if(animatorComponent) |
||
361 | { |
||
362 | //Transform hipCenter = animatorComponent.GetBoneTransform(HumanBodyBones.Hips); |
||
363 | |||
364 | Transform leftUpperArm = animatorComponent.GetBoneTransform(HumanBodyBones.LeftUpperArm); |
||
365 | Transform rightUpperArm = animatorComponent.GetBoneTransform(HumanBodyBones.RightUpperArm); |
||
366 | |||
367 | Transform leftUpperLeg = animatorComponent.GetBoneTransform(HumanBodyBones.LeftUpperLeg); |
||
368 | Transform rightUpperLeg = animatorComponent.GetBoneTransform(HumanBodyBones.RightUpperLeg); |
||
369 | |||
370 | if(leftUpperArm && rightUpperArm && leftUpperLeg && rightUpperLeg) |
||
371 | { |
||
372 | Vector3 posShoulderCenter = (leftUpperArm.position + rightUpperArm.position) / 2f; |
||
373 | Vector3 posHipCenter = (leftUpperLeg.position + rightUpperLeg.position) / 2f; // hipCenter.position |
||
374 | |||
375 | //height = (posShoulderCenter.y - posHipCenter.y); |
||
376 | height = (posShoulderCenter - posHipCenter).magnitude; |
||
377 | width = (rightUpperArm.position - leftUpperArm.position).magnitude; |
||
378 | |||
379 | return true; |
||
380 | } |
||
381 | } |
||
382 | |||
383 | return false; |
||
384 | } |
||
385 | |||
386 | private bool GetModelBodyHeight(AvatarController avatarController, ref float height, ref float width) |
||
387 | { |
||
388 | height = 0f; |
||
389 | |||
390 | if(avatarController) |
||
391 | { |
||
392 | Transform leftUpperArm = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.ShoulderLeft, false)); |
||
393 | Transform rightUpperArm = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.ShoulderRight, false)); |
||
394 | |||
395 | Transform leftUpperLeg = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.HipLeft, false)); |
||
396 | Transform rightUpperLeg = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(KinectInterop.JointType.HipRight, false)); |
||
397 | |||
398 | if(leftUpperArm && rightUpperArm && leftUpperLeg && rightUpperLeg) |
||
399 | { |
||
400 | Vector3 posShoulderCenter = (leftUpperArm.position + rightUpperArm.position) / 2f; |
||
401 | Vector3 posHipCenter = (leftUpperLeg.position + rightUpperLeg.position) / 2f; // hipCenter.position |
||
402 | |||
403 | //height = (posShoulderCenter.y - posHipCenter.y); |
||
404 | height = (posShoulderCenter - posHipCenter).magnitude; |
||
405 | width = (rightUpperArm.position - leftUpperArm.position).magnitude; |
||
406 | |||
407 | return true; |
||
408 | } |
||
409 | } |
||
410 | |||
411 | return false; |
||
412 | } |
||
413 | |||
414 | // private bool GetModelBoxHeight(ref float height, ref float width) |
||
415 | // { |
||
416 | // SkinnedMeshRenderer renderer = GetComponentInChildren<SkinnedMeshRenderer>(); |
||
417 | // |
||
418 | // if (renderer && bodyScaleTransform) |
||
419 | // { |
||
420 | // renderer.updateWhenOffscreen = true; |
||
421 | // Bounds bounds = renderer.localBounds; |
||
422 | // Vector3 scale = bodyScaleTransform.localScale; |
||
423 | // //renderer.updateWhenOffscreen = false; |
||
424 | // |
||
425 | // height = bounds.size.z / scale.y; |
||
426 | // width = bounds.size.x / scale.x; |
||
427 | // |
||
428 | // return true; |
||
429 | // } |
||
430 | // |
||
431 | // return false; |
||
432 | // } |
||
433 | |||
434 | private bool GetModelBoneLength(Animator animatorComponent, HumanBodyBones baseJoint, HumanBodyBones endJoint, ref float length) |
||
435 | { |
||
436 | length = 0f; |
||
437 | |||
438 | if(animatorComponent) |
||
439 | { |
||
440 | Transform joint1 = animatorComponent.GetBoneTransform(baseJoint); |
||
441 | Transform joint2 = animatorComponent.GetBoneTransform(endJoint); |
||
442 | |||
443 | if(joint1 && joint2) |
||
444 | { |
||
445 | length = (joint2.position - joint1.position).magnitude; |
||
446 | return true; |
||
447 | } |
||
448 | } |
||
449 | |||
450 | return false; |
||
451 | } |
||
452 | |||
453 | private bool GetModelBoneLength(AvatarController avatarController, KinectInterop.JointType baseJoint, KinectInterop.JointType endJoint, ref float length) |
||
454 | { |
||
455 | length = 0f; |
||
456 | |||
457 | if(avatarController) |
||
458 | { |
||
459 | Transform joint1 = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(baseJoint, false)); |
||
460 | Transform joint2 = avatarController.GetBoneTransform(avatarController.GetBoneIndexByJoint(endJoint, false)); |
||
461 | |||
462 | if(joint1 && joint2) |
||
463 | { |
||
464 | length = (joint2.position - joint1.position).magnitude; |
||
465 | return true; |
||
466 | } |
||
467 | } |
||
468 | |||
469 | return false; |
||
470 | } |
||
471 | |||
472 | private bool GetUserBodyHeight(KinectManager manager, float scaleFactor, float widthFactor, ref float height, ref float width) |
||
473 | { |
||
474 | height = 0f; |
||
475 | width = 0f; |
||
476 | |||
477 | Vector3 posHipLeft = GetJointPosition(manager, (int)KinectInterop.JointType.HipLeft); |
||
478 | Vector3 posHipRight = GetJointPosition(manager, (int)KinectInterop.JointType.HipRight); |
||
479 | Vector3 posShoulderLeft = GetJointPosition(manager, (int)KinectInterop.JointType.ShoulderLeft); |
||
480 | Vector3 posShoulderRight = GetJointPosition(manager, (int)KinectInterop.JointType.ShoulderRight); |
||
481 | |||
482 | if(posHipLeft != Vector3.zero && posHipRight != Vector3.zero && |
||
483 | posShoulderLeft != Vector3.zero && posShoulderRight != Vector3.zero) |
||
484 | { |
||
485 | Vector3 posHipCenter = (posHipLeft + posHipRight) / 2f; |
||
486 | Vector3 posShoulderCenter = (posShoulderLeft + posShoulderRight) / 2f; |
||
487 | //height = (posShoulderCenter.y - posHipCenter.y) * scaleFactor; |
||
488 | |||
489 | height = (posShoulderCenter - posHipCenter).magnitude * scaleFactor; |
||
490 | width = (posShoulderRight - posShoulderLeft).magnitude * widthFactor; |
||
491 | |||
492 | return true; |
||
493 | } |
||
494 | |||
495 | return false; |
||
496 | } |
||
497 | |||
498 | private bool GetUserBoneLength(KinectManager manager, KinectInterop.JointType baseJoint, KinectInterop.JointType endJoint, float scaleFactor, ref float length) |
||
499 | { |
||
500 | length = 0f; |
||
501 | |||
502 | Vector3 vPos1 = GetJointPosition(manager, (int)baseJoint); |
||
503 | Vector3 vPos2 = GetJointPosition(manager, (int)endJoint); |
||
504 | |||
505 | if(vPos1 != Vector3.zero && vPos2 != Vector3.zero) |
||
506 | { |
||
507 | length = (vPos2 - vPos1).magnitude * scaleFactor; |
||
508 | return true; |
||
509 | } |
||
510 | |||
511 | return false; |
||
512 | } |
||
513 | |||
514 | private void EqualizeBoneLength(ref float boneLen1, ref float boneLen2) |
||
515 | { |
||
516 | if(boneLen1 < boneLen2) |
||
517 | { |
||
518 | boneLen1 = boneLen2; |
||
519 | } |
||
520 | else |
||
521 | { |
||
522 | boneLen2 = boneLen1; |
||
523 | } |
||
524 | } |
||
525 | |||
526 | private bool SetupBodyScale(Transform scaleTrans, Vector3 modelBodyScale, float modelHeight, float modelWidth, float userHeight, float userWidth, |
||
527 | float fSmooth, ref float heightScale, ref float widthScale) |
||
528 | { |
||
529 | if(modelHeight > 0f && userHeight > 0f) |
||
530 | { |
||
531 | heightScale = userHeight / modelHeight; |
||
532 | } |
||
533 | |||
534 | if (modelWidth > 0f && userWidth > 0f) |
||
535 | { |
||
536 | widthScale = userWidth / modelWidth; |
||
537 | } |
||
538 | else |
||
539 | { |
||
540 | widthScale = heightScale; |
||
541 | } |
||
542 | |||
543 | if(scaleTrans && heightScale > 0f && widthScale > 0f) |
||
544 | { |
||
545 | float depthScale = heightScale; // (heightScale + widthScale) / 2f; |
||
546 | Vector3 newLocalScale = new Vector3 (modelBodyScale.x * widthScale, modelBodyScale.y * heightScale, modelBodyScale.z * depthScale); |
||
547 | |||
548 | if(fSmooth != 0f) |
||
549 | scaleTrans.localScale = Vector3.Lerp(scaleTrans.localScale, newLocalScale, fSmooth * Time.deltaTime); |
||
550 | else |
||
551 | scaleTrans.localScale = newLocalScale; |
||
552 | |||
553 | return true; |
||
554 | } |
||
555 | |||
556 | return false; |
||
557 | } |
||
558 | |||
559 | |||
560 | private bool SetupBoneScale(Transform scaleTrans, Vector3 modelBoneScale, float modelBoneLen, float userBoneLen, float parentScale, float fSmooth, ref float boneScale) |
||
561 | { |
||
562 | if(modelBoneLen > 0f && userBoneLen > 0f) |
||
563 | { |
||
564 | boneScale = userBoneLen / modelBoneLen; |
||
565 | } |
||
566 | |||
567 | float localScale = boneScale; |
||
568 | if(boneScale > 0f && parentScale > 0f) |
||
569 | { |
||
570 | localScale = boneScale / parentScale; |
||
571 | } |
||
572 | |||
573 | if(scaleTrans && localScale > 0f) |
||
574 | { |
||
575 | if(fSmooth != 0f) |
||
576 | scaleTrans.localScale = Vector3.Lerp(scaleTrans.localScale, modelBoneScale * localScale, fSmooth * Time.deltaTime); |
||
577 | else |
||
578 | scaleTrans.localScale = modelBoneScale * localScale; |
||
579 | |||
580 | return true; |
||
581 | } |
||
582 | |||
583 | return false; |
||
584 | } |
||
585 | |||
586 | |||
587 | public bool FixJointsBeforeScale() |
||
588 | { |
||
589 | Animator animatorComponent = GetComponent<Animator>(); |
||
590 | KinectManager manager = KinectManager.Instance; |
||
591 | |||
592 | if(animatorComponent && modelBodyHeight > 0f && userBodyHeight > 0f) |
||
593 | { |
||
594 | Transform hipCenter = animatorComponent.GetBoneTransform(HumanBodyBones.Hips); |
||
595 | if((hipCenter.localScale - Vector3.one).magnitude > 0.01f) |
||
596 | return false; |
||
597 | |||
598 | Transform leftUpperLeg = animatorComponent.GetBoneTransform(HumanBodyBones.LeftUpperLeg); |
||
599 | Transform rightUpperLeg = animatorComponent.GetBoneTransform(HumanBodyBones.RightUpperLeg); |
||
600 | |||
601 | Transform leftUpperArm = animatorComponent.GetBoneTransform(HumanBodyBones.LeftUpperArm); |
||
602 | Transform rightUpperArm = animatorComponent.GetBoneTransform(HumanBodyBones.RightUpperArm); |
||
603 | |||
604 | if(leftUpperArm && rightUpperArm && leftUpperLeg && rightUpperLeg) |
||
605 | { |
||
606 | Vector3 posHipCenter = GetJointPosition(manager, (int)KinectInterop.JointType.SpineBase); |
||
607 | |||
608 | Vector3 posHipLeft = GetJointPosition(manager, (int)KinectInterop.JointType.HipLeft); |
||
609 | Vector3 posHipRight = GetJointPosition(manager, (int)KinectInterop.JointType.HipRight); |
||
610 | |||
611 | Vector3 posShoulderLeft = GetJointPosition(manager, (int)KinectInterop.JointType.ShoulderLeft); |
||
612 | Vector3 posShoulderRight = GetJointPosition(manager, (int)KinectInterop.JointType.ShoulderRight); |
||
613 | |||
614 | if(posHipCenter != Vector3.zero && posHipLeft != Vector3.zero && posHipRight != Vector3.zero && |
||
615 | posShoulderLeft != Vector3.zero && posShoulderRight != Vector3.zero) |
||
616 | { |
||
617 | SetupUnscaledJoint(hipCenter, leftUpperLeg, posHipCenter, (!mirroredAvatar ? posHipLeft : posHipRight), modelBodyHeight, userBodyHeight); |
||
618 | SetupUnscaledJoint(hipCenter, rightUpperLeg, posHipCenter, (!mirroredAvatar ? posHipRight : posHipLeft), modelBodyHeight, userBodyHeight); |
||
619 | |||
620 | SetupUnscaledJoint(hipCenter, leftUpperArm, posHipCenter, (!mirroredAvatar ? posShoulderLeft : posShoulderRight), modelBodyHeight, userBodyHeight); |
||
621 | SetupUnscaledJoint(hipCenter, rightUpperArm, posHipCenter, (!mirroredAvatar ? posShoulderRight : posShoulderLeft), modelBodyHeight, userBodyHeight); |
||
622 | |||
623 | // recalculate model joints |
||
624 | Start(); |
||
625 | |||
626 | return true; |
||
627 | } |
||
628 | } |
||
629 | } |
||
630 | |||
631 | return false; |
||
632 | } |
||
633 | |||
634 | |||
635 | // gets the joint position in space |
||
636 | private Vector3 GetJointPosition(KinectManager manager, int joint) |
||
637 | { |
||
638 | Vector3 vPosJoint = Vector3.zero; |
||
639 | |||
640 | if(manager.IsJointTracked(currentUserId, joint)) |
||
641 | { |
||
642 | if(foregroundCamera) |
||
643 | { |
||
644 | // get the background rectangle (use the portrait background, if available) |
||
645 | Rect backgroundRect = foregroundCamera.pixelRect; |
||
646 | PortraitBackground portraitBack = PortraitBackground.Instance; |
||
647 | |||
648 | if(portraitBack && portraitBack.enabled) |
||
649 | { |
||
650 | backgroundRect = portraitBack.GetBackgroundRect(); |
||
651 | } |
||
652 | |||
653 | // get the color overlay position |
||
654 | vPosJoint = manager.GetJointPosColorOverlay(currentUserId, joint, foregroundCamera, backgroundRect); |
||
655 | } |
||
656 | |||
657 | // else |
||
658 | if(vPosJoint == Vector3.zero) |
||
659 | { |
||
660 | vPosJoint = manager.GetJointPosition(currentUserId, joint); |
||
661 | } |
||
662 | } |
||
663 | |||
664 | return vPosJoint; |
||
665 | } |
||
666 | |||
667 | |||
668 | // sets the joint position before scaling |
||
669 | private bool SetupUnscaledJoint(Transform hipCenter, Transform joint, Vector3 posHipCenter, Vector3 posJoint, float modelBoneLen, float userBoneLen) |
||
670 | { |
||
671 | float boneScale = 0f; |
||
672 | |||
673 | if(modelBoneLen > 0f && userBoneLen > 0f) |
||
674 | { |
||
675 | boneScale = userBoneLen / modelBoneLen; |
||
676 | //boneScale = 1f; |
||
677 | } |
||
678 | |||
679 | if(boneScale > 0f) |
||
680 | { |
||
681 | Vector3 posDiff = (posJoint - posHipCenter) / boneScale; |
||
682 | if(foregroundCamera == null) |
||
683 | posDiff.z = 0f; // ignore difference in z (non-overlay mode) |
||
684 | |||
685 | Vector3 posJointNew = hipCenter.position + posDiff; |
||
686 | joint.position = posJointNew; |
||
687 | |||
688 | return true; |
||
689 | } |
||
690 | |||
691 | return false; |
||
692 | } |
||
693 | |||
694 | |||
695 | |||
696 | } |