浏览代码

Merge pull request #982 from Unity-Technologies/MaterialUpgrader_Upgrade

Material upgrader upgrade
/main
GitHub 7 年前
当前提交
f7f81ac1
共有 16 个文件被更改,包括 699 次插入114 次删除
  1. 9
      ScriptableRenderPipeline/Core/CoreRP/Editor/MaterialUpgrader.cs
  2. 10
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/UpgradeStandardShaderMaterials.cs
  3. 8
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner.meta
  4. 208
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs
  5. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs.meta
  6. 44
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Unlit/UnlitsToHDUnlitUpgrader.cs
  7. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Unlit/UnlitsToHDUnlitUpgrader.cs.meta
  8. 11
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs.meta
  9. 9
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader.meta
  10. 282
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs
  11. 98
      ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader
  12. 12
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs.meta
  13. 12
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs.meta
  14. 45
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs
  15. 43
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs

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)

10
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/UpgradeStandardShaderMaterials.cs


static List<MaterialUpgrader> GetHDUpgraders()
{
var upgraders = new List<MaterialUpgrader>();
upgraders.Add(new StandardToHDLitMaterialUpgrader("Standard", "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass));
upgraders.Add(new StandardSpecularToHDLitMaterialUpgrader("Standard (Specular setup)", "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass));
upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard", "HDRenderPipeline/Lit"));
upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard (Specular setup)", "HDRenderPipeline/Lit"));
upgraders.Add(new StandardsToHDLitMaterialUpgrader("Standard (Roughness setup)", "HDRenderPipeline/Lit"));
upgraders.Add(new UnlitsToHDUnlitUpgrader("Unlit/Color", "HDRenderPipeline/Unlit"));
upgraders.Add(new UnlitsToHDUnlitUpgrader("Unlit/Texture", "HDRenderPipeline/Unlit"));
upgraders.Add(new UnlitsToHDUnlitUpgrader("Unlit/Transparent", "HDRenderPipeline/Unlit"));
upgraders.Add(new UnlitsToHDUnlitUpgrader("Unlit/Transparent Cutout", "HDRenderPipeline/Unlit"));
return upgraders;
}

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


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

208
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs


using UnityEngine;
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class StandardsToHDLitMaterialUpgrader : MaterialUpgrader
{
static readonly string Standard = "Standard";
static readonly string Standard_Spec = "Standard (Specular setup)";
static readonly string Standard_Rough = "Standard (Roughness setup)";
public StandardsToHDLitMaterialUpgrader(string sourceShaderName, string destShaderName, MaterialFinalizer finalizer = null)
{
RenameShader(sourceShaderName, destShaderName, finalizer);
RenameTexture("_MainTex", "_BaseColorMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_Glossiness", "_Smoothness");
RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameTexture("_ParallaxMap", "_HeightMap");
RenameTexture("_EmissionMap", "_EmissiveColorMap");
RenameTexture("_DetailAlbedoMap", "_DetailMap");
RenameFloat("_UVSec", "_UVDetail");
SetFloat("_LinkDetailsWithBase", 0);
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");
RenameFloat("_Cutoff", "_AlphaCutoff");
RenameKeywordToFloat("_ALPHATEST_ON", "_AlphaCutoffEnable", 1f, 0f);
if (sourceShaderName == Standard)
{
SetFloat("_MaterialID", 1f);
}
if (sourceShaderName == Standard_Spec)
{
SetFloat("_MaterialID", 4f);
RenameColor("_SpecColor", "_SpecularColor");
RenameTexture("_SpecGlossMap", "_SpecularColorMap");
}
}
public override void Convert(Material srcMaterial, Material dstMaterial)
{
dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;
base.Convert(srcMaterial, dstMaterial);
// ---------- Mask Map ----------
// Metallic
bool hasMetallic = false;
Texture metallicMap;
if ( (srcMaterial.shader.name == Standard) || (srcMaterial.shader.name == Standard_Rough) )
{
hasMetallic = srcMaterial.GetTexture("_MetallicGlossMap") != null;
if (hasMetallic) metallicMap = TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", Color.white);
}
else
metallicMap = Texture2D.blackTexture;
// Occlusion
bool hasOcclusion = srcMaterial.GetTexture("_OcclusionMap") != null;
Texture occlusionMap;
if (hasOcclusion) occlusionMap = TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white);
// Detail Mask
bool hasDetailMask = srcMaterial.GetTexture("_DetailMask") != null;
Texture detailMaskMap;
if (hasDetailMask) detailMaskMap = TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white);
// Smoothness
bool hasSmoothness = false;
Texture2D smoothnessMap = TextureCombiner.TextureFromColor(Color.grey);
if (srcMaterial.shader.name == Standard_Rough)
hasSmoothness = srcMaterial.GetTexture("_SpecGlossMap")!=null;
else
{
string smoothnessTextureChannel = "_MainTex";
if (srcMaterial.shader.name == Standard_Rough)
smoothnessMap = (Texture2D) TextureCombiner.GetTextureSafe(srcMaterial, "_SpecGlossMap", Color.grey);
else
{
if ( srcMaterial.GetFloat("_SmoothnessTextureChannel") == 0 )
{
if (srcMaterial.shader.name == Standard) smoothnessTextureChannel = "_MetallicGlossMap";
if (srcMaterial.shader.name == Standard_Spec) smoothnessTextureChannel = "_SpecGlossMap";
}
smoothnessMap = (Texture2D) srcMaterial.GetTexture( smoothnessTextureChannel );
if (smoothnessMap == null || !TextureCombiner.TextureHasAlpha(smoothnessMap))
{
hasSmoothness = true;
smoothnessMap = TextureCombiner.TextureFromColor(Color.white * srcMaterial.GetFloat("_Glossiness"));
}
}
}
// Build the mask map
if ( hasMetallic || hasOcclusion || hasDetailMask || hasSmoothness )
{
Texture2D maskMap;
TextureCombiner maskMapCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_MetallicGlossMap", Color.white), 4, // Metallic
TextureCombiner.GetTextureSafe(srcMaterial, "_OcclusionMap", Color.white), 4, // Occlusion
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailMask", Color.white), 4, // Detail Mask
smoothnessMap, (srcMaterial.shader.name == Standard_Rough)?-4:3 // Smoothness Texture
);
string maskMapPath = AssetDatabase.GetAssetPath(srcMaterial);
maskMapPath = maskMapPath.Remove(maskMapPath.Length-4) + "_MaskMap.png";
maskMap = maskMapCombiner.Combine( maskMapPath );
dstMaterial.SetTexture("_MaskMap", maskMap);
}
dstMaterial.SetFloat("_AORemapMin", 1f - srcMaterial.GetFloat("_OcclusionStrength"));
// Specular Setup Specific
if (srcMaterial.shader.name == Standard_Spec)
{
// if there is a specular map, change the specular color to white
if (srcMaterial.GetTexture("_SpecGlossMap") != null ) dstMaterial.SetColor("_SpecularColor", Color.white);
}
// ---------- Height Map ----------
bool hasHeightMap = srcMaterial.GetTexture("_ParallaxMap") != null;
if (hasHeightMap) // Enable Parallax Occlusion Mapping
{
dstMaterial.SetFloat("_DisplacementMode", 2);
dstMaterial.SetFloat("_HeightPoMAmplitude", srcMaterial.GetFloat("_Parallax") * 2f);
}
// ---------- Detail Map ----------
bool hasDetailAlbedo = srcMaterial.GetTexture("_DetailAlbedoMap") != null;
bool hasDetailNormal = srcMaterial.GetTexture("_DetailNormalMap") != null;
if ( hasDetailAlbedo || hasDetailNormal )
{
Texture2D detailMap;
TextureCombiner detailCombiner = new TextureCombiner(
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailAlbedoMap", Color.grey), 4, // Albedo (overlay)
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 1, // Normal Y
TextureCombiner.midGrey, 1, // Smoothness
TextureCombiner.GetTextureSafe(srcMaterial, "_DetailNormalMap", Color.grey), 0 // Normal X
);
string detailMapPath = AssetDatabase.GetAssetPath(srcMaterial);
detailMapPath = detailMapPath.Remove(detailMapPath.Length-4) + "_DetailMap.png";
detailMap = detailCombiner.Combine( detailMapPath );
dstMaterial.SetTexture("_DetailMap", detailMap);
}
// Blend Mode
int previousBlendMode = srcMaterial.GetInt("_Mode");
switch (previousBlendMode)
{
case 0: // Opaque
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
break;
case 1: // Cutout
dstMaterial.SetFloat("_SurfaceType", 0);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 1);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
break;
case 2: // Fade -> Alpha + Disable preserve specular
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 0);
break;
case 3: // Transparent -> Alpha
dstMaterial.SetFloat("_SurfaceType", 1);
dstMaterial.SetFloat("_BlendMode", 0);
dstMaterial.SetFloat("_AlphaCutoffEnable", 0);
dstMaterial.SetFloat("_EnableBlendModePreserveSpecularLighting", 1);
break;
}
// Emission: Convert the HDR emissive color to ldr color + intensity
Color hdrEmission = srcMaterial.GetColor("_EmissionColor");
float intensity = Mathf.Max(hdrEmission.r, Mathf.Max(hdrEmission.g, hdrEmission.b));
if (intensity > 1f)
{
hdrEmission.r /= intensity;
hdrEmission.g /= intensity;
hdrEmission.b /= intensity;
}
else
intensity = 1f;
intensity = Mathf.Pow(intensity, 2.2f); // Gamma to Linear conversion
dstMaterial.SetColor("_EmissiveColor", hdrEmission);
dstMaterial.SetFloat("_EmissiveIntensity", intensity);
HDEditorUtils.ResetMaterialKeywords(dstMaterial);
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardsToHDLitMaterialUpgrader.cs.meta


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

44
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Unlit/UnlitsToHDUnlitUpgrader.cs


using UnityEngine;
using System.Collections.Generic;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class UnlitsToHDUnlitUpgrader : MaterialUpgrader
{
string Unlit_Color = "Unlit/Color";
string Unlit_Texture = "Unlit/Texture";
string Unlit_Transparent = "Unlit/Transparent";
string Unlit_Cutout = "Unlit/Transparent Cutout";
public UnlitsToHDUnlitUpgrader(string sourceShaderName, string destShaderName, MaterialFinalizer finalizer = null)
{
RenameShader(sourceShaderName, destShaderName, finalizer);
if (sourceShaderName == Unlit_Color)
RenameColor("_Color", "_UnlitColor");
else // all other unlit have a texture
RenameTexture("_MainTex", "_UnlitColorMap");
if (sourceShaderName == Unlit_Cutout)
{
RenameFloat("_Cutoff", "_AlphaCutoff");
SetFloat("_AlphaCutoffEnable", 1f);
}
else
SetFloat("_AlphaCutoffEnable", 0f);
SetFloat("_SurfaceType", (sourceShaderName == Unlit_Transparent)? 1f : 0f );
}
public override void Convert(Material srcMaterial, Material dstMaterial)
{
//dstMaterial.hideFlags = HideFlags.DontUnloadUnusedAsset;
base.Convert(srcMaterial, dstMaterial);
HDEditorUtils.ResetMaterialKeywords(dstMaterial);
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Unlit/UnlitsToHDUnlitUpgrader.cs.meta


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

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:

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


fileFormatVersion: 2
guid: 8486e1ca5cc45c34c94df540d5676ec3
ShaderImporter:
externalObjects: {}
defaultTextures: []
nonModifiableTextures: []
userData:
assetBundleName:
assetBundleVariant:

282
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 = TextureFromColor(Color.grey);
return _midGrey;
}
}
private static Dictionary<Color, Texture2D> singleColorTextures = new Dictionary<Color, Texture2D>();
public static Texture2D TextureFromColor(Color color)
{
if (color == Color.white) return Texture2D.whiteTexture;
if (color == Color.black) return Texture2D.blackTexture;
bool makeTexture = !singleColorTextures.ContainsKey(color);
if (!makeTexture)
makeTexture = (singleColorTextures[color] == null);
if (makeTexture)
{
Texture2D tex = new Texture2D(1,1, TextureFormat.ARGB32, false, true);
tex.SetPixel(0,0,color);
tex.Apply();
singleColorTextures[color] = tex;
}
return singleColorTextures[color];
}
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, Color fallback)
{
return GetTextureSafe( srcMaterial, propertyName, TextureFromColor(fallback) );
}
public static Texture GetTextureSafe( Material srcMaterial, string propertyName, Texture fallback)
{
if (!srcMaterial.HasProperty(propertyName))
return fallback;
Texture tex = srcMaterial.GetTexture(propertyName);
if (tex == null)
return fallback;
else
return tex;
}
public static TextureFormat[] TextureFormatsWithouthAlpha = new TextureFormat[]{
TextureFormat.ASTC_RGB_10x10 ,
TextureFormat.ASTC_RGB_12x12 ,
TextureFormat.ASTC_RGB_4x4 ,
TextureFormat.ASTC_RGB_5x5 ,
TextureFormat.ASTC_RGB_6x6 ,
TextureFormat.ASTC_RGB_8x8 ,
TextureFormat.BC4 ,
TextureFormat.BC5 ,
TextureFormat.DXT1 ,
TextureFormat.DXT1Crunched ,
TextureFormat.EAC_R ,
TextureFormat.EAC_R_SIGNED ,
TextureFormat.EAC_RG ,
TextureFormat.EAC_RG_SIGNED ,
TextureFormat.ETC2_RGB ,
TextureFormat.ETC_RGB4 ,
TextureFormat.ETC_RGB4_3DS ,
TextureFormat.ETC_RGB4Crunched ,
TextureFormat.PVRTC_RGB2 ,
TextureFormat.PVRTC_RGB4 ,
TextureFormat.R16 ,
TextureFormat.R8 ,
TextureFormat.RFloat ,
TextureFormat.RG16 ,
TextureFormat.RGB24 ,
TextureFormat.RGB565 ,
TextureFormat.RGB9e5Float ,
TextureFormat.RGFloat ,
TextureFormat.RGHalf ,
TextureFormat.RHalf ,
TextureFormat.YUY2
};
public static bool TextureHasAlpha ( Texture2D tex )
{
if (tex == null) return false;
bool o = true;
int i=0;
while ( i < TextureFormatsWithouthAlpha.Length && o)
{
o = tex.format != TextureFormatsWithouthAlpha[i];
++i;
}
return o;
}
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
// If negative, the chanel is inverted
private int m_rChanel;
private int m_gChanel;
private int m_bChanel;
private int m_aChanel;
// Chanels remaping
private Vector4[] m_remapings = new Vector4[]{
new Vector4(0f, 1f, 0f, 0f),
new Vector4(0f, 1f, 0f, 0f),
new Vector4(0f, 1f, 0f, 0f),
new Vector4(0f, 1f, 0f, 0f)
};
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 void SetRemapping( int channel, float min, float max)
{
if (channel > 3 || channel < 0) return;
m_remapings[channel].x = min;
m_remapings[channel].y = max;
}
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);
combinerMaterial.SetVector("_RRemap", m_remapings[0]);
combinerMaterial.SetVector("_GRemap", m_remapings[1]);
combinerMaterial.SetVector("_BRemap", m_remapings[2]);
combinerMaterial.SetVector("_ARemap", m_remapings[3]);
RenderTexture combinedRT = new RenderTexture(xMin, yMin, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.sRGB);
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 = new byte[0];
if (savePath.EndsWith("png"))
bytes = ImageConversion.EncodeToPNG(combined);
if (savePath.EndsWith("exr"))
bytes = ImageConversion.EncodeToEXR(combined);
if (savePath.EndsWith("jpg"))
bytes = ImageConversion.EncodeToJPG(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();
if (savePath.EndsWith("exr"))
{
// The options for the platform string are: "Standalone", "iPhone", "Android", "WebGL", "Windows Store Apps", "PSP2", "PS4", "XboxOne", "Nintendo 3DS", "WiiU", "tvOS".
combinedImporter.SetPlatformTextureSettings(new TextureImporterPlatformSettings(){name = "Standalone", format = TextureImporterFormat.DXT5, overridden = true });
}
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, bool sRGB = false)
{
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);
TextureImporter rawImporter = (TextureImporter) TextureImporter.GetAtPath(rawPath);
rawImporter.textureType = TextureImporterType.Default;
rawImporter.mipmapEnabled = false;
rawImporter.isReadable = true;
rawImporter.filterMode = m_bilinearFilter? FilterMode.Bilinear : FilterMode.Point;
rawImporter.npotScale = TextureImporterNPOTScale.None;
rawImporter.wrapMode = TextureWrapMode.Clamp;
rawImporter.sRGBTexture = sRGB;
rawImporter.maxTextureSize = 8192;
rawImporter.textureCompression = TextureImporterCompression.Uncompressed;
rawImporter.SaveAndReimport();
m_RawTextures.Add(original, AssetDatabase.LoadAssetAtPath<Texture>(rawPath));
}
else
m_RawTextures.Add(original, original);
}
return m_RawTextures[original];
}
}

