当前位置:   article > 正文

UnityHDR渲染管线中使用3dsmax导入的模型丢失贴图恢复_unityhdrp管线丢失maps

unityhdrp管线丢失maps

思路:在3dsmax中先记录每一个材质球对应的材质名与其所使用的贴图名,写出文件,然后uniyt中读取这个材质名与贴图名,在unity中批量将材质球恢复贴图。

3dsmax导出材质名与贴图名部分

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	
	
	)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161

调用说明:

f = "E:\a.p"
matmaplist = getMatAndDifMap ()	--返回的是一个二维数组
formatList2  matmaplist f 		--写出到文件f中

  • 1
  • 2
  • 3
  • 4

Unity 代码部分

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();  
    }  
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111

使用说明:脚本保存在Editor文件夹中,快捷键n执行命令.

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/101763?site
推荐阅读
相关标签
  

闽ICP备14008679号