当前位置:网站首页>Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
2022-07-07 12:06:00 【Tang Zhe】
Powerful Unity Editor Extension
Some of you might ask : The model is made , Decompress the map first in the model settings , Just unzip the material ?
Certainly. , But now the problem is that the model has no mapping , Maps are in SP Made in , The exported maps are all separate maps , It will not be directly bound to the model
All in all , It's no problem for you to paste one by one on the shader , I think it's too painful , I have to post a model … Post a der.
Editor extension can solve many manual problems .
Look at the effect :
The shader will automatically match the map
Pay attention when matching maps :
The map name must be : The model name _ Shader name _ Map type
Property introduction
- Model path : Dragging the model will automatically identify the path
- Map suffix : similar _Albedo perhaps _Occlusion, Some students like to name it like this ,AL,AO, So open it and customize
- Export shaders : The directory where the model exists will be automatically created Material Folder , Models that do not unzip materials will unzip materials to this folder
- Set the material : Automatically match the map according to the name of the model and shader
SP Export process :
file - Export map - The configuration needs to be named like this
among :
- $project It is the present. SP Project name ( In less than )
- $mesh Is the model name
- $textureSet Is the shader name
Code Introduction :
Attribute here, you can modify the default suffix
[SerializeField]
public string m_Albedo = "_Albedo", m_Metallic = "_Metallic", m_NormalMap = "_Normal", m_HeightMap = "_Height", m_OcclusionMap = "_Occlusion";
Set the name rule here :objName + “_” + mat.name + m_Albedo, Can be modified by yourself
void setMaterialShader(string path)
{
Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);
string[] allPath = GetObjPath("Texture2D");
for (int i = 0, len = allPath.Length; i < len; i++)
{
string filePath = AssetDatabase.GUIDToAssetPath(allPath[i]);
Texture2D t = AssetDatabase.LoadAssetAtPath<Texture2D>(filePath);
string objName = Path.GetFileName(modelPath);
objName = objName.Substring(0, objName.Length - 4);
if (t.name == objName + "_" + mat.name + m_Albedo)
{
mat.SetTexture(Albedo, t);
}
else if (t.name == objName + "_" + mat.name + m_HeightMap)
{
mat.SetTexture(HeightMap, t);
}
else if (t.name == objName + "_" + mat.name + m_Metallic)
{
mat.SetTexture(Metallic, t);
}
else if (t.name == objName + "_" + mat.name + m_NormalMap)
{
mat.SetTexture(NormalMap, t);
}
else if (t.name == objName + "_" + mat.name + m_OcclusionMap)
{
mat.SetTexture(OcclusionMap, t);
}
else
{
Debug.Log(mat.name+" Shaders cannot match :" + t.name);
}
}
}
Complete code :
This code inherits EditorWindow
So we have to put it in Editor Under the folder , Make revolving door And Camera controller Is the same , All use Editor Extension Technology
using System.IO;
using UnityEngine;
using UnityEditor;
public class MatchinMaterial_Editor : EditorWindow
{
public string modelPath = "Assets";
public Rect modelRect;
public string Albedo = "_MainTex";
public string Metallic = "_MetallicGlossMap";
public string NormalMap = "_BumpMap";
public string HeightMap = "_ParallaxMap";
public string OcclusionMap = "_OcclusionMap";
private static MatchinMaterial_Editor _window;
[SerializeField]
public string m_Albedo = "_Albedo", m_Metallic = "_Metallic", m_NormalMap = "_Normal", m_HeightMap = "_Height", m_OcclusionMap = "_Occlusion";
[MenuItem("Tools/ Material matching ")]
public static void showWindow()
{
Rect wr = new Rect(0, 0, 300, 300);
// true Indicates that you cannot dock
_window = (MatchinMaterial_Editor)GetWindowWithRect(typeof(MatchinMaterial_Editor), wr, true, " Material matching ");
_window.Show();
}
public void OnGUI()
{
EditorGUILayout.Space();
EditorGUILayout.LabelField(" Model path ( Drag the folder here with the mouse )");
EditorGUILayout.Space();
GUI.SetNextControlName("input1");// Set the name of the next control
modelRect = EditorGUILayout.GetControlRect();
modelPath = EditorGUI.TextField(modelRect, modelPath);
EditorGUILayout.Space();
m_Albedo = EditorGUILayout.TextField("Albedo Picture suffix ", m_Albedo);
m_Metallic = EditorGUILayout.TextField("Metallic Picture suffix ", m_Metallic);
m_NormalMap = EditorGUILayout.TextField("NormalMap Picture suffix ", m_NormalMap);
m_HeightMap = EditorGUILayout.TextField("HeightMap Picture suffix ", m_HeightMap);
m_OcclusionMap = EditorGUILayout.TextField("OcclusionMap Picture suffix ", m_OcclusionMap);
DragFolder();
EditorGUILayout.Space();
// Export materials
if (GUILayout.Button(" Export shaders "))
{
ForEachModels();
}
EditorGUILayout.Space();
if (GUILayout.Button(" Set the material "))
{
ForEachMaterials();
}
}
/// <summary>
/// Get the drag file
/// </summary>
void DragFolder()
{
// The mouse is in the current window
if (mouseOverWindow == this)
{
// Drag into the window without releasing the mouse
if (Event.current.type == EventType.DragUpdated)
{
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;// Change the appearance of the mouse
// Judgment area
if (modelRect.Contains(Event.current.mousePosition))
GUI.FocusControl("input1");
}
// Drag into the window and release the mouse
else if (Event.current.type == EventType.DragExited)
{
string dragPath = string.Join("", DragAndDrop.paths);
// Judgment area
if (modelRect.Contains(Event.current.mousePosition))
this.modelPath = dragPath;
// Remove focus ( Otherwise GUI Not refresh )
GUI.FocusControl(null);
}
}
}
/// <summary>
/// Export materials
/// </summary>
void ForEachModels()
{
string[] allPath = AssetDatabase.FindAssets("t:GameObject", new string[] {
modelPath });
//Debug.Log("-- allPath: " + allPath.Length);
for (int i = 0, len = allPath.Length; i < len; i++)
{
string filePath = AssetDatabase.GUIDToAssetPath(allPath[i]);
// Set up the model
ExtractMaterialsFromFBX(filePath);
}
// If you choose FBX Model file
if (allPath.Length == 0)
{
if (Path.GetExtension(modelPath) == ".fbx")
{
ExtractMaterialsFromFBX(modelPath);
}
else
{
Debug.LogError(" The currently selected directory cannot be found FBX file : " + this.modelPath);
}
}
}
/// <summary>
/// Export shaders
/// </summary>
/// <param name="assetPath"></param>
public void ExtractMaterialsFromFBX(string assetPath)
{
// Material catalog
string materialFolder = Path.GetDirectoryName(assetPath) + "/Material";
//Debug.Log(assetPath);
// If the folder does not exist, create a new
if (!AssetDatabase.IsValidFolder(materialFolder))
AssetDatabase.CreateFolder(Path.GetDirectoryName(assetPath), "Material");
// obtain assetPath All resources under .
Object[] assets = AssetDatabase.LoadAllAssetsAtPath(assetPath);
foreach (Object item in assets)
{
if (item.GetType() == typeof(Material))
{
string path = System.IO.Path.Combine(materialFolder, item.name) + ".mat";
// Create a new unique path for the resource .
path = AssetDatabase.GenerateUniqueAssetPath(path);
// By importing resources ( for example ,FBX file ) Extract external resources , In object ( for example , texture of material ) Create this resource in .
string value = AssetDatabase.ExtractAsset(item, path);
// Successfully extracted ( If Unity Successfully extracted resources , Then return an empty string )
if (string.IsNullOrEmpty(value))
{
AssetDatabase.WriteImportSettingsIfDirty(assetPath);
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
Debug.Log(Path.GetFileName(assetPath) + " Of Material Export succeeded !!");
}
}
}
}
/// <summary>
/// Get all shaders
/// </summary>
void ForEachMaterials()
{
string[] allPath = GetObjPath("Material");
for (int i = 0, len = allPath.Length; i < len; i++)
{
string filePath = AssetDatabase.GUIDToAssetPath(allPath[i]);
setMaterialShader(filePath);
}
Debug.Log("Material Shader Setup completed , altogether : " + allPath.Length + " individual ");
}
void setMaterialShader(string path)
{
Material mat = AssetDatabase.LoadAssetAtPath<Material>(path);
string[] allPath = GetObjPath("Texture2D");
for (int i = 0, len = allPath.Length; i < len; i++)
{
string filePath = AssetDatabase.GUIDToAssetPath(allPath[i]);
Texture2D t = AssetDatabase.LoadAssetAtPath<Texture2D>(filePath);
string objName = Path.GetFileName(modelPath);
objName = objName.Substring(0, objName.Length - 4);
if (t.name == objName + "_" + mat.name + m_Albedo)
{
mat.SetTexture(Albedo, t);
}
else if (t.name == objName + "_" + mat.name + m_HeightMap)
{
mat.SetTexture(HeightMap, t);
}
else if (t.name == objName + "_" + mat.name + m_Metallic)
{
mat.SetTexture(Metallic, t);
}
else if (t.name == objName + "_" + mat.name + m_NormalMap)
{
mat.SetTexture(NormalMap, t);
}
else if (t.name == objName + "_" + mat.name + m_OcclusionMap)
{
mat.SetTexture(OcclusionMap, t);
}
else
{
Debug.Log(mat.name+" Shaders cannot match :" + t.name);
}
}
}
string[] GetObjPath(string mType)
{
//string path = Path.GetDirectoryName(modelPath) + "/Material";// Get the file directory of this object , Specific to the Material file
string path = Path.GetDirectoryName(modelPath);// Get the file directory of this object
return AssetDatabase.FindAssets("t:"+ mType, new string[] {
path });// Look under this file "t:Material" This file
}
}
This code needs Demo demonstration ?
边栏推荐
- Swiftui swift internal skill: five skills of using opaque type in swift
- Various uses of vim are very practical. I learned and summarized them in my work
- VIM command mode and input mode switching
- Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
- Summed up 200 Classic machine learning interview questions (with reference answers)
- Flet教程之 17 Card卡片组件 基础入门(教程含源码)
- In SQL, I want to set foreign keys. Why is this problem
- Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
- [extraction des caractéristiques de texture] extraction des caractéristiques de texture de l'image LBP basée sur le mode binaire local de Matlab [y compris le code source de Matlab 1931]
- Mastering the new functions of swiftui 4 weatherkit and swift charts
猜你喜欢
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
Flet教程之 15 GridView 基础入门(教程含源码)
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
Camera calibration (2): summary of monocular camera calibration
千人规模互联网公司研发效能成功之路
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit
随机推荐
Time bomb inside the software: 0-day log4shell is just the tip of the iceberg
Onedns helps college industry network security
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
[data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Superscalar processor design yaoyongbin Chapter 10 instruction submission excerpt
R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
Rationaldmis2022 advanced programming macro program
EasyUI learn to organize notes
powershell cs-UTF-16LE编码上线
Various uses of vim are very practical. I learned and summarized them in my work
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
Mastering the new functions of swiftui 4 weatherkit and swift charts
本地navicat连接liunx下的oracle报权限不足
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
相机标定(2): 单目相机标定总结
超标量处理器设计 姚永斌 第9章 指令执行 摘录
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
[shortest circuit] acwing1128 Messenger: Floyd shortest circuit