98
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
// If the chanel value is negative, we invert the value
[Linear][NoScaleOffset] _RSource ("R Source", 2D) = "white" {}
_RChannel ("R Channel", float) = 0
_RRemap ("R Remap", Vector) = (0, 1, 0, 0)
[Linear][NoScaleOffset] _GSource ("G Source", 2D) = "white" {}
_GChannel ("G Channel", float) = 1
_GRemap ("G Remap", Vector) = (0, 1, 0, 0)
[Linear][NoScaleOffset] _BSource ("B Source", 2D) = "white" {}
_BChannel ("B Channel", float) = 2
_BRemap ("B Remap", Vector) = (0, 1, 0, 0)
[Linear][NoScaleOffset] _ASource ("A Source", 2D) = "white" {}
_AChannel ("A Channel", float) = 3
_ARemap ("A Remap", Vector) = (0, 1, 0, 0)
}
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;
float4 _RRemap, _GRemap, _BRemap, _ARemap;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float PlotSourcetoChanel(float4 source, float param, float2 remap)
{
if (param < 0 )
{
param = -param;
source = float4(1,1,1,1) - source;
}
float o;
if (param >= 4)
o = source.r * 0.3 + source.g * 0.59 + source.b * 0.11; // Photoshop desaturation : G*.59+R*.3+B*.11
else
o = source[param];
return o * ( remap.y - remap.x) + remap.x ;
}
float PlotSourcetoChanel(float4 source, float param)
{
return PlotSourcetoChanel(source, param, float2(0,1) );
}
float4 frag (v2f i) : SV_Target
{
float4 col = float4(0,0,0,0);
col.r = PlotSourcetoChanel( tex2D(_RSource, i.uv), _RChannel, _RRemap );
col.g = PlotSourcetoChanel( tex2D(_GSource, i.uv), _GChannel, _GRemap );
col.b = PlotSourcetoChanel( tex2D(_BSource, i.uv), _BChannel, _BRemap );
col.a = PlotSourcetoChanel( tex2D(_ASource, i.uv), _AChannel, _ARemap );
return col;
}
ENDCG
}
}
}

