Exemplo n.º 1
0
        private static IEnumerator ResolveMaterialTexture(string id)
        {
            Common.Models.Material mat = GetMaterial(id);
            if (mat == null)
            {
                UnityEngine.Debug.Log($"MATERIAL {id} DOES NOT EXIST");
                _totalCalls--;
                yield return(null);

                yield break;
            }

            string url = $"{_ModelServerURL}/api/material/{mat.Id}";

            UnityEngine.Texture materialTexture = null;

            UnityEngine.Debug.Log(url);
            using (UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(url))
            {
                yield return(webRequest.SendWebRequest());

                if (webRequest.isNetworkError)
                {
                    UnityEngine.Debug.Log("Error: " + webRequest.error);
                    yield return(null);

                    _totalCalls--;
                    yield break;
                }
                else if (webRequest.isHttpError)
                {
                    // UnityEngine.Debug.Log("Error: " + webRequest.error);
                    yield return(null);

                    _totalCalls--;
                    yield break;
                }
                else
                {
                    materialTexture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
                }
            }

            if (materialTexture != null)
            {
                UnityEngine.Material baseMaterial = LookupMaterial(mat.Id);

                UnityEngine.Material newMaterial = new UnityEngine.Material(baseMaterial.shader);

                newMaterial.CopyPropertiesFromMaterial(baseMaterial);

                newMaterial.name        = mat.Name + " (With Texture)";
                newMaterial.mainTexture = materialTexture;
                newMaterial.color       = new UnityEngine.Color(
                    220f / 255f,
                    220f / 255f,
                    220f / 255f
                    );
                newMaterial.SetFloat("_Metallic", (float)mat.Shininess / byte.MaxValue);
                newMaterial.SetFloat("_Smoothness", (float)mat.Smoothness / byte.MaxValue);

                matLib.Add("_" + id, newMaterial);

                UnityEngine.Debug.Log(JsonConvert.SerializeObject(mat));

                // UnityEngine.GameObject.Find("Test").GetComponent<UnityEngine.MeshRenderer>().material = newMaterial;
            }

            _totalCalls--;
        }