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);
                }
            }
        }
Beispiel #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;
            }
        }
        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;
            }
        }
Beispiel #5
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;
            }
        }
        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;
            }
        }
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            if (!this.isDeserialized)
            {
                //Deserialize pointed node id.
                ResourceBlock dataBlock = dataBlocks[resourceType] as ResourceBlock;
                AssetResource resource  = dataBlock.GetResource(this.referenceID);

                byte[] bytes = resource.GetResourceData();

                UInt32 referencedNodeID = BitConverter.ToUInt32(bytes, 0);
                //JObject metaData = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));

                //UInt32 referencedNodeID = metaData.Value<UInt32>("targetReferenceID");

                Dictionary <UInt32, UnityNodeBase> referencedNodes = resourceResponse.GetReferencedNodes;

                if (referencedNodes.ContainsKey(referencedNodeID))
                {
                    UnityNodeBase referencedNode = referencedNodes[referencedNodeID];

                    Transform transform = referencedNode.GetTransform();

                    if (transform != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetArrayItem(transform);
                    }
                }

                this.isDeserialized = true;
            }
        }
Beispiel #8
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);

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

                postInstallActions.Add((UnityNodeBase rootNode) => {
                    UnityNodeBase referencedNode = FindNodeWithID(rootNode, pointedNodeID);

                    if (referencedNode is UnityAnimationClipNode)
                    {
                        UnityAnimationClipNode animationClip = referencedNode as UnityAnimationClipNode;

                        AnimationClip animation = animationClip.GetAnimationClip();

                        string jsonString  = System.Text.Encoding.UTF8.GetString(resource.GetMetaData());
                        JObject jsonObject = JObject.Parse(jsonString);

                        string fieldName = jsonObject.Value <string>("fieldName");
                        if (resourceResponse != null)
                        {
                            resourceResponse.GetFieldDeserializer.SetField(fieldName, animation);
                        }
                    }
                });

                this.isDeserialized = true;
            }
        }
Beispiel #9
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);
            }
        }
Beispiel #10
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();
                SerializedFieldData fieldData = ProtocolBufferSerializer.DeserializeFieldData(metaDataBuffer);

                if (fieldData != null)
                {
                    byte[]        data = resource.GetResourceData();
                    System.Object obj  = null;

                    if (data != null)
                    {
                        if (fieldData.type == 1)
                        {
                            obj = System.Text.Encoding.UTF8.GetString(data);
                        }
                        else if (fieldData.type == 2)
                        {
                            obj = BitConverter.ToInt32(data, 0);
                        }
                        else if (fieldData.type == 3)
                        {
                            obj = BitConverter.ToUInt32(data, 0);
                        }
                        else if (fieldData.type == 4)
                        {
                            obj = BitConverter.ToSingle(data, 0);
                        }
                        else if (fieldData.type == 5)
                        {
                            obj = BitConverter.ToDouble(data, 0);
                        }
                        else if (fieldData.type == 6)
                        {
                            obj = BitConverter.ToBoolean(data, 0);
                        }
                        else if (fieldData.type == 7)
                        {
                            obj = data;
                        }

                        if (resourceResponse != null)
                        {
                            if (fieldData.arrayItem)
                            {
                                resourceResponse.GetFieldDeserializer.SetArrayItem(obj);
                            }
                            else
                            {
                                resourceResponse.GetFieldDeserializer.SetField(fieldData.fieldName, obj);
                            }
                        }
                    }
                }

                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);

                string  jsonString = System.Text.Encoding.UTF8.GetString(resource.GetMetaData());
                JObject jsonObject = JObject.Parse(jsonString);

                animationClip           = new AnimationClip();
                animationClip.name      = jsonObject.Value <string>("name");
                animationClip.frameRate = jsonObject.Value <float>("frameRate");
                animationClip.wrapMode  = (UnityEngine.WrapMode)jsonObject.Value <int>("wrapMode");
                animationClip.legacy    = true;

                SerializedAnimationClip serializedAnimationClip = ProtocolBufferSerializer.DeserializeAnimationClipData(resource.GetResourceData());

                foreach (int key in serializedAnimationClip.AnimationChannels.Keys)
                {
                    SerializedAnimationChannelName channel = (SerializedAnimationChannelName)key;

                    SerializedAnimationKeyFrame[] keyFrames = serializedAnimationClip.GetChannel(channel);

                    AnimationCurve curve = CreateAnimationCurve(serializedAnimationClip.PostWrapMode, serializedAnimationClip.PreWrapMode, keyFrames);

                    animationClip.SetCurve("", typeof(Transform), AnimationClipUtils.GetAnimationClipChannelName(channel), curve);
                }

                string fieldName = jsonObject.Value <string>("fieldName");
                if (resourceResponse != null)
                {
                    resourceResponse.GetFieldDeserializer.SetField(fieldName, animationClip);
                }

                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[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;
            }
        }