Remy
7 年前
当前提交
0fe67279
共有 8 个文件被更改,包括 330 次插入 和 1 次删除
-
9ScriptableRenderPipeline/Core/CoreRP/Editor/MaterialUpgrader.cs
-
35ScriptableRenderPipeline/HDRenderPipeline/HDRP/Editor/Material/Lit/StandardToHDLitMaterialUpgrader.cs
-
8ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner.meta
-
182ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs
-
11ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.cs.meta
-
77ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader
-
9ScriptableRenderPipeline/Core/CoreRP/Editor/TextureCombiner/TextureCombiner.shader.meta
|
|||
fileFormatVersion: 2 |
|||
guid: 87845c683f4fa7546a3910e641768da8 |
|||
folderAsset: yes |
|||
DefaultImporter: |
|||
externalObjects: {} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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]; |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1a9d1eab24683cb42a7c52b9d850d154 |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
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 |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 8486e1ca5cc45c34c94df540d5676ec3 |
|||
ShaderImporter: |
|||
externalObjects: {} |
|||
defaultTextures: [] |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue