//private static GLTF.Schema.Material ConvertKHRWebGLMaterial(UnityEngine.Material material, ExportorEntry entry) //{ //} public static GLTF.Schema.Material ConvertMaterial(SeinCustomMaterial mat, ExporterEntry entry) { var material = new GLTF.Schema.Material(); material.Name = mat.unityMaterialName; if (mat.uniformsTexture.Length != 0) { foreach (var uniform in mat.uniformsTexture) { uniform.id = entry.SaveTexture(uniform.value, mat.transparent); } } if (mat.uniformsCubeTexture.Length != 0) { foreach (var uniform in mat.uniformsCubeTexture) { uniform.id = entry.SaveCubeTexture(uniform.value); } } if (material.Extensions == null) { material.Extensions = new Dictionary <string, Extension>(); } if (mat.transparent) { material.AlphaMode = AlphaMode.BLEND; } ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(Sein_customMaterialExtensionFactory)), entry, material.Extensions, mat); return(material); }
protected override void Serialize(UnityEngine.Object sourceAsset) { this._material = sourceAsset as UnityEngine.Material; this._isParticle = this._target.GetComponent <ParticleSystem>() != null; var source = this._material; var data = new MaterialData(); var technique = new Techniques(); var materialExtension = new KhrTechniquesWebglMaterialExtension(); var assetExtension = new MaterialAssetExtension() { version = "5.0", minVersion = "5.0", renderQueue = source.renderQueue }; // data.values = materialExtension.values; data.technique = technique; data.asset = assetExtension; var parser = this.getParser(this.GetMaterialType()); parser.Parse(source, data); var materialGLTF = new GLTF.Schema.Material(); materialGLTF.Name = source.name; materialGLTF.Extensions = new Dictionary <string, IExtension>() { { KhrTechniquesWebglMaterialExtension.EXTENSION_NAME, materialExtension } }; var techniqueExt = new KhrTechniqueWebglGlTfExtension(); techniqueExt.techniques.Add(technique); if (this._root.Extensions.ContainsKey(AssetVersionExtension.EXTENSION_NAME)) { this._root.Extensions.Remove(AssetVersionExtension.EXTENSION_NAME); } this._root.Extensions.Add(AssetVersionExtension.EXTENSION_NAME, assetExtension); this._root.Extensions.Add(KhrTechniquesWebglMaterialExtension.EXTENSION_NAME, techniqueExt); this._root.Materials.Add(materialGLTF); }
//private static GLTF.Schema.Material ConvertKHRWebGLMaterial(UnityEngine.Material material, ExportorEntry entry) //{ //} public static GLTF.Schema.Material ConvertMaterial(SeinCustomMaterial mat, ExporterEntry entry) { var material = new GLTF.Schema.Material(); if (mat.uniformsTexture.Length != 0) { foreach (var uniform in mat.uniformsTexture) { uniform.id = entry.SaveTexture(uniform.value, mat.transparent); } } if (mat.uniformsCubeTexture.Length != 0) { foreach (var uniform in mat.uniformsCubeTexture) { // todo: support cubemap //int diffuseTextureIndex = processTexture(uniform.value, hasTransparency ? IMAGETYPE.RGBA : IMAGETYPE.RGBA_OPAQUE); //uniform.index = diffuseTextureIndex; //uniform.texCoord = 0; } } if (material.Extensions == null) { material.Extensions = new Dictionary <string, Extension>(); } if (mat.transparent) { material.AlphaMode = AlphaMode.BLEND; } ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(Sein_customMaterialExtensionFactory)), entry, material.Extensions, mat); return(material); }
protected virtual MaterialCacheData CreateMaterial(GLTF.Schema.Material def, int materialIndex) { MaterialCacheData materialWrapper = null; if (materialIndex < 0 || _assetCache.MaterialCache[materialIndex] == null) { Shader shader; // get the shader to use for this material try { if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_pbrSpecularGlossiness")) { shader = _shaderCache[MaterialType.KHR_materials_pbrSpecularGlossiness]; } else if (def.PbrMetallicRoughness != null) { shader = _shaderCache[MaterialType.PbrMetallicRoughness]; } else if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains("KHR_materials_common") && def.CommonConstant != null) { shader = _shaderCache[MaterialType.CommonConstant]; } else { shader = _shaderCache[MaterialType.PbrMetallicRoughness]; } } catch (KeyNotFoundException) { Debug.LogWarningFormat("No shader supplied for type of glTF material {0}, using Standard fallback", def.Name); shader = Shader.Find("Standard"); } shader.maximumLOD = MaximumLod; var material = new UnityEngine.Material(shader); if (def.AlphaMode == AlphaMode.MASK) { material.SetOverrideTag("RenderType", "TransparentCutout"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest; material.SetFloat("_Cutoff", (float)def.AlphaCutoff); } else if (def.AlphaMode == AlphaMode.BLEND) { material.SetOverrideTag("RenderType", "Transparent"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); material.SetInt("_ZWrite", 0); material.DisableKeyword("_ALPHATEST_ON"); material.EnableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.Transparent; } else { material.SetOverrideTag("RenderType", "Opaque"); material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); material.DisableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); material.renderQueue = -1; } if (def.DoubleSided) { material.SetInt("_Cull", (int)CullMode.Off); } else { material.SetInt("_Cull", (int)CullMode.Back); } if (def.PbrMetallicRoughness != null) { var pbr = def.PbrMetallicRoughness; material.SetColor("_Color", pbr.BaseColorFactor.ToUnityColor()); if (pbr.BaseColorTexture != null) { var textureDef = pbr.BaseColorTexture.Index.Value; material.SetTexture("_MainTex", CreateTexture(textureDef)); ApplyTextureTransform(pbr.BaseColorTexture, material, "_MainTex"); } material.SetFloat("_Metallic", (float)pbr.MetallicFactor); if (pbr.MetallicRoughnessTexture != null) { var texture = pbr.MetallicRoughnessTexture.Index.Value; material.SetTexture("_MetallicRoughnessMap", CreateTexture(texture)); ApplyTextureTransform(pbr.MetallicRoughnessTexture, material, "_MetallicRoughnessMap"); } material.SetFloat("_Roughness", (float)pbr.RoughnessFactor); } if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains(KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME)) { KHR_materials_pbrSpecularGlossinessExtension specGloss = def.Extensions[KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME] as KHR_materials_pbrSpecularGlossinessExtension; if (specGloss.DiffuseTexture != null) { var texture = specGloss.DiffuseTexture.Index.Value; material.SetTexture("_MainTex", CreateTexture(texture)); ApplyTextureTransform(specGloss.DiffuseTexture, material, "_MainTex"); } else { material.SetColor("_Color", specGloss.DiffuseFactor.ToUnityColor()); } if (specGloss.SpecularGlossinessTexture != null) { var texture = specGloss.SpecularGlossinessTexture.Index.Value; material.SetTexture("_SpecGlossMap", CreateTexture(texture)); material.EnableKeyword("_SPECGLOSSMAP"); ApplyTextureTransform(specGloss.SpecularGlossinessTexture, material, "_SpecGlossMap"); } else { material.SetVector("_SpecColor", specGloss.SpecularFactor.ToUnityVector3()); material.SetFloat("_Glossiness", (float)specGloss.GlossinessFactor); } } if (def.CommonConstant != null) { material.SetColor("_AmbientFactor", def.CommonConstant.AmbientFactor.ToUnityColor()); if (def.CommonConstant.LightmapTexture != null) { material.EnableKeyword("LIGHTMAP_ON"); var texture = def.CommonConstant.LightmapTexture.Index.Value; material.SetTexture("_LightMap", CreateTexture(texture)); material.SetInt("_LightUV", def.CommonConstant.LightmapTexture.TexCoord); ApplyTextureTransform(def.CommonConstant.LightmapTexture, material, "_LightMap"); } material.SetColor("_LightFactor", def.CommonConstant.LightmapFactor.ToUnityColor()); } if (def.NormalTexture != null) { var texture = def.NormalTexture.Index.Value; material.SetTexture("_BumpMap", CreateTexture(texture)); material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale); material.EnableKeyword("_NORMALMAP"); ApplyTextureTransform(def.NormalTexture, material, "_BumpMap"); } if (def.OcclusionTexture != null) { var texture = def.OcclusionTexture.Index; material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength); if (def.PbrMetallicRoughness != null && def.PbrMetallicRoughness.MetallicRoughnessTexture != null && def.PbrMetallicRoughness.MetallicRoughnessTexture.Index.Id == texture.Id) { material.EnableKeyword("OCC_METAL_ROUGH_ON"); } else { material.SetTexture("_OcclusionMap", CreateTexture(texture.Value)); ApplyTextureTransform(def.OcclusionTexture, material, "_OcclusionMap"); } } if (def.EmissiveTexture != null) { var texture = def.EmissiveTexture.Index.Value; material.EnableKeyword("EMISSION_MAP_ON"); material.EnableKeyword("_EMISSION"); material.SetTexture("_EmissionMap", CreateTexture(texture)); material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord); ApplyTextureTransform(def.EmissiveTexture, material, "_EmissionMap"); } material.SetColor("_EmissionColor", def.EmissiveFactor.ToUnityColor()); materialWrapper = new MaterialCacheData { UnityMaterial = material, UnityMaterialWithVertexColor = new UnityEngine.Material(material), GLTFMaterial = def }; materialWrapper.UnityMaterialWithVertexColor.EnableKeyword("VERTEX_COLOR_ON"); if (materialIndex > 0) { _assetCache.MaterialCache[materialIndex] = materialWrapper; } } return(materialIndex > 0 ? _assetCache.MaterialCache[materialIndex] : materialWrapper); }
private MaterialId ExportMaterial(UnityEngine.Material materialObj) { MaterialId id = GetMaterialId(_root, materialObj); if (id != null) { return(id); } var material = new GLTF.Schema.Material(); if (ExportNames) { material.Name = materialObj.name; } if (materialObj.HasProperty("_Cutoff")) { material.AlphaCutoff = materialObj.GetFloat("_Cutoff"); } switch (materialObj.GetTag("RenderType", false, "")) { case "TransparentCutout": material.AlphaMode = AlphaMode.MASK; break; case "Transparent": material.AlphaMode = AlphaMode.BLEND; break; default: material.AlphaMode = AlphaMode.OPAQUE; break; } material.DoubleSided = materialObj.HasProperty("_Cull") && materialObj.GetInt("_Cull") == (float)UnityEngine.Rendering.CullMode.Off; if (materialObj.HasProperty("_EmissionColor")) { material.EmissiveFactor = materialObj.GetColor("_EmissionColor").ToNumericsColorRaw(); } if (materialObj.HasProperty("_EmissionMap")) { var emissionTex = materialObj.GetTexture("_EmissionMap"); if (emissionTex != null) { material.EmissiveTexture = ExportTextureInfo(emissionTex); ExportTextureTransform(material.EmissiveTexture, materialObj, "_EmissionMap"); } } if (materialObj.HasProperty("_BumpMap")) { var normalTex = materialObj.GetTexture("_BumpMap"); if (normalTex != null) { material.NormalTexture = ExportNormalTextureInfo(normalTex, materialObj); ExportTextureTransform(material.NormalTexture, materialObj, "_BumpMap"); } } if (materialObj.HasProperty("_OcclusionMap")) { var occTex = materialObj.GetTexture("_OcclusionMap"); if (occTex != null) { material.OcclusionTexture = ExportOcclusionTextureInfo(occTex, materialObj); ExportTextureTransform(material.OcclusionTexture, materialObj, "_OcclusionMap"); } } switch (materialObj.shader.name) { case "Standard": case "GLTF/GLTFStandard": material.PbrMetallicRoughness = ExportPBRMetallicRoughness(materialObj); break; case "GLTF/GLTFConstant": material.CommonConstant = ExportCommonConstant(materialObj); break; } _materials.Add(materialObj); id = new MaterialId { Id = _root.Materials.Count, Root = _root }; _root.Materials.Add(material); return(id); }
protected virtual void CreateUnityMaterial(GLTF.Schema.Material def, int materialIndex) { Extension specularGlossinessExtension = null; bool isSpecularPBR = def.Extensions != null && def.Extensions.TryGetValue("KHR_materials_pbrSpecularGlossiness", out specularGlossinessExtension); Shader shader = isSpecularPBR ? Shader.Find("Standard (Specular setup)") : Shader.Find("Standard"); var material = new UnityEngine.Material(shader); material.hideFlags = HideFlags.DontUnloadUnusedAsset; // Avoid material to be deleted while being built material.name = def.Name; //Transparency if (def.AlphaMode == AlphaMode.MASK) { GLTFUtils.SetupMaterialWithBlendMode(material, GLTFUtils.BlendMode.Cutout); material.SetFloat("_Mode", 1); material.SetFloat("_Cutoff", (float)def.AlphaCutoff); } else if (def.AlphaMode == AlphaMode.BLEND) { GLTFUtils.SetupMaterialWithBlendMode(material, GLTFUtils.BlendMode.Fade); material.SetFloat("_Mode", 3); } if (def.NormalTexture != null) { var texture = def.NormalTexture.Index.Id; Texture2D normalTexture = getTexture(texture) as Texture2D; //Automatically set it to normal map TextureImporter im = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(normalTexture)) as TextureImporter; im.textureType = TextureImporterType.NormalMap; im.SaveAndReimport(); material.SetTexture("_BumpMap", getTexture(texture)); material.SetFloat("_BumpScale", (float)def.NormalTexture.Scale); } if (def.EmissiveTexture != null) { material.EnableKeyword("EMISSION_MAP_ON"); var texture = def.EmissiveTexture.Index.Id; material.SetTexture("_EmissionMap", getTexture(texture)); material.SetInt("_EmissionUV", def.EmissiveTexture.TexCoord); } // PBR channels if (specularGlossinessExtension != null) { KHR_materials_pbrSpecularGlossinessExtension pbr = (KHR_materials_pbrSpecularGlossinessExtension)specularGlossinessExtension; material.SetColor("_Color", pbr.DiffuseFactor.ToUnityColor().gamma); if (pbr.DiffuseTexture != null) { var texture = pbr.DiffuseTexture.Index.Id; material.SetTexture("_MainTex", getTexture(texture)); } if (pbr.SpecularGlossinessTexture != null) { var texture = pbr.SpecularGlossinessTexture.Index.Id; material.SetTexture("_SpecGlossMap", getTexture(texture)); material.SetFloat("_GlossMapScale", (float)pbr.GlossinessFactor); material.SetFloat("_Glossiness", (float)pbr.GlossinessFactor); } else { material.SetFloat("_Glossiness", (float)pbr.GlossinessFactor); } Vector3 specularVec3 = pbr.SpecularFactor.ToUnityVector3(); material.SetColor("_SpecColor", new Color(specularVec3.x, specularVec3.y, specularVec3.z, 1.0f)); if (def.OcclusionTexture != null) { var texture = def.OcclusionTexture.Index.Id; material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength); material.SetTexture("_OcclusionMap", getTexture(texture)); } GLTFUtils.SetMaterialKeywords(material, GLTFUtils.WorkflowMode.Specular); } else if (def.PbrMetallicRoughness != null) { var pbr = def.PbrMetallicRoughness; material.SetColor("_Color", pbr.BaseColorFactor.ToUnityColor().gamma); if (pbr.BaseColorTexture != null) { var texture = pbr.BaseColorTexture.Index.Id; material.SetTexture("_MainTex", getTexture(texture)); } material.SetFloat("_Metallic", (float)pbr.MetallicFactor); material.SetFloat("_Glossiness", 1.0f - (float)pbr.RoughnessFactor); if (pbr.MetallicRoughnessTexture != null) { var texture = pbr.MetallicRoughnessTexture.Index.Id; UnityEngine.Texture2D inputTexture = getTexture(texture) as Texture2D; List <Texture2D> splitTextures = splitMetalRoughTexture(inputTexture, def.OcclusionTexture != null, (float)pbr.MetallicFactor, (float)pbr.RoughnessFactor); material.SetTexture("_MetallicGlossMap", splitTextures[0]); if (def.OcclusionTexture != null) { material.SetFloat("_OcclusionStrength", (float)def.OcclusionTexture.Strength); material.SetTexture("_OcclusionMap", splitTextures[1]); } } GLTFUtils.SetMaterialKeywords(material, GLTFUtils.WorkflowMode.Metallic); } material.SetColor("_EmissionColor", def.EmissiveFactor.ToUnityColor().gamma); material = _assetManager.saveMaterial(material, materialIndex); _assetManager._parsedMaterials.Add(material); material.hideFlags = HideFlags.None; }
public static GLTF.Schema.Material ConvertSeinCustomMaterial(UnityEngine.Material mat, ExporterEntry entry) { if (_tempGO == null) { _tempGO = new GameObject(); } var customMaterial = _tempGO.AddComponent <SeinCustomMaterial>(); var className = mat.shader.name.Replace("Sein/", ""); if (!className.Contains("Material")) { className += "Material"; } var shaderPath = AssetDatabase.GetAssetPath(mat.shader); if (shaderPath != null) { var matScriptPath = Path.Combine( shaderPath.Replace(Path.GetFileName(shaderPath), ""), className + ".js" ); if (File.Exists(matScriptPath)) { if (entry.root.Extensions == null) { entry.root.Extensions = new Dictionary <string, Extension>(); } customMaterial.matScriptPath = matScriptPath; } } customMaterial.className = className; customMaterial.renderOrder = mat.renderQueue; customMaterial.unityMaterialName = mat.name; var floatArray = new List <SeinMaterialUniformFloat>(); var vector4Array = new List <SeinMaterialUniformFloatVec4>(); var colorArray = new List <SeinMaterialUniformColor>(); var textureArray = new List <SeinMaterialUniformTexture>(); for (int i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i += 1) { var propType = ShaderUtil.GetPropertyType(mat.shader, i); var propName = ShaderUtil.GetPropertyName(mat.shader, i); if (propName == "cloneForInst") { customMaterial.cloneForInst = mat.GetInt("cloneForInst") != 0; continue; } if (ShaderUtil.IsShaderPropertyHidden(mat.shader, i)) { continue; } var n = propName; switch (propType) { case ShaderUtil.ShaderPropertyType.Float: case ShaderUtil.ShaderPropertyType.Range: floatArray.Add(new SeinMaterialUniformFloat { name = propName, value = mat.GetFloat(n) }); break; case ShaderUtil.ShaderPropertyType.Color: colorArray.Add(new SeinMaterialUniformColor { name = propName, value = mat.GetColor(n) }); break; case ShaderUtil.ShaderPropertyType.Vector: vector4Array.Add(new SeinMaterialUniformFloatVec4 { name = propName, value = mat.GetVector(n) }); break; case ShaderUtil.ShaderPropertyType.TexEnv: if (mat.GetTexture(n) != null) { textureArray.Add(new SeinMaterialUniformTexture { name = propName, value = (Texture2D)mat.GetTexture(n) }); } break; } customMaterial.uniformsFloat = floatArray.ToArray(); customMaterial.uniformsFloatVec4 = vector4Array.ToArray(); customMaterial.uniformsColor = colorArray.ToArray(); customMaterial.uniformsTexture = textureArray.ToArray(); } var tempM = new GLTF.Schema.Material(); customMaterial.transparent = ProcessTransparency(mat, tempM); var m = ConvertMaterial(customMaterial, entry); return(m); }
private static GLTF.Schema.Material ConvertSeinPBRMaterial(UnityEngine.Material mat, ExporterEntry entry) { var material = new GLTF.Schema.Material(); material.Name = mat.name; bool isMetal = mat.GetInt("workflow") == 0; bool isUnlit = mat.GetInt("unlit") == 1; if (!isMetal) { // special entry.AddExtension("KHR_materials_pbrSpecularGlossiness"); material.Extensions = new Dictionary <string, Extension>(); } else { material.PbrMetallicRoughness = new PbrMetallicRoughness(); } bool hasTransparency = ProcessTransparency(mat, material); if (isUnlit || isMetal) { if (mat.GetTexture("_baseColorMap") != null) { var id = entry.SaveTexture((Texture2D)mat.GetTexture("_baseColorMap"), hasTransparency); material.PbrMetallicRoughness.BaseColorTexture = new TextureInfo { Index = id }; } if (mat.GetColor("_baseColor") != null) { Color c = mat.GetColor("_baseColor"); material.PbrMetallicRoughness.BaseColorFactor = Utils.ExportColor(c); } } if (isUnlit) { if (material.Extensions == null) { material.Extensions = new Dictionary <string, Extension>(); } ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(KHR_materials_unlitExtensionFactory)), entry, material.Extensions); } else if (isMetal) { bool hasPBRMap = mat.GetTexture("_metallicMap") != null; if (hasPBRMap) { Texture2D metallicTexture = (Texture2D)mat.GetTexture("_metallicMap"); Texture2D roughnessTexture = (Texture2D)mat.GetTexture("_roughnessMap"); Texture2D occlusion = (Texture2D)mat.GetTexture("_occlusionMap"); var metalRoughTextureAo = CreateOcclusionMetallicRoughnessTexture( ref metallicTexture, ref roughnessTexture, ref occlusion ); var assetPath = AssetDatabase.GetAssetPath(metallicTexture); var ext = Path.GetExtension(assetPath); var id = entry.SaveTexture(metalRoughTextureAo, hasTransparency, assetPath.Replace(ext, "-orm") + ext); material.PbrMetallicRoughness.MetallicRoughnessTexture = new TextureInfo { Index = id }; if (occlusion != null) { material.OcclusionTexture = new OcclusionTextureInfo { Index = id, Strength = mat.GetFloat("_occlusionStrength") }; } } material.PbrMetallicRoughness.MetallicFactor = mat.GetFloat("_metallic"); material.PbrMetallicRoughness.RoughnessFactor = mat.GetFloat("_roughness"); } else { TextureInfo specGlossMap = null; TextureInfo diffuseMap = null; var diffuseColor = new GLTF.Math.Color(); if (mat.GetTexture("_baseColorMap") != null) { var id = entry.SaveTexture((Texture2D)mat.GetTexture("_baseColorMap"), hasTransparency); diffuseMap = new TextureInfo { Index = id }; } if (mat.GetColor("_baseColor") != null) { Color c = mat.GetColor("_baseColor"); diffuseColor = Utils.ExportColor(c); } bool hasPBRMap = mat.GetTexture("_specularGlossinessMap") != null; if (hasPBRMap) { specGlossMap = new TextureInfo { Index = entry.SaveTexture((Texture2D)mat.GetTexture("_specularGlossinessMap"), true) }; } var specularFactor = hasPBRMap ? Color.white : (Color)Utils.ExportColorVec4(mat.GetColor("_specular")); var glossinessFactor = hasPBRMap ? 1.0f : mat.GetFloat("_glossiness"); if (material.Extensions == null) { material.Extensions = new Dictionary <string, Extension>(); } material.Extensions.Add( "KHR_materials_pbrSpecularGlossiness", new KHR_materials_pbrSpecularGlossinessExtension( diffuseColor, diffuseMap, new GLTF.Math.Vector3(specularFactor.r, specularFactor.g, specularFactor.b), glossinessFactor, specGlossMap ) ); Texture2D occlusion = (Texture2D)mat.GetTexture("_occlusionMap"); if (occlusion != null) { material.OcclusionTexture = new OcclusionTextureInfo { Index = entry.SaveTexture((Texture2D)mat.GetTexture("_occlusionMap"), false), Strength = mat.GetFloat("_occlusionStrength") }; } } if (mat.GetTexture("_normalMap") != null) { material.NormalTexture = new NormalTextureInfo { Index = entry.SaveTexture((Texture2D)mat.GetTexture("_normalMap"), false), }; } if (mat.GetTexture("_emissionMap") != null) { material.EmissiveTexture = new TextureInfo { Index = entry.SaveTexture((Texture2D)mat.GetTexture("_emissionMap"), false), }; } var emissive = mat.GetColor("_emission"); if (!emissive.Equals(new Color(0, 0, 0))) { material.EmissiveFactor = Utils.ExportColor(emissive); } if (mat.GetInt("envReflection") != (int)SeinPBRShaderGUI.EnvReflection.Off || (ExporterSettings.Lighting.ambient && (RenderSettings.ambientMode == UnityEngine.Rendering.AmbientMode.Skybox || RenderSettings.ambientMode == UnityEngine.Rendering.AmbientMode.Trilight))) { if (material.Extensions == null) { material.Extensions = new Dictionary <string, Extension>(); } ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(Sein_imageBasedLightingExtensionFactory)), entry, material.Extensions, mat); } return(material); }
public override void Serialize(ExporterEntry entry, Dictionary <string, Extension> extensions, UnityEngine.Object component = null, object options = null) { if (entry.root.Extensions == null) { entry.root.Extensions = new Dictionary <string, Extension>(); } Sein_spriteExtension globalExtension; if (!entry.root.Extensions.ContainsKey(ExtensionName)) { globalExtension = new Sein_spriteExtension { isGlobal = true }; entry.root.Extensions.Add(ExtensionName, globalExtension); } else { globalExtension = (Sein_spriteExtension)entry.root.Extensions[ExtensionName]; } var sprite = component as SeinSprite; var extension = new Sein_spriteExtension { isGlobal = false }; var sp = sprite; var customMaterial = sprite.GetComponent <SeinCustomMaterial>(); var cacheId = $"w{sp.width}-h{sp.height}-at{sp.atlas.GetInstanceID()}-fn{sp.frameName}-bb{sp.isBillboard}-ft{sp.frustumTest}"; if (customMaterial != null) { cacheId += $"mat{customMaterial.GetInstanceID()}"; } else { cacheId += $"mat{sprite.material.GetInstanceID()}"; } if (!_CAHCE.ContainsKey(entry)) { _CAHCE.Add(entry, new Dictionary <string, int>()); } if (_CAHCE[entry].ContainsKey(cacheId)) { extension.index = _CAHCE[entry][cacheId]; AddExtension(extensions, extension); return; } // process atlases at first ExtensionManager.Serialize(ExtensionManager.GetExtensionName(typeof(Sein_atlasExtensionFactory)), entry, entry.root.Extensions, sprite.atlas); var s = new Sein_spriteExtension.Sprite(); s.width = sprite.width; s.height = sprite.height; s.isBillboard = sprite.isBillboard; s.frustumTest = sprite.frustumTest; s.atlasId = Sein_atlasExtensionFactory.GetAtlasIndex(entry, sprite.atlas); s.frameName = sprite.frameName; GLTF.Schema.Material gltfMat = null; if (customMaterial != null) { gltfMat = ExporterUtils.ConvertMaterial(customMaterial, entry); } else if (sprite.material.shader.name != "Sein/Sprite" && sprite.material.shader.name.Contains("Sein/")) { gltfMat = ExporterUtils.ConvertSeinCustomMaterial(sprite.material, entry); } if (gltfMat != null) { var root = entry.root; if (root.Materials == null) { root.Materials = new List <GLTF.Schema.Material>(); } root.Materials.Add(gltfMat); var id = new MaterialId { Id = root.Materials.Count - 1, Root = root }; s.materialId = id; } globalExtension.sprites.Add(s); var index = globalExtension.sprites.Count - 1; _CAHCE[entry].Add(cacheId, index); extension.index = index; AddExtension(extensions, extension); }
public override void Import(EditorImporter importer, UnityEngine.Material material, GLTF.Schema.Material gltfMat, Extension extension) { var mat = (Sein_customMaterialExtension)extension; var className = "Sein/" + mat.className; var shader = Shader.Find(className); var alphaMode = gltfMat.AlphaMode; if (alphaMode == AlphaMode.BLEND) { mat.transparent = true; } else if (alphaMode == AlphaMode.MASK) { mat.transparent = true; } if (shader == null) { shader = Shader.Find(className.Replace("Material", "")); } if (shader == null) { mat.material = material; mat.isComponent = true; return; } material.shader = shader; if (material.HasProperty("cloneForInst")) { material.SetInt("cloneForInst", 1); } material.SetInt("_Mode", (int)alphaMode); material.SetFloat("_Cutoff", (float)gltfMat.AlphaCutoff); WriteUiforms(importer, material, mat.uniformsTexture); WriteUiforms(importer, material, mat.uniformsCubeTexture); WriteUiforms(importer, material, mat.uniformsFloat); WriteUiforms(importer, material, mat.uniformsFloatVec2); WriteUiforms(importer, material, mat.uniformsFloatVec3); WriteUiforms(importer, material, mat.uniformsFloatVec4); WriteUiforms(importer, material, mat.uniformsColor); WriteUiforms(importer, material, mat.uniformsFloatMat2); WriteUiforms(importer, material, mat.uniformsFloatMat3); WriteUiforms(importer, material, mat.uniformsFloatMat4); WriteUiforms(importer, material, mat.uniformsInt); WriteUiforms(importer, material, mat.uniformsIntVec2); WriteUiforms(importer, material, mat.uniformsIntVec3); WriteUiforms(importer, material, mat.uniformsIntVec4); }
protected virtual MaterialCacheData CreateMaterial(GLTF.Schema.Material def, int materialIndex) { MaterialCacheData materialWrapper = null; if (materialIndex < 0 || _assetCache.MaterialCache[materialIndex] == null) { IUniformMap mapper; const string specGlossExtName = KHR_materials_pbrSpecularGlossinessExtensionFactory.EXTENSION_NAME; if (_root.ExtensionsUsed != null && _root.ExtensionsUsed.Contains(specGlossExtName) && def.Extensions != null && def.Extensions.ContainsKey(specGlossExtName)) { mapper = new SpecGlossMap(MaximumLod); } else { mapper = new MetalRoughMap(MaximumLod); } mapper.AlphaMode = def.AlphaMode; mapper.DoubleSided = def.DoubleSided; var mrMapper = mapper as IMetalRoughUniformMap; if (def.PbrMetallicRoughness != null && mrMapper != null) { var pbr = def.PbrMetallicRoughness; mrMapper.BaseColorFactor = pbr.BaseColorFactor.ToUnityColorRaw(); if (pbr.BaseColorTexture != null) { var textureDef = pbr.BaseColorTexture.Index.Value; mrMapper.BaseColorTexture = CreateTexture(textureDef); mrMapper.BaseColorTexCoord = pbr.BaseColorTexture.TexCoord; //ApplyTextureTransform(pbr.BaseColorTexture, material, "_MainTex"); } mrMapper.MetallicFactor = pbr.MetallicFactor; if (pbr.MetallicRoughnessTexture != null) { var texture = pbr.MetallicRoughnessTexture.Index.Value; mrMapper.MetallicRoughnessTexture = CreateTexture(texture); mrMapper.MetallicRoughnessTexCoord = pbr.MetallicRoughnessTexture.TexCoord; //ApplyTextureTransform(pbr.MetallicRoughnessTexture, material, "_MetallicRoughnessMap"); } mrMapper.RoughnessFactor = pbr.RoughnessFactor; } var sgMapper = mapper as ISpecGlossUniformMap; if (sgMapper != null) { var specGloss = def.Extensions[specGlossExtName] as KHR_materials_pbrSpecularGlossinessExtension; sgMapper.DiffuseFactor = specGloss.DiffuseFactor.ToUnityColorRaw(); if (specGloss.DiffuseTexture != null) { var texture = specGloss.DiffuseTexture.Index.Value; sgMapper.DiffuseTexture = CreateTexture(texture); sgMapper.DiffuseTexCoord = specGloss.DiffuseTexture.TexCoord; //ApplyTextureTransform(specGloss.DiffuseTexture, material, "_MainTex"); } sgMapper.SpecularFactor = specGloss.SpecularFactor.ToUnityVector3Raw(); sgMapper.GlossinessFactor = specGloss.GlossinessFactor; if (specGloss.SpecularGlossinessTexture != null) { var texture = specGloss.SpecularGlossinessTexture.Index.Value; sgMapper.SpecularGlossinessTexture = CreateTexture(texture); //ApplyTextureTransform(specGloss.SpecularGlossinessTexture, material, "_SpecGlossMap"); } } if (def.NormalTexture != null) { var texture = def.NormalTexture.Index.Value; mapper.NormalTexture = CreateTexture(texture); mapper.NormalTexCoord = def.NormalTexture.TexCoord; mapper.NormalTexScale = def.NormalTexture.Scale; //ApplyTextureTransform(def.NormalTexture, material, "_BumpMap"); } if (def.OcclusionTexture != null) { mapper.OcclusionTexStrength = def.OcclusionTexture.Strength; var texture = def.OcclusionTexture.Index; mapper.OcclusionTexture = CreateTexture(texture.Value); //ApplyTextureTransform(def.OcclusionTexture, material, "_OcclusionMap"); } if (def.EmissiveTexture != null) { var texture = def.EmissiveTexture.Index.Value; mapper.EmissiveTexture = CreateTexture(texture); mapper.EmissiveTexCoord = def.EmissiveTexture.TexCoord; //ApplyTextureTransform(def.EmissiveTexture, material, "_EmissionMap"); } mapper.EmissiveFactor = def.EmissiveFactor.ToUnityColorRaw(); var vertColorMapper = mapper.Clone(); vertColorMapper.VertexColorsEnabled = true; materialWrapper = new MaterialCacheData { UnityMaterial = mapper.Material, UnityMaterialWithVertexColor = vertColorMapper.Material, GLTFMaterial = def }; if (materialIndex > 0) { _assetCache.MaterialCache[materialIndex] = materialWrapper; } } return(materialIndex > 0 ? _assetCache.MaterialCache[materialIndex] : materialWrapper); }
private static GLTF.Schema.Material ConvertSeinCustomMaterial(UnityEngine.Material mat, ExporterEntry entry) { if (_tempGO == null) { _tempGO = new GameObject(); } var customMaterial = _tempGO.AddComponent <SeinCustomMaterial>(); var className = mat.shader.name.Replace("Sein/", ""); if (!className.Contains("Material")) { className += "Material"; } customMaterial.className = className; customMaterial.renderOrder = mat.renderQueue; var floatArray = new List <SeinMaterialUniformFloat>(); var vector4Array = new List <SeinMaterialUniformFloatVec4>(); var textureArray = new List <SeinMaterialUniformTexture>(); for (int i = 0; i < ShaderUtil.GetPropertyCount(mat.shader); i += 1) { var propType = ShaderUtil.GetPropertyType(mat.shader, i); var propName = ShaderUtil.GetPropertyName(mat.shader, i); if (propName == "cloneForInst") { customMaterial.cloneForInst = mat.GetInt("cloneForInst") != 0; continue; } if (ShaderUtil.IsShaderPropertyHidden(mat.shader, i)) { continue; } var n = propName; //if (propName.Substring(0, 1) == "_") //{ // propName = propName.Substring(1); //} switch (propType) { case ShaderUtil.ShaderPropertyType.Float: case ShaderUtil.ShaderPropertyType.Range: floatArray.Add(new SeinMaterialUniformFloat { name = propName, value = mat.GetFloat(n) }); break; case ShaderUtil.ShaderPropertyType.Color: vector4Array.Add(new SeinMaterialUniformFloatVec4 { name = propName, value = mat.GetColor(n) }); break; case ShaderUtil.ShaderPropertyType.Vector: vector4Array.Add(new SeinMaterialUniformFloatVec4 { name = propName, value = mat.GetVector(n) }); break; case ShaderUtil.ShaderPropertyType.TexEnv: if (mat.GetTexture(n) != null) { textureArray.Add(new SeinMaterialUniformTexture { name = propName, value = (Texture2D)mat.GetTexture(n) }); } break; } customMaterial.uniformsFloat = floatArray.ToArray(); customMaterial.uniformsFloatVec4 = vector4Array.ToArray(); customMaterial.uniformsTexture = textureArray.ToArray(); } var tempM = new GLTF.Schema.Material(); customMaterial.transparent = ProcessTransparency(mat, tempM); var m = ConvertMaterial(customMaterial, entry); return(m); }
public virtual void Import(EditorImporter importer, UnityEngine.Material material, GLTF.Schema.Material gltfMat, Extension extension) { }
public override void Import(EditorImporter importer, UnityEngine.Material material, GLTF.Schema.Material gltfMat, Extension extension) { if (material.HasProperty("unlit")) { material.SetInt("unlit", 1); } }
public static void Import(string extensionName, EditorImporter importer, UnityEngine.Material material, GLTF.Schema.Material gltfMat, Extension extension) { if (Name2Extensions.ContainsKey(extensionName)) { Name2Extensions[extensionName].Import(importer, material, gltfMat, extension); } }