12
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs.meta


fileFormatVersion: 2
guid: 775bef174b1e2ed47972a143845e1191
timeCreated: 1479911618
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

12
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs.meta


fileFormatVersion: 2
guid: fedbd6bc5d95f4d768cb8077e8454b4e
timeCreated: 1479851295
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

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


using UnityEngine;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class StandardToHDLitMaterialUpgrader : MaterialUpgrader
{
public StandardToHDLitMaterialUpgrader() : this("Standard", "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass) {}
public StandardToHDLitMaterialUpgrader(string sourceShaderName, string destShaderName, MaterialFinalizer finalizer)
{
RenameShader(sourceShaderName, destShaderName, finalizer);
RenameTexture("_MainTex", "_BaseColorMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_Glossiness", "_Smoothness");
RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameColor("_EmissionColor", "_EmissiveColor");
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");
RenameFloat("_Cutoff", "_AlphaCutoff");
RenameKeywordToFloat("_ALPHATEST_ON", "_AlphaCutoffEnable", 1f, 0f);
// 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");
// Metallic uses [Gamma] attribute in standard shader but not in Lit.
// @Seb: Should we convert?
RenameFloat("_Metallic", "_Metallic");
//@TODO: Seb. Why do we multiply color by intensity
// in shader when we can just store a color?
// builtinData.emissiveColor * builtinData.emissiveIntensity
}
public override void Convert(Material srcMaterial, Material dstMaterial)
{
base.Convert(srcMaterial, dstMaterial);
//@TODO: Find a good way of setting up keywords etc from properties.
// Code should be shared with material UI code.
}
}
}

