Пример #1
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.animator = parentNode.GetGameObject().AddComponent <UnityEngine.Animator>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                this.animator.applyRootMotion = metaData["applyRootMotion"].ToObject <bool>();
                UInt32 avatarID = metaData.Value <UInt32>("avatarReferenceID");

                for (int i = 0; i < this.ChildNodes.Count; i++)
                {
                    UnityNodeBase child = this.ChildNodes[i];

                    ResourceResponse avatarResponse = new ResourceResponse(avatarID, (ResourceResponse response) =>
                    {
                        this.animator.avatar = response.GetAvatarRequest;
                    });

                    child.Deserialize(dataBlocks, this, avatarResponse, postInstallActions, optimizedLoad);
                }

                this.isDeserialized = true;
            }
        }
Пример #2
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.camera = parentNode.GetGameObject().AddComponent <Camera>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int     count = (bytes.Length / 4);
                float[] data  = new float[count];

                for (int i = 0; i < count; i++)
                {
                    data[i] = BitConverter.ToSingle(bytes, (i * 4));
                }

                camera.backgroundColor = new Color(data[0], data[1], data[2], data[3]);
                camera.fieldOfView     = data[4];
                camera.aspect          = data[5];

                this.isDeserialized = true;
            }
        }
Пример #3
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                Type scriptType = typeof(Animation);

                if (scriptType != null)
                {
                    Animation animation = parentNode.GetGameObject().AddComponent(scriptType) as Animation;

                    PropertySetter clipSetter = new PropertySetter("clip", (System.Object val) => { animation.clip = val as AnimationClip; });

                    List <PropertySetter> customValueSetters = new List <PropertySetter>();
                    customValueSetters.Add(clipSetter);

                    FieldDeserializer fieldDeserializer = new FieldDeserializer(customValueSetters, animation);

                    ResourceResponse response = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

                    foreach (UnityNodeBase node in this.ChildNodes)
                    {
                        node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #4
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.light = parentNode.GetGameObject().AddComponent <Light>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int     count = (bytes.Length / 4);
                float[] data  = new float[count];

                for (int i = 0; i < count; i++)
                {
                    data[i] = BitConverter.ToSingle(bytes, (i * 4));
                }

                light.color     = new Color(data[0], data[1], data[2], data[3]);
                light.type      = (LightType)Enum.ToObject(typeof(LightType), (int)data[4]);
                light.intensity = data[5];

                this.isDeserialized = true;
            }
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                int       vectorCount = (bytes.Length / 12);
                Vector3[] vectors     = new Vector3[vectorCount];

                for (int i = 0; i < vectorCount; i++)
                {
                    vectors[i] = new Vector3(
                        BitConverter.ToSingle(bytes, (i * 12)),
                        BitConverter.ToSingle(bytes, (i * 12) + 4),
                        BitConverter.ToSingle(bytes, (i * 12) + 8)
                        );
                }

                GameObject parentGO = parentNode.GetGameObject();

                //Gameobject creates a transform itself when its created, we only need to get the component.
                if (parentGO != null)
                {
                    this.transform = parentGO.GetComponent <Transform>();
                }

                if (this.transform != null)
                {
                    //Get the node above the parentNode and parent to it.
                    Transform parentTransform = parentNode.ParentNode.GetTransform();

                    //Parent this transform to the object above it, if one exists.
                    if (parentTransform != null)
                    {
                        this.transform.SetParent(parentTransform);
                    }

                    //Set the attributes after we have parented the transform.
                    this.transform.localPosition = vectors[0];
                    this.transform.localRotation = Quaternion.Euler(vectors[1]);
                    this.transform.localScale    = vectors[2];
                }

                this.isDeserialized = true;
            }
        }
Пример #6
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                this.collider = parentNode.GetGameObject().AddComponent <BoxCollider>();

                this.isDeserialized = true;
            }
        }
Пример #7
0
        public override GameObject GetGameObject()
        {
            if (this.ChildNodes != null)
            {
                UnityNodeBase objectNode = GetChildNodeByType(PCFResourceType.OBJECT);

                if (objectNode != null)
                {
                    return(objectNode.GetGameObject());
                }
            }

            return(null);
        }
