Exemple #1
0
        public static GameObject BuildHierarchy(PmxBone bone, IEnumerable <PmxBone> coll, List <Transform> appendList, GameObject sprite)
        {
            GameObject root = GameObject.Instantiate <GameObject>(sprite);

            appendList.Add(root.transform);
            bone.UnityObject = root;

            BoneSpriteBehaviour comp = root.GetComponent <BoneSpriteBehaviour>();

            if (!bone.HasFlag(PmxBone.BoneFlags.Visible))
            {
                comp.Icon = BoneSpriteBehaviour.IconType.Invisible;
            }
            else if (bone.HasFlag(PmxBone.BoneFlags.FixedAxis))
            {
                comp.Icon = BoneSpriteBehaviour.IconType.Twist;
            }
            else if (bone.HasFlag(PmxBone.BoneFlags.Translation))
            {
                comp.Icon = BoneSpriteBehaviour.IconType.Translation;
            }

            root.layer = LayerMask.NameToLayer("UISprites");
            root.name  = bone.Name;
            PmxBone[] children = bone.Children(coll);
            for (int i = 0; i < children.Length; ++i)
            {
                GameObject child = BuildHierarchy(children[i], coll, appendList, sprite);
                child.transform.SetParent(root.transform, true);
                child.transform.position = children[i].Position - bone.Position;
            }
            return(root);
        }
Exemple #2
0
        /// <summary>
        /// Reads a bone structure from teh file and advances the stream position to the next item.
        /// </summary>
        /// <param name="encoding">The text encoding to use when reading strings.</param>
        /// <returns>The next bone in the PMX file.</returns>
        public static PmxBone ReadPmxBone(this BinaryReader reader, System.Text.Encoding encoding)
        {
            PmxBone bone = new PmxBone(reader.ReadPmxString(encoding), reader.ReadPmxString(encoding));

            bone.Position = reader.ReadVector3();
            bone.Parent   = reader.ReadIndex(PmxTypes.IndexType.Bone);
            bone.Layer    = reader.ReadInt32();
            bone.Flags    = (PmxBone.BoneFlags)reader.ReadInt16();

            // Tail
            if (bone.HasFlag(PmxBone.BoneFlags.TailIsIndex))
            {
                bone.TailIndex = reader.ReadIndex(PmxTypes.IndexType.Bone);
            }
            else
            {
                bone.TailPosition = reader.ReadVector3();
            }

            // Inherit
            if (bone.HasFlag(PmxBone.BoneFlags.InheritRotation | PmxBone.BoneFlags.InheritTranslation))
            {
                bone.InheritFromIndex = reader.ReadIndex(PmxTypes.IndexType.Bone);
                bone.InheritWeight    = reader.ReadSingle();
            }

            // Fixed axis
            if (bone.HasFlag(PmxBone.BoneFlags.FixedAxis))
            {
                bone.FixedAxis = reader.ReadVector3();
            }

            // Local transformation
            if (bone.HasFlag(PmxBone.BoneFlags.LocalTransform))
            {
                bone.LocalX = reader.ReadVector3();
                bone.LocalZ = reader.ReadVector3();
            }

            // Outside parent
            if (bone.HasFlag(PmxBone.BoneFlags.ExternalDeform))
            {
                bone.ExternalParentIndex = reader.ReadIndex(PmxTypes.IndexType.Bone);
            }

            // IK
            if (bone.HasFlag(PmxBone.BoneFlags.IK))
            {
                bone.IK             = new PmxIK();
                bone.IK.TargetIndex = reader.ReadIndex(PmxTypes.IndexType.Bone);
                bone.IK.Loop        = reader.ReadInt32();
                bone.IK.Limit       = reader.ReadSingle();
                int linkCount = reader.ReadInt32();
                bone.IK.Links = new List <PmxIKLink>();
                for (int j = 0; j < linkCount; ++j)
                {
                    PmxIKLink link = new PmxIKLink();
                    link.Index    = reader.ReadIndex(PmxTypes.IndexType.Bone);
                    link.HasLimit = reader.ReadByte() != 0;
                    if (link.HasLimit)
                    {
                        link.EulerLimitMin = reader.ReadVector3();
                        link.EulerLimitMax = reader.ReadVector3();
                    }
                    else
                    {
                        link.EulerLimitMin = new Vector3();
                        link.EulerLimitMax = new Vector3();
                    }
                    bone.IK.Links.Add(link);
                }
            }
            return(bone);
        }