public void CreateMaterial(MoesTexture moes) { var material = new Material(moes.isAlphaClip ? shaderThunderRoadLitCutoff : shaderThunderRoadLit); // Copying material properties can sometimes prevent BaseMap and BaseColor from being set if (copyMaterialProperties) { material.CopyPropertiesFromMaterial(moes.material); } var mainTex = moes.material.GetTexture("_MainTex"); material.SetColor("_BaseColor", mainTex ? Color.white : moes.isAutodesk ? moes.material.GetColor("_Color") : moes.material.GetColor("_BaseColor")); if (moes.isAutodesk) { material.SetTexture("_BaseMap", mainTex ?? moes.material.mainTexture ?? moes.material.GetTexture("_BaseMap")); } else { material.SetTexture("_BaseMap", moes.material.GetTexture("_BaseMap") ?? mainTex ?? moes.material.mainTexture); } material.SetTexture("_MetallicGlossMap", AssetDatabase.LoadAssetAtPath <Texture2D>(moes.path + "_MOES.png")); material.SetTexture("_BumpMap", moes.material.GetTexture("_BumpMap")); material.SetTexture("_OcclusionMap", null); material.SetTexture("_SpecGlossMap", null); if (moes.emission) { material.EnableKeyword("_EMISSIONMAP_ON"); } if (moes.isAutodesk) { material.SetFloat("_Smoothness", 1f); } material.renderQueue = -1; Debug.Log(material.GetTexture("_BaseMap")); Debug.Log(material.GetColor("_BaseColor")); var outputPath = moes.path + "_MOES.mat"; AssetDatabase.CreateAsset(material, outputPath); Debug.Log("Created " + outputPath); AssetDatabase.ImportAsset(outputPath); }
public void ImportMaterials() { if (!string.IsNullOrEmpty(materialPath)) { materialPath = materialPath.Substring(materialPath.LastIndexOf("Assets"), materialPath.Length - materialPath.LastIndexOf("Assets")); materials = new List <MoesTexture>(); var assets = AssetDatabase.FindAssets("t:Material", new[] { materialPath }); foreach (var guid in assets) { try { var path = AssetDatabase.GUIDToAssetPath(guid); if (path.EndsWith("_MOES.mat")) { continue; } var material = AssetDatabase.LoadAssetAtPath <Material>(path); var item = new MoesTexture() { path = path.Substring(0, path.Length - 4), material = material, metallic = (Texture2D)material.GetTexture("_MetallicGlossMap"), occlusion = (Texture2D)material.GetTexture("_OcclusionMap"), emission = (Texture2D)material.GetTexture("_EmissionMap"), smoothness = (Texture2D)material.GetTexture("_SpecGlossMap"), isAlphaClip = material.GetInt("_AlphaClip") == 1, isAutodesk = material.shader.name.Contains("Autodesk"), moesExists = AssetDatabase.LoadAssetAtPath <Material>(path.Replace(".mat", "_MOES.mat")) != null }; if (!item.smoothness) { item.smoothness = item.material.GetInt("_SmoothnessTextureChannel") == 0 ? (Texture2D)material.GetTexture("_MetallicGlossMap") : (Texture2D)material.GetTexture("_BaseMap"); item.smoothnessIsAlpha = true; } if (item.metallic || item.occlusion || item.emission || item.smoothness) { materials.Add(item); } } catch { } } } }
public void CreateTexture(MoesTexture moes) { Material _material = new Material(shaderMoesConvert) { hideFlags = HideFlags.HideAndDontSave }; int width = 1; int height = 1; if (moes.metallic != null) { width = Mathf.Max(width, moes.metallic.width); height = Mathf.Max(height, moes.metallic.height); } else if (moes.occlusion != null) { width = Mathf.Max(width, moes.occlusion.width); height = Mathf.Max(height, moes.occlusion.height); } else if (moes.emission != null) { width = Mathf.Max(width, moes.emission.width); height = Mathf.Max(height, moes.emission.height); } else if (moes.smoothness != null) { width = Mathf.Max(width, moes.smoothness.width); height = Mathf.Max(height, moes.smoothness.height); } _material.SetTexture("_MetallicGlossMap", moes.metallic); _material.SetTexture("_OcclusionMap", moes.occlusion); _material.SetTexture("_EmissionMap", moes.emission); _material.SetTexture("_SpecGlossMap", moes.smoothness); _material.SetInt("_SmoothnessIsAlpha", moes.smoothnessIsAlpha ? 1 : 0); _material.SetInt("_InvertSmoothness", moes.isAutodesk ? 1 : 0); _material.SetInt("_FixColorSpace", fixColorSpace ? 1 : 0); var previous = RenderTexture.active; RenderTexture tempRT = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32); tempRT.Create(); Graphics.Blit(null, tempRT, _material); Texture2D output = new Texture2D(tempRT.width, tempRT.height, TextureFormat.ARGB32, true, true); RenderTexture.active = tempRT; output.ReadPixels(new Rect(0, 0, tempRT.width, tempRT.height), 0, 0); output.Apply(); output.filterMode = FilterMode.Bilinear; RenderTexture.active = previous; tempRT.Release(); var outputPath = moes.path + "_MOES.png"; File.WriteAllBytes(outputPath, output.EncodeToPNG()); Debug.Log("Created " + outputPath); AssetDatabase.ImportAsset(outputPath); }