Пример #8
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                GameObject parentGameObject = parentNode.GetGameObject();

                this.meshFilter   = parentGameObject.AddComponent <MeshFilter>();
                this.meshRenderer = parentGameObject.AddComponent <MeshRenderer>();

                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));
                UInt32  materialID     = metaData.Value <UInt32>("materialID");

                this.mesh = MeshSerializeUtilities.ReadMesh(resource.GetResourceData(), false);
                this.meshFilter.sharedMesh = this.mesh;

                if (optimizedLoad)
                {
                    //Free up cached ram memory for this mesh, disables access to mesh components like verts etc.
                    this.mesh.UploadMeshData(true);
                }

                for (int i = 0; i < this.ChildNodes.Count; i++)
                {
                    UnityNodeBase child = this.ChildNodes[i];

                    //Create a request to be send down the chain of children, see if any child will handle it.
                    ResourceResponse materialRequest = new ResourceResponse(materialID, (ResourceResponse response) => {
                        meshRenderer.material = response.GetMaterialRequest;
                    });

                    child.Deserialize(dataBlocks, this, materialRequest, postInstallActions, optimizedLoad);
                }

                this.isDeserialized = true;
            }
        }
Пример #9
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                string scriptName = metaData["scriptname"].ToString();
                Type   scriptType = null;

                if (scriptMask == null)
                {
                    scriptType = Type.GetType(scriptName);
                }
                else if (this.scriptMask != null && this.scriptMask.Contains(scriptName))
                {
                    scriptType = Type.GetType(scriptName);
                }

                if (scriptType != null)
                {
                    this.script = parentNode.GetGameObject().AddComponent(scriptType) as MonoBehaviour;
                    FieldDeserializer fieldDeserializer = new FieldDeserializer(scriptType.GetFields(), this.script);

                    ResourceResponse response = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

                    foreach (UnityNodeBase node in this.ChildNodes)
                    {
                        node.Deserialize(dataBlocks, this, response, postInstallActions, optimizedLoad);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #10
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            GameObject parentGameObject = parentNode.GetGameObject();

            this.skinnedMesh = parentGameObject.AddComponent <SkinnedMeshRenderer>();

            ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
            AssetResource resource  = dataBlock.GetResource(this.referenceID);

            byte[]  metaDataBuffer = resource.GetMetaData();
            JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

            UInt32 materialID    = metaData.Value <UInt32>("materialID");
            string rootBoneName  = metaData.Value <string>("rootBone");
            string probeBoneName = metaData.Value <string>("probeAnchor");
            int    quality       = metaData.Value <int>("quality");

            string[] boneNames         = metaData.Value <JArray>("bones").ToObject <string[]>();
            float[]  blendShapeWeights = metaData.Value <JArray>("blendShapeWeights").ToObject <float[]>();

            // Add a post install action so the bones will have time to be created.
            postInstallActions.Add((UnityNodeBase node) => {
                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                Transform[] bones = new Transform[boneNames.Length];

                //Mapp all bone transform in the hierarchy.
                Dictionary <string, Transform> bonesMapping = new Dictionary <string, Transform>();
                FindTransformsInHierarchy(node, bonesMapping);

                for (int i = 0; i < boneNames.Length; i++)
                {
                    string name = boneNames[i];

                    if (bonesMapping.ContainsKey(name))
                    {
                        bones[i] = bonesMapping[name];
                    }
                    else
                    {
                        bones[i] = null;
                    }
                }

                this.mesh = MeshSerializeUtilities.ReadMesh(resource.GetResourceData(), true);

                if (optimizedLoad)
                {
                    //Free up mono memory for this mesh, now owned by the GPU.
                    this.mesh.UploadMeshData(true);
                }

                this.skinnedMesh.sharedMesh = this.mesh;
                this.skinnedMesh.bones      = bones;
                this.skinnedMesh.quality    = (SkinQuality)Enum.ToObject(typeof(SkinQuality), quality);

                for (int i = 0; i < blendShapeWeights.Length; i++)
                {
                    this.skinnedMesh.SetBlendShapeWeight(i, blendShapeWeights[i]);
                }

                if (bonesMapping.ContainsKey(rootBoneName))
                {
                    this.skinnedMesh.rootBone = bonesMapping[rootBoneName];
                }
                if (probeBoneName != null)
                {
                    this.skinnedMesh.probeAnchor = bonesMapping[probeBoneName];
                }

                watch.Stop();
                //Debug.Log("time to deserialize skinned mesh: " + watch.ElapsedMilliseconds);
            });

            for (int i = 0; i < this.ChildNodes.Count; i++)
            {
                UnityNodeBase child = this.ChildNodes[i];

                //Create a request to be send down the chain of children, see if any child will handle it.
                ResourceResponse materialRequest = new ResourceResponse(materialID, (ResourceResponse response) => {
                    skinnedMesh.material = response.GetMaterialRequest;
                });

                child.Deserialize(dataBlocks, this, materialRequest, postInstallActions, optimizedLoad);
            }
        }
Пример #11
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[]  metaDataBuffer = resource.GetMetaData();
                JObject metaData       = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));
                string  avatarName     = metaData.Value <string>("avatarName");
                this.sharedAvatar = bool.Parse(metaData.Value <string>("sharedAvatar"));
                string rootBone    = metaData.Value <string>("rootBone");
                JArray mappedBones = metaData.Value <JArray>("mappedBones");

                if (!sharedAvatar)
                {
                    //Run after hierarchy is created since we need it when creating a avatar.
                    postInstallActions.Add((UnityNodeBase rootNode) => {
                        UnityNodeBase skeletonRootNode = FindNodeWithName(rootNode, rootBone);

                        if (skeletonRootNode != null)
                        {
                            //recreate avatar from meta data.
                            List <SkeletonBone> skeletonBones = new List <SkeletonBone>(mappedBones.Count);
                            List <HumanBone> humanBones       = new List <HumanBone>(mappedBones.Count);

                            int humanBoneCount    = 0;
                            int skeletonBoneCount = 0;
                            foreach (JObject bone in mappedBones)
                            {
                                JObject humanBoneData = bone.Value <JObject>("humanBone");

                                if (humanBoneData != null)
                                {
                                    HumanBone humanBone = new HumanBone();
                                    humanBone.humanName = humanBoneData.Value <string>("humanName");
                                    humanBone.boneName  = humanBoneData.Value <string>("boneName");
                                    humanBone.limit.useDefaultValues = bool.Parse(humanBoneData.Value <string>("useDefaultValues"));

                                    humanBones.Add(humanBone);
                                }

                                JObject skeletonBoneData  = bone.Value <JObject>("skeletonBone");
                                SkeletonBone skeletonBone = new SkeletonBone();
                                skeletonBone.name         = skeletonBoneData.Value <string>("name");

                                float[] posArray      = skeletonBoneData.Value <JArray>("position").ToObject <float[]>();
                                skeletonBone.position = new Vector3(posArray[0], posArray[1], posArray[2]);

                                float[] rotArray      = skeletonBoneData.Value <JArray>("rotation").ToObject <float[]>();
                                skeletonBone.rotation = new Quaternion(rotArray[0], rotArray[1], rotArray[2], rotArray[3]);

                                float[] scaleArray = skeletonBoneData.Value <JArray>("scale").ToObject <float[]>();
                                skeletonBone.scale = new Vector3(scaleArray[0], scaleArray[1], scaleArray[2]);

                                skeletonBones.Add(skeletonBone);
                            }

                            HumanDescription desc = new HumanDescription();
                            desc.human            = humanBones.ToArray();
                            desc.skeleton         = skeletonBones.ToArray();

                            //set the default values for the rest of the human descriptor parameters
                            desc.upperArmTwist = 0.5f;
                            desc.lowerArmTwist = 0.5f;
                            desc.upperLegTwist = 0.5f;
                            desc.lowerLegTwist = 0.5f;
                            desc.armStretch    = 0.05f;
                            desc.legStretch    = 0.05f;
                            desc.feetSpacing   = 0.0f;

                            this.avatar      = AvatarBuilder.BuildHumanAvatar(skeletonRootNode.GetGameObject(), desc);
                            this.avatar.name = avatarName;

                            ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                            if (request != null)
                            {
                                request.HandleAvatarResponse(this.avatar);
                            }
                        }
                        else
                        {
                            Debug.LogError("Unable to find rootnode for avatar: " + avatarName);
                        }
                    });
                }
                else
                {
                    //Load avatar from resources by name.
                    string avatarResourcePath = Path.Combine("Avatars", avatarName);
                    this.avatar = Resources.Load(avatarResourcePath) as Avatar;

                    ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                    if (request != null)
                    {
                        request.HandleAvatarResponse(this.avatar);
                    }
                }

                this.isDeserialized = true;
            }
        }