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

                byte[] metaDataBuffer         = resource.GetMetaData();
                SerializedFieldData fieldData = ProtocolBufferSerializer.DeserializeFieldData(metaDataBuffer);
                string assemblyName           = ProtocolBufferSerializer.GetAssemblyName(fieldData.assemblyType);
                string scriptTypeName         = fieldData.typeName;
                string fieldName = fieldData.fieldName;

                Type scriptType = null;

                //Qualify type check with assembly name, GetType only looks in current assembly otherwise.
                if (!string.IsNullOrEmpty(assemblyName))
                {
                    scriptType = Type.GetType(scriptTypeName + ", " + assemblyName);
                }
                else
                {
                    scriptType = Type.GetType(scriptTypeName);
                }

                if (scriptType != null)
                {
                    Gradient gradient = new Gradient();

                    PropertySetter colorKeySetter = new PropertySetter("colorKeys", (System.Object val) => { gradient.colorKeys = val as GradientColorKey[]; });
                    PropertySetter alphaKeySetter = new PropertySetter("alphaKeys", (System.Object val) => { gradient.alphaKeys = val as GradientAlphaKey[]; });

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

                    FieldDeserializer fieldDeserializer = new FieldDeserializer(customValueSetters, gradient);
                    ResourceResponse  response          = new ResourceResponse();
                    response.SetFieldDeserializer(fieldDeserializer);

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

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

                    if (resourceResponse != null)
                    {
                        resourceResponse.GetFieldDeserializer.SetField(fieldName, gradient);
                    }
                }

                this.isDeserialized = true;
            }
        }
Пример #3
0
 public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks, UnityNodeBase parentNode, ResourceResponse resourceRequest, List <Action <UnityNodeBase> > postInstallActions, bool optimizedLoad)
 {
     for (int i = 0; i < this.ChildNodes.Count; i++)
     {
         UnityNodeBase child = this.ChildNodes[i];
         child.Deserialize(dataBlocks, this, null, postInstallActions, optimizedLoad);
     }
 }
Пример #4
0
        public override void Deserialize(Dictionary <PCFResourceType, DataBlockBase> dataBlocks,
                                         UnityNodeBase parentNode,
                                         ResourceResponse resourceResponse,
                                         List <Action <UnityNodeBase> > postInstallActions,
                                         bool optimizedLoad)
        {
            this.gameobject = new GameObject(this.name);
            this.gameobject.SetActive(false);

            for (int i = 0; i < this.ChildNodes.Count; i++)
            {
                UnityNodeBase child = this.ChildNodes[i];
                child.Deserialize(dataBlocks, this, null, postInstallActions, optimizedLoad);
            }
        }
Пример #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;
            }
        }
Пример #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);

                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;
            }
        }
Пример #7
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);
            }
        }
        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[] serializedProbes = resource.GetResourceData();
                 *
                 * //30 floats in each array element.
                 * int sizeOfArray = numberOfProbes * 30;
                 * float[] lightProbeData = new float[sizeOfArray];
                 *
                 * for (int i = 0; i < sizeOfArray; i++)
                 * {
                 *  lightProbeData[i] = BitConverter.ToSingle(serializedProbes, i * sizeof(float));
                 * }
                 *
                 * Vector3[] probePositions = new Vector3[numberOfProbes];
                 * SphericalHarmonicsL2[] bakedProbes = new SphericalHarmonicsL2[numberOfProbes];
                 *
                 * int stride = 0;
                 * for (int i = 0; i < numberOfProbes; i++)
                 * {
                 *  probePositions[i] = new Vector3(lightProbeData[stride], lightProbeData[stride + 1], lightProbeData[stride + 2]);
                 *  stride += 3;
                 *
                 *  SphericalHarmonicsL2 probe = new SphericalHarmonicsL2();
                 *  for (int k = 0; k < 27; k++)
                 *  {
                 *      probe[0, k] = lightProbeData[stride + k];
                 *  }
                 *  bakedProbes[i] = probe;
                 *
                 *  stride += 27;
                 * }
                 */

                byte[]  metaDataBuffer   = resource.GetMetaData();
                JObject metaData         = JObject.Parse(System.Text.Encoding.UTF8.GetString(metaDataBuffer));
                int     numberOfProbes   = metaData.Value <int>("numberOfProbes");
                JArray  bundleReferences = metaData.Value <JArray>("bundleReferences");

                UInt32 mappedNodeID = 0;

                //Load the correct bundle depending on what platform we are running.
                if (mapping.ContainsKey(Application.platform))
                {
                    string mappedPlatform = mapping[Application.platform];

                    foreach (JObject item in bundleReferences)
                    {
                        string platform = item.Value <string>("platform");

                        if (string.CompareOrdinal(mappedPlatform, platform) == 0)
                        {
                            mappedNodeID = item.Value <UInt32>("referenceID");
                        }
                    }
                }

                if (mappedNodeID != 0)
                {
                    //Create a request to be send down the chain of children, see if any child will handle it.
                    ResourceResponse lightprobeResponse = new ResourceResponse(mappedNodeID, (ResourceResponse response) => {
                        AssetBundle internalBundle = response.GetAssetBundleRequest;

                        if (internalBundle != null)
                        {
                            UnityEngine.Object[] objects = internalBundle.LoadAllAssets <UnityEngine.Object>();

                            foreach (UnityEngine.Object obj in objects)
                            {
                                if (obj is LightProbes)
                                {
                                    this.lightProbes = obj as LightProbes;
                                    break;
                                }
                            }

                            if (this.lightProbes == null)
                            {
                                Debug.LogError("Unable to find lightprobe data!");
                            }
                        }
                    });

                    for (int i = 0; i < this.ChildNodes.Count; i++)
                    {
                        UnityNodeBase child = this.ChildNodes[i];
                        child.Deserialize(dataBlocks, this, lightprobeResponse, postInstallActions, optimizedLoad);
                    }
                }

                this.isDeserialized = true;
            }
        }