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

            UInt32 pointedNodeID = BitConverter.ToUInt32(resource.GetResourceData(), 0);

            UnityNodeBase currentParent = parentNode;

            while (currentParent.ParentNode != null)
            {
                currentParent = currentParent.ParentNode;
            }

            UnityNodeBase referencedNode = FindNodeWithID(currentParent, pointedNodeID);

            if (referencedNode is UnityMaterialNode)
            {
                UnityMaterialNode materialNode = referencedNode as UnityMaterialNode;

                Material mat = materialNode.GetMaterial();

                ResourceResponse request = resourceResponse.CanHandle(pointedNodeID);
                if (request != null)
                {
                    request.HandleMaterialResponse(mat);
                }
            }
        }
예제 #2
0
        public ResourceResponse CanHandle(UInt32 nodeReferenceID)
        {
            if (this.referenceID == nodeReferenceID)
            {
                return(this);
            }
            else
            {
                if (next != null)
                {
                    ResourceResponse foundHandler = next.CanHandle(nodeReferenceID);

                    if (foundHandler != null)
                    {
                        return(foundHandler);
                    }
                }
            }

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

                ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                if (request != null)
                {
                    byte[] bundleBytes = resource.GetResourceData();
                    this.internalBundle = AssetBundle.LoadFromMemory(bundleBytes);

                    request.HandleAssetBundleResponse(this.internalBundle);
                }

                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)
            {
                ResourceBlock dataBlock = dataBlocks[this.resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                byte[]             metaDataBuffer     = resource.GetMetaData();
                SerializedMaterial serializedMaterial = ProtocolBufferSerializer.DeserializeMaterialData(metaDataBuffer);

                string shaderName = serializedMaterial.ShaderName;
                this.material      = new UnityEngine.Material(Shader.Find(shaderName));
                this.material.name = serializedMaterial.DisplayName;

                ResourceResponse previousRequest = null;

                if (serializedMaterial.MaterialProperties != null)
                {
                    foreach (KeyValuePair <string, SerializedMaterialProperty> pair in serializedMaterial.MaterialProperties)
                    {
                        if (pair.Value != null)
                        {
                            MaterialPropertyType propertyType = (MaterialPropertyType)pair.Value.PropertyType;

                            if (propertyType == MaterialPropertyType.FloatType)
                            {
                                SerializedMaterialFloatProperty floatProperty = pair.Value as SerializedMaterialFloatProperty;
                                this.material.SetFloat(pair.Key, floatProperty.Val);
                            }
                            else if (propertyType == MaterialPropertyType.VectorType)
                            {
                                SerializedMaterialVectorProperty vectorProperty = pair.Value as SerializedMaterialVectorProperty;

                                Vector4 vector = new Vector4(vectorProperty.X, vectorProperty.Y, vectorProperty.Z, vectorProperty.W);
                                this.material.SetVector(pair.Key, vector);
                            }
                            else if (propertyType == MaterialPropertyType.ColorType)
                            {
                                SerializedMaterialColorProperty colorProperty = pair.Value as SerializedMaterialColorProperty;

                                Color color = new Color(colorProperty.R, colorProperty.G, colorProperty.B, colorProperty.A);
                                this.material.SetColor(pair.Key, color);
                            }
                            else if (propertyType == MaterialPropertyType.TextureType)
                            {
                                SerializedMaterialTextureProperty textureProperty = pair.Value as SerializedMaterialTextureProperty;
                                string propertyName = pair.Key;

                                ResourceResponse textureRequest = new ResourceResponse(textureProperty.ReferenceID, (ResourceResponse response) =>
                                {
                                    material.SetTexture(propertyName, response.GetTextureRequest);
                                });

                                if (previousRequest != null)
                                {
                                    textureRequest.SetPreviousResponse(previousRequest);
                                    previousRequest.SetNextResponse(textureRequest);
                                }

                                previousRequest = textureRequest;
                            }
                        }
                    }
                }

                if (previousRequest != null)
                {
                    //Get the first request created.
                    ResourceResponse firstRequest = previousRequest;
                    while (true)
                    {
                        ResourceResponse previous = firstRequest.GetPreviousResponse();

                        if (previous == null)
                        {
                            break;
                        }

                        firstRequest = previous;
                    }

                    //Have each request set by one of the child nodes.
                    for (int i = 0; i < this.ChildNodes.Count; i++)
                    {
                        UnityNodeBase child = this.ChildNodes[i];

                        child.Deserialize(dataBlocks, parentNode, firstRequest, postInstallActions, optimizedLoad);
                    }
                }

                //Finish up and set material to whatever field/object that owns this node.
                ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                if (request != null)
                {
                    request.HandleMaterialResponse(this.material);
                }

                watch.Stop();
                //Debug.Log("Time to deserialize material: " + watch.ElapsedMilliseconds);

                this.isDeserialized = true;
            }
        }
