浏览代码

Upgrade material upgrader

/main
Remy 7 年前
当前提交
0fe67279
共有 8 个文件被更改,包括 330 次插入1 次删除
  1. 9
      ScriptableRenderPipeline/Core/CoreRP/Editor/MaterialUpgrader.cs
  2. 35
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs
  3. 8
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner.meta
  4. 182
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs
  5. 11
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs.meta
  6. 77
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader
  7. 9
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader.meta

9
ScriptableRenderPipeline/Core/CoreRP/Editor/MaterialUpgrader.cs


Dictionary<string, float> m_FloatPropertiesToSet = new Dictionary<string, float>();
Dictionary<string, Color> m_ColorPropertiesToSet = new Dictionary<string, Color>();
List<string> m_TexturesToRemove = new List<string>();
Dictionary<string, Texture> m_TexturesToSet = new Dictionary<string, Texture>();
class KeywordFloatRename

foreach (var prop in m_TexturesToRemove)
dstMaterial.SetTexture(prop, null);
foreach (var prop in m_TexturesToSet)
dstMaterial.SetTexture(prop.Key, prop.Value);
foreach (var prop in m_FloatPropertiesToSet)
dstMaterial.SetFloat(prop.Key, prop.Value);

public void SetColor(string propertyName, Color value)
{
m_ColorPropertiesToSet[propertyName] = value;
}
public void SetTexture(string propertyName, Texture value)
{
m_TexturesToSet[propertyName] = value;
}
public void RenameKeywordToFloat(string oldName, string newName, float setVal, float unsetVal)

35
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs


// the HD renderloop packs detail albedo and detail normals into a single texture.
// mapping the detail normal map, if any, to the detail map, should do the right thing if
// there is no detail albedo.
RenameTexture("_DetailNormalMap", "_DetailMap");
RenameTexture("_DetailAlbedoMap", "_DetailMap");
// Moved to convert function
// Metallic uses [Gamma] attribute in standard shader but not in Lit.
// @Seb: Should we convert?

public override void Convert(Material srcMaterial, Material dstMaterial)
{
dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;
//*
Texture2D maskMap;
TextureCombiner maskMapCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", 1), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", 0), 4,
TextureCombiner.GetTextureSafe(srcMaterial, (srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0)?"_MetallicGlossMap": "_MainTex", 2), 3
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial).Replace(".mat", "_MaskMap.exr");
maskMap = maskMapCombiner.Combine( maskMapPath );
dstMaterial.SetTexture("_MaskMap", maskMap);
// */
//*
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", 2), 4,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 1,
TextureCombiner.midGrey, 1,
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", 2), 0
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial).Replace(".mat", "_DetailMap.exr");
detailMap = detailCombiner.Combine( detailMapPath );
Debug.Log("Coucou");
dstMaterial.SetTexture("_DetailMap", detailMap);
//*/
HDEditorUtils.ResetMaterialKeywords(dstMaterial);
}
}

8
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner.meta


fileFormatVersion: 2
guid: 87845c683f4fa7546a3910e641768da8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

