220 lines
7.1 KiB
C#
220 lines
7.1 KiB
C#
|
|
using System.Collections.Generic;
|
||
|
|
using System.Text;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEditor;
|
||
|
|
using UnityEditor.AssetImporters;
|
||
|
|
|
||
|
|
public class URPMaterialPostprocessor : AssetPostprocessor
|
||
|
|
{
|
||
|
|
private const string UrpLitPath = "Universal Render Pipeline/Lit";
|
||
|
|
private const bool LogPropertyLists = true; // список всех свойств
|
||
|
|
private const bool LogSuccessfulAssignments = true;
|
||
|
|
|
||
|
|
private static void LogBlock(StringBuilder sb, string title)
|
||
|
|
{
|
||
|
|
sb.AppendLine();
|
||
|
|
sb.AppendLine("======================================");
|
||
|
|
sb.AppendLine(title);
|
||
|
|
sb.AppendLine("======================================");
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void LogSection(StringBuilder sb, string title)
|
||
|
|
{
|
||
|
|
sb.AppendLine();
|
||
|
|
sb.AppendLine($"--- {title} ---");
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void LogKeyValue(StringBuilder sb, string key, object value)
|
||
|
|
{
|
||
|
|
sb.AppendLine($"{key}: {value}");
|
||
|
|
}
|
||
|
|
|
||
|
|
private static void LogList(StringBuilder sb, string title, List<string> items)
|
||
|
|
{
|
||
|
|
sb.AppendLine($"{title} ({items.Count}):");
|
||
|
|
for (int i = 0; i < items.Count; i++)
|
||
|
|
sb.AppendLine($" [{i}] {items[i]}");
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnPreprocessModel()
|
||
|
|
{
|
||
|
|
ModelImporter importer = (ModelImporter)assetImporter;
|
||
|
|
importer.materialImportMode = ModelImporterMaterialImportMode.ImportViaMaterialDescription;
|
||
|
|
|
||
|
|
Debug.Log(
|
||
|
|
$"[URP Material Postprocessor] OnPreprocessModel\n" +
|
||
|
|
$"Asset: {assetPath}\n" +
|
||
|
|
$"Material Import Mode: {importer.materialImportMode}"
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnPreprocessMaterialDescription(
|
||
|
|
MaterialDescription description,
|
||
|
|
Material material,
|
||
|
|
AnimationClip[] clips)
|
||
|
|
{
|
||
|
|
StringBuilder log = new StringBuilder(4096);
|
||
|
|
|
||
|
|
LogBlock(log, "[URP Material Postprocessor] OnPreprocessMaterialDescription");
|
||
|
|
LogKeyValue(log, "Asset Path", assetPath);
|
||
|
|
LogKeyValue(log, "Material", material != null ? material.name : "<null>");
|
||
|
|
|
||
|
|
Shader shader = Shader.Find(UrpLitPath);
|
||
|
|
if (shader == null)
|
||
|
|
{
|
||
|
|
log.AppendLine();
|
||
|
|
log.AppendLine($"ERROR: Shader not found: {UrpLitPath}");
|
||
|
|
Debug.LogError(log.ToString());
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
material.shader = shader;
|
||
|
|
|
||
|
|
if (LogSuccessfulAssignments)
|
||
|
|
{
|
||
|
|
LogSection(log, "Shader");
|
||
|
|
LogKeyValue(log, "Assigned", UrpLitPath);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------
|
||
|
|
// All available properties
|
||
|
|
// ---------------------------
|
||
|
|
if (LogPropertyLists)
|
||
|
|
{
|
||
|
|
List<string> floatProps = new List<string>();
|
||
|
|
List<string> vectorProps = new List<string>();
|
||
|
|
List<string> textureProps = new List<string>();
|
||
|
|
|
||
|
|
description.GetFloatPropertyNames(floatProps);
|
||
|
|
description.GetVector4PropertyNames(vectorProps);
|
||
|
|
description.GetTexturePropertyNames(textureProps);
|
||
|
|
|
||
|
|
LogSection(log, "Available properties");
|
||
|
|
LogList(log, "Float", floatProps);
|
||
|
|
LogList(log, "Vector4", vectorProps);
|
||
|
|
LogList(log, "Texture", textureProps);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------
|
||
|
|
// Base color
|
||
|
|
// ---------------------------
|
||
|
|
bool hasBaseColor = description.TryGetProperty("baseColor", out Vector4 baseColor);
|
||
|
|
LogSection(log, "Base Color");
|
||
|
|
if (hasBaseColor)
|
||
|
|
{
|
||
|
|
material.SetColor("_BaseColor", baseColor);
|
||
|
|
LogKeyValue(log, "Found", true);
|
||
|
|
LogKeyValue(log, "Value", baseColor);
|
||
|
|
|
||
|
|
if (LogSuccessfulAssignments)
|
||
|
|
LogKeyValue(log, "Applied To", "_BaseColor");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", false);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------
|
||
|
|
// Base map
|
||
|
|
// ---------------------------
|
||
|
|
bool hasBaseMap = description.TryGetProperty("baseColorMap", out TexturePropertyDescription baseMap);
|
||
|
|
LogSection(log, "Base Map");
|
||
|
|
if (hasBaseMap)
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", true);
|
||
|
|
|
||
|
|
if (baseMap.texture != null)
|
||
|
|
{
|
||
|
|
material.SetTexture("_BaseMap", baseMap.texture);
|
||
|
|
LogKeyValue(log, "Texture", baseMap.texture.name);
|
||
|
|
|
||
|
|
if (LogSuccessfulAssignments)
|
||
|
|
LogKeyValue(log, "Applied To", "_BaseMap");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Texture", "<null>");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", false);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------
|
||
|
|
// Normal map
|
||
|
|
// ---------------------------
|
||
|
|
bool hasNormalMap = description.TryGetProperty("normalMap", out TexturePropertyDescription normalMap);
|
||
|
|
LogSection(log, "Normal Map");
|
||
|
|
if (hasNormalMap)
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", true);
|
||
|
|
|
||
|
|
if (normalMap.texture != null)
|
||
|
|
{
|
||
|
|
material.SetTexture("_BumpMap", normalMap.texture);
|
||
|
|
material.EnableKeyword("_NORMALMAP");
|
||
|
|
|
||
|
|
LogKeyValue(log, "Texture", normalMap.texture.name);
|
||
|
|
LogKeyValue(log, "Keyword", "_NORMALMAP");
|
||
|
|
|
||
|
|
if (LogSuccessfulAssignments)
|
||
|
|
LogKeyValue(log, "Applied To", "_BumpMap");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Texture", "<null>");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", false);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------
|
||
|
|
// Metallic
|
||
|
|
// ---------------------------
|
||
|
|
bool hasMetalness = description.TryGetProperty("metalness", out float metallic);
|
||
|
|
LogSection(log, "Metalness");
|
||
|
|
if (hasMetalness)
|
||
|
|
{
|
||
|
|
material.SetFloat("_Metallic", metallic);
|
||
|
|
LogKeyValue(log, "Found", true);
|
||
|
|
LogKeyValue(log, "Value", metallic);
|
||
|
|
|
||
|
|
if (LogSuccessfulAssignments)
|
||
|
|
LogKeyValue(log, "Applied To", "_Metallic");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", false);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ---------------------------
|
||
|
|
// Roughness -> Smoothness
|
||
|
|
// ---------------------------
|
||
|
|
bool hasRoughness = description.TryGetProperty("roughness", out float roughness);
|
||
|
|
LogSection(log, "Roughness / Smoothness");
|
||
|
|
if (hasRoughness)
|
||
|
|
{
|
||
|
|
float smoothness = 1.0f - roughness;
|
||
|
|
material.SetFloat("_Smoothness", smoothness);
|
||
|
|
|
||
|
|
LogKeyValue(log, "Found", true);
|
||
|
|
LogKeyValue(log, "Roughness", roughness);
|
||
|
|
LogKeyValue(log, "Smoothness", smoothness);
|
||
|
|
|
||
|
|
if (LogSuccessfulAssignments)
|
||
|
|
LogKeyValue(log, "Applied To", "_Smoothness");
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
LogKeyValue(log, "Found", false);
|
||
|
|
}
|
||
|
|
|
||
|
|
LogSection(log, "Result");
|
||
|
|
LogKeyValue(log, "Material Converted", material.name);
|
||
|
|
|
||
|
|
Debug.Log(log.ToString());
|
||
|
|
}
|
||
|
|
}
|