日落20181220001 - Unity插件应用之ShaderForge使用噪声纹理制作效果
程序员文章站
2022-03-02 15:50:37
环境系统:Windows 10引擎:Unity 2017.2.1f1插件:Shader Forge 1.38工具:Photoshop目的Shader Forge使用噪声纹理制作效果的实例。(1)首先需要四张图:(1-1)材质纹理,使用Shader Forge自带。(1-2)法线纹理,使用Shader Forge自带。(1-3)噪声纹理,使用Shader Forge自......
环境
系统:Windows 10
引擎:Unity 2017.2.1f1
插件:Shader Forge 1.38
工具:Photoshop
目的
Shader Forge使用噪声纹理制作效果的实例。
(1)首先需要四张图:
(1-1)材质纹理,使用Shader Forge自带。
(1-2)法线纹理,使用Shader Forge自带。
(1-3)噪声纹理,使用Shader Forge自带。
(1-4)渐变图,使用Photoshop制作,注意左侧边缘处加入1px的透明。
(2)场景布局如下。
(3)Shader Forge编辑如下。(编辑方式看《Unity插件应用之ShaderForge制作颜色渐变效果》)
(4)记得勾选OpenGLES 3.0哦。
(5)其shader代码如下。
// 前略...
Shader "Shader Forge/DissolveShader" {
Properties {
_noise ("noise", 2D) = "white" {}
_dissolve_amount ("dissolve_amount", Range(0, 1)) = 0.4982279
_node_9421 ("node_9421", 2D) = "bump" {}
_node_5041 ("node_5041", 2D) = "white" {}
_ramp ("ramp", 2D) = "white" {}
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"Queue"="AlphaTest"
"RenderType"="TransparentCutout"
}
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardBRDF.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _noise; uniform float4 _noise_ST;
uniform float _dissolve_amount;
uniform sampler2D _node_5041; uniform float4 _node_5041_ST;
uniform sampler2D _node_9421; uniform float4 _node_9421_ST;
uniform sampler2D _ramp; uniform float4 _ramp_ST;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
float3 tangentDir : TEXCOORD3;
float3 bitangentDir : TEXCOORD4;
LIGHTING_COORDS(5,6)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
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 );
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
float isFrontFace = ( facing >= 0 ? 1 : 0 );
float faceSign = ( facing >= 0 ? 1 : -1 );
i.normalDir = normalize(i.normalDir);
i.normalDir *= faceSign;
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 _node_9421_var = UnpackNormal(tex2D(_node_9421,TRANSFORM_TEX(i.uv0, _node_9421)));
float3 normalLocal = _node_9421_var.rgb;
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float3 viewReflectDirection = reflect( -viewDirection, normalDirection );
float4 _noise_var = tex2D(_noise,TRANSFORM_TEX(i.uv0, _noise));
float node_9277 = ((_dissolve_amount*1.4+-0.7)+_noise_var.r);
clip(node_9277 - 0.5);
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 = 0.5;
float perceptualRoughness = 1.0 - 0.5;
float roughness = perceptualRoughness * perceptualRoughness;
float specPow = exp2( gloss * 10.0 + 1.0 );
/////// GI Data:
UnityLight light;
#ifdef LIGHTMAP_OFF
light.color = lightColor;
light.dir = lightDirection;
light.ndotl = LambertTerm (normalDirection, light.dir);
#else
light.color = half3(0.f, 0.f, 0.f);
light.ndotl = 0.0f;
light.dir = half3(0.f, 0.f, 0.f);
#endif
UnityGIInput d;
d.light = light;
d.worldPos = i.posWorld.xyz;
d.worldViewDir = viewDirection;
d.atten = attenuation;
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 = 0.0;
float specularMonochrome;
float4 _node_5041_var = tex2D(_node_5041,TRANSFORM_TEX(i.uv0, _node_5041));
float3 diffuseColor = _node_5041_var.rgb; // Need this for specular when using metallic
diffuseColor = 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_GAMMA
specularPBL = sqrt(max(1e-4h, specularPBL));
#endif
specularPBL = max(0, specularPBL * NdotL);
#if defined(_SPECULARHIGHLIGHTS_OFF)
specularPBL = 0.0;
#endif
specularPBL *= 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 indirectDiffuse = float3(0,0,0);
indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
////// Emissive:
float2 node_6946 = float2(saturate((node_9277*8.0+-4.0)),0.0);
float4 node_2059 = tex2D(_ramp,TRANSFORM_TEX(node_6946, _ramp));
float3 emissive = node_2059.rgb;
/// Final Color:
float3 finalColor = diffuse + specular + emissive;
return fixed4(finalColor,1);
}
ENDCG
}
Pass {
Name "FORWARD_DELTA"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardBRDF.cginc"
#pragma multi_compile_fwdadd_fullshadows
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _noise; uniform float4 _noise_ST;
uniform float _dissolve_amount;
uniform sampler2D _node_5041; uniform float4 _node_5041_ST;
uniform sampler2D _node_9421; uniform float4 _node_9421_ST;
uniform sampler2D _ramp; uniform float4 _ramp_ST;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
float3 tangentDir : TEXCOORD3;
float3 bitangentDir : TEXCOORD4;
LIGHTING_COORDS(5,6)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
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 );
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
float isFrontFace = ( facing >= 0 ? 1 : 0 );
float faceSign = ( facing >= 0 ? 1 : -1 );
i.normalDir = normalize(i.normalDir);
i.normalDir *= faceSign;
float3x3 tangentTransform = float3x3( i.tangentDir, i.bitangentDir, i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 _node_9421_var = UnpackNormal(tex2D(_node_9421,TRANSFORM_TEX(i.uv0, _node_9421)));
float3 normalLocal = _node_9421_var.rgb;
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals
float4 _noise_var = tex2D(_noise,TRANSFORM_TEX(i.uv0, _noise));
float node_9277 = ((_dissolve_amount*1.4+-0.7)+_noise_var.r);
clip(node_9277 - 0.5);
float3 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 = 0.5;
float perceptualRoughness = 1.0 - 0.5;
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 = 0.0;
float specularMonochrome;
float4 _node_5041_var = tex2D(_node_5041,TRANSFORM_TEX(i.uv0, _node_5041));
float3 diffuseColor = _node_5041_var.rgb; // Need this for specular when using metallic
diffuseColor = 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_GAMMA
specularPBL = sqrt(max(1e-4h, specularPBL));
#endif
specularPBL = max(0, specularPBL * NdotL);
#if defined(_SPECULARHIGHLIGHTS_OFF)
specularPBL = 0.0;
#endif
specularPBL *= 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;
return fixed4(finalColor * 1,0);
}
ENDCG
}
Pass {
Name "ShadowCaster"
Tags {
"LightMode"="ShadowCaster"
}
Offset 1, 1
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_SHADOWCASTER
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardBRDF.cginc"
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcaster
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _noise; uniform float4 _noise_ST;
uniform float _dissolve_amount;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
V2F_SHADOW_CASTER;
float2 uv0 : TEXCOORD1;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos( v.vertex );
TRANSFER_SHADOW_CASTER(o)
return o;
}
float4 frag(VertexOutput i, float facing : VFACE) : COLOR {
float isFrontFace = ( facing >= 0 ? 1 : 0 );
float faceSign = ( facing >= 0 ? 1 : -1 );
float4 _noise_var = tex2D(_noise,TRANSFORM_TEX(i.uv0, _noise));
float node_9277 = ((_dissolve_amount*1.4+-0.7)+_noise_var.r);
clip(node_9277 - 0.5);
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
(6)给控件加入Gradient.cs,其代码如下。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gradient : MonoBehaviour {
public float m_GradientRate = 0.01f;
void LateUpdate () {
// 获取当前材质球
Renderer rend = this.gameObject.GetComponent<Renderer>();
Material mat = rend.material;
// 获取当前材质球属性
float dissolve_amount = mat.GetFloat("_dissolve_amount");
// 按固定速率改变材质球属性
dissolve_amount -= m_GradientRate;
if (dissolve_amount <= 0)
dissolve_amount = 1;
mat.SetFloat("_dissolve_amount", dissolve_amount);
}
}
(7)实测效果。
以上简单回顾。
参考:
Shader Forge - Gradients (Part 3, Color Ramps & Dissolve)
https://www.youtube.com/watch?v=qCkdVX1RhJ4
ShaderForge官网
http://acegikmo.com/shaderforge/nodes/?lang=zh_cn
《Unity Shader入门精要》第15章
本文地址:https://blog.csdn.net/minami_takumi/article/details/85900556