public void SetMesh(Asset asset) { FaceMeshAsset avatarFace = asset as FaceMeshAsset; if (avatarFace == null) { throw new Exception("Could not convert asset from Repo to FaceMeshAsset."); } mMesh = avatarFace.Mesh; mUvShellNameToIndex.Clear(); /* * Create a list of UV shells ( A submesh maps to one UV shell ) * and populate the corresponding frameSequence and currentFrame arrays */ int uvShellCount = avatarFace.UvShellCount; mUvShells = new UvShell[uvShellCount]; mCurrentFrames = new int[uvShellCount]; mFrameSequence = new int[uvShellCount][]; mGridSize = new Vector2[uvShellCount]; mCellSize = new Vector2[uvShellCount]; mNumberOfCells = new int[uvShellCount]; for (int x = 0; x < uvShellCount; x++) { mUvShells[x] = new UvShell(mMesh, x); mCurrentFrames[x] = -1; // OFF mGridSize[x] = avatarFace.GetUvGridDimensions(x); mCellSize[x] = new Vector2(1.0f / mGridSize[x].x, 1.0f / mGridSize[x].y); mNumberOfCells[x] = (int)(mGridSize[x].x * mGridSize[x].y); mUvShellNameToIndex.Add(avatarFace.GetUvShellName(x), x); } /* * Combine all submeshes into one mesh ( i.e. create a new mesh ) */ Mesh newMesh = new Mesh(); newMesh.vertices = mMesh.vertices; newMesh.triangles = mMesh.triangles; newMesh.uv = mMesh.uv; newMesh.normals = mMesh.normals; newMesh.RecalculateBounds(); mMesh = newMesh; mMeshFilter.mesh = newMesh; /* * Set the atlas space for the UV shells */ for (int x = 0; x < mUvShells.Length; ++x) { mUvShells[x].Atlas = mTexturePalette.GetTextureZoneUvArea(avatarFace.GetUvShellTextureZoneName(x)); } /* Update Uvs */ mNewUvs = mMesh.uv; for (int x = 0; x < mUvShells.Length; ++x) { UpdateUvShell(mUvShells[x]); } mMesh.uv = mNewUvs; mUvsAreDirty = false; /* Update Material */ Material avatarMaterial = AvatarEntity.AvatarMaterial; avatarMaterial.mainTexture = mMeshRenderer.materials[0].mainTexture; mMeshRenderer.materials = new Material[] { avatarMaterial }; }
//Interface which all classes will use to get an asset. //It first checks to see if the asset is in the dictionary. If so it returns that asset. //Otherwise it attempts to load it from disk or download it from the web based on that path. private void GetAsset <T> ( XmlNode assetData, System.Type assetType, AssetSubType assetSubType, string key, string path, Action <T> returnAssetCallback ) where T : Asset { //Check if the asset is in the dictionary already. Asset returnAsset; if (mAssetDictionary.TryGetValue(key, out returnAsset)) { if (!(returnAsset.GetType() == assetType)) { throw new ArgumentException("The Asset with key: (" + key + ") expected type " + typeof(T).Name + " actually was " + returnAsset.GetType()); } returnAssetCallback((T)returnAsset); } else { //Determine type of asset, build a delegate to handle creating the asset when it's downloaded //or instantiated, and return it using the callback supplied. if (assetType == typeof(TextureAsset)) { LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject) { Texture2D texture = (Texture2D)loadedObject; TexturePixelSource texturePixelSource = new TexturePixelSource(texture); TextureAsset textureAsset = new TextureAsset(assetSubType, texturePixelSource, texture.name, path, key); if (textureAsset == null) { throw new Exception("Couldn't create TexturePixelSource."); } returnAsset = textureAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); }); } else if (assetType == typeof(ImageAsset)) { LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject) { Texture2D texture = (Texture2D)loadedObject; ImageAsset imageAsset = new ImageAsset(assetSubType, texture, texture.name, path, key); if (imageAsset == null) { throw new Exception("Couldn't create ImageAsset."); } returnAsset = imageAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); }); } else if (assetType == typeof(ColorAsset)) { //Pull out color values. string hexColorValue = assetData.SelectSingleNode("AssetColor").InnerText; string alphaValue = assetData.SelectSingleNode("Alpha").InnerText; //Set color values. Color color = ColorUtility.HexToColor(hexColorValue); color.a = float.Parse(alphaValue); ColorAsset colorAsset = new ColorAsset(assetSubType, color, path, path, key); if (colorAsset == null) { throw new Exception("Couldn't create color asset."); } returnAsset = colorAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); } else if (assetType == typeof(MeshAsset)) { LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject) { GameObject avatar = loadedObject as GameObject; if (avatar == null) { throw new Exception("couldn't instantiate gameobject at path: " + path); } string meshFilterToFind = assetData.SelectSingleNode("MeshName").InnerText; foreach (Transform child in avatar.GetComponentsInChildren(typeof(Transform))) { if (child.name == meshFilterToFind) { MeshFilter meshFilter = child.GetComponent(typeof(MeshFilter)) as MeshFilter; Mesh mesh = meshFilter.mesh; MeshAsset meshAsset = new MeshAsset(assetSubType, mesh, path, path, key); if (meshAsset == null) { throw new Exception("Couldn't create MeshAsset."); } returnAsset = meshAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); break; } } }); } else if (assetType == typeof(SkinnedMeshAsset)) { LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject) { GameObject avatar = loadedObject as GameObject; if (avatar == null) { throw new Exception("couldn't instantiate gameobject at path: " + path); } string skinnedMeshRendererToFind = assetData.SelectSingleNode("MeshName").InnerText; foreach (Transform child in avatar.GetComponentsInChildren(typeof(Transform))) { if (child.name == skinnedMeshRendererToFind) { SkinnedMeshRenderer skinnedMeshRenderer = child.GetComponent(typeof(SkinnedMeshRenderer)) as SkinnedMeshRenderer; SkinnedMeshAsset skinnedMeshAsset = new SkinnedMeshAsset(assetSubType, skinnedMeshRenderer, "", path, key); if (skinnedMeshRenderer == null) { throw new Exception("Couldn't create MeshAsset."); } returnAsset = skinnedMeshAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); break; } } }); } else if (assetType == typeof(FaceMeshAsset)) { LoadUnityObjectFromPath <T>(path, delegate(UnityEngine.Object loadedObject) { GameObject avatar = loadedObject as GameObject; if (avatar == null) { throw new Exception("couldn't instantiate gameobject at path: " + path); } string meshFilterToFind = assetData.SelectSingleNode("MeshName").InnerText; foreach (Transform child in avatar.GetComponentsInChildren(typeof(Transform))) { if (child.name == meshFilterToFind) { XmlNode meshInfoNode = assetData.SelectSingleNode("MeshData/FaceMeshInfo"); if (meshInfoNode == null) { throw new XmlException("Couldn't get face mesh info xml from node: " + assetData.OuterXml); } MeshFilter meshFilter = child.GetComponent(typeof(MeshFilter)) as MeshFilter; meshFilter.gameObject.active = true; Mesh mesh = meshFilter.mesh; FaceMeshAsset faceMeshAsset = new FaceMeshAsset(assetSubType, mesh, path, path, key, meshInfoNode); if (faceMeshAsset == null) { throw new Exception("Couldn't create MeshAsset."); } returnAsset = faceMeshAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); break; } } }); } else if (assetType == typeof(FaceAnimationAsset)) { string moodName = assetData.SelectSingleNode("Name").InnerText; FaceAnimationName faceAnimationName = (FaceAnimationName)Enum.Parse(typeof(FaceAnimationName), moodName); FaceAnimation faceAnimation = mFaceAnimationFactory.GetFaceAnimation(moodName); FaceAnimationAsset faceAnimationAsset = new FaceAnimationAsset(assetSubType, faceAnimation, moodName, path, key, faceAnimationName); GameFacade.Instance.RetrieveProxy <AnimationProxy>().AddFaceAnimation(faceAnimationAsset); returnAsset = faceAnimationAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); } else if (assetType == typeof(RigAnimationAsset)) { LoadUnityObjectFromPath <UnityEngineAsset>(path, delegate(UnityEngine.Object loadedObject) { GameObject animationGameObject = GameObject.Instantiate(loadedObject as UnityEngine.Object) as GameObject; if (animationGameObject == null) { Console.LogError("Could not cast loadedObject to GameObject from path: " + path); } Animation animation = animationGameObject.GetComponent(typeof(Animation)) as Animation; if (animation == null) { Console.LogError("No animation component on loadedObject cast as a gameobject loaded from path: " + path); } string nameOfAnimation = assetData.SelectSingleNode("AnimationName").InnerText; RigAnimationName animationName; if (nameOfAnimation == null) { Console.LogError("AssetData on rig animation asset info does not contain an RigAnimationName node."); animationName = RigAnimationName.None; } else { animationName = (RigAnimationName)Enum.Parse(typeof(RigAnimationName), nameOfAnimation); } RigAnimationAsset rigAnimationAsset = new RigAnimationAsset(assetSubType, animation.clip, animation.clip.name, path, key, animationName); GameObject.Destroy(animationGameObject); GameFacade.Instance.RetrieveProxy <AnimationProxy>().AddRigAnimation(rigAnimationAsset); returnAsset = rigAnimationAsset; if (!mAssetDictionary.ContainsKey(key)) { mAssetDictionary.Add(key, returnAsset); } returnAssetCallback((T)returnAsset); }); } else { throw new ArgumentException("The ClientAssetRepository does not know how to load an asset of type " + typeof(T).Name); } } }