private GameObject CreatePrefabFromModel(string tgt_path, GameObject modelAsset) { Tools.EnsureDirectoryExists(tgt_path); string destinationPath = GetSafeFilename(tgt_path + "/" + modelAsset.name + ".prefab"); var pfbsrc = GetPrefabAsset(tgt_path, modelAsset.name); if (pfbsrc != null) { return(pfbsrc); } m_created_prefabs.Add(destinationPath); var pfb = PrefabUtility.SaveAsPrefabAsset(modelAsset, destinationPath); GameObject.DestroyImmediate(modelAsset); return(null); }
public override void OnImportAsset(AssetImportContext ctx) { Tools.EnsureDirectoryExists(PREFAB_DESTINATION_DIRECTORY); int idx = ctx.assetPath.LastIndexOf("geobin"); if (-1 == idx) { ctx.LogImportWarning("File is not located under 'geobin', skipping"); return; } if (null == s_runtime_data) //!m_RuntimeReady { s_runtime_data = new RuntimeData(); string basepath = ctx.assetPath.Substring(0, idx - 1); if (!basepath.EndsWith("/") && basepath[basepath.Length - 1] != Path.DirectorySeparatorChar) { basepath += Path.DirectorySeparatorChar; } s_runtime_data.prepare(basepath); } if (m_previous_asset == null || m_previous == null) { sg = SceneGraph.loadWholeMap(ctx.assetPath); } else { Debug.Log("Reusing previously loaded graph"); sg = m_previous; } m_previous = sg; m_previous_asset = ctx.assetPath; var top_nodes = sg.calculateUsages(); if (top_nodes.Count != 0) { bool some_prefabs_were_missing = false; foreach (var pair in top_nodes) { GameObject top_level = convertFromRoot(pair.Value); if (top_level == null) { some_prefabs_were_missing = true; } else { GameObject.DestroyImmediate(top_level); } } foreach (KeyValuePair <SceneNode, GameObject> pair in m_imported_prefabs) { if (!top_nodes.ContainsValue(pair.Key)) { GameObject.DestroyImmediate(pair.Value); // remove all non-top-level prefabs from scene. } } if (some_prefabs_were_missing == false) { createTopNodesInstances(top_nodes); } else { string saved_ones = String.Join("\n", m_created_prefabs); ctx.LogImportWarning(String.Format( "The following prefab assets were missing and were created, please retry: {0}", saved_ones)); } } }
private static void convertMaterials(MeshRenderer ren, Model mdl, GameObject tgt) { ModelModifiers model_trick = mdl.trck_node; GeometryModifiersData geom_trick = mdl.src_mod; RuntimeData rd = RuntimeData.get(); MaterialDescriptor descriptor; string model_base_name = mdl.name.Split(new[] { "__" }, StringSplitOptions.None)[0]; string mesh_path = mdl.geoset.full_geo_path; int obj_lib_idx = mesh_path.IndexOf("object_library"); if (obj_lib_idx != -1) { mesh_path = "Assets/Materials/" + mesh_path.Substring(obj_lib_idx); } string material_base_path = mesh_path + "/" + Path.GetFileNameWithoutExtension(mdl.name); if (mdl.name == "Crate1_med_Wood__TintCrates") { Debug.LogFormat("Crate {0}", mdl.BlendMode.ToString()); } if (model_trick != null && model_trick._TrickFlags.HasFlag(TrickFlags.ColorOnly)) { // result = result.Clone(result.GetName()+"Colored"); // result.SetShaderParameter("MatDiffColor",Vector4(1.0, 1.0, 0.2f, 1.0f)); } // Select material based on the model blend state // Modify material based on the applied model tricks Color onlyColor; Color tint1 = new Color(1, 1, 1, 1); // Shader Constant 0 Color tint2 = new Color(1, 1, 1, 1); // Shader Constant 1 float alphaRef = 0.0f; descriptor.depthWrite = true; descriptor.isAdditive = false; descriptor.targetCulling = UnityEngine.Rendering.CullMode.Back; descriptor.depthTest = CompareFunction.LessEqual; descriptor.targetCulling = UnityEngine.Rendering.CullMode.Back; string shader_to_use = ""; if (null != model_trick && model_trick._TrickFlags != 0) { var tflags = model_trick._TrickFlags; if (tflags.HasFlag(TrickFlags.Additive)) { descriptor.isAdditive = true; } if (tflags.HasFlag(TrickFlags.ColorOnly)) { onlyColor = model_trick.TintColor0; } if (tflags.HasFlag(TrickFlags.DoubleSided)) { descriptor.targetCulling = UnityEngine.Rendering.CullMode.Off; } if (tflags.HasFlag(TrickFlags.NoZTest)) { // simulate disabled Z test descriptor.depthTest = CompareFunction.Always; //depthTest = CompareFunction.Always; descriptor.depthWrite = false; } if (tflags.HasFlag(TrickFlags.NoZWrite)) { descriptor.depthWrite = false; } if (tflags.HasFlag(TrickFlags.SetColor)) { tint1 = model_trick.TintColor0; tint2 = model_trick.TintColor1; tint1.a = 1.0f; tint2.a = 1.0f; } if (tflags.HasFlag(TrickFlags.ReflectTex0) || tflags.HasFlag(TrickFlags.ReflectTex1)) { shader_to_use = "Custom/ReflectGen"; if (mdl.flags.HasFlag(ModelFlags.OBJ_CUBEMAP)) { Debug.Log("Unhandled Cubemap"); } } if (tflags.HasFlag(TrickFlags.AlphaRef)) { //qDebug() << "Unhandled alpha ref"; alphaRef = geom_trick.AlphaRef; } if (tflags.HasFlag(TrickFlags.TexBias)) { Debug.Log("Unhandled TexBias"); } } CompareFunction depthTest = CompareFunction.LessEqual; TextureWrapper whitetex = GeoSet.loadTexHeader("white"); var pixel_defines = new List <string>(); pixel_defines.Add("DIFFMAP"); pixel_defines.Add("ALPHAMASK"); switch (mdl.BlendMode) { case CoHBlendMode.MULTIPLY: pixel_defines.Add("COH_MULTIPLY"); break; case CoHBlendMode.MULTIPLY_REG: if (!descriptor.depthWrite && descriptor.isAdditive) { descriptor.targetCulling = UnityEngine.Rendering.CullMode.Off; } pixel_defines.Add("COH_MULTIPLY"); break; case CoHBlendMode.COLORBLEND_DUAL: pixel_defines.Add("COH_COLOR_BLEND_DUAL"); break; case CoHBlendMode.ADDGLOW: pixel_defines.Add("COH_ADDGLOW"); break; case CoHBlendMode.ALPHADETAIL: pixel_defines.Add("COH_ALPHADETAIL"); break; case CoHBlendMode.BUMPMAP_MULTIPLY: pixel_defines.Add("COH_MULTIPLY"); break; case CoHBlendMode.BUMPMAP_COLORBLEND_DUAL: pixel_defines.Add("COH_COLOR_BLEND_DUAL"); break; } if (mdl.flags.HasFlag(ModelFlags.OBJ_TREE)) { shader_to_use = "CoH/Vegetation"; alphaRef = 0.4f; tint1.a = 254.0f / 255.0f; } Material[] materials = new Material[mdl.texture_bind_info.Count]; int idx = 0; Tools.EnsureDirectoryExists(material_base_path); var sup = tgt.GetComponent <ModelNodeMods>(); sup.GeomTricks = geom_trick; VBOPointers vbo = mdl.vbo; Shader selected = null; if (descriptor.isAdditive) { if (shader_to_use == "Custom/ReflectGen") { selected = Shader.Find("Custom/ReflectGen"); } else { selected = Shader.Find("Unlit/Additive"); } } else { if (!String.IsNullOrEmpty(shader_to_use)) { selected = Shader.Find(shader_to_use); } else { selected = Shader.Find("CoH/CoHMult"); } } sup.TexWrappers = vbo.assigned_textures; foreach (TextureWrapper wrap in vbo.assigned_textures) { string path_material_name = String.Format("{0}/{1}_{2}.mat", material_base_path, idx, descriptor.GetHashCode()); Material available = AssetDatabase.LoadAssetAtPath <Material>(path_material_name); if (available == null) { if (wrap != null) { TextureWrapper detail = null; if (!String.IsNullOrEmpty(wrap.detailname)) { detail = GeoSet.loadTexHeader(wrap.detailname); } Material mat = new Material(selected); mat.enableInstancing = true; mat.SetColor("_Color", tint1); mat.SetColor("_Color2", tint2); mat.SetFloat("_AlphaRef", alphaRef); mat.SetInt("_CoHMod", (int)mdl.BlendMode); mat.SetTexture("_MainTex", wrap.tex); if (detail != null) { mat.SetTexture("_Detail", detail.tex); } mat.SetInt("_ZWrite", descriptor.depthWrite ? 1 : 0); mat.SetInt(ZTest, (int)descriptor.depthTest); mat.SetInt(CullMode, (int)descriptor.targetCulling); mat.SetTextureScale(MainTex, wrap.scaleUV1); mat.SetTextureScale(Detail, wrap.scaleUV0); AssetDatabase.CreateAsset(mat, path_material_name); AssetDatabase.SaveAssets(); available = AssetDatabase.LoadAssetAtPath <Material>(path_material_name); } } materials[idx++] = available; } if (mdl.flags.HasFlag(ModelFlags.OBJ_HIDE)) { ren.enabled = false; } ren.sharedMaterials = materials; }