赞
踩
思路:在3dsmax中先记录每一个材质球对应的材质名与其所使用的贴图名,写出文件,然后uniyt中读取这个材质名与贴图名,在unity中批量将材质球恢复贴图。
struct HMtl(mtlDiffuse,mtlReflection,mtlRefraction,mtlBump,mtlOpacity) --将list里的内容写入in_test fn formatList2 list in_text= ( file = CreatEFile in_text if list.count !=0 then( for listInner in list do( if listInner.count != 0 then( format "usemat %\n" (listInner[1]) to:file format "\tdifmap:%\n" (listInner[2]) to:file ) )--for i结束 )--if 结束 ) ------------------------------------------------------------------------------------------------------------------------ --通道过滤 --通道迭代找出贴图路径 --MapChannel 通道名 --MapList 接受贴图路径的数组 fn Get_MapChannel MapChannel = ( --MapList = #() --清空数组 --如果这个通道不为空则进行下面的判断 if classof MapChannel != UndefinedClass do ( filepath =case classof MapChannel of ( --1 Bitmaptexture: if MapChannel.filename!=undefined then MapChannel.filename --2 falloff: Get_MapChannel MapChannel.map1 --3 Color_Correction: Get_MapChannel MapChannel.map --4 Mask: Get_MapChannel MapChannel.map Mix: Get_MapChannel MapChannel.mask --20190423添加 CompositeTexturemap: Get_MapChannel MapChannel.mapList[1] --20190424 合成材质通道 VRayNormalMap:Get_MapChannel MapChannel.normal_map --20192424VrayNormalMap VRayHDRI: if MapChannel.HDRIMapName!=undefined and MapChannel.HDRIMapName!= "" then MapChannel.HDRIMapName --20190605对hdr贴图的处理 ) ) ) ------------------------------------------------------------------------------------------------------------------------ --获取Mtl的贴图 fn Get_Map Mtl = ( if ((superclassof Mtl)!=material) then return Mtl matClass= classof Mtl --获取选择物体的材质类型 VR:VRayMtl; 标准:Standardmaterial; 多维子材质:Multimaterial ;混全材质:Blend case matClass of ( --0 CoronaMtl:( ---漫反射通道 mtlDiffuse =if Mtl.texmapDiffuse!=undefined then Get_MapChannel Mtl.texmapDiffuse --print mtlDiffuseMap.count ---凹凸通道 mtlBump = if Mtl.texmapBump!=undefined then Get_MapChannel Mtl.texmapBump --print mtlBump.count ---反射通道 mtlReflection = if Mtl.texmapReflect!=undefined then Get_MapChannel Mtl.texmapReflect --print mtlReflectionMap.count ---折射通道 mtlRefraction = if Mtl.texmapRefract!=undefined then Get_MapChannel Mtl.texmapRefract --print mtlRefractionMap.count ---镂空通道 mtlOpacity = if Mtl.texmapOpacity!=undefined then Get_MapChannel Mtl.texmapOpacity --print mtlOpacity.count ) --1 VrayMtl:( ---漫反射通道 mtlDiffuse =if Mtl.texmap_diffuse!=undefined then Get_MapChannel Mtl.texmap_diffuse --print mtlDiffuseMap.count ---凹凸通道 mtlBump = if Mtl.texmap_bump!=undefined then Get_MapChannel Mtl.texmap_bump --print mtlBump.count ---反射通道 mtlReflection = if Mtl.texmap_reflection!=undefined then Get_MapChannel Mtl.texmap_reflection --print mtlReflectionMap.count ---折射通道 mtlRefraction = if Mtl.texmap_refraction!=undefined then Get_MapChannel Mtl.texmap_refraction --print mtlRefractionMap.count ---镂空通道 mtlOpacity = if Mtl.texmap_opacity!=undefined then Get_MapChannel Mtl.texmap_opacity --print mtlOpacity.count ) ------------------------------------------------- Standardmaterial:(--print "Standardmaterial:" ---漫反射通道 mtlDiffuse =if Mtl.diffuseMap!=undefined then Get_MapChannel Mtl.diffuseMap --print mtlDiffuseMap.count ---凹凸通道 mtlBump = if Mtl.bumpMap!=undefined then Get_MapChannel Mtl.bumpMap --print mtlBump.count ---反射通道 mtlReflection = if Mtl.reflectionMap!=undefined then Get_MapChannel Mtl.reflectionMap --print mtlReflectionMap.count ---折射通道 mtlRefraction = if Mtl.refractionMap!=undefined then Get_MapChannel Mtl.refractionMap --print mtlRefractionMap.count ---镂空通道 mtlOpacity = if Mtl.opacityMap!=undefined then Get_MapChannel Mtl.opacityMap ) ------------------------------------------------- CoronaLightMtl:(--print "CoronaLightMtl:" mtlDiffuse =if Mtl.texmap!=undefined then Get_MapChannel Mtl.texmap ) VRayLightMtl:(--print --"VRayLightMtl:" mtlDiffuse =if Mtl.texmap!=undefined then Get_MapChannel Mtl.texmap ) ------------------------------------------------- Multimaterial:(messagebox "这是一个多维子材质,请处理后再给材质!") ------------------------------------------------- Blend:(messagebox "这是一个混合材质Blend,请处理后再给材质!") --VRayMtlWrapper:Get_Map Mtl.baseMtl --VRayBlendMtl:Get_Map Mtl.baseMtl --VRay2SidedMtl:Get_Map Mtl.frontMtl default: return undefined ) --print "原始材质的贴图有:" --print("mtlDiffuse:"+mtlDiffuse as string ) --print("mtlReflection:"+mtlReflection as string) --print("mtlRefraction:"+mtlRefraction as string) --print("mtlBump:"+mtlBump as string) --print("mtlOpacity:"+mtlOpacity as string) HMtl2 = HMtl mtlDiffuse:mtlDiffuse mtlReflection:mtlReflection mtlRefraction:mtlRefraction mtlBump:mtlBump mtlOpacity:mtlOpacity ) fn getMatAndDifMap = ( mats = scenematerials matAndMapList = #() --mat and diffusemap for mat in mats do( matmaps = Get_Map mat diffuseMapName = matmaps.mtlDiffuse if diffuseMapName != undefined then ( diffuseMapName2 = (filenameFromPath diffuseMapName) append matAndMapList #(mat.name,diffuseMapName2) --print(mat.name) --print (diffuseMapName2) --print("----------------") ) ) matAndMapList )
调用说明:
f = "E:\a.p"
matmaplist = getMatAndDifMap () --返回的是一个二维数组
formatList2 matmaplist f --写出到文件f中
using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEngine; using System.Text.RegularExpressions; //存储材质相关信息的结构体 public struct MatMap{ public string MatName; public string DiffuseMap; public MatMap( string matname, string diffusemap ) { this.MatName = matname; this.DiffuseMap = diffusemap; } public string matname{ get{return this.MatName;} set{this.MatName = value;} } public string diffusemap{ get{return this.DiffuseMap;} set{this.DiffuseMap = value;} } } public class HyqMenumTools : MonoBehaviour { //MARSK 材质贴图对应关系数据库 public static string MatTextureFilePath = "Assets/Hyq/Models/a.p"; public static string modelName = ""; //材质球所在的资源文件夹路径 public static string matDirPath = "Assets/Hyq/Models/NavTest1/Materials"; // MARSK _w 单一的快捷键 w // MARSK #w shift + w // MARSK %w CTRL + w // MARSK &W alt + w [MenuItem("HYQTOOLS/MenumTools/Test _n")] public static void Test(){ Debug.Log("单一的快捷键 ::>>> n"); List<MatMap> matMaps = GetMatMaps(MatTextureFilePath); foreach(MatMap m in matMaps){ SetMatMap(m.matname,m.diffusemap); } Debug.Log(matMaps.Count); } //shift + i 导入材质贴图 [MenuItem("HYQTOOLS/MenumTools/Test2# #i")] public static void Test2(){ Debug.Log("#i ::>>>>> shift + i"); } //ctrl + i [MenuItem("HYQTOOLS/MenumTools/Test#% #%i")] public static void Test3(){ Debug.Log("%i ::>>>>> ctrl+shift+ i"); } //alt + i [MenuItem("HYQTOOLS/MenumTools/Test3& &i")] public static void Test4(){ string fullPath = "Assets/Hyq/Models/NavTest1/Materials"; //获取指定路径下面的所有资源文件 if (Directory.Exists(fullPath)){ DirectoryInfo direction = new DirectoryInfo(fullPath); FileInfo[] files = direction.GetFiles("*",SearchOption.AllDirectories); Debug.Log(files.Length); for(int i=0;i<files.Length;i++){ if (files[i].Name.EndsWith(".meta")){ continue; } Debug.Log( "Name:" + files[i].Name ); //Debug.Log( "FullName:" + files[i].FullName ); //Debug.Log( "DirectoryName:" + files[i].DirectoryName ); } } } public static List<MatMap> GetMatMaps(string path){ List<MatMap> matMaps = new List<MatMap>(); string lines = File.ReadAllText(path); string[] linesList = lines.Split('\n'); int len = linesList.Length; for(int i = 0;i < len-1;i+=2){ string matName = linesList[i].Replace("usemat ",modelName).Trim(); string diffuseMapName = linesList[i+1].TrimStart().Replace("difmap:","").Trim(); matMaps.Add(new MatMap(matName,diffuseMapName)); } return matMaps; } public static void SetMatMap(string matName,string mapName){ string matpath = matDirPath + "/" + matName +".mat"; Material mat = AssetDatabase.LoadAssetAtPath<Material>(matpath); Texture map = AssetDatabase.LoadAssetAtPath<Texture>("Assets/Hyq/Models/NavTest1/textures/"+mapName); if (mat != null && map != null){ mat.SetTexture("_BaseColorMap",map); } AssetDatabase.Refresh(); } }
使用说明:脚本保存在Editor文件夹中,快捷键n执行命令.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。