private IBody RecenterBody(IBody body) { var joints = body.Joints; Dictionary <JointType, IJoint> newjoints = new Dictionary <JointType, IJoint>(joints.Count); foreach (var item in joints) { IJoint joint = new CustomJoint(item.Key); joint.ColorPosition = item.Value.ColorPosition; joint.DepthPosition = item.Value.DepthPosition; joint.Position = item.Value.Position; joint.TrackingState = item.Value.TrackingState; newjoints.Add(item.Key, joint); } newjoints[REFERENCEJOINT].Position = new CameraSpacePoint(); foreach (var item in newjoints) { if (item.Key == REFERENCEJOINT) { continue; } CameraSpacePoint convertedPoint = new CameraSpacePoint(); convertedPoint.X = newjoints[item.Key].Position.X - joints[JointType.SpineMid].Position.X; convertedPoint.Y = newjoints[item.Key].Position.Y - joints[JointType.SpineMid].Position.Y; convertedPoint.Z = newjoints[item.Key].Position.Z - joints[JointType.SpineMid].Position.Z; newjoints[item.Key].Position = convertedPoint; } body.Joints = newjoints; return(body); }
private void AddToJointDict(CustomJoint joint) { // Go through dict and try to find matching string name. foreach (KeyValuePair <int, string> jointInfo in jointTypeDict) { // if found, set and break out of loop if (jointInfo.Value == joint.name) { joint.JointType = jointInfo.Key; return; } } //Debug.Log(joints[z].name + " : " + joints[z].JointTest); // otherwise add to dict int jointID = jointTypeDict.Count; jointTypeDict.Add(jointTypeDict.Count, joint.name); // set joint type for joint joint.JointType = jointID; }
private void AddCharacter(GameObject character) { if (character == null || character.activeInHierarchy) { Debug.LogError("ERROR: Provided character or all game objs is NULL or character is present in the hierarchy."); return; } // make sure we didnt already create the character if (createdPrefabs.ContainsKey(character.name)) { Debug.LogError("ERROR: prefab already created"); return; } // final character name string characterName = character.name; // create tmp character character = Instantiate(character); // hold root bodypart BodyPart rootBodyPart = null; CustomJoint rootJoint = null; // APPLY CHANGES // Go through all joints and process relationships CustomJoint[] joints = character.GetComponentsInChildren <CustomJoint>(); List <BodyPart> leafBodyParts = new List <BodyPart>(); // change to array? List <BodyPart> allBodyParts = new List <BodyPart>(); // verify joint layout for (int z = 0; z < joints.Length; ++z) { // add to joint dict AddToJointDict(joints[z]); // ensure joint has atleast 1 child if (joints[z].transform.childCount < 1) { throw new UnityException("ERROR: Joint does not have enough children!!"); } // Ensure subjoint child is a body part BodyPart bodyPart = null; foreach (Transform child in joints[z].transform) { if ((bodyPart = child.GetComponent <BodyPart>()) != null) { allBodyParts.Add(bodyPart); break; } } if (bodyPart == null) { throw new UnityException("ERROR: Body part does not exist in joint: " + joints[z].name); } bodyPart.BodyPartType = joints[z].JointType; bodyPart.itemIcon = null; //bodyPart.InitialLocalPosition = bodyPart.transform.localPosition; //bodyPart.InitialLocalRotation = bodyPart.transform.localRotation; if (bodyPart.name.ToLower().Contains("torso")) { rootJoint = joints[z]; rootBodyPart = bodyPart; } else if (joints[z].GetComponentsInChildren <CustomJoint>().Length == 1) { Debug.Log(bodyPart.name + " has no child "); leafBodyParts.Add(bodyPart); } // connect prefabs if they exist in scene /* * if (gameObjPrefab.ContainsKey(bodyPart.name)) * { * for (int h = 0; h < gameObjPrefab[bodyPart.name].Count; ++h) * { * if (gameObjPrefab[bodyPart.name][h] != null) * { * // Save rotation and location, then connect * Vector3 savedLocation = gameObjPrefab[bodyPart.name][h].transform.position; * Quaternion saveRotation = gameObjPrefab[bodyPart.name][h].transform.rotation; * * GameObject finalConnection = PrefabUtility.ConnectGameObjectToPrefab(gameObjPrefab[bodyPart.name][h], createdPrefabs[bodyPart.name]); * * // bring back pos and rot * finalConnection.transform.position = savedLocation; * finalConnection.transform.rotation = saveRotation; * } * } * * // remove all null go's * gameObjPrefab[bodyPart.name].RemoveAll(x => x == null); * * // remove all 0 count lists * if (gameObjPrefab[bodyPart.name].Count == 0) * gameObjPrefab.Remove(bodyPart.name); * } */ } // construct tmp character with following layout // BASE NAME (delete animator) // - bodyParts // find torso bodypart and set as root. rootBodyPart.transform.parent = character.transform; //GameObject bodyPartRoot = character.transform.f //bodyPartRoot.transform.parent = character.transform; //bodyPartRoot.transform.localPosition = Vector3.zero; //bodyPartRoot.transform.localRotation = Quaternion.identity; // -- clothing -- Clothing[] clothing = character.GetComponentsInChildren <Clothing>(); for (int i = 0; i < clothing.Length; ++i) { clothing[i].Cloth.capsuleColliders = new CapsuleCollider[clothing[i].ExpectedBodyPartColliders.Length]; } // -- bparts -- for (int i = 0; i < leafBodyParts.Count; ++i) { IsolateBodyPart(leafBodyParts[i], rootBodyPart, clothing); } rootBodyPart.transform.localPosition = Vector3.zero; // - skeleton (animator) //GameObject skeletonRoot = new GameObject("skeleton"); rootJoint.transform.parent = character.transform; rootJoint.transform.localPosition = Vector3.zero; rootJoint.transform.localRotation = Quaternion.identity; // -- clothing -- /* * Transform clothing = null; * for (int i = 0; i < character.transform.childCount; ++i) * if ((clothing = character.transform.GetChild(i)).tag == "clothing") * { * clothing.localPosition = new Vector3(clothing.localPosition.x, 0, clothing.localPosition.z); * break; * } */ // copy animator Animator animator; if ((animator = character.GetComponent <Animator>()) != null) { // set to apply root motion animator.applyRootMotion = true; if (UnityEditorInternal.ComponentUtility.CopyComponent(animator)) { if (UnityEditorInternal.ComponentUtility.PasteComponentAsNew(rootJoint.gameObject)) { DestroyImmediate(animator); Debug.Log("Copied animator as well..."); } } } // -- joints -- // set immediate joints to skeleton gameobject. CustomJoint[] childJoints = character.GetComponentsInChildren <CustomJoint>(); for (int i = 0; i < childJoints.Length; ++i) { if (childJoints[i].transform.parent == character.transform) { childJoints[i].transform.parent = rootJoint.transform; } } // attempt to add bpart to clothing // TODO: GET RID OF HORRIBLENESS BodyPart[] bodyParts = rootBodyPart.GetComponentsInChildren <BodyPart>(); for (int i = 0; i < clothing.Length; ++i) { List <CapsuleCollider> capsuleColliders = new List <CapsuleCollider>(); for (int z = 0; z < bodyParts.Length; ++z) { if (clothing[i].ExpectedBodyPartColliders.Contains(bodyParts[z].BodyPartType)) { capsuleColliders.Add(bodyParts[z].GetComponent <CapsuleCollider>()); } } clothing[i].Cloth.capsuleColliders = capsuleColliders.ToArray(); } // set skinned mesh renderer to update offscreen so clothes dont dc SkinnedMeshRenderer[] skinnedMeshRenderers; if ((skinnedMeshRenderers = character.GetComponentsInParent <SkinnedMeshRenderer>()) != null) { for (int i = 0; i < skinnedMeshRenderers.Length; i++) { skinnedMeshRenderers[i].updateWhenOffscreen = true; } } // CREATE PREFABS // generate prefabs /* * for (int i = 0; i < allBodyParts.Count; ++i) * if (!createdPrefabs.ContainsKey(allBodyParts[i].name)) * { * // create as prefab * string prefabPath = "Assets/Resources/Prefabs/BodyParts/" + allBodyParts[i].name + ".prefab"; * * GeneratePrefab(allBodyParts[i].gameObject, "Assets/Resources/Prefabs/BodyParts/", "Assets/Resources/Prefabs/Icons/"); * * GameObject isolatedBodyPart = Instantiate(allBodyParts[i].gameObject); * isolatedBodyPart.transform.parent = null; * isolatedBodyPart.transform.DetachChildren(); * * GameObject bodyPartPrefab = PrefabUtility.CreatePrefab(prefabPath, isolatedBodyPart, ReplacePrefabOptions.Default); * DestroyImmediate(isolatedBodyPart); * * //GameObject actualBodyPartRef = allBodyParts[i].gameObject; * //createdPrefabs[allBodyParts[i].name] = bodyPartPrefab; * * // create icon * Texture2D icon = AssetPreview.GetAssetPreview(bodyPartPrefab); * * // create threaded job until all assets are done generating * ContinuationManager.Run(() => !AssetPreview.IsLoadingAssetPreview(bodyPartPrefab.GetInstanceID()), () => * { * //Debug.Log("Finished with " + bodyPartPrefab.name); * icon = AssetPreview.GetAssetPreview(bodyPartPrefab); * Texture2D tmp = new Texture2D(icon.width, icon.height, icon.format, false); * * tmp.LoadRawTextureData(icon.GetRawTextureData()); * tmp.Apply(); * * // make the texture transparent * Color bgGray = new Color(82 / 255f, 82 / 255f, 82 / 255f); * Color alpha = new Color(0, 0, 0, 0); * for (int x = 0; x < tmp.width; ++x) * for (int y = 0; y < tmp.height; ++y) * if (tmp.GetPixel(x, y) == bgGray) * tmp.SetPixel(x, y, alpha); * * tmp.Apply(); * * // write texture to file * byte[] bytesToWrite = tmp.EncodeToPNG(); * File.WriteAllBytes("Assets/Resources/Prefabs/Icons/" + bodyPartPrefab.name + ".png", bytesToWrite); * * // refresh db * AssetDatabase.Refresh(); * * // set the import type as SPRITE * TextureImporter importer = AssetImporter.GetAtPath("Assets/Resources/Prefabs/Icons/" + bodyPartPrefab.name + ".png") as TextureImporter; * if (importer == null) * { * Debug.LogError("FAILED " + bodyPartPrefab.name); * return; * } * * * importer.textureType = TextureImporterType.Sprite; * importer.spritePixelsPerUnit = 100; * * // save settings and reimport * importer.SaveAndReimport(); * * // get the asset * Sprite spriteIcon = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/Resources/Prefabs/Icons/" + bodyPartPrefab.name + ".png"); * * // set the icon to prefab and bpart, apply changes * bodyPartPrefab.GetComponent<Item>().itemIcon = spriteIcon; * * * //GameObject changes = PrefabUtility.InstantiatePrefab(bodyPartPrefab) as GameObject; * GameObject changes = Instantiate(bodyPartPrefab); * changes.GetComponent<Item>().itemIcon = spriteIcon; * //Debug.Log("Changed: " + changes.name); * PrefabUtility.ReplacePrefab(changes, bodyPartPrefab, ReplacePrefabOptions.Default); * //Debug.Log("Got: " + test.name); * DestroyImmediate(changes); * * }); * * } */ // Create job to make sure all bodyparts have assigned icons //Object[] bodyPartsToCheck = allBodyParts.ToArray(); // Wait until ALL body parts have been created. //ContinuationManager.Run(() => !IsStillGenerating(bodyPartsToCheck), () => //{ Debug.Log("CREATING CHARACTER..."); createdPrefabs[characterName] = PrefabUtility.CreatePrefab("Assets/Resources/Prefabs/Characters/" + characterName + ".prefab", character, ReplacePrefabOptions.ConnectToPrefab); DestroyImmediate(character); //}); //createdPrefabs[characterName] = PrefabUtility.CreatePrefab("Assets/Resources/Prefabs/Characters/" + characterName + ".prefab", character, ReplacePrefabOptions.ConnectToPrefab); // CONNECT OBJS TO PREFABS AT THEIR ROOT // connect prefabs if they exist in scene /* * if (gameObjPrefab.ContainsKey(character.name)) * { * for (int h = 0; h < gameObjPrefab[character.name].Count; ++h) * { * if (gameObjPrefab[character.name][h] != null) * { * // Save rotation and location, then connect * Vector3 savedLocation = gameObjPrefab[character.name][h].transform.position; * Quaternion saveRotation = gameObjPrefab[character.name][h].transform.rotation; * * GameObject finalConnection = PrefabUtility.ConnectGameObjectToPrefab(gameObjPrefab[character.name][h], createdPrefabs[character.name]); * * // bring back pos and rot * finalConnection.transform.position = savedLocation; * finalConnection.transform.rotation = saveRotation; * } * } * * // remove all null go's * gameObjPrefab[character.name].RemoveAll(x => x == null); * * // remove all 0 count lists * if (gameObjPrefab[character.name].Count == 0) * gameObjPrefab.Remove(character.name); * } * */ //DestroyImmediate(character); }
protected static CustomBody CreateBodyFromReader(BinaryReader reader, Version version) { CustomBody body = new CustomBody(); try { body.IsTracked = reader.ReadBoolean(); if (body.IsTracked) { #region Old Version if (version < v02) { int count; count = reader.ReadInt32(); // Activities.Count for (int i = 0; i < count; i++) { reader.ReadInt32(); // key reader.ReadInt32(); // value } count = reader.ReadInt32(); // Appearance.Count for (int i = 0; i < count; i++) { reader.ReadInt32(); // key reader.ReadInt32(); // value } } #endregion body.ClippedEdges = (FrameEdges)reader.ReadInt32(); #region Old Version if (version < v02) { reader.ReadInt32(); // Engaged int count; count = reader.ReadInt32(); // Expressions.Count for (int i = 0; i < count; i++) { reader.ReadInt32(); // key reader.ReadInt32(); // value } } #endregion body.HandLeftConfidence = (TrackingConfidence)reader.ReadInt32(); body.HandLeftState = (HandState)reader.ReadInt32(); body.HandRightConfidence = (TrackingConfidence)reader.ReadInt32(); body.HandRightState = (HandState)reader.ReadInt32(); body.IsRestricted = reader.ReadBoolean(); int orientationCount = reader.ReadInt32(); for (var i = 0; i < orientationCount; i++) { var key = (JointType)reader.ReadInt32(); var value = new JointOrientation(); value.JointType = (JointType)reader.ReadInt32(); var vector = new Vector4(); vector.W = reader.ReadSingle(); vector.X = reader.ReadSingle(); vector.Y = reader.ReadSingle(); vector.Z = reader.ReadSingle(); value.Orientation = vector; var dict = body.JointOrientations as IDictionary <JointType, JointOrientation>; if (dict != null) { dict[key] = value; } } int jointCount = reader.ReadInt32(); for (var i = 0; i < jointCount; i++) { var key = (JointType)reader.ReadInt32(); var value = new CustomJoint(key); value.JointType = (JointType)reader.ReadInt32(); var position = new CameraSpacePoint(); position.X = reader.ReadSingle(); position.Y = reader.ReadSingle(); position.Z = reader.ReadSingle(); value.Position = position; if (version > v03) { var depthPosition = new DepthSpacePoint(); depthPosition.X = reader.ReadSingle(); depthPosition.Y = reader.ReadSingle(); value.DepthPosition = depthPosition; var colorPosition = new ColorSpacePoint(); colorPosition.X = reader.ReadSingle(); colorPosition.Y = reader.ReadSingle(); value.ColorPosition = colorPosition; } value.TrackingState = (TrackingState)reader.ReadInt32(); var dict = body.Joints as IDictionary <JointType, IJoint>; if (dict != null) { dict[key] = value; } } #if NETFX_CORE var lean = new Point(); #else var lean = new PointF(); #endif lean.X = reader.ReadSingle(); lean.Y = reader.ReadSingle(); body.Lean = lean; body.LeanTrackingState = (TrackingState)reader.ReadInt32(); body.TrackingId = reader.ReadUInt64(); if (version > v03) { body.HasMappedDepthPositions = reader.ReadBoolean(); body.HasMappedColorPositions = reader.ReadBoolean(); } } } catch (Exception ex) { // TODO: Log This System.Diagnostics.Debug.WriteLine(ex); } return(body); }
/* * Use this function as the "constructor" since it occurs at the same time * as instantiation. */ void Awake() { parentJoint = transform.parent.GetComponent <CustomJoint>(); tag = "Joint"; }