43
ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardSpecularToHDLitMaterialUpgrader.cs


using UnityEngine;
namespace UnityEditor.Experimental.Rendering.HDPipeline
{
public class StandardSpecularToHDLitMaterialUpgrader : MaterialUpgrader
{
public StandardSpecularToHDLitMaterialUpgrader() : this("Standard (Specular setup)", "HDRenderPipeline/Lit", LitGUI.SetupMaterialKeywordsAndPass) {}
public StandardSpecularToHDLitMaterialUpgrader(string sourceShaderName, string destShaderName, MaterialFinalizer finalizer)
{
RenameShader(sourceShaderName, destShaderName, finalizer);
RenameTexture("_MainTex", "_BaseColorMap");
RenameColor("_Color", "_BaseColor");
RenameFloat("_Glossiness", "_Smoothness");
RenameTexture("_BumpMap", "_NormalMap");
RenameFloat("_BumpScale", "_NormalScale");
RenameColor("_EmissionColor", "_EmissiveColor");
RenameFloat("_DetailNormalMapScale", "_DetailNormalScale");
RenameFloat("_Cutoff", "_AlphaCutoff");
RenameKeywordToFloat("_ALPHATEST_ON", "_AlphaCutoffEnable", 1f, 0f);
// 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");
// Anything reasonable that can be done here?
//RenameFloat("_SpecColor", ...);
//@TODO: Seb. Why do we multiply color by intensity
// in shader when we can just store a color?
// builtinData.emissiveColor * builtinData.emissiveIntensity
}
public override void Convert(Material srcMaterial, Material dstMaterial)
{
base.Convert(srcMaterial, dstMaterial);
//@TODO: Find a good way of setting up keywords etc from properties.
// Code should be shared with material UI code.
}
}
}
正在加载...
取消
保存