182
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class TextureCombiner
{
public static Texture2D _midGrey;
public static Texture2D midGrey
{
get
{
if (_midGrey == null)
{
_midGrey = new Texture2D(4, 4, TextureFormat.ARGB32, false, false);
_midGrey.SetPixels(new Color[] {
Color.gray, Color.gray, Color.gray, Color.gray,
Color.gray, Color.gray, Color.gray, Color.gray,
Color.gray, Color.gray, Color.gray, Color.gray,
Color.gray, Color.gray, Color.gray, Color.gray
});
_midGrey.Apply();
}
return _midGrey;
}
}
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, int fallback = 0)
{
Texture tex = srcMaterial.GetTexture(propertyName);
if (tex == null)
{
switch(fallback)
{
case 0: tex = Texture2D.whiteTexture; break;
case 1: tex = Texture2D.blackTexture; break;
case 2: tex = TextureCombiner.midGrey; break;
}
}
return tex;
}
private Texture m_rSource;
private Texture m_gSource;
private Texture m_bSource;
private Texture m_aSource;
// Chanels are : r=0, g=1, b=2, a=3, greyscale from rgb = 4
private int m_rChanel;
private int m_gChanel;
private int m_bChanel;
private int m_aChanel;
private bool m_bilinearFilter;
private Dictionary<Texture, Texture> m_RawTextures;
public TextureCombiner( Texture rSource, int rChanel, Texture gSource, int gChanel, Texture bSource, int bChanel, Texture aSource, int achanel, bool bilinearFilter = true )
{
m_rSource = rSource;
m_gSource = gSource;
m_bSource = bSource;
m_aSource = aSource;
m_rChanel = rChanel;
m_gChanel = gChanel;
m_bChanel = bChanel;
m_aChanel = achanel;
m_bilinearFilter = bilinearFilter;
}
public Texture2D Combine( string savePath )
{
int xMin = int.MaxValue;
int yMin = int.MaxValue;
if (m_rSource.width > 4 && m_rSource.width < xMin) xMin = m_rSource.width;
if (m_gSource.width > 4 && m_gSource.width < xMin) xMin = m_gSource.width;
if (m_bSource.width > 4 && m_bSource.width < xMin) xMin = m_bSource.width;
if (m_aSource.width > 4 && m_aSource.width < xMin) xMin = m_aSource.width;
if (xMin == int.MaxValue) xMin = 4;
if (m_rSource.height > 4 && m_rSource.height < yMin) yMin = m_rSource.height;
if (m_gSource.height > 4 && m_gSource.height < yMin) yMin = m_gSource.height;
if (m_bSource.height > 4 && m_bSource.height < yMin) yMin = m_bSource.height;
if (m_aSource.height > 4 && m_aSource.height < yMin) yMin = m_aSource.height;
if (yMin == int.MaxValue) yMin = 4;
Texture2D combined = new Texture2D(xMin, yMin, TextureFormat.RGBAFloat, true, true);
combined.hideFlags = HideFlags.DontUnloadUnusedAsset;
Material combinerMaterial = new Material(Shader.Find("Hidden/SRP_Core/TextureCombiner"));
combinerMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;
combinerMaterial.SetTexture("_RSource", GetRawTexture(m_rSource));
combinerMaterial.SetTexture("_GSource", GetRawTexture(m_gSource));
combinerMaterial.SetTexture("_BSource", GetRawTexture(m_bSource));
combinerMaterial.SetTexture("_ASource", GetRawTexture(m_aSource));
combinerMaterial.SetFloat("_RChannel", m_rChanel);
combinerMaterial.SetFloat("_GChannel", m_gChanel);
combinerMaterial.SetFloat("_BChannel", m_bChanel);
combinerMaterial.SetFloat("_AChannel", m_aChanel);
RenderTexture combinedRT = new RenderTexture(xMin, yMin, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
Graphics.Blit(Texture2D.whiteTexture, combinedRT, combinerMaterial);
// Readback the render texture
RenderTexture previousActive = RenderTexture.active;
RenderTexture.active = combinedRT;
combined.ReadPixels(new Rect(0, 0, xMin, yMin), 0, 0, false);
combined.Apply();
RenderTexture.active = previousActive;
byte[] bytes = ImageConversion.EncodeToEXR(combined);
string systemPath = Path.Combine(Application.dataPath.Remove(Application.dataPath.Length-6), savePath);
File.WriteAllBytes(systemPath, bytes);
Object.DestroyImmediate(combined);
AssetDatabase.ImportAsset(savePath);
TextureImporter combinedImporter = (TextureImporter) AssetImporter.GetAtPath(savePath);
combinedImporter.sRGBTexture = false;
combinedImporter.SaveAndReimport();
combined = AssetDatabase.LoadAssetAtPath<Texture2D>(savePath);
//cleanup "raw" textures
foreach( KeyValuePair<Texture, Texture> prop in m_RawTextures )
{
if (AssetDatabase.Contains(prop.Value))
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(prop.Value));
}
Object.DestroyImmediate(combinerMaterial);
m_RawTextures.Clear();
return combined;
}
private Texture GetRawTexture (Texture original)
{
if (m_RawTextures == null) m_RawTextures = new Dictionary<Texture, Texture>();
if (!m_RawTextures.ContainsKey(original))
{
if ( AssetDatabase.Contains(original))
{
string path = AssetDatabase.GetAssetPath(original);
string rawPath = "Assets/raw_"+Path.GetFileName(path);
AssetDatabase.CopyAsset(path, rawPath);
AssetDatabase.ImportAsset(rawPath);
Debug.Log("Import raw texture: "+rawPath);
TextureImporter rawImporter = (TextureImporter) TextureImporter.GetAtPath(rawPath);
rawImporter.mipmapEnabled = false;
rawImporter.isReadable = true;
rawImporter.filterMode = m_bilinearFilter? FilterMode.Bilinear : FilterMode.Point;
rawImporter.npotScale = TextureImporterNPOTScale.None;
rawImporter.wrapMode = TextureWrapMode.Clamp;
rawImporter.textureCompression = TextureImporterCompression.Uncompressed;
rawImporter.textureType = TextureImporterType.Default;
rawImporter.SaveAndReimport();
m_RawTextures.Add(original, AssetDatabase.LoadAssetAtPath<Texture>(rawPath));
}
else
m_RawTextures.Add(original, original);
}
return m_RawTextures[original];
}
}

11
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs.meta


fileFormatVersion: 2
guid: 1a9d1eab24683cb42a7c52b9d850d154
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

77
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader


Shader "Hidden/SRP_Core/TextureCombiner"
{
Properties
{
// Chanels are : r=0, g=1, b=2, a=3, greyscale from rgb = 4
[NoScaleOffset] _RSource ("R Source", 2D) = "white" {}
_RChannel ("R Channel", float) = 0
[NoScaleOffset] _GSource ("G Source", 2D) = "white" {}
_GChannel ("G Channel", float) = 1
[NoScaleOffset] _BSource ("B Source", 2D) = "white" {}
_BChannel ("B Channel", float) = 2
[NoScaleOffset] _ASource ("A Source", 2D) = "white" {}
_AChannel ("A Channel", float) = 3
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _RSource, _GSource, _BSource, _ASource;
float _RChannel, _GChannel, _BChannel, _AChannel;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float PlotSourcetoChanel(float4 source, float param)
{
if (param >= 4)
return source.r * 0.3 + source.g * 0.59 + source.b * 0.11; // Photoshop desaturation : G*.59+R*.3+B*.11
else
return source[param];
}
float4 frag (v2f i) : SV_Target
{
float4 col = float4(0,0,0,0);
col.r = PlotSourcetoChanel( tex2D(_RSource, i.uv), _RChannel );
col.g = PlotSourcetoChanel( tex2D(_GSource, i.uv), _GChannel );
col.b = PlotSourcetoChanel( tex2D(_BSource, i.uv), _BChannel );
col.a = PlotSourcetoChanel( tex2D(_ASource, i.uv), _AChannel );
return col;
}
ENDCG
}
}
}

9
ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader.meta


fileFormatVersion: 2
guid: 8486e1ca5cc45c34c94df540d5676ec3
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存