예제 #5
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;
            }
        }
예제 #6
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[this.resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                if (resource == null)
                {
                    ResourceResponse textureLookUp = resourceResponse.CanHandle(GetReferenceID());
                    if (textureLookUp != null)
                    {
                        textureLookUp.HandleTextureResponse(null);
                    }

                    return;
                }

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

                int width  = metaData.Value <int>("width");
                int height = metaData.Value <int>("height");
                TextureDataFormat textureFormat = (TextureDataFormat)metaData.Value <int>("textureFormat");
                string            fieldName     = metaData.Value <string>("fieldName");

                if (textureFormat == TextureDataFormat.PVRTC4BPP)
                {
                    #if MEASURE_PERFORMANCE
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    #endif

                    #if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_EDITOR
                    int    dataSize    = 0;
                    IntPtr dataPointer = PVRTCEncoderWrapper.DecompressData(textureBytes, width, height, false, ref dataSize);
                    if (dataPointer != IntPtr.Zero)
                    {
                        this.texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
                        this.texture.LoadRawTextureData(dataPointer, dataSize);
                        this.texture.Apply(false, true);

                        //TODO: Probably better to use IDisposable here....
                        PVRTCEncoderWrapper.FreeCompressedDataPointer(dataPointer);
                    }
                    #else
                    this.texture = new Texture2D(width, height, TextureFormat.PVRTC_RGBA4, false);
                    this.texture.LoadRawTextureData(textureBytes);
                    this.texture.Apply(false, true);
                    #endif

                    #if MEASURE_PERFORMANCE
                    watch.Stop();
                    Debug.Log("Time to deserialize texture: " + watch.ElapsedMilliseconds);
                    #endif
                }
                else if (textureFormat == TextureDataFormat.ASTC6X6)
                {
                    #if MEASURE_PERFORMANCE
                    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
                    watch.Start();
                    #endif

                    this.texture = new Texture2D(width, height, TextureFormat.ASTC_RGBA_6x6, false);
                    this.texture.LoadRawTextureData(textureBytes);
                    this.texture.Apply(false, true);

                    #if MEASURE_PERFORMANCE
                    watch.Stop();
                    Debug.Log("Time to deserialize texture: " + watch.ElapsedMilliseconds);
                    #endif
                }
                else if (textureFormat == TextureDataFormat.RGB32)
                {
                    this.texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
                    this.texture.LoadRawTextureData(textureBytes);

                    if (optimizedLoad)
                    {
                        this.texture.Apply(false, true);
                    }
                    else
                    {
                        this.texture.Apply(false);
                    }
                }

                if (fieldName == null)
                {
                    ResourceResponse request = resourceResponse.CanHandle(GetReferenceID());
                    if (request != null)
                    {
                        request.HandleTextureResponse(texture);
                    }
                }
                else
                {
                    if (resourceResponse != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetField(fieldName, this.texture);
                    }
                }

                this.isDeserialized = true;
            }
        }