Unity信号干扰shader(参照崩坏3源码翻译剧情对话效果)

最近做的项目是二次元,二次元的标杆就是崩坏3,效果真的好

学习崩坏3里面shader最好的方法就是看源码

可以AssetStudio来看资源,因为崩坏3资源是没有加密直接可以看

Unity信号干扰shader(参照崩坏3源码翻译剧情对话效果)_第1张图片
 

不过shader源码是已经编译好的,其实大概算法都在

我已经研究角色身上的shader,这个有空写博客,用到顶点绘制的工具来填充颜色来处理阴影

这次我参考崩坏里面的信号干扰的shader

之前搞个一个类似信号干扰

https://blog.csdn.net/SnoopyNa2Co3/article/details/84673736

下面效果

我看了一下没有特别的算法,那个噪音的都是经验公式,了解的话可以自行搜索一下

下面是编译过的shader

Shader "miHoYo/UI/Image TV Distortion" {
Properties {
_MainTex ("Sprite Texture", 2D) = "white" { }
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
_BackgroundColor ("Barckground Color (RGBA)", Color) = (0,0,0,1)
_AdjustColor ("Adjust Color (RGB)", Color) = (0,0,0,1)
_DistortionTex ("Distortion Tex (RG)", 2D) = "gray" { }
_DistortionFrequency ("Distortion Frequency", Float) = 1
_DistortionAmplitude ("Distortion Amplitude", Range(0, 1)) = 1
_DistortionAnmSpeed ("Distortion Animation Speed", Float) = 1
_ColorScatterStrength ("Color Scatter Strength", Range(-0.1, 0.1)) = 0.01
_NoiseTex ("Noise Tex (RGB)", 2D) = "black" { }
_NoiseAnmSpeed ("Noise Animation Speed", Float) = 1
_NoiseStrength ("Noise Strength", Float) = 1
_BloomFactor ("Bloom Factor", Float) = 1
[Toggle(RECT_MASK)] _RectMask ("Rect Mask", Float) = 0
_RMRect ("Rect Mask Rect", Vector) = (0,0,1,1)
_RMTransitWidth ("Rect Mask Transit Width", Vector) = (0,0,0,0)
}
SubShader {
 Tags { "CanUseSpriteAtlas" = "true" "IGNOREPROJECTOR" = "true" "PreviewType" = "Plane" "QUEUE" = "Transparent+1" "RenderType" = "Transparent" }
 Pass {
  Tags { "CanUseSpriteAtlas" = "true" "IGNOREPROJECTOR" = "true" "PreviewType" = "Plane" "QUEUE" = "Transparent+1" "RenderType" = "Transparent" }
  ZClip Off
  ZTest Off
  ZWrite Off
  Cull Off
  GpuProgramID 59740
Program "vp" {
SubProgram "gles hw_tier00 " {
"#version 100

#ifdef VERTEX
attribute vec4 _glesVertex;
attribute vec4 _glesColor;
attribute vec4 _glesMultiTexCoord0;
uniform highp vec4 _Time;
uniform highp vec4 _SinTime;
uniform highp vec4 _CosTime;
uniform highp mat4 unity_ObjectToWorld;
uniform highp mat4 unity_MatrixVP;
uniform mediump float _DistortionFrequency;
uniform mediump float _DistortionAnmSpeed;
uniform highp vec4 _NoiseTex_ST;
uniform highp float _NoiseAnmSpeed;
uniform lowp vec4 _Color;
varying lowp vec4 xlv_COLOR;
varying mediump vec2 xlv_TEXCOORD0;
varying mediump vec2 xlv_TEXCOORD1;
varying mediump vec2 xlv_TEXCOORD2;
void main ()
{
  highp vec2 tmpvar_1;
  tmpvar_1 = _glesMultiTexCoord0.xy;
  lowp vec4 tmpvar_2;
  mediump vec2 tmpvar_3;
  mediump vec2 tmpvar_4;
  mediump vec2 tmpvar_5;
  highp vec4 tmpvar_6;
  tmpvar_6.w = 1.0;
  tmpvar_6.xyz = _glesVertex.xyz;
  tmpvar_3 = tmpvar_1;
  highp vec2 tmpvar_7;
  tmpvar_7.x = (_Time.y * _DistortionAnmSpeed);
  tmpvar_7.y = (_glesMultiTexCoord0.y * _DistortionFrequency);
  tmpvar_4 = tmpvar_7;
  tmpvar_5 = ((_glesMultiTexCoord0.xy * _NoiseTex_ST.xy) + _NoiseTex_ST.zw);
  highp vec3 tmpvar_8;
  tmpvar_8 = fract((sin(
    (_SinTime.w * vec3(12.9898, 78.233, 45.5432))
  ) * 43758.55));
  highp vec3 tmpvar_9;
  tmpvar_9 = fract((sin(
    (_CosTime.x * vec3(12.9898, 78.233, 45.5432))
  ) * 43758.55));
  tmpvar_5.x = (tmpvar_5.x + ((tmpvar_8.x + tmpvar_9.x) * _NoiseAnmSpeed));
  highp vec3 tmpvar_10;
  tmpvar_10 = fract((sin(
    (_SinTime.x * vec3(12.9898, 78.233, 45.5432))
  ) * 43758.55));
  highp vec3 tmpvar_11;
  tmpvar_11 = fract((sin(
    (_CosTime.w * vec3(12.9898, 78.233, 45.5432))
  ) * 43758.55));
  tmpvar_5.y = (tmpvar_5.y + ((tmpvar_10.x + tmpvar_11.x) * _NoiseAnmSpeed));
  tmpvar_2 = (_glesColor * _Color);
  gl_Position = (unity_MatrixVP * (unity_ObjectToWorld * tmpvar_6));
  xlv_COLOR = tmpvar_2;
  xlv_TEXCOORD0 = tmpvar_3;
  xlv_TEXCOORD1 = tmpvar_4;
  xlv_TEXCOORD2 = tmpvar_5;
}


#endif
#ifdef FRAGMENT
uniform mediump vec4 _BackgroundColor;
uniform mediump vec3 _AdjustColor;
uniform sampler2D _DistortionTex;
uniform mediump float _DistortionAmplitude;
uniform mediump float _ColorScatterStrength;
uniform sampler2D _NoiseTex;
uniform mediump float _NoiseStrength;
uniform sampler2D _MainTex;
varying lowp vec4 xlv_COLOR;
varying mediump vec2 xlv_TEXCOORD0;
varying mediump vec2 xlv_TEXCOORD1;
varying mediump vec2 xlv_TEXCOORD2;
void main ()
{
  lowp vec4 tmpvar_1;
  mediump vec4 color_2;
  mediump float offset_3;
  lowp float tmpvar_4;
  tmpvar_4 = (texture2D (_DistortionTex, xlv_TEXCOORD1) - 0.498).x;
  offset_3 = tmpvar_4;
  offset_3 = (offset_3 * _DistortionAmplitude);
  color_2.yz = vec2(0.0, 0.0);
  mediump vec2 tmpvar_5;
  tmpvar_5.y = 0.0;
  tmpvar_5.x = _ColorScatterStrength;
  lowp vec4 tmpvar_6;
  mediump vec2 P_7;
  P_7 = ((xlv_TEXCOORD0 + offset_3) + tmpvar_5);
  tmpvar_6 = texture2D (_MainTex, P_7);
  color_2.xw = tmpvar_6.xw;
  lowp vec4 tmpvar_8;
  mediump vec2 P_9;
  P_9 = (xlv_TEXCOORD0 + offset_3);
  tmpvar_8 = texture2D (_MainTex, P_9);
  color_2.yw = (color_2.yw + tmpvar_8.yw);
  mediump vec2 tmpvar_10;
  tmpvar_10.y = 0.0;
  tmpvar_10.x = _ColorScatterStrength;
  lowp vec4 tmpvar_11;
  mediump vec2 P_12;
  P_12 = ((xlv_TEXCOORD0 + offset_3) - tmpvar_10);
  tmpvar_11 = texture2D (_MainTex, P_12);
  color_2.zw = (color_2.zw + tmpvar_11.zw);
  color_2.xyz = (color_2.xyz * xlv_COLOR.xyz);
  color_2.w = clamp (color_2.w, 0.0, 1.0);
  mediump vec4 tmpvar_13;
  if ((color_2.w < 0.5)) {
    tmpvar_13 = _BackgroundColor;
  } else {
    tmpvar_13 = color_2;
  };
  color_2.w = tmpvar_13.w;
  color_2.xyz = (1.0 - ((1.0 - tmpvar_13.xyz) * (1.0 - _AdjustColor)));
  lowp vec4 tmpvar_14;
  tmpvar_14 = texture2D (_NoiseTex, xlv_TEXCOORD2);
  color_2.xyz = (1.0 - ((1.0 - color_2.xyz) * (1.0 - 
    (tmpvar_14 * _NoiseStrength)
  .xyz)));
  tmpvar_1 = color_2;
  gl_FragData[0] = tmpvar_1;
}

就不多说如果会shader应该会翻译上面的shader,就直接放出来,里面有一些注释方便学习

下面直接放我根据上面源码实现的unity shader

Shader "Custom/TVDistortion" 
{
	Properties 
	{
		_MainTex("Sprite Texture", 2D) = "white" { }
		_Color("Tint", Color) = (1,1,1,1)
		_BackgroundColor("Barckground Color (RGBA)", Color) = (0,0,0,1)
		_AdjustColor("Adjust Color (RGB)", Color) = (0,0,0,1)
		_DistortionTex("Distortion Tex (RG)", 2D) = "gray" { }
		_DistortionFrequency("Distortion Frequency", Float) = 1
		_DistortionAmplitude("Distortion Amplitude", Range(0, 1)) = 1
		_DistortionAnmSpeed("Distortion Animation Speed", Float) = 1
		_ColorScatterStrength("Color Scatter Strength", Range(-0.1, 0.1)) = 0.01
		_NoiseTex("Noise Tex (RGB)", 2D) = "black" { }
		_NoiseAnmSpeed("Noise Animation Speed", Float) = 1
		_NoiseStrength("Noise Strength", Float) = 1
	}

	SubShader
	{
		Pass
		{
			ZTest Always Cull Off ZWrite Off
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#pragma target 3.0
			#pragma multi_compile_fog
			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};
			struct v2f
			{
				float4 uv : TEXCOORD0;
				float4 Color : COLOR;
				float4 Distortion_UV : TEXCOORD1;
				float4 Noise_UV : TEXCOORD2;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float4 _Color;
			float4 _BackgroundColor;
			float4 _AdjustColor;
			sampler2D _DistortionTex;
			float _DistortionFrequency;
			float _DistortionAmplitude;
			float _DistortionAnmSpeed;
			float _ColorScatterStrength;
			sampler2D _NoiseTex;
			float4 _NoiseTex_ST;
			float _NoiseAnmSpeed;
			float _NoiseStrength;

		v2f vert(appdata v)
		{
			v2f o;
			o.uv.xy = v.uv.xy;
			o.vertex = UnityObjectToClipPos(v.vertex);

			float4 distortUV;
			//扭曲图UV
			distortUV.x = (_Time.y * _DistortionAnmSpeed);
			distortUV.y = (v.uv.y * _DistortionFrequency);
			o.Distortion_UV = distortUV;

			//噪音图UV
			float2 noiseUV = TRANSFORM_TEX(v.uv, _NoiseTex);
			float3 noise1 = frac((sin((_SinTime.w * float3(12.9898, 78.233, 45.5432))) * 43758.55));
			float3 noise2 = frac((sin((_CosTime.x * float3(12.9898, 78.233, 45.5432))) * 43758.55));
			noiseUV.x = (noiseUV.x + ((noise1.x + noise2.x) * _NoiseAnmSpeed));

			float3 noise3 = frac((sin((_SinTime.x * float3(12.9898, 78.233, 45.5432))) * 43758.55));
			float3 noise4 = frac((sin((_CosTime.w * float3(12.9898, 78.233, 45.5432))) * 43758.55));
			noiseUV.y = (noiseUV.y + ((noise3.x + noise4.x) * _NoiseAnmSpeed));


			o.Noise_UV = float4(noiseUV, 0, 0);
			UNITY_TRANSFER_FOG(o, o.vertex);
			return o;
		}

		half4 frag(v2f i) : SV_Target
		{
			float4 color;
			color.yz = float2(0.0, 0.0);
			//获取扭曲图的偏移位置
			half offset = (tex2D(_DistortionTex, i.Distortion_UV.xy) - 0.498).x * _DistortionAmplitude;
			//颜色偏移强度(左右)
			float2 ColorStrength = float2(_ColorScatterStrength, 0.0);
			//红色偏移
			float4 redOffset = tex2D(_MainTex, ((i.uv.xy + offset) + ColorStrength));
			color.xw = redOffset.xw;
			//绿色位置不变
			float4 greenOffset = tex2D(_MainTex, i.uv.xy + offset);
			color.yw = (color.yw + greenOffset.yw);
			//蓝色偏移
			float4 blueOffset = tex2D(_MainTex,(i.uv.xy + offset) - ColorStrength);
			color.zw = (color.zw + blueOffset.zw);

			color.w = clamp(color.w, 0.0, 1.0);
			//如果是半透则使用背景颜色
			if ((color.w < 0.5)) 
			{
				color = _BackgroundColor;
			}

			//颜色调整
			color.xyz = (1.0 - ((1.0 - color.xyz) * (1.0 - _AdjustColor)));

			//噪音图叠加
			float4 noiseColor;
			noiseColor = tex2D(_NoiseTex, i.Noise_UV.xy);
			color.xyz = (1.0 - ((1.0 - color.xyz) * (1.0 -(noiseColor * _NoiseStrength).xyz)));
			return color;
		}
			ENDCG
		}
	}
	FallBack "Diffuse"
}

 

你可能感兴趣的:(Shader)