private void OnMasterModelLoaded(GameObject masterModel) { loadingOriginalModel = false; if (!masterModel) { Debug.LogError("Model failed to load!"); return; } originalModel = masterModel; originalModel.layer = Layer; foreach (Transform child in originalModel.transform) { child.gameObject.layer = Layer; string textureOverride; textureOverrides.TryGetValue(child.name, out textureOverride); if (textureOverride == null) { textureOverrides.TryGetValue("*", out textureOverride); } if (textureOverride != null) { MeshRenderer renderer = child.GetComponent <MeshRenderer>(); TextureReference texture = TextureReference.GetTextureReference(textureOverride); Material newMaterial = new Material(renderer.sharedMaterial); renderer.sharedMaterial = newMaterial; CoroutineManager.Instance.QueueCoroutine(texture.LoadOrGetTexture(loadedTexture => newMaterial.mainTexture = loadedTexture)); } } originalModel.transform.SetParent(modelRoot.transform); ModelProperties originalProperties = new ModelProperties(0, null); modifiedModels[originalProperties] = originalModel; foreach (ModelRequest modelRequest in modelRequests) { InitializeModifiedModel(modelRequest.ModelProperties); GameObject instance = Object.Instantiate(modifiedModels[modelRequest.ModelProperties]); modelRequest.Callback.Invoke(instance); } modelRequests.Clear(); }
private static IEnumerator LoadMaterial(BinaryReader source, string modelFolder, Action <Material> callback) { string texName = ReadString(source); string texLocation = Path.Combine(modelFolder, texName); string matName = ReadString(source); MaterialKey materialKey = new MaterialKey(matName, texLocation); float glossiness = 0; bool hasMaterialProperties = source.ReadBoolean(); if (hasMaterialProperties) { bool hasEmissive = source.ReadBoolean(); if (hasEmissive) { source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); } bool hasShininess = source.ReadBoolean(); if (hasShininess) { glossiness = source.ReadSingle() / 100; if (glossiness > 1) { glossiness = 0; } } bool hasSpecular = source.ReadBoolean(); if (hasSpecular) { source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); } bool hasTransparencyColor = source.ReadBoolean(); if (hasTransparencyColor) { source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); source.ReadSingle(); } } if (cachedMaterials.ContainsKey(materialKey)) { Debug.Log("Loading material from cache"); Material cachedMaterial = cachedMaterials[materialKey]; callback.Invoke(cachedMaterial); } else { Material material = new Material(GraphicsManager.Instance.WomDefaultMaterial); material.name = matName; TextureReference textureReference = TextureReference.GetTextureReference(texLocation); Texture2D texture = null; if (textureReference != null) { yield return(textureReference.LoadOrGetTexture(loadedTexture => texture = loadedTexture)); } if (texture) { material.SetTexture(ShaderPropertyIds.MainTex, texture); } else { material.SetColor(ShaderPropertyIds.Color, new Color(1, 1, 1, 0)); } material.SetFloat(ShaderPropertyIds.Glossiness, glossiness); cachedMaterials[materialKey] = material; callback.Invoke(material); } }