浏览代码
Merge branch 'hackweek2017' of https://github.com/stramit/MaterialGraph into hackweek2017
/main
Merge branch 'hackweek2017' of https://github.com/stramit/MaterialGraph into hackweek2017
/main
Florent Guinier
8 年前
当前提交
fac62848
共有 45 个文件被更改,包括 3089 次插入 和 28 次删除
-
2MaterialGraphProject/Assets/Eduardo/EduardoTestGraph.ShaderGraph
-
7MaterialGraphProject/Assets/Eduardo/FunctionNInNOut.cs
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/MaterialGraphPresenter.cs
-
101MaterialGraphProject/Assets/UnityShaderEditor/Editor/HelperShader.shader
-
10MaterialGraphProject/Assets/UnityShaderEditor/Editor/HelperShader.shader.meta
-
1MaterialGraphProject/Assets/UnityShaderEditor/Editor/Testing/UnitTests/MaterialNodeTests.cs
-
16MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/AbstractMaterialNode.cs
-
2MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/SlotValue.cs
-
63MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs
-
12MaterialGraphProject/Assets/Eduardo/HeightToNormalNode.cs.meta
-
9MaterialGraphProject/Assets/Matt.meta
-
9MaterialGraphProject/Assets/NewNodes.meta
-
9MaterialGraphProject/Assets/NewNodes/WIP.meta
-
1MaterialGraphProject/Assets/Rinaldo/HeightToNormalMap.ShaderGraph
-
9MaterialGraphProject/Assets/Rinaldo/HeightToNormalMap.ShaderGraph.meta
-
52MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Editor/Drawing/Presenters/TextureAssetPresenter.cs.meta
-
872MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/advancedSubshader.template
-
8MaterialGraphProject/Assets/UnityShaderEditor/Editor/Templates/advancedSubshader.template.meta
-
68MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/BoxNode.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/BoxNode.cs.meta
-
56MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/PulseNode.cs
-
12MaterialGraphProject/Assets/UnityShaderEditor/Runtime/Nodes/Procedural/PulseNode.cs.meta
-
267MaterialGraphProject/Assets/Matt/AbstractAdvancedMasterNode.cs
-
12MaterialGraphProject/Assets/Matt/AbstractAdvancedMasterNode.cs.meta
-
198MaterialGraphProject/Assets/Matt/AdvancedBRDF.cginc
-
9MaterialGraphProject/Assets/Matt/AdvancedBRDF.cginc.meta
-
162MaterialGraphProject/Assets/Matt/AdvancedLighting.cginc
-
9MaterialGraphProject/Assets/Matt/AdvancedLighting.cginc.meta
-
461MaterialGraphProject/Assets/Matt/AdvancedShading.cginc
-
9MaterialGraphProject/Assets/Matt/AdvancedShading.cginc.meta
-
1MaterialGraphProject/Assets/Matt/AnisotropicMaster.ShaderGraph
-
9MaterialGraphProject/Assets/Matt/AnisotropicMaster.ShaderGraph.meta
-
66MaterialGraphProject/Assets/Matt/AnisotropicMetallicMasterNode.cs
-
12MaterialGraphProject/Assets/Matt/AnisotropicMetallicMasterNode.cs.meta
-
113MaterialGraphProject/Assets/Matt/New Material.mat
-
9MaterialGraphProject/Assets/Matt/New Material.mat.meta
-
139MaterialGraphProject/Assets/Matt/Work.shader
-
9MaterialGraphProject/Assets/Matt/Work.shader.meta
-
152MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs
-
12MaterialGraphProject/Assets/Vlad/TextureAssetNode.cs.meta
-
112MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs
-
12MaterialGraphProject/Assets/Vlad/UVTriPlanar.cs.meta
2
MaterialGraphProject/Assets/Eduardo/EduardoTestGraph.ShaderGraph
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 9ab5e16c2083a4fe689209a8c1ae425e |
|||
<<<<<<< HEAD |
|||
timeCreated: 1495529915 |
|||
======= |
|||
timeCreated: 1495532053 |
|||
>>>>>>> f9e75cae7d0f088d17985a220f4ca7ae3da1222a |
|||
timeCreated: 1495569001 |
|||
nonModifiableTextures: |
|||
- Texture_5537060b_db74_4900_8f2b_178ba97b7f11_Uniform: {fileID: 2800000, guid: 6e8d12f68bae2294da814f9d4c81b29a, |
|||
type: 3} |
|||
nonModifiableTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine.Graphing; |
|||
using System.Linq; |
|||
using System.Collections; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("HeightToNormal")] |
|||
public class HeightToNormalNode : FunctionNInNOut, IGeneratesFunction |
|||
{ |
|||
|
|||
public HeightToNormalNode() |
|||
{ |
|||
name = "HeightToNormal"; |
|||
AddSlot("HeightMap", "heightmap", Graphing.SlotType.Input, SlotValueType.sampler2D, Vector4.zero); |
|||
AddSlot("UV", "texCoord", Graphing.SlotType.Input, SlotValueType.Vector2, Vector4.zero); |
|||
AddSlot("Offset", "texOffset", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(0.005f, 0,0,0)); |
|||
AddSlot("Strength", "strength", Graphing.SlotType.Input, SlotValueType.Vector1, new Vector4(8,0,0,0)); |
|||
|
|||
AddSlot("Normal", "normalRes", Graphing.SlotType.Output, SlotValueType.Vector3, Vector4.zero); |
|||
|
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
protected override string GetFunctionName() |
|||
{ |
|||
return "unity_ObjectScale"; |
|||
} |
|||
|
|||
public override bool hasPreview |
|||
{ |
|||
get { return true; } |
|||
} |
|||
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var outputString = new ShaderGenerator(); |
|||
outputString.AddShaderChunk(GetFunctionPrototype(), false); |
|||
outputString.AddShaderChunk("{", false); |
|||
outputString.AddShaderChunk("float2 offsetU = float2(texCoord.x + texOffset, texCoord.y);", false); |
|||
outputString.AddShaderChunk("float2 offsetV = float2(texCoord.x, texCoord.y + texOffset);", false); |
|||
|
|||
outputString.AddShaderChunk("float normalSample = tex2D(heightmap, texCoord).r;", false); |
|||
outputString.AddShaderChunk("float uSample = tex2D(heightmap, offsetU).r;", false); |
|||
outputString.AddShaderChunk("float vSample = tex2D(heightmap, offsetV).r;", false); |
|||
|
|||
outputString.AddShaderChunk("float uMinusNormal = uSample - normalSample;", false); |
|||
outputString.AddShaderChunk("float vMinusNormal = vSample - normalSample;", false); |
|||
|
|||
outputString.AddShaderChunk("uMinusNormal = uMinusNormal * strength;", false); |
|||
outputString.AddShaderChunk("vMinusNormal = vMinusNormal * strength;", false); |
|||
|
|||
outputString.AddShaderChunk("float3 va = float3(1, 0, uMinusNormal);", false); |
|||
outputString.AddShaderChunk("float3 vb = float3(0, 1, vMinusNormal);", false); |
|||
|
|||
outputString.AddShaderChunk("normalRes = cross(va, vb);", false); |
|||
|
|||
|
|||
outputString.AddShaderChunk("}", false); |
|||
|
|||
visitor.AddShaderChunk(outputString.GetShaderString(0), true); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: c943843c9c7424a4d9b629c087d93df5 |
|||
timeCreated: 1495565761 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: bf6c1f35d15d67d46a176383182e8988 |
|||
folderAsset: yes |
|||
timeCreated: 1495550636 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 8d1bfadcc792fe44c80d0bdd2961fa26 |
|||
folderAsset: yes |
|||
timeCreated: 1495558038 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
fileFormatVersion: 2 |
|||
guid: 1035abaf40f232e4585818283e199b96 |
|||
folderAsset: yes |
|||
timeCreated: 1495558038 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1
MaterialGraphProject/Assets/Rinaldo/HeightToNormalMap.ShaderGraph
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 65df1abda0bc01c4d8343c1657497eb0 |
|||
timeCreated: 1495556781 |
|||
licenseType: Pro |
|||
ScriptedImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 11500000, guid: 625f186215c104763be7675aa2d941aa, type: 3} |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using RMGUI.GraphView; |
|||
using UnityEditor.Graphing.Drawing; |
|||
using UnityEngine; |
|||
using UnityEngine.MaterialGraph; |
|||
|
|||
namespace UnityEditor.MaterialGraph.Drawing |
|||
{ |
|||
class TextureAssetContolPresenter : GraphControlPresenter |
|||
{ |
|||
private string[] m_TextureTypeNames; |
|||
private string[] textureTypeNames |
|||
{ |
|||
get |
|||
{ |
|||
if (m_TextureTypeNames == null) |
|||
m_TextureTypeNames = Enum.GetNames(typeof(TextureType)); |
|||
return m_TextureTypeNames; |
|||
} |
|||
} |
|||
|
|||
public override void OnGUIHandler() |
|||
{ |
|||
base.OnGUIHandler(); |
|||
|
|||
var tNode = node as UnityEngine.MaterialGraph.TextureAssetNode; |
|||
if (tNode == null) |
|||
return; |
|||
|
|||
tNode.exposedState = (PropertyNode.ExposedState)EditorGUILayout.EnumPopup(new GUIContent("Exposed"), tNode.exposedState); |
|||
tNode.defaultTexture = EditorGUILayout.MiniThumbnailObjectField(new GUIContent("Texture"), tNode.defaultTexture, typeof(Texture2D), null) as Texture2D; |
|||
tNode.textureType = (TextureType)EditorGUILayout.Popup((int)tNode.textureType, textureTypeNames, EditorStyles.popup); |
|||
} |
|||
|
|||
public override float GetHeight() |
|||
{ |
|||
return 3 * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) + EditorGUIUtility.standardVerticalSpacing; |
|||
} |
|||
} |
|||
|
|||
[Serializable] |
|||
public class TextureAssetNodePresenter : MaterialNodePresenter |
|||
{ |
|||
protected override IEnumerable<GraphElementPresenter> GetControlData() |
|||
{ |
|||
var instance = CreateInstance<TextureAssetContolPresenter>(); |
|||
instance.Initialize(node); |
|||
return new List<GraphElementPresenter> { instance }; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: fea6f3daed9e37042828a425483c9a5c |
|||
timeCreated: 1495541485 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
SubShader |
|||
{ |
|||
${Tags} |
|||
${Blending} |
|||
${Culling} |
|||
${ZTest} |
|||
${ZWrite} |
|||
|
|||
LOD ${LOD} |
|||
|
|||
CGPROGRAM |
|||
#include "UnityCG.cginc" |
|||
//#include "AdvancedBRDF.cginc" |
|||
//#include "AdvancedShading.cginc" |
|||
//#include "AdvancedLighting.cginc" |
|||
|
|||
${MaterialID} |
|||
|
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Diffuse |
|||
|
|||
// From UE4 - Used for Cloth (Deprecated) |
|||
float3 Diffuse_Lambert(float3 DiffuseColor) |
|||
{ |
|||
return DiffuseColor * (1 / UNITY_PI); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Fresnel |
|||
|
|||
// From UE4 - Used for Cloth |
|||
// [Schlick 1994, "An Inexpensive BRDF Model for Physically-Based Rendering"] |
|||
float3 F_Schlick(float3 SpecularColor, float VoH) |
|||
{ |
|||
float Fc = Pow5(1 - VoH); // 1 sub, 3 mul |
|||
//return Fc + (1 - Fc) * SpecularColor; // 1 add, 3 mad |
|||
// Anything less than 2% is physically impossible and is instead considered to be shadowing |
|||
return saturate(50.0 * SpecularColor.g) * Fc + (1 - Fc) * SpecularColor; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Distribution |
|||
|
|||
// From UE4 - USed for Cloth |
|||
// GGX / Trowbridge-Reitz |
|||
// [Walter et al. 2007, "Microfacet models for refraction through rough surfaces"] |
|||
float D_GGX(float roughness, float NdotH) |
|||
{ |
|||
float a = roughness * roughness; |
|||
float a2 = a * a; |
|||
float d = (NdotH * a2 - NdotH) * NdotH + 1; // 2 mad |
|||
return a2 / (UNITY_PI*d*d); // 4 mul, 1 rcp |
|||
} |
|||
|
|||
// Anisotropic GGX |
|||
// Taken from HDRenderPipeline |
|||
float D_GGXAnisotropic(float TdotH, float BdotH, float NdotH, float roughnessT, float roughnessB) |
|||
{ |
|||
float f = TdotH * TdotH / (roughnessT * roughnessT) + BdotH * BdotH / (roughnessB * roughnessB) + NdotH * NdotH; |
|||
return 1.0 / (roughnessT * roughnessB * f * f); |
|||
} |
|||
|
|||
// From UE4 - Used for Cloth |
|||
float D_InvGGX(float roughness, float NdotH) |
|||
{ |
|||
float a = roughness * roughness; |
|||
float a2 = a * a; |
|||
float A = 4; |
|||
float d = (NdotH - a2 * NdotH) * NdotH + a2; |
|||
return 1/(UNITY_PI * (1 + A*a2)) * (1 + 4 * a2*a2 / (d*d)); //RCP |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Visibility |
|||
|
|||
// From UE4 - Used for Cloth |
|||
// Appoximation of joint Smith term for GGX |
|||
// [Heitz 2014, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs"] |
|||
float Vis_SmithJointApprox(float Roughness, float NoV, float NoL) |
|||
{ |
|||
float a = (Roughness*Roughness); |
|||
float Vis_SmithV = NoL * (NoV * (1 - a) + a); |
|||
float Vis_SmithL = NoV * (NoL * (1 - a) + a); |
|||
// Note: will generate NaNs with Roughness = 0. MinRoughness is used to prevent this |
|||
return 0.5 * 1/(Vis_SmithV + Vis_SmithL); //RCP |
|||
} |
|||
|
|||
// From UE4 - Used for Cloth |
|||
float Vis_Cloth(float NoV, float NoL) |
|||
{ |
|||
return 1/(4 * (NoL + NoV - NoL * NoV)); //RCP |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// SORT THESE |
|||
|
|||
// Smith Joint GGX Anisotropic Visibility |
|||
// Taken from https://cedec.cesa.or.jp/2015/session/ENG/14698.html |
|||
float SmithJointGGXAnisotropic(float TdotV, float BdotV, float NdotV, float TdotL, float BdotL, float NdotL, float roughnessT, float roughnessB) |
|||
{ |
|||
float aT = roughnessT; |
|||
float aT2 = aT * aT; |
|||
float aB = roughnessB; |
|||
float aB2 = aB * aB; |
|||
|
|||
float lambdaV = NdotL * sqrt(aT2 * TdotV * TdotV + aB2 * BdotV * BdotV + NdotV * NdotV); |
|||
float lambdaL = NdotV * sqrt(aT2 * TdotL * TdotL + aB2 * BdotL * BdotL + NdotL * NdotL); |
|||
|
|||
return 0.5 / (lambdaV + lambdaL); |
|||
} |
|||
|
|||
// Convert Anistropy to roughness |
|||
void ConvertAnisotropyToRoughness(float roughness, float anisotropy, out float roughnessT, out float roughnessB) |
|||
{ |
|||
// (0 <= anisotropy <= 1), therefore (0 <= anisoAspect <= 1) |
|||
// The 0.9 factor limits the aspect ratio to 10:1. |
|||
float anisoAspect = sqrt(1.0 - 0.9 * anisotropy); |
|||
roughnessT = roughness / anisoAspect; // Distort along tangent (rougher) |
|||
roughnessB = roughness * anisoAspect; // Straighten along bitangent (smoother) |
|||
} |
|||
|
|||
// Schlick Fresnel |
|||
float FresnelSchlick(float f0, float f90, float u) |
|||
{ |
|||
float x = 1.0 - u; |
|||
float x5 = x * x; |
|||
x5 = x5 * x5 * x; |
|||
return (f90 - f0) * x5 + f0; // sub mul mul mul sub mad |
|||
} |
|||
|
|||
//Clamp roughness |
|||
float ClampRoughnessForAnalyticalLights(float roughness) |
|||
{ |
|||
return max(roughness, 0.000001); |
|||
} |
|||
|
|||
//Calculate tangent warp for IBL (Reference Version - not used) |
|||
float3 SpecularGGXIBLRef(float3 viewDir, float3 normalDir, float3 tangentDir, float3 bitangentDir, float roughnessT, float roughnessB) |
|||
{ |
|||
return float3(1, 1, 1); |
|||
//Hidden in UnityAnisotropicLighting.cginc |
|||
} |
|||
|
|||
// Sample Anisotropic Direction for IBL (Reference Version - not used) |
|||
void SampleAnisoGGXDir(float2 u, float3 viewDir, float3 normalDir, float3 tangent, float3 bitangent, float roughnessT, float roughnessB, out float3 halfDir, out float3 lightDir) |
|||
{ |
|||
// AnisoGGX NDF sampling |
|||
halfDir = sqrt(u.x / (1.0 - u.x)) * (roughnessT * cos((UNITY_PI * 2) * u.y) * tangent + roughnessB * sin((UNITY_PI * 2) * u.y) * bitangent) + normalDir; |
|||
halfDir = normalize(halfDir); |
|||
|
|||
// Convert sample from half angle to incident angle |
|||
lightDir = 2.0 * saturate(dot(viewDir, halfDir)) * halfDir - viewDir; |
|||
} |
|||
|
|||
// Ref: Donald Revie - Implementing Fur Using Deferred Shading (GPU Pro 2) |
|||
// The grain direction (e.g. hair or brush direction) is assumed to be orthogonal to the normal. |
|||
// The returned normal is NOT normalized. |
|||
float3 ComputeGrainNormal(float3 grainDir, float3 V) |
|||
{ |
|||
float3 B = cross(-V, grainDir); |
|||
return cross(B, grainDir); |
|||
} |
|||
|
|||
//Modify Normal for Anisotropic IBL (Realtime version) |
|||
// Fake anisotropic by distorting the normal. |
|||
// The grain direction (e.g. hair or brush direction) is assumed to be orthogonal to N. |
|||
// Anisotropic ratio (0->no isotropic; 1->full anisotropy in tangent direction) |
|||
float3 GetAnisotropicModifiedNormal(float3 grainDir, float3 N, float3 V, float anisotropy) |
|||
{ |
|||
float3 grainNormal = ComputeGrainNormal(grainDir, V); |
|||
// TODO: test whether normalizing 'grainNormal' is worth it. |
|||
return normalize(lerp(N, grainNormal, anisotropy)); |
|||
} |
|||
|
|||
/// REGION END - ANISOTROPY |
|||
|
|||
/// REGION START - SUBSURFACE SCATTERING |
|||
|
|||
half Fresnel(half3 H, half3 V, half F0) |
|||
{ |
|||
half base = 1.0 - dot(V, H); |
|||
half exponential = pow(base, 5.0); |
|||
return exponential + F0 * (1.0 - exponential); |
|||
} |
|||
/* |
|||
inline half3 KelemenSzirmayKalosSpecular(half3 normal, half3 lightDir, half3 viewDir, float roughness, float rho_s) |
|||
{ |
|||
half3 result = half3(0, 0, 0); |
|||
half NdotL = dot(normal, lightDir); |
|||
if (NdotL > 0.0) |
|||
{ |
|||
half3 h = lightDir + viewDir; |
|||
half3 H = normalize(h); |
|||
half NdotH = dot(normal, H); |
|||
half PH = pow(2.0 * tex2D(_BeckmannPrecomputedTex, half2(NdotH, roughness)).r, 10.0); |
|||
half F = Fresnel(H, viewDir, 0.028); |
|||
half frSpec = max(PH * F / dot(h, h), 0); |
|||
half term = NdotL * rho_s * frSpec; |
|||
result = half3(term, term, term); |
|||
} |
|||
return result; |
|||
}*/ |
|||
/* |
|||
half3 SkinDiffuse(float curv, float3 NdotL) |
|||
{ |
|||
float3 lookup = NdotL * 0.5 + 0.5; |
|||
float3 diffuse; |
|||
|
|||
diffuse.r = tex2D(_DiffusionProfileTexture, float2(lookup.r, curv)).r; |
|||
diffuse.g = tex2D(_DiffusionProfileTexture, float2(lookup.g, curv)).g; |
|||
diffuse.b = tex2D(_DiffusionProfileTexture, float2(lookup.b, curv)).b; |
|||
|
|||
return diffuse; |
|||
}*/ |
|||
|
|||
/// REGION END - SUBSURFACE SCATTERING |
|||
|
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_CLEARCOAT' with 'defined (SHADINGMODELID_CLEARCOAT)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_CLOTH' with 'defined (SHADINGMODELID_CLOTH)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_EYE' with 'defined (SHADINGMODELID_EYE)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_FOLIAGE' with 'defined (SHADINGMODELID_FOLIAGE)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_HAIR' with 'defined (SHADINGMODELID_HAIR)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_SKIN' with 'defined (SHADINGMODELID_SKIN)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_SUBSURFACE' with 'defined (SHADINGMODELID_SUBSURFACE)' |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Shading models |
|||
|
|||
//#pragma multi_compile SHADINGMODELID_UNLIT SHADINGMODELID_STANDARD SHADINGMODELID_SUBSURFACE SHADINGMODELID_SKIN SHADINGMODELID_FOLIAGE SHADINGMODELID_CLEARCOAT SHADINGMODELID_CLOTH SHADINGMODELID_EYE |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Input |
|||
|
|||
half _ShadingModel; |
|||
|
|||
sampler2D _AnisotropyMap; |
|||
half _Anisotropy; |
|||
sampler2D _TangentMap; |
|||
|
|||
half4 _TranslucentColor; |
|||
sampler2D _TranslucencyMap; |
|||
|
|||
sampler2D _FuzzTex; |
|||
half3 _FuzzColor; |
|||
half _Cloth; |
|||
|
|||
sampler2D _IrisNormal; |
|||
sampler2D _IrisMask; |
|||
half _IrisDistance; |
|||
|
|||
half _TDistortion; |
|||
half _TScale; |
|||
half _TAmbient; |
|||
half _TPower; |
|||
half _TAttenuation; |
|||
half _TransmissionOverallStrength; |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Maths helpers |
|||
|
|||
// Octahedron Normal Vectors |
|||
// [Cigolle 2014, "A Survey of Efficient Representations for Independent Unit Vectors"] |
|||
// Mean Max |
|||
// oct 8:8 0.33709 0.94424 |
|||
// snorm 8:8:8 0.17015 0.38588 |
|||
// oct 10:10 0.08380 0.23467 |
|||
// snorm 10:10:10 0.04228 0.09598 |
|||
// oct 12:12 0.02091 0.05874 |
|||
|
|||
float2 UnitVectorToOctahedron(float3 N) |
|||
{ |
|||
N.xy /= dot(float3(1,1,1), abs(N)); |
|||
if (N.z <= 0) |
|||
{ |
|||
N.xy = (1 - abs(N.yx)) * (N.xy >= 0 ? float2(1, 1) : float2(-1, -1)); |
|||
} |
|||
return N.xy; |
|||
} |
|||
|
|||
float3 OctahedronToUnitVector(float2 Oct) |
|||
{ |
|||
float3 N = float3(Oct, 1 - dot(float2(1,1), abs(Oct))); |
|||
if (N.z < 0) |
|||
{ |
|||
N.xy = (1 - abs(N.yx)) * (N.xy >= 0 ? float2(1, 1) : float2(-1, -1)); |
|||
} |
|||
return float3(1, 1, 1); |
|||
return normalize(N); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Surface helpers |
|||
|
|||
half Anisotropy(float2 uv) |
|||
{ |
|||
return tex2D(_AnisotropyMap, uv) * _Anisotropy; |
|||
} |
|||
|
|||
half3 Fuzz(float2 uv) |
|||
{ |
|||
return tex2D(_FuzzTex, uv) * _FuzzColor; |
|||
} |
|||
|
|||
half Cloth() |
|||
{ |
|||
return _Cloth; |
|||
} |
|||
|
|||
half4 Iris(float2 uv) |
|||
{ |
|||
float2 n = UnitVectorToOctahedron(normalize(UnpackNormal(tex2D(_IrisNormal, uv)).rgb)) * 0.5 + 0.5; |
|||
float m = saturate(tex2D(_IrisMask, uv).r); // Iris Mask |
|||
float d = saturate(_IrisDistance); // Iris Distance |
|||
return float4(n.x, n.y, m, d); |
|||
} |
|||
|
|||
half3 Translucency(float2 uv) |
|||
{ |
|||
return tex2D(_TranslucencyMap, uv).rgb * _TranslucentColor.rgb; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Unlit Shading Function |
|||
|
|||
float4 UnlitShading(float3 diffColor) |
|||
{ |
|||
return half4(diffColor, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Standard Shading Function |
|||
|
|||
float4 StandardShading(float3 diffColor, float3 specColor, float oneMinusReflectivity, float smoothness, float3 normal, float3x3 worldVectors, |
|||
float anisotropy, float metallic, float3 viewDir, UnityLight light, UnityIndirect gi) |
|||
{ |
|||
//Unpack world vectors |
|||
float3 tangent = worldVectors[0]; |
|||
float3 bitangent = worldVectors[1]; |
|||
//Normal shift |
|||
float shiftAmount = dot(normal, viewDir); |
|||
normal = shiftAmount < 0.0f ? normal + viewDir * (-shiftAmount + 1e-5f) : normal; |
|||
//Regular vectors |
|||
float NdotL = saturate(dot(normal, light.dir)); //sat? |
|||
float NdotV = abs(dot(normal, viewDir)); //abs? |
|||
float LdotV = dot(light.dir, viewDir); |
|||
float3 H = Unity_SafeNormalize(light.dir + viewDir); |
|||
float invLenLV = rsqrt(abs(2 + 2 * normalize(LdotV))); |
|||
//float invLenLV = rsqrt(abs(2 + 2 * LdotV)); |
|||
//float NdotH = (NdotL + normalize(NdotV)) * invLenLV; |
|||
float NdotH = saturate(dot(normal, H)); |
|||
//float NdotH = saturate((NdotL + normalize(NdotV)) * invLenLV); |
|||
//float H = (light.dir + viewDir) * invLenLV; |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
//Tangent vectors |
|||
float TdotH = dot(tangent, H); |
|||
float TdotL = dot(tangent, light.dir); |
|||
float BdotH = dot(bitangent, H); |
|||
float BdotL = dot(bitangent, light.dir); |
|||
float TdotV = dot(viewDir, tangent); |
|||
float BdotV = dot(viewDir, bitangent); |
|||
//Fresnels |
|||
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
float3 F = FresnelLerp(specColor, grazingTerm, NdotV); //Original Schlick - Replace from SRP? |
|||
//float3 fresnel0 = lerp(specColor, diffColor, metallic); |
|||
//float3 F = FresnelSchlick(fresnel0, 1.0, LdotH); |
|||
//Calculate roughness |
|||
float roughnessT; |
|||
float roughnessB; |
|||
float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
ConvertAnisotropyToRoughness(roughness, anisotropy, roughnessT, roughnessB); |
|||
//Clamp roughness |
|||
//roughness = ClampRoughnessForAnalyticalLights(roughness); |
|||
roughnessT = ClampRoughnessForAnalyticalLights(roughnessT); |
|||
roughnessB = ClampRoughnessForAnalyticalLights(roughnessB); |
|||
//Visibility & Distribution terms |
|||
float V = SmithJointGGXAnisotropic(TdotV, BdotV, NdotV, TdotL, BdotL, NdotL, roughnessT, roughnessB); |
|||
float D = D_GGXAnisotropic(TdotH, BdotH, NdotH, roughnessT, roughnessB); |
|||
//Specular term |
|||
float3 specularTerm = V * D; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm = sqrt(max(1e-4h, specularTerm)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
specularTerm = max(0, specularTerm * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm = 0.0; |
|||
#endif |
|||
//Diffuse term |
|||
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, perceptualRoughness) * NdotL;// - Need this NdotL multiply? |
|||
//Reduction |
|||
half surfaceReduction; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness; // 1-0.28*x^3 as approximation for (1/(x^4+1))^(1/2.2) on the domain [0;1] |
|||
# else |
|||
surfaceReduction = 1.0 / (roughness*roughness + 1.0); // fade \in [0.5;1] |
|||
# endif |
|||
//Final |
|||
half3 color = (diffColor * (gi.diffuse + light.color * diffuseTerm)) |
|||
+ specularTerm * light.color * FresnelTerm(specColor, LdotH) |
|||
+ surfaceReduction * gi.specular * FresnelLerp(specColor, grazingTerm, NdotV); |
|||
return half4(color, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Cloth Shading Function |
|||
|
|||
//float3 ClothShading(FGBufferData GBuffer, float3 LobeRoughness, float3 LobeEnergy, float3 L, float3 V, half3 N) |
|||
float4 ClothShading(float3 diffColor, float3 specColor, float3 fuzzColor, float cloth, float oneMinusReflectivity, float smoothness, float3 normal, float3 viewDir, UnityLight light, UnityIndirect gi, float3x3 worldVectors, float anisotropy) |
|||
{ |
|||
const float3 FuzzColor = saturate(fuzzColor); |
|||
const float Cloth = saturate(cloth); |
|||
|
|||
//Regular vectors |
|||
float NdotL = saturate(dot(normal, light.dir)); //sat? |
|||
float NdotV = abs(dot(normal, viewDir)); //abs? |
|||
float LdotV = dot(light.dir, viewDir); |
|||
//float invLenLV = rsqrt(abs(2 + 2 * normalize(LdotV))); |
|||
////float invLenLV = rsqrt(abs(2 + 2 * LdotV)); |
|||
//float NdotH = (NdotL + normalize(NdotV)) * invLenLV; |
|||
//float NdotH = saturate((NdotL + normalize(NdotV)) * invLenLV); |
|||
float3 H = Unity_SafeNormalize(light.dir + viewDir); |
|||
//float H = (light.dir + viewDir) * invLenLV; |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
//float3 H = normalize(viewDir + light.dir); |
|||
//float NdotL = saturate(dot(normal, light.dir)); |
|||
//float NdotV = saturate(abs(dot(normal, viewDir)) + 1e-5); |
|||
float NdotH = saturate(dot(normal, H)); |
|||
float VdotH = saturate(dot(viewDir, H)); |
|||
//float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
|
|||
// Diffuse |
|||
float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, perceptualRoughness) * NdotL;// - Need this NdotL multiply? |
|||
|
|||
// Cloth - Asperity Scattering - Inverse Beckmann Layer |
|||
float3 F1 = FresnelTerm(fuzzColor, LdotH);// FresnelLerp(fuzzColor, grazingTerm, NdotV);// FresnelTerm(FuzzColor, LdotH);// F_Schlick(FuzzColor, VdotH); |
|||
float D1 = D_InvGGX(roughness, NdotH); |
|||
float V1 = Vis_Cloth(NdotV, NdotL); |
|||
//Specular term |
|||
float3 specularTerm1 = V1 * D1; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm1 = sqrt(max(1e-4h, specularTerm1)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
// specularTerm1 = max(0, specularTerm1 * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm1 = 0.0; |
|||
#endif |
|||
float3 Spec1 = specularTerm1 * light.color * FresnelTerm(fuzzColor, LdotH); |
|||
|
|||
// Generalized microfacet specular |
|||
/*float3 F2 = F_Schlick(specColor, VdotH); |
|||
float D2 = D_GGX(roughness, NdotH); |
|||
float V2 = Vis_SmithJointApprox(roughness, NdotV, NdotL); |
|||
float3 Spec2 = D2 * V2 * F2 * light.color;*/ |
|||
|
|||
//Unpack world vectors |
|||
float3 tangent = worldVectors[0]; |
|||
float3 bitangent = worldVectors[1]; |
|||
//Tangent vectors |
|||
float TdotH = dot(tangent, H); |
|||
float TdotL = dot(tangent, light.dir); |
|||
float BdotH = dot(bitangent, H); |
|||
float BdotL = dot(bitangent, light.dir); |
|||
float TdotV = dot(viewDir, tangent); |
|||
float BdotV = dot(viewDir, bitangent); |
|||
//Fresnels |
|||
float3 F2 = FresnelLerp(specColor, grazingTerm, NdotV);// FresnelTerm(specColor, LdotH);// FresnelLerp(specColor, grazingTerm, NdotV); //Original Schlick - Replace from SRP? |
|||
float roughnessT; |
|||
float roughnessB; |
|||
//float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
//float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
ConvertAnisotropyToRoughness(roughness, anisotropy, roughnessT, roughnessB); |
|||
//Clamp roughness |
|||
//roughness = ClampRoughnessForAnalyticalLights(roughness); |
|||
roughnessT = ClampRoughnessForAnalyticalLights(roughnessT); |
|||
roughnessB = ClampRoughnessForAnalyticalLights(roughnessB); |
|||
//Visibility & Distribution terms |
|||
float V2 = SmithJointGGXAnisotropic(TdotV, BdotV, NdotV, TdotL, BdotL, NdotL, roughnessT, roughnessB); |
|||
float D2 = D_GGXAnisotropic(TdotH, BdotH, NdotH, roughnessT, roughnessB); |
|||
//Specular term |
|||
float3 specularTerm2 = V2 * D2; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm2 = sqrt(max(1e-4h, specularTerm2)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
specularTerm2 = max(0, specularTerm2 * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm2 = 0.0; |
|||
#endif |
|||
float3 Spec2 = specularTerm2 * light.color * FresnelTerm(specColor, LdotH); |
|||
|
|||
float3 Spec = lerp(Spec2, Spec1, Cloth); |
|||
|
|||
//Reduction |
|||
half surfaceReduction; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness; // 1-0.28*x^3 as approximation for (1/(x^4+1))^(1/2.2) on the domain [0;1] |
|||
# else |
|||
surfaceReduction = 1.0 / (roughness*roughness + 1.0); // fade \in [0.5;1] |
|||
# endif |
|||
//Final |
|||
//half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
half3 color = (diffColor * (gi.diffuse + light.color * diffuseTerm)) |
|||
+ Spec |
|||
+ surfaceReduction * gi.specular * FresnelLerp(specColor, grazingTerm, NdotV); |
|||
return half4(color, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Eye Shading Function |
|||
|
|||
//float3 EyeShading(FGBufferData GBuffer, float3 LobeRoughness, float3 LobeEnergy, float3 L, float3 V, half3 N) |
|||
float4 EyeShading(float3 diffColor, float3 specColor, float3 viewDir, half3 normal, float smoothness, float oneMinusReflectivity, UnityLight light, UnityIndirect gi) |
|||
{ |
|||
float3 H = normalize(viewDir + light.dir); |
|||
float NdotL = saturate(dot(normal, light.dir)); |
|||
float NdotV = saturate(abs(dot(normal, viewDir)) + 1e-5); |
|||
float NdotH = saturate(dot(normal, H)); |
|||
float VdotH = saturate(dot(viewDir, H)); |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
// Generalized microfacet specular |
|||
float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
|
|||
float D = D_GGX(roughness, NdotH);// *LobeEnergy[1]; |
|||
float V = Vis_SmithJointApprox(roughness, NdotV, NdotL); |
|||
float3 F = F_Schlick(specColor, VdotH); |
|||
|
|||
float3 specularTerm = V * D; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm = sqrt(max(1e-4h, specularTerm)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
specularTerm = max(0, specularTerm * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm = 0.0; |
|||
#endif |
|||
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
half surfaceReduction; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness; // 1-0.28*x^3 as approximation for (1/(x^4+1))^(1/2.2) on the domain [0;1] |
|||
# else |
|||
surfaceReduction = 1.0 / (roughness*roughness + 1.0); // fade \in [0.5;1] |
|||
# endif |
|||
|
|||
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, perceptualRoughness) * NdotL; // TODO - Unreal does not apply diffuse in Shading function |
|||
//Final |
|||
half3 color = (diffColor * (gi.diffuse + light.color * diffuseTerm)) |
|||
+ specularTerm * light.color * FresnelTerm(specColor, LdotH) |
|||
+ surfaceReduction * gi.specular * FresnelLerp(specColor, grazingTerm, NdotV); |
|||
return half4(color, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Subsurface Shading Function |
|||
|
|||
float3 SubsurfaceShadingSimple(float3 diffColor, float3 normal, float3 viewDir, float3 thickness, UnityLight light) |
|||
{ |
|||
half3 vLTLight = light.dir + normal * _TDistortion; |
|||
half fLTDot = pow(saturate(dot(viewDir, -vLTLight)), _TPower) * _TScale; |
|||
half3 fLT = _TAttenuation * (fLTDot + _TAmbient) * (thickness); |
|||
return diffColor * ((light.color * fLT) * _TransmissionOverallStrength); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Eye Subsurface Shading Function |
|||
|
|||
//float3 EyeSubsurfaceShading(FGBufferData GBuffer, float3 L, float3 V, half3 N) |
|||
float3 EyeSubsurfaceShading(float3 diffColor, float3 specColor, float3 viewDir, half3 normal, float smoothness, float4 iris, UnityLight light) |
|||
{ |
|||
float2 irisNormal = iris.rg; |
|||
float irisMask = iris.z; |
|||
float irisDistance = iris.w; |
|||
|
|||
float3 H = normalize(viewDir + light.dir); |
|||
float VdotH = saturate(dot(viewDir, H)); |
|||
float NdotV = saturate(abs(dot(normal, viewDir)) + 1e-5); |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
// F_Schlick |
|||
//float F0 = GBuffer.Specular * 0.08; |
|||
//float Fc = Pow5(1 - VoH); |
|||
//float F = Fc + (1 - Fc) * F0; |
|||
float3 fresnel0 = lerp(specColor, diffColor, smoothness); |
|||
float3 F = FresnelSchlick(fresnel0, 1.0, LdotH); |
|||
|
|||
//float IrisDistance = GBuffer.CustomData.w; |
|||
//float IrisMask = GBuffer.CustomData.z; |
|||
|
|||
float3 IrisNormal; |
|||
IrisNormal = OctahedronToUnitVector(irisNormal * 2 - 1); |
|||
|
|||
// Blend in the negative intersection normal to create some concavity |
|||
// Not great as it ties the concavity to the convexity of the cornea surface |
|||
// No good justification for that. On the other hand, if we're just looking to |
|||
// introduce some concavity, this does the job. |
|||
float3 CausticNormal = normalize(lerp(IrisNormal, -normal, irisMask*irisDistance)); |
|||
|
|||
float NdotL = saturate(dot(IrisNormal, light.dir)); |
|||
float Power = lerp(12, 1, NdotL); |
|||
float Caustic = 0.6 + 0.2 * (Power + 1) * pow(saturate(dot(CausticNormal, light.dir)), Power); |
|||
float Iris = NdotL * Caustic; |
|||
|
|||
// http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ |
|||
float Wrap = 0.15; |
|||
float Sclera = saturate((dot(normal, light.dir) + Wrap) / (1 + Wrap) * (1 + Wrap)); |
|||
|
|||
return (1 - F) * lerp(Sclera, Iris, irisMask) * diffColor / UNITY_PI; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Shading function selectors |
|||
|
|||
//float3 SurfaceShading(/*FGBufferData GBuffer,*/ float3 LobeRoughness, float3 LobeEnergy, float3 L, float3 V, half3 N, uint2 Random) |
|||
float4 SurfaceShading(float3 diffColor, float3 specColor, float oneMinusReflectivity, float smoothness, float3 normal, |
|||
float3x3 worldVectors, float anisotropy, float4 customData, float metallic, float3 viewDir, UnityLight light, UnityIndirect gi) |
|||
{ |
|||
#if defined(SHADINGMODELID_UNLIT) |
|||
{ |
|||
return UnlitShading(diffColor); |
|||
} |
|||
#elif defined(SHADINGMODELID_STANDARD) || defined(SHADINGMODELID_SUBSURFACE) || defined(SHADINGMODELID_SKIN) || defined(SHADINGMODELID_FOLIAGE) |
|||
{ |
|||
return StandardShading(diffColor, specColor, oneMinusReflectivity, smoothness, |
|||
normal, worldVectors, anisotropy, metallic, viewDir, light, gi); |
|||
} |
|||
#elif defined (SHADINGMODELID_CLEARCOAT) |
|||
{ |
|||
return float4(1, 1, 1, 1); //ClearCoatShading(GBuffer, LobeRoughness, LobeEnergy, L, V, N); |
|||
} |
|||
#elif defined (SHADINGMODELID_CLOTH) |
|||
{ |
|||
return ClothShading(diffColor, specColor, customData.rgb, customData.a, oneMinusReflectivity, smoothness, normal, viewDir, light, gi, worldVectors, anisotropy); |
|||
} |
|||
#elif defined (SHADINGMODELID_EYE) |
|||
{ |
|||
return EyeShading(diffColor, specColor, viewDir, normal, smoothness, oneMinusReflectivity, light, gi); //EyeShading(GBuffer, LobeRoughness, LobeEnergy, L, V, N); |
|||
} |
|||
#endif |
|||
return float4(0, 0, 0, 0); |
|||
} |
|||
|
|||
//float3 SubsurfaceShading(/*FGBufferData GBuffer,*/ float3 L, float3 V, half3 N, float Shadow, uint2 Random) |
|||
float3 SubsurfaceShading(float3 diffColor, float3 specColor, float3 normal, float smoothness, float3 viewDir, float4 customData, UnityLight light) |
|||
{ |
|||
#if defined (SHADINGMODELID_SUBSURFACE) |
|||
{ |
|||
return SubsurfaceShadingSimple(diffColor, normal, viewDir, customData.rgb, light); |
|||
} |
|||
#elif defined (SHADINGMODELID_SKIN) |
|||
{ |
|||
return float3(0, 0, 0); //SubsurfaceShadingPreintegratedSkin(GBuffer, L, V, N); |
|||
} |
|||
#elif defined (SHADINGMODELID_FOLIAGE) |
|||
{ |
|||
return float3(0, 0, 0); //SubsurfaceShadingTwoSided(SubsurfaceColor, L, V, N); |
|||
} |
|||
#elif defined (SHADINGMODELID_HAIR) |
|||
{ |
|||
return float3(0, 0, 0); //HairShading(GBuffer, L, V, N, Shadow, 1, 0, Random); |
|||
} |
|||
#elif defined (SHADINGMODELID_EYE) |
|||
{ |
|||
return EyeSubsurfaceShading(diffColor, specColor, viewDir, normal, smoothness, customData, light); //EyeSubsurfaceShading(GBuffer, L, V, N); |
|||
} |
|||
#endif |
|||
return float3(0, 0, 0); |
|||
} |
|||
|
|||
//#endif UNITY_ADVANCED_SHADINGMODELS_INCLUDED |
|||
|
|||
//------------------------------------------------------------------------------------- |
|||
// Lighting Helpers |
|||
|
|||
// Glossy Environment |
|||
half3 Unity_AnisotropicGlossyEnvironment(UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn, half anisotropy) //Reference IBL from HD Pipe (Add half3 L input and replace R) |
|||
{ |
|||
half perceptualRoughness = glossIn.roughness /* perceptualRoughness */; |
|||
|
|||
// TODO: CAUTION: remap from Morten may work only with offline convolution, see impact with runtime convolution! |
|||
// For now disabled |
|||
#if 0 |
|||
float m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the real roughness parameter |
|||
const float fEps = 1.192092896e-07F; // smallest such that 1.0+FLT_EPSILON != 1.0 (+1e-4h is NOT good here. is visibly very wrong) |
|||
float n = (2.0 / max(fEps, m*m)) - 2.0; // remap to spec power. See eq. 21 in --> https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf |
|||
|
|||
n /= 4; // remap from n_dot_h formulatino to n_dot_r. See section "Pre-convolved Cube Maps vs Path Tracers" --> https://s3.amazonaws.com/docs.knaldtech.com/knald/1.0.0/lys_power_drops.html |
|||
|
|||
perceptualRoughness = pow(2 / (n + 2), 0.25); // remap back to square root of real roughness (0.25 include both the sqrt root of the conversion and sqrt for going from roughness to perceptualRoughness) |
|||
#else |
|||
// MM: came up with a surprisingly close approximation to what the #if 0'ed out code above does. |
|||
perceptualRoughness = perceptualRoughness*(1.7 - 0.7*perceptualRoughness); |
|||
#endif |
|||
|
|||
|
|||
half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness); |
|||
half3 R = glossIn.reflUVW;// -half3(anisotropy, 0, 0); |
|||
half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip); |
|||
|
|||
return DecodeHDR(rgbm, hdr); |
|||
} |
|||
|
|||
// Indirect Specular |
|||
inline half3 UnityGI_AnisotropicIndirectSpecular(UnityGIInput data, half occlusion, Unity_GlossyEnvironmentData glossIn, half anisotropy, half3x3 worldVectors) |
|||
{ |
|||
half3 specular; |
|||
float3 tangentX = worldVectors[0]; |
|||
float3 tangentY = worldVectors[1]; |
|||
float3 N = worldVectors[2]; |
|||
float3 V = data.worldViewDir; |
|||
float3 iblNormalWS = GetAnisotropicModifiedNormal(tangentY, N, V, anisotropy); |
|||
float3 iblR = reflect(-V, iblNormalWS); |
|||
|
|||
#ifdef UNITY_SPECCUBE_BOX_PROJECTION |
|||
// we will tweak reflUVW in glossIn directly (as we pass it to Unity_GlossyEnvironment twice for probe0 and probe1), so keep original to pass into BoxProjectedCubemapDirection |
|||
|
|||
half3 originalReflUVW = glossIn.reflUVW; |
|||
glossIn.reflUVW = BoxProjectedCubemapDirection(iblR, data.worldPos, data.probePosition[0], data.boxMin[0], data.boxMax[0]); |
|||
#endif |
|||
|
|||
#ifdef _GLOSSYREFLECTIONS_OFF |
|||
specular = unity_IndirectSpecColor.rgb; |
|||
#else |
|||
half3 env0 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), data.probeHDR[0], glossIn, anisotropy); |
|||
//half3 env0 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), data.probeHDR[0], glossIn, anisotropy, L); //Reference IBL from HD Pipe |
|||
#ifdef UNITY_SPECCUBE_BLENDING |
|||
const float kBlendFactor = 0.99999; |
|||
float blendLerp = data.boxMin[0].w; |
|||
UNITY_BRANCH |
|||
if (blendLerp < kBlendFactor) |
|||
{ |
|||
#ifdef UNITY_SPECCUBE_BOX_PROJECTION |
|||
glossIn.reflUVW = BoxProjectedCubemapDirection(iblR, data.worldPos, data.probePosition[1], data.boxMin[1], data.boxMax[1]); |
|||
#endif |
|||
half3 env1 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0), data.probeHDR[1], glossIn, anisotropy); |
|||
//half3 env1 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0), data.probeHDR[1], glossIn, anisotropy, L); //Reference IBL from HD Pipe |
|||
specular = lerp(env1, env0, blendLerp); |
|||
} |
|||
else |
|||
{ |
|||
specular = env0; |
|||
} |
|||
#else |
|||
specular = env0; |
|||
#endif |
|||
#endif |
|||
|
|||
return specular * occlusion;// *weightOverPdf; //Reference IBL from HD Pipe |
|||
//return specular * occlusion * weightOverPdf; //Reference IBL from HD Pipe |
|||
} |
|||
|
|||
// Global Illumination |
|||
inline UnityGI UnityAnisotropicGlobalIllumination(UnityGIInput data, half occlusion, half3 normalWorld, Unity_GlossyEnvironmentData glossIn, half anisotropy, half3x3 worldVectors) |
|||
{ |
|||
UnityGI o_gi = UnityGI_Base(data, occlusion, normalWorld); |
|||
o_gi.indirect.specular = UnityGI_AnisotropicIndirectSpecular(data, occlusion, glossIn, anisotropy, worldVectors); |
|||
return o_gi; |
|||
} |
|||
|
|||
//------------------------------------------------------------------------------------- |
|||
// Lighting Functions |
|||
|
|||
//Surface Description |
|||
struct SurfaceOutputAdvanced |
|||
{ |
|||
fixed3 Albedo; // base (diffuse or specular) color |
|||
fixed3 Normal; // tangent space normal, if written |
|||
half3 Emission; |
|||
half Metallic; // 0=non-metal, 1=metal |
|||
// Smoothness is the user facing name, it should be perceptual smoothness but user should not have to deal with it. |
|||
// Everywhere in the code you meet smoothness it is perceptual smoothness |
|||
half Smoothness; // 0=rough, 1=smooth |
|||
half Occlusion; // occlusion (default 1) |
|||
fixed Alpha; // alpha for transparencies |
|||
half3 Tangent; |
|||
half Anisotropy; |
|||
half4 CustomData; |
|||
float3x3 WorldVectors; |
|||
//half ShadingModel; |
|||
}; |
|||
|
|||
inline half4 LightingAdvanced(SurfaceOutputAdvanced s, half3 viewDir, UnityGI gi) |
|||
{ |
|||
s.Normal = normalize(s.Normal); |
|||
|
|||
half oneMinusReflectivity; |
|||
half3 specColor; |
|||
s.Albedo = DiffuseAndSpecularFromMetallic(s.Albedo, s.Metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity); |
|||
|
|||
// shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha) |
|||
// this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha |
|||
half outputAlpha; |
|||
s.Albedo = PreMultiplyAlpha(s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha); |
|||
|
|||
half4 c = SurfaceShading(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, s.WorldVectors, s.Anisotropy, s.CustomData, s.Metallic, viewDir, gi.light, gi.indirect); |
|||
c.rgb += SubsurfaceShading(s.Albedo, specColor, s.Normal, s.Smoothness, viewDir, s.CustomData, gi.light); |
|||
|
|||
//c.rgb += UNITY_BRDF_GI(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, s.Occlusion, gi); |
|||
c.a = outputAlpha; |
|||
return c; |
|||
} |
|||
|
|||
//This is pointless as always forward? |
|||
inline half4 LightingAdvanced_Deferred(SurfaceOutputAdvanced s, half3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2) |
|||
{ |
|||
half oneMinusReflectivity; |
|||
half3 specColor; |
|||
s.Albedo = DiffuseAndSpecularFromMetallic(s.Albedo, s.Metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity); |
|||
|
|||
half4 c = SurfaceShading(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, s.WorldVectors, s.Anisotropy, s.CustomData, s.Metallic, viewDir, gi.light, gi.indirect); |
|||
c.rgb += SubsurfaceShading(s.Albedo, specColor, s.Normal, s.Smoothness, viewDir, s.CustomData, gi.light); |
|||
|
|||
UnityStandardData data; |
|||
data.diffuseColor = s.Albedo; |
|||
data.occlusion = s.Occlusion; |
|||
data.specularColor = specColor; |
|||
data.smoothness = s.Smoothness; |
|||
data.normalWorld = s.Normal; |
|||
|
|||
UnityStandardDataToGbuffer(data, outGBuffer0, outGBuffer1, outGBuffer2); |
|||
|
|||
half4 emission = half4(s.Emission + c.rgb, 1); |
|||
return emission; |
|||
} |
|||
|
|||
inline void LightingAdvanced_GI(SurfaceOutputAdvanced s, UnityGIInput data, inout UnityGI gi) |
|||
{ |
|||
#if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS |
|||
gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal); |
|||
#else |
|||
Unity_GlossyEnvironmentData g = UnityGlossyEnvironmentSetup(s.Smoothness, data.worldViewDir, s.Normal, lerp(unity_ColorSpaceDielectricSpec.rgb, s.Albedo, s.Metallic)); |
|||
gi = UnityAnisotropicGlobalIllumination(data, s.Occlusion, s.Normal, g, s.Anisotropy, s.WorldVectors); |
|||
#endif |
|||
} |
|||
|
|||
|
|||
///END |
|||
|
|||
|
|||
#pragma target 5.0 |
|||
#pragma surface surf ${LightingFunctionName} ${VertexShaderDecl} |
|||
#pragma glsl |
|||
#pragma debug |
|||
|
|||
${ShaderFunctions} |
|||
${ShaderPropertyUsages} |
|||
|
|||
struct Input |
|||
{ |
|||
${ShaderInputs} |
|||
}; |
|||
|
|||
void vert (inout appdata_full v, out Input o) |
|||
{ |
|||
UNITY_INITIALIZE_OUTPUT(Input,o); |
|||
${VertexShaderBody} |
|||
} |
|||
|
|||
void surf (Input IN, inout ${SurfaceOutputStructureName} o) |
|||
{ |
|||
${PixelShaderBody} |
|||
} |
|||
ENDCG |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f508073728247984aaa6da15ea564f6e |
|||
timeCreated: 1495550333 |
|||
licenseType: Pro |
|||
DefaultImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("Procedural/Box")] |
|||
public class BoxNode : Function3Input, IGeneratesFunction |
|||
{ |
|||
public BoxNode() |
|||
{ |
|||
name = "Box"; |
|||
} |
|||
|
|||
protected override string GetFunctionName() |
|||
{ |
|||
return "unity_boxnode_" + precision; |
|||
} |
|||
|
|||
protected override string GetInputSlot1Name() |
|||
{ |
|||
return "X and Y"; |
|||
} |
|||
|
|||
protected override string GetInputSlot2Name() |
|||
{ |
|||
return "X Min and Max"; |
|||
} |
|||
|
|||
protected override string GetInputSlot3Name() |
|||
{ |
|||
return "Y Min and Max"; |
|||
} |
|||
|
|||
protected override MaterialSlot GetInputSlot1() |
|||
{ |
|||
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector3.zero); |
|||
} |
|||
|
|||
protected override MaterialSlot GetInputSlot2() |
|||
{ |
|||
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector3.zero); |
|||
} |
|||
|
|||
protected override MaterialSlot GetInputSlot3() |
|||
{ |
|||
return new MaterialSlot(InputSlot3Id, GetInputSlot3Name(), kInputSlot3ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero); |
|||
} |
|||
|
|||
protected override MaterialSlot GetOutputSlot() |
|||
{ |
|||
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector1, Vector2.zero); |
|||
} |
|||
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var outputString = new ShaderGenerator(); |
|||
outputString.AddShaderChunk(GetFunctionPrototype("xy", "xMinAndMax", "yMinAndMax"), false); |
|||
outputString.AddShaderChunk("{", false); |
|||
outputString.Indent(); |
|||
|
|||
outputString.AddShaderChunk("float x = step( xMinAndMax.x, xy.x ) - step( xMinAndMax.y, xy.x );", false); |
|||
outputString.AddShaderChunk("float y = step( yMinAndMax.x, xy.y ) - step( yMinAndMax.y, xy.y );", false); |
|||
outputString.AddShaderChunk("return x * y;", false); |
|||
|
|||
outputString.Deindent(); |
|||
outputString.AddShaderChunk("}", false); |
|||
|
|||
visitor.AddShaderChunk(outputString.GetShaderString(0), true); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 0f23589566e318c408af431d95bb492c |
|||
timeCreated: 1495565599 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("Procedural/Pulse")] |
|||
public class PulseNode : Function2Input, IGeneratesFunction |
|||
{ |
|||
public PulseNode() |
|||
{ |
|||
name = "Pulse"; |
|||
} |
|||
|
|||
protected override string GetFunctionName() |
|||
{ |
|||
return "unity_pulsenode_" + precision; |
|||
} |
|||
|
|||
protected override string GetInputSlot1Name() |
|||
{ |
|||
return "X Value"; |
|||
} |
|||
|
|||
protected override string GetInputSlot2Name() |
|||
{ |
|||
return "X Min and Max"; |
|||
} |
|||
|
|||
protected override MaterialSlot GetInputSlot1() |
|||
{ |
|||
return new MaterialSlot(InputSlot1Id, GetInputSlot1Name(), kInputSlot1ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector1, Vector2.zero); |
|||
} |
|||
|
|||
protected override MaterialSlot GetInputSlot2() |
|||
{ |
|||
return new MaterialSlot(InputSlot2Id, GetInputSlot2Name(), kInputSlot2ShaderName, UnityEngine.Graphing.SlotType.Input, SlotValueType.Vector2, Vector2.zero); |
|||
} |
|||
|
|||
protected override MaterialSlot GetOutputSlot() |
|||
{ |
|||
return new MaterialSlot(OutputSlotId, GetOutputSlotName(), kOutputSlotShaderName, UnityEngine.Graphing.SlotType.Output, SlotValueType.Vector1, Vector2.zero); |
|||
} |
|||
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var outputString = new ShaderGenerator(); |
|||
outputString.AddShaderChunk(GetFunctionPrototype("xValue", "xMinAndMax"), false); |
|||
outputString.AddShaderChunk("{", false); |
|||
outputString.Indent(); |
|||
|
|||
outputString.AddShaderChunk("return step( xMinAndMax.x, xValue ) - step( xMinAndMax.y, xValue );", false); |
|||
|
|||
outputString.Deindent(); |
|||
outputString.AddShaderChunk("}", false); |
|||
|
|||
visitor.AddShaderChunk(outputString.GetShaderString(0), true); |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f6a740415478c444bb056f9d9d941de2 |
|||
timeCreated: 1495559217 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text.RegularExpressions; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Serializable] |
|||
public abstract class AbstractAdvancedMasterNode : AbstractMasterNode |
|||
{ |
|||
public const string AlbedoSlotName = "Albedo"; |
|||
public const string NormalSlotName = "Normal"; |
|||
public const string EmissionSlotName = "Emission"; |
|||
public const string SmoothnessSlotName = "Smoothness"; |
|||
public const string OcclusionSlotName = "Occlusion"; |
|||
public const string AlphaSlotName = "Alpha"; |
|||
|
|||
public const int AlbedoSlotId = 0; |
|||
public const int NormalSlotId = 1; |
|||
public const int EmissionSlotId = 3; |
|||
public const int SmoothnessSlotId = 4; |
|||
public const int OcclusionSlotId = 5; |
|||
public const int AlphaSlotId = 6; |
|||
|
|||
public const string AnisotropySlotName = "Anisotropy"; |
|||
public const int AnisotropySlotId = 7; |
|||
public const string TangentSlotName = "Tangent"; |
|||
public const int TangentSlotId = 8; |
|||
|
|||
[SerializeField] |
|||
private SurfaceMaterialOptions m_MaterialOptions = new SurfaceMaterialOptions(); |
|||
|
|||
public SurfaceMaterialOptions options |
|||
{ |
|||
get { return m_MaterialOptions; } |
|||
} |
|||
|
|||
public abstract string GetSurfaceOutputName(); |
|||
public abstract string GetLightFunction(); |
|||
public abstract string GetMaterialID(); |
|||
|
|||
public override string GetSubShader(GenerationMode mode, PropertyGenerator shaderPropertiesVisitor) |
|||
{ |
|||
var templateLocation = ShaderGenerator.GetTemplatePath("advancedSubshader.template"); |
|||
|
|||
if (!File.Exists(templateLocation)) |
|||
return string.Empty; |
|||
|
|||
var activeNodeList = new List<INode>(); |
|||
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this); |
|||
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>()) |
|||
node.GeneratePropertyBlock(shaderPropertiesVisitor, mode); |
|||
|
|||
var templateText = File.ReadAllText(templateLocation); |
|||
var shaderBodyVisitor = new ShaderGenerator(); |
|||
var shaderFunctionVisitor = new ShaderGenerator(); |
|||
var shaderPropertyUsagesVisitor = new ShaderGenerator(); |
|||
var shaderInputVisitor = new ShaderGenerator(); |
|||
var vertexShaderBlock = new ShaderGenerator(); |
|||
|
|||
GenerateSurfaceShaderInternal( |
|||
shaderPropertyUsagesVisitor, |
|||
shaderBodyVisitor, |
|||
shaderFunctionVisitor, |
|||
shaderInputVisitor, |
|||
vertexShaderBlock, |
|||
mode); |
|||
|
|||
var tagsVisitor = new ShaderGenerator(); |
|||
var blendingVisitor = new ShaderGenerator(); |
|||
var cullingVisitor = new ShaderGenerator(); |
|||
var zTestVisitor = new ShaderGenerator(); |
|||
var zWriteVisitor = new ShaderGenerator(); |
|||
|
|||
m_MaterialOptions.GetTags(tagsVisitor); |
|||
m_MaterialOptions.GetBlend(blendingVisitor); |
|||
m_MaterialOptions.GetCull(cullingVisitor); |
|||
m_MaterialOptions.GetDepthTest(zTestVisitor); |
|||
m_MaterialOptions.GetDepthWrite(zWriteVisitor); |
|||
|
|||
var resultShader = templateText.Replace("${ShaderPropertyUsages}", shaderPropertyUsagesVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${LightingFunctionName}", GetLightFunction()); |
|||
resultShader = resultShader.Replace("${SurfaceOutputStructureName}", GetSurfaceOutputName()); |
|||
resultShader = resultShader.Replace("${ShaderFunctions}", shaderFunctionVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${ShaderInputs}", shaderInputVisitor.GetShaderString(3)); |
|||
resultShader = resultShader.Replace("${PixelShaderBody}", shaderBodyVisitor.GetShaderString(3)); |
|||
resultShader = resultShader.Replace("${Tags}", tagsVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${Blending}", blendingVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${Culling}", cullingVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${ZTest}", zTestVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${ZWrite}", zWriteVisitor.GetShaderString(2)); |
|||
resultShader = resultShader.Replace("${LOD}", ""+m_MaterialOptions.lod); |
|||
|
|||
resultShader = resultShader.Replace("${VertexShaderDecl}", "vertex:vert"); |
|||
resultShader = resultShader.Replace("${VertexShaderBody}", vertexShaderBlock.GetShaderString(3)); |
|||
|
|||
resultShader = resultShader.Replace("${MaterialID}", "#define "+GetMaterialID()); |
|||
|
|||
return resultShader; |
|||
} |
|||
|
|||
public override string GetFullShader( |
|||
GenerationMode mode, |
|||
out List<PropertyGenerator.TextureInfo> configuredTextures) |
|||
{ |
|||
var templateLocation = ShaderGenerator.GetTemplatePath("shader.template"); |
|||
|
|||
if (!File.Exists(templateLocation)) |
|||
{ |
|||
configuredTextures = new List<PropertyGenerator.TextureInfo>(); |
|||
return string.Empty; |
|||
} |
|||
|
|||
var templateText = File.ReadAllText(templateLocation); |
|||
|
|||
var shaderPropertiesVisitor = new PropertyGenerator(); |
|||
var resultShader = templateText.Replace("${ShaderName}", GetType() + guid.ToString()); |
|||
resultShader = resultShader.Replace("${SubShader}", GetSubShader(mode, shaderPropertiesVisitor)); |
|||
resultShader = resultShader.Replace("${ShaderPropertiesHeader}", shaderPropertiesVisitor.GetShaderString(2)); |
|||
configuredTextures = shaderPropertiesVisitor.GetConfiguredTexutres(); |
|||
return Regex.Replace(resultShader, @"\r\n|\n\r|\n|\r", Environment.NewLine); |
|||
} |
|||
|
|||
private void GenerateSurfaceShaderInternal( |
|||
ShaderGenerator propertyUsages, |
|||
ShaderGenerator shaderBody, |
|||
ShaderGenerator nodeFunction, |
|||
ShaderGenerator shaderInputVisitor, |
|||
ShaderGenerator vertexShaderBlock, |
|||
GenerationMode mode) |
|||
{ |
|||
var activeNodeList = new List<INode>(); |
|||
NodeUtils.DepthFirstCollectNodesFromNode(activeNodeList, this); |
|||
|
|||
foreach (var node in activeNodeList.OfType<AbstractMaterialNode>()) |
|||
{ |
|||
if (node is IGeneratesFunction) |
|||
(node as IGeneratesFunction).GenerateNodeFunction(nodeFunction, mode); |
|||
|
|||
node.GeneratePropertyUsages(propertyUsages, mode); |
|||
} |
|||
|
|||
// always add color because why not.
|
|||
shaderInputVisitor.AddShaderChunk("float4 color : COLOR;", true); |
|||
|
|||
for (int uvIndex = 0; uvIndex < ShaderGeneratorNames.UVCount; ++uvIndex) |
|||
{ |
|||
var channel = (UVChannel)uvIndex; |
|||
if (activeNodeList.OfType<IMayRequireMeshUV>().Any(x => x.RequiresMeshUV(channel))) |
|||
{ |
|||
shaderInputVisitor.AddShaderChunk(string.Format("half4 meshUV{0};", uvIndex), true); |
|||
vertexShaderBlock.AddShaderChunk(string.Format("o.meshUV{0} = v.texcoord{1};", uvIndex, uvIndex == 0 ? "" : uvIndex.ToString()), true); |
|||
shaderBody.AddShaderChunk(string.Format("half4 {0} = IN.meshUV{1};", channel.GetUVName(), uvIndex), true); |
|||
} |
|||
} |
|||
|
|||
if (activeNodeList.OfType<IMayRequireViewDirection>().Any(x => x.RequiresViewDirection())) |
|||
{ |
|||
shaderInputVisitor.AddShaderChunk("float3 worldViewDir;", true); |
|||
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceViewDirection + " = IN.worldViewDir;", true); |
|||
} |
|||
|
|||
if (activeNodeList.OfType<IMayRequireWorldPosition>().Any(x => x.RequiresWorldPosition())) |
|||
{ |
|||
shaderInputVisitor.AddShaderChunk("float3 worldPos;", true); |
|||
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpacePosition + " = IN.worldPos;", true); |
|||
} |
|||
|
|||
if (activeNodeList.OfType<IMayRequireScreenPosition>().Any(x => x.RequiresScreenPosition())) |
|||
{ |
|||
shaderInputVisitor.AddShaderChunk("float4 screenPos;", true); |
|||
shaderBody.AddShaderChunk("float4 " + ShaderGeneratorNames.ScreenPosition + " = IN.screenPos;", true); |
|||
} |
|||
|
|||
// Always need normals/tangents/bitangents for anisotropy
|
|||
bool needBitangent = true;//= activeNodeList.OfType<IMayRequireBitangent>().Any(x => x.RequiresBitangent());
|
|||
if (needBitangent || activeNodeList.OfType<IMayRequireTangent>().Any(x => x.RequiresTangent())) |
|||
{ |
|||
shaderInputVisitor.AddShaderChunk("float4 worldTangent;", true); |
|||
vertexShaderBlock.AddShaderChunk("o.worldTangent = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w);", true); |
|||
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceTangent + " = normalize(IN.worldTangent.xyz);", true); |
|||
} |
|||
|
|||
if (needBitangent || activeNodeList.OfType<IMayRequireNormal>().Any(x => x.RequiresNormal())) |
|||
{ |
|||
// is the normal connected?
|
|||
var normalSlot = FindInputSlot<MaterialSlot>(NormalSlotId); |
|||
var edges = owner.GetEdges(normalSlot.slotReference); |
|||
|
|||
shaderInputVisitor.AddShaderChunk("float3 worldNormal;", true); |
|||
if (edges.Any()) |
|||
shaderInputVisitor.AddShaderChunk("INTERNAL_DATA", true); |
|||
|
|||
shaderBody.AddShaderChunk("float3 " + ShaderGeneratorNames.WorldSpaceNormal + " = normalize(IN.worldNormal);", true); |
|||
} |
|||
|
|||
if (needBitangent) |
|||
{ |
|||
shaderBody.AddShaderChunk(string.Format("float3 {0} = cross({1}, {2}) * IN.worldTangent.w;", ShaderGeneratorNames.WorldSpaceBitangent, ShaderGeneratorNames.WorldSpaceNormal, ShaderGeneratorNames.WorldSpaceTangent), true); |
|||
} |
|||
|
|||
if (activeNodeList.OfType<IMayRequireVertexColor>().Any(x => x.RequiresVertexColor())) |
|||
{ |
|||
shaderBody.AddShaderChunk("float4 " + ShaderGeneratorNames.VertexColor + " = IN.color;", true); |
|||
} |
|||
|
|||
GenerateNodeCode(shaderBody, mode); |
|||
} |
|||
|
|||
public void GenerateNodeCode(ShaderGenerator shaderBody, GenerationMode generationMode) |
|||
{ |
|||
var nodes = ListPool<INode>.Get(); |
|||
|
|||
//shaderBody.AddShaderChunk(, false); // TODO - Switch to defines
|
|||
//Get the rest of the nodes for all the other slots
|
|||
NodeUtils.DepthFirstCollectNodesFromNode(nodes, this, NodeUtils.IncludeSelf.Exclude); |
|||
for (var i = 0; i < nodes.Count; i++) |
|||
{ |
|||
var node = nodes[i]; |
|||
if (node is IGeneratesBodyCode) |
|||
(node as IGeneratesBodyCode).GenerateNodeCode(shaderBody, generationMode); |
|||
} |
|||
ListPool<INode>.Release(nodes); |
|||
|
|||
foreach (var slot in GetInputSlots<MaterialSlot>()) |
|||
{ |
|||
foreach (var edge in owner.GetEdges(slot.slotReference)) |
|||
{ |
|||
var outputRef = edge.outputSlot; |
|||
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(outputRef.nodeGuid); |
|||
if (fromNode == null) |
|||
continue; |
|||
|
|||
var remapper = fromNode as INodeGroupRemapper; |
|||
if (remapper != null && !remapper.IsValidSlotConnection(outputRef.slotId)) |
|||
continue; |
|||
|
|||
shaderBody.AddShaderChunk("o." + slot.shaderOutputName + " = " + fromNode.GetVariableNameForSlot(outputRef.slotId) + ";", true); |
|||
|
|||
if (slot.id == NormalSlotId) |
|||
shaderBody.AddShaderChunk("o." + slot.shaderOutputName + " += 1e-6;", true); |
|||
} |
|||
|
|||
// Write tangent data to WorldVectors input on SurfaceOutput
|
|||
if (slot.id == TangentSlotId) |
|||
{ |
|||
var edges = owner.GetEdges(slot.slotReference); |
|||
if (edges.Any()) |
|||
{ |
|||
shaderBody.AddShaderChunk("float3x3 worldToTangent;", false); |
|||
shaderBody.AddShaderChunk("worldToTangent[0] = float3(1, 0, 0);", false); |
|||
shaderBody.AddShaderChunk("worldToTangent[1] = float3(0, 1, 0);", false); |
|||
shaderBody.AddShaderChunk("worldToTangent[2] = float3(0, 0, 1);", false); |
|||
shaderBody.AddShaderChunk("float3 tangentTWS = mul(o." + slot.shaderOutputName + ", worldToTangent);", false); |
|||
shaderBody.AddShaderChunk("o.WorldVectors = float3x3(tangentTWS, "+ ShaderGeneratorNames.WorldSpaceBitangent + ", "+ ShaderGeneratorNames.WorldSpaceNormal+ ");", false); |
|||
} |
|||
else |
|||
{ |
|||
shaderBody.AddShaderChunk("o.WorldVectors = float3x3(" + ShaderGeneratorNames.WorldSpaceTangent + ", " + ShaderGeneratorNames.WorldSpaceBitangent + ", " + ShaderGeneratorNames.WorldSpaceNormal + ");", false); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: f2222398f736c284287840b4c4c126c3 |
|||
timeCreated: 1495550795 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
// ------------------------------------------------------------------ |
|||
// Diffuse |
|||
|
|||
// From UE4 - Used for Cloth (Deprecated) |
|||
float3 Diffuse_Lambert(float3 DiffuseColor) |
|||
{ |
|||
return DiffuseColor * (1 / UNITY_PI); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Fresnel |
|||
|
|||
// From UE4 - Used for Cloth |
|||
// [Schlick 1994, "An Inexpensive BRDF Model for Physically-Based Rendering"] |
|||
float3 F_Schlick(float3 SpecularColor, float VoH) |
|||
{ |
|||
float Fc = Pow5(1 - VoH); // 1 sub, 3 mul |
|||
//return Fc + (1 - Fc) * SpecularColor; // 1 add, 3 mad |
|||
// Anything less than 2% is physically impossible and is instead considered to be shadowing |
|||
return saturate(50.0 * SpecularColor.g) * Fc + (1 - Fc) * SpecularColor; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Distribution |
|||
|
|||
// From UE4 - USed for Cloth |
|||
// GGX / Trowbridge-Reitz |
|||
// [Walter et al. 2007, "Microfacet models for refraction through rough surfaces"] |
|||
float D_GGX(float roughness, float NdotH) |
|||
{ |
|||
float a = roughness * roughness; |
|||
float a2 = a * a; |
|||
float d = (NdotH * a2 - NdotH) * NdotH + 1; // 2 mad |
|||
return a2 / (UNITY_PI*d*d); // 4 mul, 1 rcp |
|||
} |
|||
|
|||
// Anisotropic GGX |
|||
// Taken from HDRenderPipeline |
|||
float D_GGXAnisotropic(float TdotH, float BdotH, float NdotH, float roughnessT, float roughnessB) |
|||
{ |
|||
float f = TdotH * TdotH / (roughnessT * roughnessT) + BdotH * BdotH / (roughnessB * roughnessB) + NdotH * NdotH; |
|||
return 1.0 / (roughnessT * roughnessB * f * f); |
|||
} |
|||
|
|||
// From UE4 - Used for Cloth |
|||
float D_InvGGX(float roughness, float NdotH) |
|||
{ |
|||
float a = roughness * roughness; |
|||
float a2 = a * a; |
|||
float A = 4; |
|||
float d = (NdotH - a2 * NdotH) * NdotH + a2; |
|||
return 1/(UNITY_PI * (1 + A*a2)) * (1 + 4 * a2*a2 / (d*d)); //RCP |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Visibility |
|||
|
|||
// From UE4 - Used for Cloth |
|||
// Appoximation of joint Smith term for GGX |
|||
// [Heitz 2014, "Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs"] |
|||
float Vis_SmithJointApprox(float Roughness, float NoV, float NoL) |
|||
{ |
|||
float a = (Roughness*Roughness); |
|||
float Vis_SmithV = NoL * (NoV * (1 - a) + a); |
|||
float Vis_SmithL = NoV * (NoL * (1 - a) + a); |
|||
// Note: will generate NaNs with Roughness = 0. MinRoughness is used to prevent this |
|||
return 0.5 * 1/(Vis_SmithV + Vis_SmithL); //RCP |
|||
} |
|||
|
|||
// From UE4 - Used for Cloth |
|||
float Vis_Cloth(float NoV, float NoL) |
|||
{ |
|||
return 1/(4 * (NoL + NoV - NoL * NoV)); //RCP |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// SORT THESE |
|||
|
|||
// Smith Joint GGX Anisotropic Visibility |
|||
// Taken from https://cedec.cesa.or.jp/2015/session/ENG/14698.html |
|||
float SmithJointGGXAnisotropic(float TdotV, float BdotV, float NdotV, float TdotL, float BdotL, float NdotL, float roughnessT, float roughnessB) |
|||
{ |
|||
float aT = roughnessT; |
|||
float aT2 = aT * aT; |
|||
float aB = roughnessB; |
|||
float aB2 = aB * aB; |
|||
|
|||
float lambdaV = NdotL * sqrt(aT2 * TdotV * TdotV + aB2 * BdotV * BdotV + NdotV * NdotV); |
|||
float lambdaL = NdotV * sqrt(aT2 * TdotL * TdotL + aB2 * BdotL * BdotL + NdotL * NdotL); |
|||
|
|||
return 0.5 / (lambdaV + lambdaL); |
|||
} |
|||
|
|||
// Convert Anistropy to roughness |
|||
void ConvertAnisotropyToRoughness(float roughness, float anisotropy, out float roughnessT, out float roughnessB) |
|||
{ |
|||
// (0 <= anisotropy <= 1), therefore (0 <= anisoAspect <= 1) |
|||
// The 0.9 factor limits the aspect ratio to 10:1. |
|||
float anisoAspect = sqrt(1.0 - 0.9 * anisotropy); |
|||
roughnessT = roughness / anisoAspect; // Distort along tangent (rougher) |
|||
roughnessB = roughness * anisoAspect; // Straighten along bitangent (smoother) |
|||
} |
|||
|
|||
// Schlick Fresnel |
|||
float FresnelSchlick(float f0, float f90, float u) |
|||
{ |
|||
float x = 1.0 - u; |
|||
float x5 = x * x; |
|||
x5 = x5 * x5 * x; |
|||
return (f90 - f0) * x5 + f0; // sub mul mul mul sub mad |
|||
} |
|||
|
|||
//Clamp roughness |
|||
float ClampRoughnessForAnalyticalLights(float roughness) |
|||
{ |
|||
return max(roughness, 0.000001); |
|||
} |
|||
|
|||
//Calculate tangent warp for IBL (Reference Version - not used) |
|||
float3 SpecularGGXIBLRef(float3 viewDir, float3 normalDir, float3 tangentDir, float3 bitangentDir, float roughnessT, float roughnessB) |
|||
{ |
|||
return float3(1, 1, 1); |
|||
//Hidden in UnityAnisotropicLighting.cginc |
|||
} |
|||
|
|||
// Sample Anisotropic Direction for IBL (Reference Version - not used) |
|||
void SampleAnisoGGXDir(float2 u, float3 viewDir, float3 normalDir, float3 tangent, float3 bitangent, float roughnessT, float roughnessB, out float3 halfDir, out float3 lightDir) |
|||
{ |
|||
// AnisoGGX NDF sampling |
|||
halfDir = sqrt(u.x / (1.0 - u.x)) * (roughnessT * cos((UNITY_PI * 2) * u.y) * tangent + roughnessB * sin((UNITY_PI * 2) * u.y) * bitangent) + normalDir; |
|||
halfDir = normalize(halfDir); |
|||
|
|||
// Convert sample from half angle to incident angle |
|||
lightDir = 2.0 * saturate(dot(viewDir, halfDir)) * halfDir - viewDir; |
|||
} |
|||
|
|||
// Ref: Donald Revie - Implementing Fur Using Deferred Shading (GPU Pro 2) |
|||
// The grain direction (e.g. hair or brush direction) is assumed to be orthogonal to the normal. |
|||
// The returned normal is NOT normalized. |
|||
float3 ComputeGrainNormal(float3 grainDir, float3 V) |
|||
{ |
|||
float3 B = cross(-V, grainDir); |
|||
return cross(B, grainDir); |
|||
} |
|||
|
|||
//Modify Normal for Anisotropic IBL (Realtime version) |
|||
// Fake anisotropic by distorting the normal. |
|||
// The grain direction (e.g. hair or brush direction) is assumed to be orthogonal to N. |
|||
// Anisotropic ratio (0->no isotropic; 1->full anisotropy in tangent direction) |
|||
float3 GetAnisotropicModifiedNormal(float3 grainDir, float3 N, float3 V, float anisotropy) |
|||
{ |
|||
float3 grainNormal = ComputeGrainNormal(grainDir, V); |
|||
// TODO: test whether normalizing 'grainNormal' is worth it. |
|||
return normalize(lerp(N, grainNormal, anisotropy)); |
|||
} |
|||
|
|||
/// REGION END - ANISOTROPY |
|||
|
|||
/// REGION START - SUBSURFACE SCATTERING |
|||
|
|||
half Fresnel(half3 H, half3 V, half F0) |
|||
{ |
|||
half base = 1.0 - dot(V, H); |
|||
half exponential = pow(base, 5.0); |
|||
return exponential + F0 * (1.0 - exponential); |
|||
} |
|||
/* |
|||
inline half3 KelemenSzirmayKalosSpecular(half3 normal, half3 lightDir, half3 viewDir, float roughness, float rho_s) |
|||
{ |
|||
half3 result = half3(0, 0, 0); |
|||
half NdotL = dot(normal, lightDir); |
|||
if (NdotL > 0.0) |
|||
{ |
|||
half3 h = lightDir + viewDir; |
|||
half3 H = normalize(h); |
|||
half NdotH = dot(normal, H); |
|||
half PH = pow(2.0 * tex2D(_BeckmannPrecomputedTex, half2(NdotH, roughness)).r, 10.0); |
|||
half F = Fresnel(H, viewDir, 0.028); |
|||
half frSpec = max(PH * F / dot(h, h), 0); |
|||
half term = NdotL * rho_s * frSpec; |
|||
result = half3(term, term, term); |
|||
} |
|||
return result; |
|||
}*/ |
|||
/* |
|||
half3 SkinDiffuse(float curv, float3 NdotL) |
|||
{ |
|||
float3 lookup = NdotL * 0.5 + 0.5; |
|||
float3 diffuse; |
|||
|
|||
diffuse.r = tex2D(_DiffusionProfileTexture, float2(lookup.r, curv)).r; |
|||
diffuse.g = tex2D(_DiffusionProfileTexture, float2(lookup.g, curv)).g; |
|||
diffuse.b = tex2D(_DiffusionProfileTexture, float2(lookup.b, curv)).b; |
|||
|
|||
return diffuse; |
|||
}*/ |
|||
|
|||
/// REGION END - SUBSURFACE SCATTERING |
|
|||
fileFormatVersion: 2 |
|||
guid: 6501b60ee041551449323ec8088990e4 |
|||
timeCreated: 1491560472 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
//------------------------------------------------------------------------------------- |
|||
// Lighting Helpers |
|||
|
|||
// Glossy Environment |
|||
half3 Unity_AnisotropicGlossyEnvironment(UNITY_ARGS_TEXCUBE(tex), half4 hdr, Unity_GlossyEnvironmentData glossIn, half anisotropy) //Reference IBL from HD Pipe (Add half3 L input and replace R) |
|||
{ |
|||
half perceptualRoughness = glossIn.roughness /* perceptualRoughness */; |
|||
|
|||
// TODO: CAUTION: remap from Morten may work only with offline convolution, see impact with runtime convolution! |
|||
// For now disabled |
|||
#if 0 |
|||
float m = PerceptualRoughnessToRoughness(perceptualRoughness); // m is the real roughness parameter |
|||
const float fEps = 1.192092896e-07F; // smallest such that 1.0+FLT_EPSILON != 1.0 (+1e-4h is NOT good here. is visibly very wrong) |
|||
float n = (2.0 / max(fEps, m*m)) - 2.0; // remap to spec power. See eq. 21 in --> https://dl.dropboxusercontent.com/u/55891920/papers/mm_brdf.pdf |
|||
|
|||
n /= 4; // remap from n_dot_h formulatino to n_dot_r. See section "Pre-convolved Cube Maps vs Path Tracers" --> https://s3.amazonaws.com/docs.knaldtech.com/knald/1.0.0/lys_power_drops.html |
|||
|
|||
perceptualRoughness = pow(2 / (n + 2), 0.25); // remap back to square root of real roughness (0.25 include both the sqrt root of the conversion and sqrt for going from roughness to perceptualRoughness) |
|||
#else |
|||
// MM: came up with a surprisingly close approximation to what the #if 0'ed out code above does. |
|||
perceptualRoughness = perceptualRoughness*(1.7 - 0.7*perceptualRoughness); |
|||
#endif |
|||
|
|||
|
|||
half mip = perceptualRoughnessToMipmapLevel(perceptualRoughness); |
|||
half3 R = glossIn.reflUVW;// -half3(anisotropy, 0, 0); |
|||
half4 rgbm = UNITY_SAMPLE_TEXCUBE_LOD(tex, R, mip); |
|||
|
|||
return DecodeHDR(rgbm, hdr); |
|||
} |
|||
|
|||
// Indirect Specular |
|||
inline half3 UnityGI_AnisotropicIndirectSpecular(UnityGIInput data, half occlusion, Unity_GlossyEnvironmentData glossIn, half anisotropy, half3x3 worldVectors) |
|||
{ |
|||
half3 specular; |
|||
float3 tangentX = worldVectors[0]; |
|||
float3 tangentY = worldVectors[1]; |
|||
float3 N = worldVectors[2]; |
|||
float3 V = data.worldViewDir; |
|||
float3 iblNormalWS = GetAnisotropicModifiedNormal(tangentY, N, V, anisotropy); |
|||
float3 iblR = reflect(-V, iblNormalWS); |
|||
|
|||
#ifdef UNITY_SPECCUBE_BOX_PROJECTION |
|||
// we will tweak reflUVW in glossIn directly (as we pass it to Unity_GlossyEnvironment twice for probe0 and probe1), so keep original to pass into BoxProjectedCubemapDirection |
|||
|
|||
half3 originalReflUVW = glossIn.reflUVW; |
|||
glossIn.reflUVW = BoxProjectedCubemapDirection(iblR, data.worldPos, data.probePosition[0], data.boxMin[0], data.boxMax[0]); |
|||
#endif |
|||
|
|||
#ifdef _GLOSSYREFLECTIONS_OFF |
|||
specular = unity_IndirectSpecColor.rgb; |
|||
#else |
|||
half3 env0 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), data.probeHDR[0], glossIn, anisotropy); |
|||
//half3 env0 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), data.probeHDR[0], glossIn, anisotropy, L); //Reference IBL from HD Pipe |
|||
#ifdef UNITY_SPECCUBE_BLENDING |
|||
const float kBlendFactor = 0.99999; |
|||
float blendLerp = data.boxMin[0].w; |
|||
UNITY_BRANCH |
|||
if (blendLerp < kBlendFactor) |
|||
{ |
|||
#ifdef UNITY_SPECCUBE_BOX_PROJECTION |
|||
glossIn.reflUVW = BoxProjectedCubemapDirection(iblR, data.worldPos, data.probePosition[1], data.boxMin[1], data.boxMax[1]); |
|||
#endif |
|||
half3 env1 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0), data.probeHDR[1], glossIn, anisotropy); |
|||
//half3 env1 = Unity_AnisotropicGlossyEnvironment(UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0), data.probeHDR[1], glossIn, anisotropy, L); //Reference IBL from HD Pipe |
|||
specular = lerp(env1, env0, blendLerp); |
|||
} |
|||
else |
|||
{ |
|||
specular = env0; |
|||
} |
|||
#else |
|||
specular = env0; |
|||
#endif |
|||
#endif |
|||
|
|||
return specular * occlusion;// *weightOverPdf; //Reference IBL from HD Pipe |
|||
//return specular * occlusion * weightOverPdf; //Reference IBL from HD Pipe |
|||
} |
|||
|
|||
// Global Illumination |
|||
inline UnityGI UnityAnisotropicGlobalIllumination(UnityGIInput data, half occlusion, half3 normalWorld, Unity_GlossyEnvironmentData glossIn, half anisotropy, half3x3 worldVectors) |
|||
{ |
|||
UnityGI o_gi = UnityGI_Base(data, occlusion, normalWorld); |
|||
o_gi.indirect.specular = UnityGI_AnisotropicIndirectSpecular(data, occlusion, glossIn, anisotropy, worldVectors); |
|||
return o_gi; |
|||
} |
|||
|
|||
//------------------------------------------------------------------------------------- |
|||
// Lighting Functions |
|||
|
|||
//Surface Description |
|||
struct SurfaceOutputAdvanced |
|||
{ |
|||
fixed3 Albedo; // base (diffuse or specular) color |
|||
fixed3 Normal; // tangent space normal, if written |
|||
half3 Emission; |
|||
half Metallic; // 0=non-metal, 1=metal |
|||
// Smoothness is the user facing name, it should be perceptual smoothness but user should not have to deal with it. |
|||
// Everywhere in the code you meet smoothness it is perceptual smoothness |
|||
half Smoothness; // 0=rough, 1=smooth |
|||
half Occlusion; // occlusion (default 1) |
|||
fixed Alpha; // alpha for transparencies |
|||
half Anisotropy; |
|||
half4 CustomData; |
|||
float3x3 WorldVectors; |
|||
//half ShadingModel; |
|||
}; |
|||
|
|||
inline half4 LightingAdvanced(SurfaceOutputAdvanced s, half3 viewDir, UnityGI gi) |
|||
{ |
|||
s.Normal = normalize(s.Normal); |
|||
|
|||
half oneMinusReflectivity; |
|||
half3 specColor; |
|||
s.Albedo = DiffuseAndSpecularFromMetallic(s.Albedo, s.Metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity); |
|||
|
|||
// shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha) |
|||
// this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha |
|||
half outputAlpha; |
|||
s.Albedo = PreMultiplyAlpha(s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha); |
|||
|
|||
half4 c = SurfaceShading(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, s.WorldVectors, s.Anisotropy, s.CustomData, s.Metallic, viewDir, gi.light, gi.indirect); |
|||
c.rgb += SubsurfaceShading(s.Albedo, specColor, s.Normal, s.Smoothness, viewDir, s.CustomData, gi.light); |
|||
|
|||
//c.rgb += UNITY_BRDF_GI(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, s.Occlusion, gi); |
|||
c.a = outputAlpha; |
|||
return c; |
|||
} |
|||
|
|||
//This is pointless as always forward? |
|||
inline half4 LightingAdvanced_Deferred(SurfaceOutputAdvanced s, half3 viewDir, UnityGI gi, out half4 outGBuffer0, out half4 outGBuffer1, out half4 outGBuffer2) |
|||
{ |
|||
half oneMinusReflectivity; |
|||
half3 specColor; |
|||
s.Albedo = DiffuseAndSpecularFromMetallic(s.Albedo, s.Metallic, /*out*/ specColor, /*out*/ oneMinusReflectivity); |
|||
|
|||
half4 c = SurfaceShading(s.Albedo, specColor, oneMinusReflectivity, s.Smoothness, s.Normal, s.WorldVectors, s.Anisotropy, s.CustomData, s.Metallic, viewDir, gi.light, gi.indirect); |
|||
c.rgb += SubsurfaceShading(s.Albedo, specColor, s.Normal, s.Smoothness, viewDir, s.CustomData, gi.light); |
|||
|
|||
UnityStandardData data; |
|||
data.diffuseColor = s.Albedo; |
|||
data.occlusion = s.Occlusion; |
|||
data.specularColor = specColor; |
|||
data.smoothness = s.Smoothness; |
|||
data.normalWorld = s.Normal; |
|||
|
|||
UnityStandardDataToGbuffer(data, outGBuffer0, outGBuffer1, outGBuffer2); |
|||
|
|||
half4 emission = half4(s.Emission + c.rgb, 1); |
|||
return emission; |
|||
} |
|||
|
|||
inline void LightingAdvanced_GI(SurfaceOutputAdvanced s, UnityGIInput data, inout UnityGI gi) |
|||
{ |
|||
#if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS |
|||
gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal); |
|||
#else |
|||
Unity_GlossyEnvironmentData g = UnityGlossyEnvironmentSetup(s.Smoothness, data.worldViewDir, s.Normal, lerp(unity_ColorSpaceDielectricSpec.rgb, s.Albedo, s.Metallic)); |
|||
gi = UnityAnisotropicGlobalIllumination(data, s.Occlusion, s.Normal, g, s.Anisotropy, s.WorldVectors); |
|||
#endif |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 2993ed30aea77ea4595ef86a42c759f0 |
|||
timeCreated: 1485089973 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_CLEARCOAT' with 'defined (SHADINGMODELID_CLEARCOAT)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_CLOTH' with 'defined (SHADINGMODELID_CLOTH)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_EYE' with 'defined (SHADINGMODELID_EYE)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_FOLIAGE' with 'defined (SHADINGMODELID_FOLIAGE)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_HAIR' with 'defined (SHADINGMODELID_HAIR)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_SKIN' with 'defined (SHADINGMODELID_SKIN)' |
|||
// Upgrade NOTE: replaced 'defined SHADINGMODELID_SUBSURFACE' with 'defined (SHADINGMODELID_SUBSURFACE)' |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Shading models |
|||
|
|||
//#pragma multi_compile SHADINGMODELID_UNLIT SHADINGMODELID_STANDARD SHADINGMODELID_SUBSURFACE SHADINGMODELID_SKIN SHADINGMODELID_FOLIAGE SHADINGMODELID_CLEARCOAT SHADINGMODELID_CLOTH SHADINGMODELID_EYE |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Input |
|||
|
|||
half _ShadingModel; |
|||
|
|||
sampler2D _AnisotropyMap; |
|||
half _Anisotropy; |
|||
sampler2D _TangentMap; |
|||
|
|||
half4 _TranslucentColor; |
|||
sampler2D _TranslucencyMap; |
|||
|
|||
sampler2D _FuzzTex; |
|||
half3 _FuzzColor; |
|||
half _Cloth; |
|||
|
|||
sampler2D _IrisNormal; |
|||
sampler2D _IrisMask; |
|||
half _IrisDistance; |
|||
|
|||
half _TDistortion; |
|||
half _TScale; |
|||
half _TAmbient; |
|||
half _TPower; |
|||
half _TAttenuation; |
|||
half _TransmissionOverallStrength; |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Maths helpers |
|||
|
|||
// Octahedron Normal Vectors |
|||
// [Cigolle 2014, "A Survey of Efficient Representations for Independent Unit Vectors"] |
|||
// Mean Max |
|||
// oct 8:8 0.33709 0.94424 |
|||
// snorm 8:8:8 0.17015 0.38588 |
|||
// oct 10:10 0.08380 0.23467 |
|||
// snorm 10:10:10 0.04228 0.09598 |
|||
// oct 12:12 0.02091 0.05874 |
|||
|
|||
float2 UnitVectorToOctahedron(float3 N) |
|||
{ |
|||
N.xy /= dot(float3(1,1,1), abs(N)); |
|||
if (N.z <= 0) |
|||
{ |
|||
N.xy = (1 - abs(N.yx)) * (N.xy >= 0 ? float2(1, 1) : float2(-1, -1)); |
|||
} |
|||
return N.xy; |
|||
} |
|||
|
|||
float3 OctahedronToUnitVector(float2 Oct) |
|||
{ |
|||
float3 N = float3(Oct, 1 - dot(float2(1,1), abs(Oct))); |
|||
if (N.z < 0) |
|||
{ |
|||
N.xy = (1 - abs(N.yx)) * (N.xy >= 0 ? float2(1, 1) : float2(-1, -1)); |
|||
} |
|||
return float3(1, 1, 1); |
|||
return normalize(N); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Surface helpers |
|||
|
|||
half Anisotropy(float2 uv) |
|||
{ |
|||
return tex2D(_AnisotropyMap, uv) * _Anisotropy; |
|||
} |
|||
|
|||
half3 Fuzz(float2 uv) |
|||
{ |
|||
return tex2D(_FuzzTex, uv) * _FuzzColor; |
|||
} |
|||
|
|||
half Cloth() |
|||
{ |
|||
return _Cloth; |
|||
} |
|||
|
|||
half4 Iris(float2 uv) |
|||
{ |
|||
float2 n = UnitVectorToOctahedron(normalize(UnpackNormal(tex2D(_IrisNormal, uv)).rgb)) * 0.5 + 0.5; |
|||
float m = saturate(tex2D(_IrisMask, uv).r); // Iris Mask |
|||
float d = saturate(_IrisDistance); // Iris Distance |
|||
return float4(n.x, n.y, m, d); |
|||
} |
|||
|
|||
half3 Translucency(float2 uv) |
|||
{ |
|||
return tex2D(_TranslucencyMap, uv).rgb * _TranslucentColor.rgb; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Unlit Shading Function |
|||
|
|||
float4 UnlitShading(float3 diffColor) |
|||
{ |
|||
return half4(diffColor, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Standard Shading Function |
|||
|
|||
float4 StandardShading(float3 diffColor, float3 specColor, float oneMinusReflectivity, float smoothness, float3 normal, float3x3 worldVectors, |
|||
float anisotropy, float metallic, float3 viewDir, UnityLight light, UnityIndirect gi) |
|||
{ |
|||
//Unpack world vectors |
|||
float3 tangent = worldVectors[0]; |
|||
float3 bitangent = worldVectors[1]; |
|||
//Normal shift |
|||
float shiftAmount = dot(normal, viewDir); |
|||
normal = shiftAmount < 0.0f ? normal + viewDir * (-shiftAmount + 1e-5f) : normal; |
|||
//Regular vectors |
|||
float NdotL = saturate(dot(normal, light.dir)); //sat? |
|||
float NdotV = abs(dot(normal, viewDir)); //abs? |
|||
float LdotV = dot(light.dir, viewDir); |
|||
float3 H = Unity_SafeNormalize(light.dir + viewDir); |
|||
float invLenLV = rsqrt(abs(2 + 2 * normalize(LdotV))); |
|||
//float invLenLV = rsqrt(abs(2 + 2 * LdotV)); |
|||
//float NdotH = (NdotL + normalize(NdotV)) * invLenLV; |
|||
float NdotH = saturate(dot(normal, H)); |
|||
//float NdotH = saturate((NdotL + normalize(NdotV)) * invLenLV); |
|||
//float H = (light.dir + viewDir) * invLenLV; |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
//Tangent vectors |
|||
float TdotH = dot(tangent, H); |
|||
float TdotL = dot(tangent, light.dir); |
|||
float BdotH = dot(bitangent, H); |
|||
float BdotL = dot(bitangent, light.dir); |
|||
float TdotV = dot(viewDir, tangent); |
|||
float BdotV = dot(viewDir, bitangent); |
|||
//Fresnels |
|||
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
float3 F = FresnelLerp(specColor, grazingTerm, NdotV); //Original Schlick - Replace from SRP? |
|||
//float3 fresnel0 = lerp(specColor, diffColor, metallic); |
|||
//float3 F = FresnelSchlick(fresnel0, 1.0, LdotH); |
|||
//Calculate roughness |
|||
float roughnessT; |
|||
float roughnessB; |
|||
float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
ConvertAnisotropyToRoughness(roughness, anisotropy, roughnessT, roughnessB); |
|||
//Clamp roughness |
|||
//roughness = ClampRoughnessForAnalyticalLights(roughness); |
|||
roughnessT = ClampRoughnessForAnalyticalLights(roughnessT); |
|||
roughnessB = ClampRoughnessForAnalyticalLights(roughnessB); |
|||
//Visibility & Distribution terms |
|||
float V = SmithJointGGXAnisotropic(TdotV, BdotV, NdotV, TdotL, BdotL, NdotL, roughnessT, roughnessB); |
|||
float D = D_GGXAnisotropic(TdotH, BdotH, NdotH, roughnessT, roughnessB); |
|||
//Specular term |
|||
float3 specularTerm = V * D; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm = sqrt(max(1e-4h, specularTerm)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
specularTerm = max(0, specularTerm * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm = 0.0; |
|||
#endif |
|||
//Diffuse term |
|||
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, perceptualRoughness) * NdotL;// - Need this NdotL multiply? |
|||
//Reduction |
|||
half surfaceReduction; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness; // 1-0.28*x^3 as approximation for (1/(x^4+1))^(1/2.2) on the domain [0;1] |
|||
# else |
|||
surfaceReduction = 1.0 / (roughness*roughness + 1.0); // fade \in [0.5;1] |
|||
# endif |
|||
//Final |
|||
half3 color = (diffColor * (gi.diffuse + light.color * diffuseTerm)) |
|||
+ specularTerm * light.color * FresnelTerm(specColor, LdotH) |
|||
+ surfaceReduction * gi.specular * FresnelLerp(specColor, grazingTerm, NdotV); |
|||
return half4(color, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Cloth Shading Function |
|||
|
|||
//float3 ClothShading(FGBufferData GBuffer, float3 LobeRoughness, float3 LobeEnergy, float3 L, float3 V, half3 N) |
|||
float4 ClothShading(float3 diffColor, float3 specColor, float3 fuzzColor, float cloth, float oneMinusReflectivity, float smoothness, float3 normal, float3 viewDir, UnityLight light, UnityIndirect gi, float3x3 worldVectors, float anisotropy) |
|||
{ |
|||
const float3 FuzzColor = saturate(fuzzColor); |
|||
const float Cloth = saturate(cloth); |
|||
|
|||
//Regular vectors |
|||
float NdotL = saturate(dot(normal, light.dir)); //sat? |
|||
float NdotV = abs(dot(normal, viewDir)); //abs? |
|||
float LdotV = dot(light.dir, viewDir); |
|||
//float invLenLV = rsqrt(abs(2 + 2 * normalize(LdotV))); |
|||
////float invLenLV = rsqrt(abs(2 + 2 * LdotV)); |
|||
//float NdotH = (NdotL + normalize(NdotV)) * invLenLV; |
|||
//float NdotH = saturate((NdotL + normalize(NdotV)) * invLenLV); |
|||
float3 H = Unity_SafeNormalize(light.dir + viewDir); |
|||
//float H = (light.dir + viewDir) * invLenLV; |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
//float3 H = normalize(viewDir + light.dir); |
|||
//float NdotL = saturate(dot(normal, light.dir)); |
|||
//float NdotV = saturate(abs(dot(normal, viewDir)) + 1e-5); |
|||
float NdotH = saturate(dot(normal, H)); |
|||
float VdotH = saturate(dot(viewDir, H)); |
|||
//float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
|
|||
// Diffuse |
|||
float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, perceptualRoughness) * NdotL;// - Need this NdotL multiply? |
|||
|
|||
// Cloth - Asperity Scattering - Inverse Beckmann Layer |
|||
float3 F1 = FresnelTerm(fuzzColor, LdotH);// FresnelLerp(fuzzColor, grazingTerm, NdotV);// FresnelTerm(FuzzColor, LdotH);// F_Schlick(FuzzColor, VdotH); |
|||
float D1 = D_InvGGX(roughness, NdotH); |
|||
float V1 = Vis_Cloth(NdotV, NdotL); |
|||
//Specular term |
|||
float3 specularTerm1 = V1 * D1; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm1 = sqrt(max(1e-4h, specularTerm1)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
// specularTerm1 = max(0, specularTerm1 * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm1 = 0.0; |
|||
#endif |
|||
float3 Spec1 = specularTerm1 * light.color * FresnelTerm(fuzzColor, LdotH); |
|||
|
|||
// Generalized microfacet specular |
|||
/*float3 F2 = F_Schlick(specColor, VdotH); |
|||
float D2 = D_GGX(roughness, NdotH); |
|||
float V2 = Vis_SmithJointApprox(roughness, NdotV, NdotL); |
|||
float3 Spec2 = D2 * V2 * F2 * light.color;*/ |
|||
|
|||
//Unpack world vectors |
|||
float3 tangent = worldVectors[0]; |
|||
float3 bitangent = worldVectors[1]; |
|||
//Tangent vectors |
|||
float TdotH = dot(tangent, H); |
|||
float TdotL = dot(tangent, light.dir); |
|||
float BdotH = dot(bitangent, H); |
|||
float BdotL = dot(bitangent, light.dir); |
|||
float TdotV = dot(viewDir, tangent); |
|||
float BdotV = dot(viewDir, bitangent); |
|||
//Fresnels |
|||
float3 F2 = FresnelLerp(specColor, grazingTerm, NdotV);// FresnelTerm(specColor, LdotH);// FresnelLerp(specColor, grazingTerm, NdotV); //Original Schlick - Replace from SRP? |
|||
float roughnessT; |
|||
float roughnessB; |
|||
//float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
//float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
ConvertAnisotropyToRoughness(roughness, anisotropy, roughnessT, roughnessB); |
|||
//Clamp roughness |
|||
//roughness = ClampRoughnessForAnalyticalLights(roughness); |
|||
roughnessT = ClampRoughnessForAnalyticalLights(roughnessT); |
|||
roughnessB = ClampRoughnessForAnalyticalLights(roughnessB); |
|||
//Visibility & Distribution terms |
|||
float V2 = SmithJointGGXAnisotropic(TdotV, BdotV, NdotV, TdotL, BdotL, NdotL, roughnessT, roughnessB); |
|||
float D2 = D_GGXAnisotropic(TdotH, BdotH, NdotH, roughnessT, roughnessB); |
|||
//Specular term |
|||
float3 specularTerm2 = V2 * D2; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm2 = sqrt(max(1e-4h, specularTerm2)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
specularTerm2 = max(0, specularTerm2 * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm2 = 0.0; |
|||
#endif |
|||
float3 Spec2 = specularTerm2 * light.color * FresnelTerm(specColor, LdotH); |
|||
|
|||
float3 Spec = lerp(Spec2, Spec1, Cloth); |
|||
|
|||
//Reduction |
|||
half surfaceReduction; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness; // 1-0.28*x^3 as approximation for (1/(x^4+1))^(1/2.2) on the domain [0;1] |
|||
# else |
|||
surfaceReduction = 1.0 / (roughness*roughness + 1.0); // fade \in [0.5;1] |
|||
# endif |
|||
//Final |
|||
//half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
half3 color = (diffColor * (gi.diffuse + light.color * diffuseTerm)) |
|||
+ Spec |
|||
+ surfaceReduction * gi.specular * FresnelLerp(specColor, grazingTerm, NdotV); |
|||
return half4(color, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Eye Shading Function |
|||
|
|||
//float3 EyeShading(FGBufferData GBuffer, float3 LobeRoughness, float3 LobeEnergy, float3 L, float3 V, half3 N) |
|||
float4 EyeShading(float3 diffColor, float3 specColor, float3 viewDir, half3 normal, float smoothness, float oneMinusReflectivity, UnityLight light, UnityIndirect gi) |
|||
{ |
|||
float3 H = normalize(viewDir + light.dir); |
|||
float NdotL = saturate(dot(normal, light.dir)); |
|||
float NdotV = saturate(abs(dot(normal, viewDir)) + 1e-5); |
|||
float NdotH = saturate(dot(normal, H)); |
|||
float VdotH = saturate(dot(viewDir, H)); |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
// Generalized microfacet specular |
|||
float perceptualRoughness = SmoothnessToPerceptualRoughness(smoothness); |
|||
float roughness = PerceptualRoughnessToRoughness(perceptualRoughness); |
|||
|
|||
float D = D_GGX(roughness, NdotH);// *LobeEnergy[1]; |
|||
float V = Vis_SmithJointApprox(roughness, NdotV, NdotL); |
|||
float3 F = F_Schlick(specColor, VdotH); |
|||
|
|||
float3 specularTerm = V * D; //*UNITY_PI; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
specularTerm = sqrt(max(1e-4h, specularTerm)); |
|||
# endif |
|||
// specularTerm * nl can be NaN on Metal in some cases, use max() to make sure it's a sane value |
|||
specularTerm = max(0, specularTerm * NdotL); |
|||
#if defined(_SPECULARHIGHLIGHTS_OFF) |
|||
specularTerm = 0.0; |
|||
#endif |
|||
half grazingTerm = saturate(smoothness + (1 - oneMinusReflectivity)); |
|||
half surfaceReduction; |
|||
# ifdef UNITY_COLORSPACE_GAMMA |
|||
surfaceReduction = 1.0 - 0.28*roughness*perceptualRoughness; // 1-0.28*x^3 as approximation for (1/(x^4+1))^(1/2.2) on the domain [0;1] |
|||
# else |
|||
surfaceReduction = 1.0 / (roughness*roughness + 1.0); // fade \in [0.5;1] |
|||
# endif |
|||
|
|||
float diffuseTerm = DisneyDiffuse(NdotV, NdotL, LdotH, perceptualRoughness) * NdotL; // TODO - Unreal does not apply diffuse in Shading function |
|||
//Final |
|||
half3 color = (diffColor * (gi.diffuse + light.color * diffuseTerm)) |
|||
+ specularTerm * light.color * FresnelTerm(specColor, LdotH) |
|||
+ surfaceReduction * gi.specular * FresnelLerp(specColor, grazingTerm, NdotV); |
|||
return half4(color, 1); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Subsurface Shading Function |
|||
|
|||
float3 SubsurfaceShadingSimple(float3 diffColor, float3 normal, float3 viewDir, float3 thickness, UnityLight light) |
|||
{ |
|||
half3 vLTLight = light.dir + normal * _TDistortion; |
|||
half fLTDot = pow(saturate(dot(viewDir, -vLTLight)), _TPower) * _TScale; |
|||
half3 fLT = _TAttenuation * (fLTDot + _TAmbient) * (thickness); |
|||
return diffColor * ((light.color * fLT) * _TransmissionOverallStrength); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Eye Subsurface Shading Function |
|||
|
|||
//float3 EyeSubsurfaceShading(FGBufferData GBuffer, float3 L, float3 V, half3 N) |
|||
float3 EyeSubsurfaceShading(float3 diffColor, float3 specColor, float3 viewDir, half3 normal, float smoothness, float4 iris, UnityLight light) |
|||
{ |
|||
float2 irisNormal = iris.rg; |
|||
float irisMask = iris.z; |
|||
float irisDistance = iris.w; |
|||
|
|||
float3 H = normalize(viewDir + light.dir); |
|||
float VdotH = saturate(dot(viewDir, H)); |
|||
float NdotV = saturate(abs(dot(normal, viewDir)) + 1e-5); |
|||
float LdotH = saturate(dot(light.dir, H)); |
|||
|
|||
// F_Schlick |
|||
//float F0 = GBuffer.Specular * 0.08; |
|||
//float Fc = Pow5(1 - VoH); |
|||
//float F = Fc + (1 - Fc) * F0; |
|||
float3 fresnel0 = lerp(specColor, diffColor, smoothness); |
|||
float3 F = FresnelSchlick(fresnel0, 1.0, LdotH); |
|||
|
|||
//float IrisDistance = GBuffer.CustomData.w; |
|||
//float IrisMask = GBuffer.CustomData.z; |
|||
|
|||
float3 IrisNormal; |
|||
IrisNormal = OctahedronToUnitVector(irisNormal * 2 - 1); |
|||
|
|||
// Blend in the negative intersection normal to create some concavity |
|||
// Not great as it ties the concavity to the convexity of the cornea surface |
|||
// No good justification for that. On the other hand, if we're just looking to |
|||
// introduce some concavity, this does the job. |
|||
float3 CausticNormal = normalize(lerp(IrisNormal, -normal, irisMask*irisDistance)); |
|||
|
|||
float NdotL = saturate(dot(IrisNormal, light.dir)); |
|||
float Power = lerp(12, 1, NdotL); |
|||
float Caustic = 0.6 + 0.2 * (Power + 1) * pow(saturate(dot(CausticNormal, light.dir)), Power); |
|||
float Iris = NdotL * Caustic; |
|||
|
|||
// http://blog.stevemcauley.com/2011/12/03/energy-conserving-wrapped-diffuse/ |
|||
float Wrap = 0.15; |
|||
float Sclera = saturate((dot(normal, light.dir) + Wrap) / (1 + Wrap) * (1 + Wrap)); |
|||
|
|||
return (1 - F) * lerp(Sclera, Iris, irisMask) * diffColor / UNITY_PI; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------ |
|||
// Shading function selectors |
|||
|
|||
//float3 SurfaceShading(/*FGBufferData GBuffer,*/ float3 LobeRoughness, float3 LobeEnergy, float3 L, float3 V, half3 N, uint2 Random) |
|||
float4 SurfaceShading(float3 diffColor, float3 specColor, float oneMinusReflectivity, float smoothness, float3 normal, |
|||
float3x3 worldVectors, float anisotropy, float4 customData, float metallic, float3 viewDir, UnityLight light, UnityIndirect gi) |
|||
{ |
|||
#if defined(SHADINGMODELID_UNLIT) || defined(SHADINGMODELID_STANDARD) || defined(SHADINGMODELID_SUBSURFACE) || defined(SHADINGMODELID_SKIN) || defined(SHADINGMODELID_FOLIAGE) |
|||
{ |
|||
return UnlitShading(diffColor); |
|||
} |
|||
#elif defined(SHADINGMODELID_UNLIT) || defined(SHADINGMODELID_STANDARD) || defined(SHADINGMODELID_SUBSURFACE) || defined(SHADINGMODELID_SKIN) || defined(SHADINGMODELID_FOLIAGE) |
|||
{ |
|||
return StandardShading(diffColor, specColor, oneMinusReflectivity, smoothness, |
|||
normal, worldVectors, anisotropy, metallic, viewDir, light, gi); |
|||
} |
|||
#elif defined (SHADINGMODELID_CLEARCOAT) |
|||
{ |
|||
return float4(1, 1, 1, 1); //ClearCoatShading(GBuffer, LobeRoughness, LobeEnergy, L, V, N); |
|||
} |
|||
#elif defined (SHADINGMODELID_CLOTH) |
|||
{ |
|||
return ClothShading(diffColor, specColor, customData.rgb, customData.a, oneMinusReflectivity, smoothness, normal, viewDir, light, gi, worldVectors, anisotropy); |
|||
} |
|||
#elif defined (SHADINGMODELID_EYE) |
|||
{ |
|||
return EyeShading(diffColor, specColor, viewDir, normal, smoothness, oneMinusReflectivity, light, gi); //EyeShading(GBuffer, LobeRoughness, LobeEnergy, L, V, N); |
|||
} |
|||
#endif |
|||
return float4(0, 0, 0, 0); |
|||
} |
|||
|
|||
//float3 SubsurfaceShading(/*FGBufferData GBuffer,*/ float3 L, float3 V, half3 N, float Shadow, uint2 Random) |
|||
float3 SubsurfaceShading(float3 diffColor, float3 specColor, float3 normal, float smoothness, float3 viewDir, float4 customData, UnityLight light) |
|||
{ |
|||
#if defined (SHADINGMODELID_SUBSURFACE) |
|||
{ |
|||
return SubsurfaceShadingSimple(diffColor, normal, viewDir, customData.rgb, light); |
|||
} |
|||
#elif defined (SHADINGMODELID_SKIN) |
|||
{ |
|||
return float3(0, 0, 0); //SubsurfaceShadingPreintegratedSkin(GBuffer, L, V, N); |
|||
} |
|||
#elif defined (SHADINGMODELID_FOLIAGE) |
|||
{ |
|||
return float3(0, 0, 0); //SubsurfaceShadingTwoSided(SubsurfaceColor, L, V, N); |
|||
} |
|||
#elif defined (SHADINGMODELID_HAIR) |
|||
{ |
|||
return float3(0, 0, 0); //HairShading(GBuffer, L, V, N, Shadow, 1, 0, Random); |
|||
} |
|||
#elif defined (SHADINGMODELID_EYE) |
|||
{ |
|||
return EyeSubsurfaceShading(diffColor, specColor, viewDir, normal, smoothness, customData, light); //EyeSubsurfaceShading(GBuffer, L, V, N); |
|||
} |
|||
#endif |
|||
return float3(0, 0, 0); |
|||
} |
|||
|
|||
//#endif UNITY_ADVANCED_SHADINGMODELS_INCLUDED |
|||
|
|
|||
fileFormatVersion: 2 |
|||
guid: 452b8b694ffaad94cb0cc2cadd5e413b |
|||
timeCreated: 1485089973 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
1
MaterialGraphProject/Assets/Matt/AnisotropicMaster.ShaderGraph
文件差异内容过多而无法显示
查看文件
文件差异内容过多而无法显示
查看文件
|
|||
fileFormatVersion: 2 |
|||
guid: 2a192f63b29cdfb469ebbd4bef80ff66 |
|||
timeCreated: 1495564592 |
|||
licenseType: Pro |
|||
ScriptedImporter: |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|||
script: {fileID: 11500000, guid: 625f186215c104763be7675aa2d941aa, type: 3} |
|
|||
using System; |
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Serializable] |
|||
[Title("Master/AnisotropicMetallic")] |
|||
public class AnisotropicMetallicMasterNode : AbstractAdvancedMasterNode |
|||
{ |
|||
public const string MetallicSlotName = "Metallic"; |
|||
public const int MetallicSlotId = 2; |
|||
|
|||
public const string LightFunctionName = "Advanced"; |
|||
public const string SurfaceOutputStructureName = "SurfaceOutputAdvanced"; |
|||
|
|||
public AnisotropicMetallicMasterNode() |
|||
{ |
|||
name = "AnisotropicMetallicMasterNode"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public override string GetMaterialID() |
|||
{ |
|||
return "SHADINGMODELID_STANDARD"; |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new MaterialSlot(AlbedoSlotId, AlbedoSlotName, AlbedoSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(NormalSlotId, NormalSlotName, NormalSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(EmissionSlotId, EmissionSlotName, EmissionSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(MetallicSlotId, MetallicSlotName, MetallicSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(SmoothnessSlotId, SmoothnessSlotName, SmoothnessSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(OcclusionSlotId, OcclusionSlotName, OcclusionSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(AnisotropySlotId, AnisotropySlotName, AnisotropySlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(TangentSlotId, TangentSlotName, TangentSlotName, SlotType.Input, SlotValueType.Vector3, Vector4.zero)); |
|||
AddSlot(new MaterialSlot(AlphaSlotId, AlphaSlotName, AlphaSlotName, SlotType.Input, SlotValueType.Vector1, Vector4.zero)); |
|||
|
|||
// clear out slot names that do not match the slots
|
|||
// we support
|
|||
RemoveSlotsNameNotMatching( |
|||
new[] |
|||
{ |
|||
AlbedoSlotId, |
|||
NormalSlotId, |
|||
EmissionSlotId, |
|||
MetallicSlotId, |
|||
SmoothnessSlotId, |
|||
OcclusionSlotId, |
|||
AlphaSlotId, |
|||
AnisotropySlotId, |
|||
TangentSlotId |
|||
}); |
|||
} |
|||
|
|||
public override string GetSurfaceOutputName() |
|||
{ |
|||
return SurfaceOutputStructureName; |
|||
} |
|||
|
|||
public override string GetLightFunction() |
|||
{ |
|||
return LightFunctionName; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 5fe91aeebab1aed41ada4bc3ab693499 |
|||
timeCreated: 1478188276 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
%YAML 1.1 |
|||
%TAG !u! tag:unity3d.com,2011: |
|||
--- !u!21 &2100000 |
|||
Material: |
|||
serializedVersion: 6 |
|||
m_ObjectHideFlags: 0 |
|||
m_PrefabParentObject: {fileID: 0} |
|||
m_PrefabInternal: {fileID: 0} |
|||
m_Name: New Material |
|||
m_Shader: {fileID: 4800000, guid: 9ab5e16c2083a4fe689209a8c1ae425e, type: 3} |
|||
m_ShaderKeywords: |
|||
m_LightmapFlags: 4 |
|||
m_EnableInstancingVariants: 0 |
|||
m_CustomRenderQueue: -1 |
|||
stringTagMap: {} |
|||
disabledShaderPasses: [] |
|||
m_SavedProperties: |
|||
serializedVersion: 3 |
|||
m_TexEnvs: |
|||
- _AnisotropyMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _BumpMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _DetailAlbedoMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _DetailMask: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _DetailNormalMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _EmissionMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _FuzzTex: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _IrisMask: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _IrisNormal: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _MainTex: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _MetallicGlossMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _OcclusionMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _ParallaxMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _TangentMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
- _TranslucencyMap: |
|||
m_Texture: {fileID: 0} |
|||
m_Scale: {x: 1, y: 1} |
|||
m_Offset: {x: 0, y: 0} |
|||
m_Floats: |
|||
- Vector1_9fc5b769_bf77_4f9d_8bc7_07fc5576501e_Uniform: 0.5 |
|||
- _Anisotropy: 0 |
|||
- _BumpScale: 1 |
|||
- _Cloth: 0 |
|||
- _Cutoff: 0.5 |
|||
- _DetailNormalMapScale: 1 |
|||
- _DstBlend: 0 |
|||
- _GlossMapScale: 1 |
|||
- _Glossiness: 0.5 |
|||
- _GlossyReflections: 1 |
|||
- _IrisDistance: 0.5 |
|||
- _Metallic: 0 |
|||
- _Mode: 0 |
|||
- _OcclusionStrength: 1 |
|||
- _Parallax: 0.02 |
|||
- _ShadingModel: 0 |
|||
- _SmoothnessTextureChannel: 0 |
|||
- _SpecularHighlights: 1 |
|||
- _SrcBlend: 1 |
|||
- _TAmbient: 1.7 |
|||
- _TAttenuation: 1 |
|||
- _TDistortion: 2 |
|||
- _TPower: 3.5 |
|||
- _TScale: 1.5 |
|||
- _TransmissionOverallStrength: 1 |
|||
- _UVSec: 0 |
|||
- _ZWrite: 1 |
|||
m_Colors: |
|||
- Color_eb55295a_2a33_4a48_b612_9d279e2cf480_Uniform: {r: 1, g: 1, b: 1, a: 1} |
|||
- _Color: {r: 1, g: 1, b: 1, a: 1} |
|||
- _EmissionColor: {r: 0, g: 0, b: 0, a: 1} |
|||
- _FuzzColor: {r: 0, g: 0, b: 0, a: 1} |
|||
- _TranslucentColor: {r: 0, g: 0, b: 0, a: 1} |
|
|||
fileFormatVersion: 2 |
|||
guid: 1209994acc27f1e49bcc17c3c89d3e93 |
|||
timeCreated: 1495558202 |
|||
licenseType: Pro |
|||
NativeFormatImporter: |
|||
mainObjectFileID: 2100000 |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
Shader "Work" { |
|||
Properties |
|||
{ |
|||
// Shading model |
|||
[Enum(Unlit, Standard, Subsurface, Skin, Foliage, Clearcoat, Cloth, Eye, Hair)] _ShadingModel("Shading model", Float) = 0 |
|||
|
|||
_Color("Color", Color) = (1,1,1,1) |
|||
_MainTex("Albedo", 2D) = "white" {} |
|||
|
|||
_Glossiness("Smoothness", Range(0.0, 1.0)) = 0.5 |
|||
|
|||
[Gamma] _Metallic("Metallic", Range(0.0, 1.0)) = 0.0 |
|||
_MetallicGlossMap("Metallic", 2D) = "white" {} |
|||
|
|||
_BumpMap("Normal Map", 2D) = "bump" {} |
|||
|
|||
_OcclusionMap("Occlusion", 2D) = "white" {} |
|||
|
|||
_EmissionColor("Color", Color) = (0,0,0) |
|||
_EmissionMap("Emission", 2D) = "white" {} |
|||
|
|||
// Anisotropy |
|||
_Anisotropy("Anisotropy", Range(0,1)) = 0.0 |
|||
_AnisotropyMap("Anisotropy", 2D) = "white" {} |
|||
_TangentMap("Tangent", 2D) = "white" {} |
|||
|
|||
// Subsurface Scattering |
|||
_TranslucencyMap("Translucency Map", 2D) = "white" {} |
|||
_TranslucentColor("Color", Color) = (0,0,0) |
|||
_TDistortion("Transmission Distortion", Range(-10, 10)) = 2.0 |
|||
_TScale("Transmission Scale", Range(0, 10)) = 1.5 |
|||
_TAmbient("Transmission Ambient", Range(0, 10)) = 1.7 |
|||
_TPower("Transmission Power", Range(1, 10)) = 3.5 |
|||
_TAttenuation("Transmission Fake Attenuation", Range(0, 10)) = 1.0 |
|||
_TransmissionOverallStrength("Transmission Overall Strength", Range(0, 1)) = 1.0 |
|||
|
|||
// Cloth |
|||
_FuzzTex("Fuzz Map", 2D) = "white" {} |
|||
_FuzzColor("Fuzz", Color) = (0,0,0) |
|||
_Cloth("Cloth Factor", Range(0, 1)) = 0 |
|||
|
|||
//Eye |
|||
_IrisNormal("Iris Normal (RGB)", 2D) = "black" {} |
|||
_IrisMask("Iris Mask (R)", 2D) = "black" {} |
|||
_IrisDistance("Iris Distance", Range(0, 1)) = 0.5 |
|||
} |
|||
SubShader |
|||
{ |
|||
Tags { "RenderType"="Opaque" } |
|||
LOD 200 |
|||
|
|||
CGPROGRAM |
|||
|
|||
//#define UNITY_BRDF_PBS BRDF_Unity_Anisotropic |
|||
#include "UnityCG.cginc" |
|||
#include "AdvancedBRDF.cginc" |
|||
#include "AdvancedShading.cginc" |
|||
#include "AdvancedLighting.cginc" |
|||
#pragma target 5.0 |
|||
|
|||
|
|||
#pragma surface SurfaceAdvanced Advanced vertex:vert fullforwardshadows |
|||
|
|||
sampler2D _MainTex; |
|||
|
|||
struct Input |
|||
{ |
|||
float2 uv_MainTex; |
|||
float3 normal; |
|||
float3 viewDir; |
|||
float3 normalDir; |
|||
float3 tangentDir; |
|||
float3 bitangentDir; |
|||
}; |
|||
|
|||
//Vertex shader |
|||
void vert(inout appdata_full v, out Input o) |
|||
{ |
|||
UNITY_INITIALIZE_OUTPUT(Input, o); |
|||
//Normal 2 World |
|||
o.normalDir = normalize(UnityObjectToWorldNormal(v.normal)); |
|||
//Tangent 2 World |
|||
float3 tangentMul = normalize(mul(unity_ObjectToWorld, v.tangent.xyz)); |
|||
o.tangentDir = float4(tangentMul, v.tangent.w); |
|||
// Bitangent |
|||
o.bitangentDir = cross(o.normalDir, o.tangentDir); |
|||
} |
|||
|
|||
half _Glossiness; |
|||
half _Metallic; |
|||
fixed4 _Color; |
|||
|
|||
UNITY_INSTANCING_CBUFFER_START(Props) |
|||
UNITY_INSTANCING_CBUFFER_END |
|||
|
|||
void SurfaceAdvanced(Input IN, inout SurfaceOutputAdvanced o) |
|||
{ |
|||
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; |
|||
o.Albedo = c; |
|||
o.Metallic = _Metallic; |
|||
o.Smoothness = _Glossiness; |
|||
o.Alpha = c.a; |
|||
o.Anisotropy = tex2D(_AnisotropyMap, IN.uv_MainTex) * _Anisotropy; |
|||
|
|||
float4 custom; |
|||
if (_ShadingModel == 2 /*Subsurface*/) |
|||
{ |
|||
custom = float4(Translucency(IN.uv_MainTex),0); |
|||
} |
|||
else if (_ShadingModel == 6 /*Cloth*/) |
|||
{ |
|||
custom = float4(Fuzz(IN.uv_MainTex), Cloth()); |
|||
} |
|||
else if (_ShadingModel == 7 /*Eye*/) |
|||
{ |
|||
custom = Iris(IN.uv_MainTex); |
|||
} |
|||
o.CustomData = custom; |
|||
|
|||
float3 tangentTS = tex2D(_TangentMap, IN.uv_MainTex); |
|||
float3 fTangent; |
|||
if (tangentTS.z < 1) // TODO - Clean this |
|||
{ |
|||
float3x3 worldToTangent; |
|||
worldToTangent[0] = float3(1, 0, 0); |
|||
worldToTangent[1] = float3(0, 1, 0); |
|||
worldToTangent[2] = float3(0, 0, 1); |
|||
float3 tangentTWS = mul(tangentTS, worldToTangent); |
|||
fTangent = tangentTWS; |
|||
} |
|||
else |
|||
fTangent = IN.tangentDir; |
|||
o.WorldVectors = float3x3(fTangent, IN.bitangentDir, IN.normalDir); |
|||
//o.ShadingModel = _ShadingModel; |
|||
} |
|||
ENDCG |
|||
} |
|||
FallBack "Diffuse" |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: b14f65d41b4066e429fa01faad3edc33 |
|||
timeCreated: 1495544348 |
|||
licenseType: Pro |
|||
ShaderImporter: |
|||
defaultTextures: [] |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
#if UNITY_EDITOR
|
|||
using UnityEditor; |
|||
#endif
|
|||
using UnityEngine.Graphing; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("Input/Texture/Texture Asset")] |
|||
public class TextureAssetNode : PropertyNode |
|||
{ |
|||
protected const string kOutputSlotRGBAName = "Texture"; |
|||
|
|||
public const int OutputSlotRgbaId = 0; |
|||
|
|||
[SerializeField] |
|||
private string m_SerializedTexture; |
|||
|
|||
[SerializeField] |
|||
private TextureType m_TextureType; |
|||
|
|||
[Serializable] |
|||
private class TextureHelper |
|||
{ |
|||
public Texture2D texture; |
|||
} |
|||
|
|||
public override bool hasPreview { get { return false; } } |
|||
|
|||
#if UNITY_EDITOR
|
|||
public Texture2D defaultTexture |
|||
{ |
|||
get |
|||
{ |
|||
if (string.IsNullOrEmpty(m_SerializedTexture)) |
|||
return null; |
|||
|
|||
var tex = new TextureHelper(); |
|||
EditorJsonUtility.FromJsonOverwrite(m_SerializedTexture, tex); |
|||
return tex.texture; |
|||
} |
|||
set |
|||
{ |
|||
if (defaultTexture == value) |
|||
return; |
|||
|
|||
var tex = new TextureHelper(); |
|||
tex.texture = value; |
|||
m_SerializedTexture = EditorJsonUtility.ToJson(tex, true); |
|||
|
|||
if (onModified != null) |
|||
{ |
|||
onModified(this, ModificationScope.Node); |
|||
} |
|||
} |
|||
} |
|||
#else
|
|||
public Texture2D defaultTexture {get; set; } |
|||
#endif
|
|||
|
|||
public TextureType textureType |
|||
{ |
|||
get { return m_TextureType; } |
|||
set |
|||
{ |
|||
if (m_TextureType == value) |
|||
return; |
|||
|
|||
|
|||
m_TextureType = value; |
|||
if (onModified != null) |
|||
{ |
|||
onModified(this, ModificationScope.Graph); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public TextureAssetNode() |
|||
{ |
|||
name = "TextureAsset"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
public sealed override void UpdateNodeAfterDeserialization() |
|||
{ |
|||
AddSlot(new MaterialSlot(OutputSlotRgbaId, kOutputSlotRGBAName, kOutputSlotRGBAName, SlotType.Output, SlotValueType.sampler2D, Vector4.zero, false)); |
|||
RemoveSlotsNameNotMatching(validSlots); |
|||
} |
|||
|
|||
protected int[] validSlots |
|||
{ |
|||
get { return new[] { OutputSlotRgbaId }; } |
|||
} |
|||
|
|||
public override string GetVariableNameForSlot(int slotId) |
|||
{ |
|||
string slotOutput; |
|||
switch (slotId) |
|||
{ |
|||
default: |
|||
slotOutput = ""; |
|||
break; |
|||
} |
|||
return GetVariableNameForNode() + slotOutput; |
|||
} |
|||
|
|||
public override void CollectPreviewMaterialProperties(List<PreviewProperty> properties) |
|||
{ |
|||
properties.Add(GetPreviewProperty()); |
|||
} |
|||
|
|||
// Properties
|
|||
public override void GeneratePropertyBlock(PropertyGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
visitor.AddShaderProperty( |
|||
new TexturePropertyChunk( |
|||
propertyName, |
|||
description, |
|||
defaultTexture, m_TextureType, |
|||
PropertyChunk.HideState.Visible, |
|||
exposedState == ExposedState.Exposed ? |
|||
TexturePropertyChunk.ModifiableState.Modifiable |
|||
: TexturePropertyChunk.ModifiableState.NonModifiable)); |
|||
} |
|||
|
|||
|
|||
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
visitor.AddShaderChunk("sampler2D " + propertyName + ";", true); |
|||
} |
|||
|
|||
|
|||
|
|||
public override PreviewProperty GetPreviewProperty() |
|||
{ |
|||
return new PreviewProperty |
|||
{ |
|||
m_Name = propertyName, |
|||
m_PropType = PropertyType.Texture2D, |
|||
m_Texture = defaultTexture |
|||
}; |
|||
} |
|||
|
|||
|
|||
|
|||
|
|||
public override PropertyType propertyType { get { return PropertyType.Texture2D; } } |
|||
|
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9def76d530f56804b9f696a9b36eccfa |
|||
timeCreated: 1495535587 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
|
|||
using UnityEngine.Graphing; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace UnityEngine.MaterialGraph |
|||
{ |
|||
[Title("UV/Tri-Planar Mapping")] |
|||
public class UVTriPlanar : Function1Input, IGeneratesFunction, IMayRequireNormal, IMayRequireWorldPosition |
|||
{ |
|||
|
|||
private const string kTextureSlotName = "Texture"; |
|||
|
|||
protected override string GetFunctionName() |
|||
{ |
|||
return "unity_triplanar_" + precision; |
|||
} |
|||
|
|||
protected override MaterialSlot GetInputSlot() |
|||
{ |
|||
return new MaterialSlot(InputSlotId, kTextureSlotName, kTextureSlotName, SlotType.Input, SlotValueType.sampler2D, Vector4.zero, false); |
|||
} |
|||
|
|||
protected override MaterialSlot GetOutputSlot() |
|||
{ |
|||
return new MaterialSlot(OutputSlotId, kTextureSlotName, kOutputSlotShaderName, SlotType.Output, SlotValueType.Vector4, Vector4.zero); |
|||
} |
|||
|
|||
public UVTriPlanar() |
|||
{ |
|||
name = "UVTriPlanar"; |
|||
UpdateNodeAfterDeserialization(); |
|||
} |
|||
|
|||
protected override string GetFunctionPrototype(string argName) |
|||
{ |
|||
return "inline " + precision + outputDimension + " " + GetFunctionName() + " (" |
|||
+ "sampler2D " + argName + ", float3 normal, float3 pos)"; |
|||
} |
|||
|
|||
protected override string GetFunctionCallBody(string inputValue) |
|||
{ |
|||
return GetFunctionName() + " (" + inputValue + "_Uniform" + ", IN.worldNormal, IN.worldPos)"; |
|||
} |
|||
|
|||
public override void GeneratePropertyUsages(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
base.GeneratePropertyUsages(visitor, generationMode); |
|||
} |
|||
|
|||
//TODO:Externalize
|
|||
//Reference code from:http://www.chilliant.com/rgb2hsv.html
|
|||
public void GenerateNodeFunction(ShaderGenerator visitor, GenerationMode generationMode) |
|||
{ |
|||
var outputString = new ShaderGenerator(); |
|||
|
|||
var textureSlot = FindInputSlot<MaterialSlot>(InputSlotId); |
|||
if (textureSlot == null) |
|||
return; |
|||
|
|||
var textureName = ""; |
|||
|
|||
var edges = owner.GetEdges(textureSlot.slotReference).ToList(); |
|||
if (edges.Count > 0) |
|||
{ |
|||
var edge = edges[0]; |
|||
var fromNode = owner.GetNodeFromGuid<AbstractMaterialNode>(edge.outputSlot.nodeGuid); |
|||
textureName = ShaderGenerator.AdaptNodeOutput(fromNode, edge.outputSlot.slotId, ConcreteSlotValueType.sampler2D, true); |
|||
} |
|||
|
|||
|
|||
////////////////////////////////QQQ: Any better way of getting "_Uniform" at the end? //////////////////////////////////
|
|||
// outputString.AddShaderChunk("sampler2D " + textureName + "_Uniform;", false);
|
|||
// outputString.AddShaderChunk("", false);
|
|||
|
|||
outputString.AddShaderChunk(GetFunctionPrototype("arg"), false); |
|||
outputString.AddShaderChunk("{", false); |
|||
outputString.Indent(); |
|||
|
|||
|
|||
// use absolute value of normal as texture weights
|
|||
outputString.AddShaderChunk("half3 blend = abs(normal);", false); |
|||
// make sure the weights sum up to 1 (divide by sum of x+y+z)
|
|||
outputString.AddShaderChunk("blend /= dot(blend, 1.0);", false); |
|||
|
|||
|
|||
// read the three texture projections, for x,y,z axes
|
|||
outputString.AddShaderChunk("fixed4 cx = tex2D(arg, pos.yz);", false); |
|||
outputString.AddShaderChunk("fixed4 cy = tex2D(arg, pos.xz);", false); |
|||
outputString.AddShaderChunk("fixed4 cz = tex2D(arg, pos.xy);", false); |
|||
|
|||
|
|||
// blend the textures based on weights
|
|||
outputString.AddShaderChunk("fixed4 c = cx * blend.x + cy * blend.y + cz * blend.z;", false); |
|||
|
|||
outputString.AddShaderChunk("return " + "c;", false); |
|||
outputString.Deindent(); |
|||
outputString.AddShaderChunk("}", false); |
|||
|
|||
visitor.AddShaderChunk(outputString.GetShaderString(0), true); |
|||
} |
|||
|
|||
public bool RequiresNormal() |
|||
{ |
|||
return true; |
|||
} |
|||
|
|||
public bool RequiresWorldPosition() |
|||
{ |
|||
return true; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: c355f376dad60404fbfd488ec80b88a4 |
|||
timeCreated: 1495485045 |
|||
licenseType: Pro |
|||
MonoImporter: |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue