public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions) { //Make sure this is always 36 characters long. this.referenceID = this.rootNode.GenerateID(); ComponentNode componentNode = new ComponentNode(PCFResourceType.MESH, referenceID, null); //Component nodes must always be parented to objNodes. objNode.AddChildNode(componentNode); //Serialize mesh into a byte array. byte[] bytes = MeshSerializeUtilities.WriteMesh(this.meshFilter.sharedMesh, true, false); //Serialize Material UnitySerializeMaterial materialSerializer = new UnitySerializeMaterial(this.meshRenderer.sharedMaterial, this.rootNode); materialSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions); //Make sure mesh knows that material it needs. JObject metaData = new JObject(); metaData["materialID"] = materialSerializer.GetPointerID(); //Create serialized asset by converting data to a bytearray and give it to the constructor. AssetResource resource = new AssetResource(false); byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None)); resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, bytes); serializedAssets.AddResource(referenceID, PCFResourceType.MESH, resource); //Nodes store their resource when serializing componentNode.SetSerializer(this); }
public override void Serialize(SerializedAssets serializedAssets, object[] serializeOptions, NodeBase objNode, List <Action <NodeBase> > postSerializeActions) { //Make sure this is always 36 characters long. this.referenceID = this.rootNode.GenerateID(); ComponentNode componentNode = new ComponentNode(PCFResourceType.SKINNEDMESH, referenceID, null); //Component nodes must always be parented to objNodes. objNode.AddChildNode(componentNode); byte[] bytes = MeshSerializeUtilities.WriteMesh(this.skinnedMesh.sharedMesh, true, true); //Serialize Material UnitySerializeMaterial materialSerializer = new UnitySerializeMaterial(this.skinnedMesh.sharedMaterial, this.rootNode); materialSerializer.Serialize(serializedAssets, serializeOptions, componentNode, postSerializeActions); //Make sure mesh knows that material it needs. JObject metaData = new JObject(); metaData["materialID"] = materialSerializer.GetPointerID(); metaData["rootBone"] = skinnedMesh.rootBone.name; if (skinnedMesh.probeAnchor != null) { metaData["probeAnchor"] = skinnedMesh.probeAnchor.name; } metaData["quality"] = (int)skinnedMesh.quality; JArray bones = new JArray(); for (int i = 0; i < skinnedMesh.bones.Length; i++) { bones.Add(skinnedMesh.bones[i].name); } metaData["bones"] = bones; JArray blendShapeWeights = new JArray(); for (int i = 0; i < skinnedMesh.sharedMesh.blendShapeCount; i++) { blendShapeWeights.Add(skinnedMesh.GetBlendShapeWeight(i)); } metaData["blendShapeWeights"] = blendShapeWeights; //Create serialized asset by converting data to a bytearray and give it to the constructor. AssetResource resource = new AssetResource(false); byte[] metaDataBuffer = System.Text.Encoding.UTF8.GetBytes(metaData.ToString(Formatting.None)); resource.Serialize(referenceID, MetaDataType.JSON, metaDataBuffer, bytes); serializedAssets.AddResource(referenceID, PCFResourceType.SKINNEDMESH, resource); //Nodes store their resource when serializing componentNode.SetSerializer(this); }
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) { 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); } }