崩坏三 - 武器 - 童谣 NPR渲染分析
- 童谣 NPR渲染
- 演示视频链接
- 大致想法
- Ramp
- 高光效果
- 边缘光效果
- 镰刀刃部
- 效果截图
- 完整代码
- 镰刀身体部分
- 镰刀刃部分
童谣 NPR渲染
演示视频链接
https://www.bilibili.com/video/av68650714
大致想法
镰刀身体部分:
描边 + Ramp多阶色 + 高光 + 边缘光 + 菲涅尔反射 + HSV调节
镰刀刃部:
扰动 + Emission + 金属 + Gloss + 菲涅尔反射
Ramp
分阶色Shader代码
// multi stepsfloat diff = smoothstep(_RampThreshold - ndl, _RampThreshold + ndl, ndl);float interval = 1 / _ToonSteps;// float ramp = floor(diff * _ToonSteps) / _ToonSteps;float level = round(diff * _ToonSteps) / _ToonSteps;float ramp ;if (_RampSmooth == 1){ramp = interval * linearstep(level - _RampSmooth * interval * 0.5, level + _RampSmooth * interval * 0.5, diff) + level - interval;}else{ramp = interval * smoothstep(level - _RampSmooth * interval * 0.5, level + _RampSmooth * interval * 0.5, diff) + level - interval;}ramp = max(0, ramp);ramp *= atten;_SColor = lerp(_HColor, _SColor, _SColor.a);float3 rampColor = lerp(_SColor.rgb, _HColor.rgb, ramp);
高光效果
高光Shader代码
// specularfloat spec = pow(ndh, s.Specular * 128.0) * s.Gloss;spec *= atten;spec = smoothstep(0.5 - _SpecSmooth * 0.5, 0.5 + _SpecSmooth * 0.5, spec);
边缘光效果
边缘光Shader代码
float rim = (1.0 - ndv) * ndl;rim *= atten;rim = smoothstep(_RimThreshold - _RimSmooth * 0.5, _RimThreshold + _RimSmooth * 0.5, rim);fixed4 color;fixed3 diffuse = s.Albedo * rampColor *(ndv + 1.5);fixed3 specular = _SpecColor.rgb * spec;fixed3 rimColor = _RimColor.rgb * _RimColor.a * rim;
镰刀刃部
Shader代码
float4 frag(VertexOutput i) : COLOR {i.normalDir = normalize(i.normalDir);float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);float4 _Normal_var = tex2D(_Normal,TRANSFORM_TEX(i.uv0, _Normal));float3 normalLocal = _Normal_var.rgb;float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normalsfloat3 viewReflectDirection = reflect( -viewDirection, normalDirection );float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);float3 lightColor = _LightColor0.rgb;float3 halfDirection = normalize(viewDirection+lightDirection);
// Lighting:float attenuation = LIGHT_ATTENUATION(i);float3 attenColor = attenuation * _LightColor0.xyz;float Pi = 3.141592654;float InvPi = 0.31830988618;
/ Gloss:float gloss = _Gloss;float perceptualRoughness = 1.0 - _Gloss;float roughness = perceptualRoughness * perceptualRoughness;float specPow = exp2( gloss * 10.0 + 1.0 );
/// GI Data:UnityLight light;#ifdef LIGHTMAP_OFFlight.color = lightColor;light.dir = lightDirection;light.ndotl = LambertTerm (normalDirection, light.dir);#elselight.color = half3(0.f, 0.f, 0.f);light.ndotl = 0.0f;light.dir = half3(0.f, 0.f, 0.f);#endifUnityGIInput d;d.light = light;d.worldPos = i.posWorld.xyz;d.worldViewDir = viewDirection;d.atten = attenuation;#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)d.ambient = 0;d.lightmapUV = i.ambientOrLightmapUV;#elsed.ambient = i.ambientOrLightmapUV;#endif#if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTIONd.boxMin[0] = unity_SpecCube0_BoxMin;d.boxMin[1] = unity_SpecCube1_BoxMin;#endif#if UNITY_SPECCUBE_BOX_PROJECTIONd.boxMax[0] = unity_SpecCube0_BoxMax;d.boxMax[1] = unity_SpecCube1_BoxMax;d.probePosition[0] = unity_SpecCube0_ProbePosition;d.probePosition[1] = unity_SpecCube1_ProbePosition;#endifd.probeHDR[0] = unity_SpecCube0_HDR;d.probeHDR[1] = unity_SpecCube1_HDR;Unity_GlossyEnvironmentData ugls_en_data;ugls_en_data.roughness = 1.0 - gloss;ugls_en_data.reflUVW = viewReflectDirection;UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );lightDirection = gi.light.dir;lightColor = gi.light.color;
// Specular:float NdotL = saturate(dot( normalDirection, lightDirection ));float LdotH = saturate(dot(lightDirection, halfDirection));float3 specularColor = _Metallic;float specularMonochrome;float4 node_3538 = _Time;float2 node_6877 = (i.uv0+(_Speed*node_3538.g)*float2(1,1));float4 _MeltTex_var = tex2D(_MeltTex,TRANSFORM_TEX(node_6877, _MeltTex));float2 node_8337 = lerp(i.uv0,(float3(i.uv0,0.0)*_MeltTex_var.rgb*2.0).rg,_Intensity);float4 node_4378 = tex2D(_MainTex,TRANSFORM_TEX(node_8337, _MainTex));float3 node_5719 = (_MianColor.rgb*node_4378.rgb);float3 diffuseColor = ((_FresnelColor.rgb*pow(1.0-max(0,dot(normalDirection, viewDirection)),_FresnelScale))+(node_5719+node_5719)); // Need this for specular when using metallicdiffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );specularMonochrome = 1.0-specularMonochrome;float NdotV = abs(dot( normalDirection, viewDirection ));float NdotH = saturate(dot( normalDirection, halfDirection ));float VdotH = saturate(dot( viewDirection, halfDirection ));float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );float normTerm = GGXTerm(NdotH, roughness);float specularPBL = (visTerm*normTerm) * UNITY_PI;#ifdef UNITY_COLORSPACE_GAMMAspecularPBL = sqrt(max(1e-4h, specularPBL));#endifspecularPBL = max(0, specularPBL * NdotL);#if defined(_SPECULARHIGHLIGHTS_OFF)specularPBL = 0.0;#endifhalf surfaceReduction;#ifdef UNITY_COLORSPACE_GAMMAsurfaceReduction = 1.0-0.28*roughness*perceptualRoughness;#elsesurfaceReduction = 1.0/(roughness*roughness + 1.0);#endifspecularPBL *= any(specularColor) ? 1.0 : 0.0;float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);half grazingTerm = saturate( gloss + specularMonochrome );float3 indirectSpecular = (gi.indirect.specular);indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);indirectSpecular *= surfaceReduction;float3 specular = (directSpecular + indirectSpecular);
/// Diffuse:NdotL = max(0.0,dot( normalDirection, lightDirection ));half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);float nlPow5 = Pow5(1-NdotL);float nvPow5 = Pow5(1-NdotV);float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;float3 indirectDiffuse = float3(0,0,0);indirectDiffuse += gi.indirect.diffuse;float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
// Emissive:float node_5997 = (node_4378.b*_Emission);float3 emissive = float3(node_5997,node_5997,node_5997);
/// Final Color:float3 finalColor = diffuse + specular + emissive;fixed4 finalRGBA = fixed4(finalColor,1);UNITY_APPLY_FOG(i.fogCoord, finalRGBA);return finalRGBA;}
效果截图
截图一
截图二
完整代码
镰刀身体部分
Shader "Toon"
{Properties{// Colors_Color ("Color", Color) = (1, 1, 1, 1)_HColor ("Highlight Color", Color) = (0.8, 0.8, 0.8, 1.0)_SColor ("Shadow Color", Color) = (0.2, 0.2, 0.2, 1.0)// texture_MainTex ("Main Texture", 2D) = "white" { }// ramp_ToonSteps ("Steps of Toon", range(1, 9)) = 2_RampThreshold ("Ramp Threshold", Range(0.1, 1)) = 0.5_RampSmooth ("Ramp Smooth", Range(0, 1)) = 0.1// specular_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)_SpecSmooth ("Specular Smooth", Range(0, 1)) = 0.1_Shininess ("Shininess", Range(0.001, 10)) = 0.2// rim light[HDR]_RimColor ("Rim Color", Color) = (1,1,1,1)_RimThreshold ("Rim Threshold", Range(0, 1)) = 0.5_RimSmooth ("Rim Smooth", Range(0, 1)) = 0.289_Outline ("Outline", Range(0, 1)) = 0.002_OutColor ("Outline Color", Color) = (0, 0, 0, 1)_Factor("Factor",range(0,1)) = 0.5//rgb to hsv_HueOffset("Hue Offset", Range(0,1)) = 0_SaturationOffset("Hue Offset", Range(-1,1)) = 0.3_ValueOffset("Hue Offset", Range(-1,1)) = 0.15_BloomFactor("_BloomFactor", Range(0.0, 0.9)) = 0[HDR]_FresnealColor("FresnealColor",Color) = (1,0.4191,0.6715,1)_FresnealScale("FresnealScale",Range(0,10)) = 1.88 }SubShader{Tags { "RenderType" = "Opaque" }Pass {NAME "OUTLINE"Tags{ "LightMode" = "Always" }Cull FrontZWrite OnCGPROGRAM#pragma multi_compile_fog#pragma vertex vert#pragma fragment frag#include "UnityCG.cginc"float _Outline;fixed4 _OutColor;float _Factor;struct a2v {float4 vertex : POSITION;float3 normal : NORMAL;}; struct v2f {float4 pos : SV_POSITION;UNITY_FOG_COORDS(0)};v2f vert (a2v v) {v2f o;float3 dir = normalize(v.vertex.xyz);float3 dir2 = v.normal;float D = dot(dir,dir2);dir = dir * sign(D);dir = dir * _Factor + dir2 * (1 - _Factor);v.vertex.xyz += dir * _Outline;o.pos = UnityObjectToClipPos(v.vertex);UNITY_TRANSFER_FOG(o, o.pos);return o;}float4 frag(v2f i) : SV_Target { float4 c = _OutColor;UNITY_APPLY_FOG(i.fogCoord, c);return c; }ENDCG}CGPROGRAM#pragma surface surf Toon addshadow fullforwardshadows exclude_path:deferred exclude_path:prepass#pragma surface surf Standard fullforwardshadows keepalpha#pragma target 3.0fixed4 _Color;fixed4 _HColor;fixed4 _SColor;sampler2D _MainTex;float _RampThreshold;float _RampSmooth;float _ToonSteps;float _SpecSmooth;fixed _Shininess;fixed4 _RimColor;fixed _RimThreshold;float _RimSmooth;float _HueOffset;float _SaturationOffset;float _ValueOffset;float _BloomFactor;fixed4 _FresnealColor;float _FresnealScale;struct Input{float2 uv_MainTex;float3 viewDir; };struct v2f { float4 pos : SV_POSITION; float3 worldNormal : TEXCOORD0; }; v2f vert(appdata_base v) { v2f o; o.pos = UnityObjectToClipPos(v.vertex); o.worldNormal = mul(v.normal, (float3x3)unity_WorldToObject); return o; } float linearstep(float min, float max, float t){return saturate((t - min) / (max - min));}inline fixed4 LightingToon(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten){half3 normalDir = normalize(s.Normal);half3 halfDir = normalize(lightDir + viewDir);float ndl = max(0, dot(normalDir, lightDir));float ndh = max(0, dot(normalDir, halfDir));float ndv = max(0, dot(normalDir, viewDir));fixed3 Fresneal = pow(1.0 - max(0,dot(normalDir, viewDir)),_FresnealScale)*_FresnealColor.rgb;fixed4 finalFresneal = fixed4(Fresneal,1);// multi stepsfloat diff = smoothstep(_RampThreshold - ndl, _RampThreshold + ndl, ndl);float interval = 1 / _ToonSteps;// float ramp = floor(diff * _ToonSteps) / _ToonSteps;float level = round(diff * _ToonSteps) / _ToonSteps;float ramp ;if (_RampSmooth == 1){ramp = interval * linearstep(level - _RampSmooth * interval * 0.5, level + _RampSmooth * interval * 0.5, diff) + level - interval;}else{ramp = interval * smoothstep(level - _RampSmooth * interval * 0.5, level + _RampSmooth * interval * 0.5, diff) + level - interval;}ramp = max(0, ramp);ramp *= atten;_SColor = lerp(_HColor, _SColor, _SColor.a);float3 rampColor = lerp(_SColor.rgb, _HColor.rgb, ramp);// specularfloat spec = pow(ndh, s.Specular * 128.0) * s.Gloss;spec *= atten;spec = smoothstep(0.5 - _SpecSmooth * 0.5, 0.5 + _SpecSmooth * 0.5, spec);// rimfloat rim = (1.0 - ndv) * ndl;rim *= atten;rim = smoothstep(_RimThreshold - _RimSmooth * 0.5, _RimThreshold + _RimSmooth * 0.5, rim);fixed4 color;fixed3 diffuse = s.Albedo * rampColor *(ndv + 1.5);fixed3 specular = _SpecColor.rgb * spec;fixed3 rimColor = _RimColor.rgb * _RimColor.a * rim;float3 rgb = diffuse.rgb;float4 k = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);float4 p = lerp(float4(rgb.bg, k.wz), float4(rgb.gb, k.xy), step(rgb.b, rgb.g));// 比较r和max(b,g)float4 q = lerp(float4(p.xyw, rgb.r), float4(rgb.r, p.yzx), step(p.x, rgb.r));float d = q.x - min(q.w, q.y);float e = 1.0e-10;float3 hsv = float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);hsv.x = hsv.x + _HueOffset;hsv.y = hsv.y + _SaturationOffset;hsv.z = hsv.z + _ValueOffset;rgb = saturate(3.0*abs(1.0-2.0*frac(hsv.x+float3(0.0,-1.0/3.0,1.0/3.0)))-1); //明度和饱和度为1时的颜色rgb = (lerp(float3(1,1,1),rgb,hsv.y)*hsv.z); // hsvcolor.rgb = rgb + specular + rimColor;color.a = s.Alpha;fixed4 col;col.rgb = color.rgb;col.a = 1;return col + finalFresneal;}void surf(Input IN, inout SurfaceOutput o){fixed4 mainTex = tex2D(_MainTex, IN.uv_MainTex);o.Albedo = mainTex.rgb * _Color.rgb;o.Alpha = _BloomFactor;o.Specular = _Shininess;o.Gloss = mainTex.a;}ENDCG}FallBack "Diffuse"
}
镰刀刃部分
Shader "Shader Forge/WeaponShader" {Properties {_MainTex ("MainTex", 2D) = "white" {}_Emission ("Emission", Range(0, 1)) = 0.6_Metallic ("Metallic", Range(0, 1)) = 0.73_Gloss ("Gloss", Range(0, 1)) = 0.69_Normal ("Normal", 2D) = "black" {}_Speed ("Speed", Range(0, 5)) = 0.4_MeltTex ("MeltTex", 2D) = "black" {}_Intensity ("Intensity", Range(0, 1)) = 0.2436957[HDR]_MianColor ("MianColor", Color) = (0.2426471,0.8119676,1,1)_FresnelScale ("FresnelScale", Range(0.01, 10)) = 4.188035[HDR]_FresnelColor ("FresnelColor", Color) = (0.3676471,0.8430021,1,1)}SubShader {Tags {"RenderType"="Opaque"}Pass {Name "FORWARD"Tags {"LightMode"="ForwardBase"}CGPROGRAM#pragma vertex vert#pragma fragment frag#define UNITY_PASS_FORWARDBASE#define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )#define _GLOSSYENV 1#include "UnityCG.cginc"#include "AutoLight.cginc"#include "Lighting.cginc"#include "UnityPBSLighting.cginc"#include "UnityStandardBRDF.cginc"#pragma multi_compile_fwdbase_fullshadows#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON#pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE#pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform sampler2D _MainTex; uniform float4 _MainTex_ST;uniform float _Emission;uniform float _Metallic;uniform float _Gloss;uniform sampler2D _Normal; uniform float4 _Normal_ST;uniform float _Speed;uniform sampler2D _MeltTex; uniform float4 _MeltTex_ST;uniform float _Intensity;uniform float4 _MianColor;uniform float _FresnelScale;uniform float4 _FresnelColor;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float2 texcoord0 : TEXCOORD0;float2 texcoord1 : TEXCOORD1;float2 texcoord2 : TEXCOORD2;};struct VertexOutput {float4 pos : SV_POSITION;float2 uv0 : TEXCOORD0;float2 uv1 : TEXCOORD1;float2 uv2 : TEXCOORD2;float4 posWorld : TEXCOORD3;float3 normalDir : TEXCOORD4;float3 tangentDir : TEXCOORD5;float3 bitangentDir : TEXCOORD6;LIGHTING_COORDS(7,8)UNITY_FOG_COORDS(9)#if defined(LIGHTMAP_ON) || defined(UNITY_SHOULD_SAMPLE_SH)float4 ambientOrLightmapUV : TEXCOORD10;#endif};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.uv0 = v.texcoord0;o.uv1 = v.texcoord1;o.uv2 = v.texcoord2;#ifdef LIGHTMAP_ONo.ambientOrLightmapUV.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;o.ambientOrLightmapUV.zw = 0;#elif UNITY_SHOULD_SAMPLE_SH#endif#ifdef DYNAMICLIGHTMAP_ONo.ambientOrLightmapUV.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw;#endifo.normalDir = UnityObjectToWorldNormal(v.normal);o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);o.posWorld = mul(unity_ObjectToWorld, v.vertex);float3 lightColor = _LightColor0.rgb;o.pos = UnityObjectToClipPos( v.vertex );UNITY_TRANSFER_FOG(o,o.pos);TRANSFER_VERTEX_TO_FRAGMENT(o)return o;}float4 frag(VertexOutput i) : COLOR {i.normalDir = normalize(i.normalDir);float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);float4 _Normal_var = tex2D(_Normal,TRANSFORM_TEX(i.uv0, _Normal));float3 normalLocal = _Normal_var.rgb;float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normalsfloat3 viewReflectDirection = reflect( -viewDirection, normalDirection );float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);float3 lightColor = _LightColor0.rgb;float3 halfDirection = normalize(viewDirection+lightDirection);
// Lighting:float attenuation = LIGHT_ATTENUATION(i);float3 attenColor = attenuation * _LightColor0.xyz;float Pi = 3.141592654;float InvPi = 0.31830988618;
/ Gloss:float gloss = _Gloss;float perceptualRoughness = 1.0 - _Gloss;float roughness = perceptualRoughness * perceptualRoughness;float specPow = exp2( gloss * 10.0 + 1.0 );
/// GI Data:UnityLight light;#ifdef LIGHTMAP_OFFlight.color = lightColor;light.dir = lightDirection;light.ndotl = LambertTerm (normalDirection, light.dir);#elselight.color = half3(0.f, 0.f, 0.f);light.ndotl = 0.0f;light.dir = half3(0.f, 0.f, 0.f);#endifUnityGIInput d;d.light = light;d.worldPos = i.posWorld.xyz;d.worldViewDir = viewDirection;d.atten = attenuation;#if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)d.ambient = 0;d.lightmapUV = i.ambientOrLightmapUV;#elsed.ambient = i.ambientOrLightmapUV;#endif#if UNITY_SPECCUBE_BLENDING || UNITY_SPECCUBE_BOX_PROJECTIONd.boxMin[0] = unity_SpecCube0_BoxMin;d.boxMin[1] = unity_SpecCube1_BoxMin;#endif#if UNITY_SPECCUBE_BOX_PROJECTIONd.boxMax[0] = unity_SpecCube0_BoxMax;d.boxMax[1] = unity_SpecCube1_BoxMax;d.probePosition[0] = unity_SpecCube0_ProbePosition;d.probePosition[1] = unity_SpecCube1_ProbePosition;#endifd.probeHDR[0] = unity_SpecCube0_HDR;d.probeHDR[1] = unity_SpecCube1_HDR;Unity_GlossyEnvironmentData ugls_en_data;ugls_en_data.roughness = 1.0 - gloss;ugls_en_data.reflUVW = viewReflectDirection;UnityGI gi = UnityGlobalIllumination(d, 1, normalDirection, ugls_en_data );lightDirection = gi.light.dir;lightColor = gi.light.color;
// Specular:float NdotL = saturate(dot( normalDirection, lightDirection ));float LdotH = saturate(dot(lightDirection, halfDirection));float3 specularColor = _Metallic;float specularMonochrome;float4 node_3538 = _Time;float2 node_6877 = (i.uv0+(_Speed*node_3538.g)*float2(1,1));float4 _MeltTex_var = tex2D(_MeltTex,TRANSFORM_TEX(node_6877, _MeltTex));float2 node_8337 = lerp(i.uv0,(float3(i.uv0,0.0)*_MeltTex_var.rgb*2.0).rg,_Intensity);float4 node_4378 = tex2D(_MainTex,TRANSFORM_TEX(node_8337, _MainTex));float3 node_5719 = (_MianColor.rgb*node_4378.rgb);float3 diffuseColor = ((_FresnelColor.rgb*pow(1.0-max(0,dot(normalDirection, viewDirection)),_FresnelScale))+(node_5719+node_5719)); // Need this for specular when using metallicdiffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );specularMonochrome = 1.0-specularMonochrome;float NdotV = abs(dot( normalDirection, viewDirection ));float NdotH = saturate(dot( normalDirection, halfDirection ));float VdotH = saturate(dot( viewDirection, halfDirection ));float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );float normTerm = GGXTerm(NdotH, roughness);float specularPBL = (visTerm*normTerm) * UNITY_PI;#ifdef UNITY_COLORSPACE_GAMMAspecularPBL = sqrt(max(1e-4h, specularPBL));#endifspecularPBL = max(0, specularPBL * NdotL);#if defined(_SPECULARHIGHLIGHTS_OFF)specularPBL = 0.0;#endifhalf surfaceReduction;#ifdef UNITY_COLORSPACE_GAMMAsurfaceReduction = 1.0-0.28*roughness*perceptualRoughness;#elsesurfaceReduction = 1.0/(roughness*roughness + 1.0);#endifspecularPBL *= any(specularColor) ? 1.0 : 0.0;float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);half grazingTerm = saturate( gloss + specularMonochrome );float3 indirectSpecular = (gi.indirect.specular);indirectSpecular *= FresnelLerp (specularColor, grazingTerm, NdotV);indirectSpecular *= surfaceReduction;float3 specular = (directSpecular + indirectSpecular);
/// Diffuse:NdotL = max(0.0,dot( normalDirection, lightDirection ));half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);float nlPow5 = Pow5(1-NdotL);float nvPow5 = Pow5(1-NdotV);float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;float3 indirectDiffuse = float3(0,0,0);indirectDiffuse += gi.indirect.diffuse;float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
// Emissive:float node_5997 = (node_4378.b*_Emission);float3 emissive = float3(node_5997,node_5997,node_5997);
/// Final Color:float3 finalColor = diffuse + specular + emissive;fixed4 finalRGBA = fixed4(finalColor,1);UNITY_APPLY_FOG(i.fogCoord, finalRGBA);return finalRGBA;}ENDCG}Pass {Name "FORWARD_DELTA"Tags {"LightMode"="ForwardAdd"}Blend One OneCGPROGRAM#pragma vertex vert#pragma fragment frag#define UNITY_PASS_FORWARDADD#define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )#define _GLOSSYENV 1#include "UnityCG.cginc"#include "AutoLight.cginc"#include "Lighting.cginc"#include "UnityPBSLighting.cginc"#include "UnityStandardBRDF.cginc"#pragma multi_compile_fwdadd_fullshadows#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON#pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE#pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform sampler2D _MainTex; uniform float4 _MainTex_ST;uniform float _Emission;uniform float _Metallic;uniform float _Gloss;uniform sampler2D _Normal; uniform float4 _Normal_ST;uniform float _Speed;uniform sampler2D _MeltTex; uniform float4 _MeltTex_ST;uniform float _Intensity;uniform float4 _MianColor;uniform float _FresnelScale;uniform float4 _FresnelColor;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;float4 tangent : TANGENT;float2 texcoord0 : TEXCOORD0;float2 texcoord1 : TEXCOORD1;float2 texcoord2 : TEXCOORD2;};struct VertexOutput {float4 pos : SV_POSITION;float2 uv0 : TEXCOORD0;float2 uv1 : TEXCOORD1;float2 uv2 : TEXCOORD2;float4 posWorld : TEXCOORD3;float3 normalDir : TEXCOORD4;float3 tangentDir : TEXCOORD5;float3 bitangentDir : TEXCOORD6;LIGHTING_COORDS(7,8)UNITY_FOG_COORDS(9)};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.uv0 = v.texcoord0;o.uv1 = v.texcoord1;o.uv2 = v.texcoord2;o.normalDir = UnityObjectToWorldNormal(v.normal);o.tangentDir = normalize( mul( unity_ObjectToWorld, float4( v.tangent.xyz, 0.0 ) ).xyz );o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w);o.posWorld = mul(unity_ObjectToWorld, v.vertex);float3 lightColor = _LightColor0.rgb;o.pos = UnityObjectToClipPos( v.vertex );UNITY_TRANSFER_FOG(o,o.pos);TRANSFER_VERTEX_TO_FRAGMENT(o)return o;}float4 frag(VertexOutput i) : COLOR {i.normalDir = normalize(i.normalDir);float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);float4 _Normal_var = tex2D(_Normal,TRANSFORM_TEX(i.uv0, _Normal));float3 normalLocal = _Normal_var.rgb;float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normalsfloat3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));float3 lightColor = _LightColor0.rgb;float3 halfDirection = normalize(viewDirection+lightDirection);
// Lighting:float attenuation = LIGHT_ATTENUATION(i);float3 attenColor = attenuation * _LightColor0.xyz;float Pi = 3.141592654;float InvPi = 0.31830988618;
/ Gloss:float gloss = _Gloss;float perceptualRoughness = 1.0 - _Gloss;float roughness = perceptualRoughness * perceptualRoughness;float specPow = exp2( gloss * 10.0 + 1.0 );
// Specular:float NdotL = saturate(dot( normalDirection, lightDirection ));float LdotH = saturate(dot(lightDirection, halfDirection));float3 specularColor = _Metallic;float specularMonochrome;float4 node_3538 = _Time;float2 node_6877 = (i.uv0+(_Speed*node_3538.g)*float2(1,1));float4 _MeltTex_var = tex2D(_MeltTex,TRANSFORM_TEX(node_6877, _MeltTex));float2 node_8337 = lerp(i.uv0,(float3(i.uv0,0.0)*_MeltTex_var.rgb*2.0).rg,_Intensity);float4 node_4378 = tex2D(_MainTex,TRANSFORM_TEX(node_8337, _MainTex));float3 node_5719 = (_MianColor.rgb*node_4378.rgb);float3 diffuseColor = ((_FresnelColor.rgb*pow(1.0-max(0,dot(normalDirection, viewDirection)),_FresnelScale))+(node_5719+node_5719)); // Need this for specular when using metallicdiffuseColor = DiffuseAndSpecularFromMetallic( diffuseColor, specularColor, specularColor, specularMonochrome );specularMonochrome = 1.0-specularMonochrome;float NdotV = abs(dot( normalDirection, viewDirection ));float NdotH = saturate(dot( normalDirection, halfDirection ));float VdotH = saturate(dot( viewDirection, halfDirection ));float visTerm = SmithJointGGXVisibilityTerm( NdotL, NdotV, roughness );float normTerm = GGXTerm(NdotH, roughness);float specularPBL = (visTerm*normTerm) * UNITY_PI;#ifdef UNITY_COLORSPACE_GAMMAspecularPBL = sqrt(max(1e-4h, specularPBL));#endifspecularPBL = max(0, specularPBL * NdotL);#if defined(_SPECULARHIGHLIGHTS_OFF)specularPBL = 0.0;#endifspecularPBL *= any(specularColor) ? 1.0 : 0.0;float3 directSpecular = attenColor*specularPBL*FresnelTerm(specularColor, LdotH);float3 specular = directSpecular;
/// Diffuse:NdotL = max(0.0,dot( normalDirection, lightDirection ));half fd90 = 0.5 + 2 * LdotH * LdotH * (1-gloss);float nlPow5 = Pow5(1-NdotL);float nvPow5 = Pow5(1-NdotV);float3 directDiffuse = ((1 +(fd90 - 1)*nlPow5) * (1 + (fd90 - 1)*nvPow5) * NdotL) * attenColor;float3 diffuse = directDiffuse * diffuseColor;
/// Final Color:float3 finalColor = diffuse + specular;fixed4 finalRGBA = fixed4(finalColor * 1,0);UNITY_APPLY_FOG(i.fogCoord, finalRGBA);return finalRGBA;}ENDCG}Pass {Name "Meta"Tags {"LightMode"="Meta"}Cull OffCGPROGRAM#pragma vertex vert#pragma fragment frag#define UNITY_PASS_META 1#define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) )#define _GLOSSYENV 1#include "UnityCG.cginc"#include "Lighting.cginc"#include "UnityPBSLighting.cginc"#include "UnityStandardBRDF.cginc"#include "UnityMetaPass.cginc"#pragma fragmentoption ARB_precision_hint_fastest#pragma multi_compile_shadowcaster#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON#pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE#pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON#pragma multi_compile_fog#pragma only_renderers d3d9 d3d11 glcore gles #pragma target 3.0uniform sampler2D _MainTex; uniform float4 _MainTex_ST;uniform float _Emission;uniform float _Metallic;uniform float _Gloss;uniform float _Speed;uniform sampler2D _MeltTex; uniform float4 _MeltTex_ST;uniform float _Intensity;uniform float4 _MianColor;uniform float _FresnelScale;uniform float4 _FresnelColor;struct VertexInput {float4 vertex : POSITION;float3 normal : NORMAL;float2 texcoord0 : TEXCOORD0;float2 texcoord1 : TEXCOORD1;float2 texcoord2 : TEXCOORD2;};struct VertexOutput {float4 pos : SV_POSITION;float2 uv0 : TEXCOORD0;float2 uv1 : TEXCOORD1;float2 uv2 : TEXCOORD2;float4 posWorld : TEXCOORD3;float3 normalDir : TEXCOORD4;};VertexOutput vert (VertexInput v) {VertexOutput o = (VertexOutput)0;o.uv0 = v.texcoord0;o.uv1 = v.texcoord1;o.uv2 = v.texcoord2;o.normalDir = UnityObjectToWorldNormal(v.normal);o.posWorld = mul(unity_ObjectToWorld, v.vertex);o.pos = UnityMetaVertexPosition(v.vertex, v.texcoord1.xy, v.texcoord2.xy, unity_LightmapST, unity_DynamicLightmapST );return o;}float4 frag(VertexOutput i) : SV_Target {i.normalDir = normalize(i.normalDir);float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);float3 normalDirection = i.normalDir;UnityMetaInput o;UNITY_INITIALIZE_OUTPUT( UnityMetaInput, o );float4 node_3538 = _Time;float2 node_6877 = (i.uv0+(_Speed*node_3538.g)*float2(1,1));float4 _MeltTex_var = tex2D(_MeltTex,TRANSFORM_TEX(node_6877, _MeltTex));float2 node_8337 = lerp(i.uv0,(float3(i.uv0,0.0)*_MeltTex_var.rgb*2.0).rg,_Intensity);float4 node_4378 = tex2D(_MainTex,TRANSFORM_TEX(node_8337, _MainTex));float node_5997 = (node_4378.b*_Emission);o.Emission = float3(node_5997,node_5997,node_5997);float3 node_5719 = (_MianColor.rgb*node_4378.rgb);float3 diffColor = ((_FresnelColor.rgb*pow(1.0-max(0,dot(normalDirection, viewDirection)),_FresnelScale))+(node_5719+node_5719));float specularMonochrome;float3 specColor;diffColor = DiffuseAndSpecularFromMetallic( diffColor, _Metallic, specColor, specularMonochrome );float roughness = 1.0 - _Gloss;o.Albedo = diffColor + specColor * roughness * roughness * 0.5;return UnityMetaFragment( o );}ENDCG}}FallBack "Diffuse"CustomEditor "ShaderForgeMaterialInspector"
}