UnityShader实现简单的流光效果

本文主要实现了两个类型的流光效果,一个是Unilt shader实现的法阵流光,另一个是SurfaceShader实现的武器流光,其实也可以用Unilt shader实现,只是SurfaceShader的金属效果实现起来更加的简单。
先来看效果,开始是法阵流光
Alt
这个shader主要参考了本篇博客从圆心向外流光的魔法阵shader.
下一个是武器流光
Alt
由于UV的原因导致流光方向不一致,要是有个遮罩图就好了
以下是法阵流光的代码

Shader "Learn/FlowInsideOut"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_FlowColor ("Flow Color", Color) = (1, 1, 1, 1)//流光颜色
        _Period ("Period (Seconds)", float) = 1//周期
        _FlowWidth ("Flow Width", Range(0, 1)) = 0.1//宽度
        _FlowTex ("Flow Texture (RGB)", 2D) = "black" {}//光带从内环到外环的alpha分布贴图
    }
	SubShader
	{
        CGINCLUDE 
        #include "UnityCG.cginc"
 
        sampler2D _MainTex;
        fixed4 _FlowColor;
        float _Period;
        fixed _FlowWidth;
        sampler2D _FlowTex;
 
        struct appdata_t {
            float4 vertex : POSITION;
            half2 texcoord : TEXCOORD0;
        };
 
        struct v2f {
            float4 pos : POSITION;
            half2 mainTex : TEXCOORD0;
        };
 
        v2f vert (appdata_t v)
        {
            v2f o;
            o.pos = UnityObjectToClipPos(v.vertex);
            o.mainTex = v.texcoord;
            return o;
        }
 
        fixed4 frag (v2f i) : COLOR
        {
            fixed4 baseColor = tex2D(_MainTex, i.mainTex);
            float2 center=float2(0.5,0.5);
            //float2 test=float2(i.mainTex.x*0.8+0.1,i.mainTex.y*0.6+0.2);
            float r=distance(i.mainTex,center);
            float radiusMax=0.5;
            float flowRadiusMax=fmod(_Time.y,_Period)/_Period*(radiusMax+_FlowWidth);
            float flowRadiusMin=flowRadiusMax-_FlowWidth;
            float isInFlow=step(flowRadiusMin,r)-step(flowRadiusMax,r);
            float2 flowTexUV = float2((r - flowRadiusMin) / (flowRadiusMax - flowRadiusMin), 0);
            fixed4 finalColor=baseColor+isInFlow*_FlowColor*baseColor*tex2D(_FlowTex,flowTexUV);
            return finalColor;
        }
        ENDCG
		Pass
		{
			Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        	Cull Off
        	Lighting Off
        	ZWrite Off
        	Fog { Mode Off }
        	Blend One One

			CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            ENDCG
		}
	}
	FallBack "Diffuse" 
}
float2 center=float2(0.5,0.5);
本行代码是设置UV的中心,因为流光由中心向外扩散,但这有个缺点,那就是只能用正方形的贴图,否则流光会是个椭圆。
以下是解决方案
//float2 test=float2(i.mainTex.x*0.8+0.1,i.mainTex.y*0.6+0.2);
//float r=distance(test);
注释掉的代码就是用了一张8:6的图并进行了位置修正
float flowRadiusMax=fmod(_Time.y,_Period)/_Period*(radiusMax+_FlowWidth);
float flowRadiusMin=flowRadiusMax-_FlowWidth;
计算流光的范围,fmod(返回a / b的浮点余数)。
float isInFlow=step(flowRadiusMin,r)-step(flowRadiusMax,r);
step 返回(x >= a)? 1 : 0。计算是否位于流光范围
float2 flowTexUV = float2((r - flowRadiusMin) / (flowRadiusMax - flowRadiusMin), 0);
计算流光纹理坐标
fixed4 finalColor=baseColor+isInFlow*_FlowColor*baseColor*tex2D(_FlowTex,flowTexUV);

alpha分布贴图
在这里插入图片描述

以下是武器流光的代码

Shader "MySurfShader/MyFirstSurfaceShader" {
	Properties {
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_LiuGuangTex ("流光 (RGB)", 2D) = "white" {}//流光图
		_Glossiness ("Smoothness", Range(0,1)) = 0.5
		_Metallic ("Metallic", Range(0,1)) = 0.0
		_LiuGuangCol("流光颜色",Color)=(1,1,1,1)
		_LightWidth("宽度",Range(0.0,1.0))=1.0
		_SpeedX("X轴速度",Range(0.0,1.0))=0.5
		_SpeedY("Y轴速度",Range(0.0,1.0))=0.3
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Standard fullforwardshadows

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;
		sampler2D _LiuGuangTex;

		struct Input {
			float2 uv_MainTex;
		};

		half _Glossiness;
		half _Metallic;
		fixed4 _Color;
		fixed4 _LiuGuangCol;
		fixed _LightWidth;
		fixed _SpeedX;
		fixed _SpeedY;

		UNITY_INSTANCING_BUFFER_START(Props)
			// put more per-instance properties here
		UNITY_INSTANCING_BUFFER_END(Props)

		void surf (Input IN, inout SurfaceOutputStandard o) {
			// Albedo comes from a texture tinted by color
			float2 uv=IN.uv_MainTex/_LightWidth;
			uv.x+=_Time.y*_SpeedX;
			uv.y+=_Time.y*_SpeedY;
			fixed light=tex2D(_LiuGuangTex,uv).b;
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color+light*_LiuGuangCol;
			o.Albedo = c.rgb;
			// Metallic and smoothness come from slider variables
			o.Metallic = _Metallic;
			o.Smoothness = _Glossiness;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}
	// 主要代码
	float2 uv=IN.uv_MainTex/_LightWidth;//用以调整流光宽度
	uv.x+=_Time.y*_SpeedX;//X轴偏移
	uv.y+=_Time.y*_SpeedY;//Y轴偏移
	fixed light=tex2D(_LiuGuangTex,uv).b;//对流光图采样(rgb任选一个都可以)
	fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color+light*_LiuGuangCol;//计算最终颜色

流光图:
Alt

  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Unity中可以通过Shader和Post-processing来实现图片边缘流光效果。 首先,我们可以自定义一个边缘流光Shader。在Shader中,我们可以通过计算像素点与其周围像素点的差异来确定边缘的位置。然后,使用一段代码来设置边缘处的颜色为流光颜色,可以是亮闪闪的白色或其他高亮颜色。最后,将原始图片和流光效果合成,就可以得到边缘流光效果。 除了Shader,我们还可以使用Post-processing来增强边缘流光效果。在Unity中,我们可以使用Post-processing Stack来快速应用后期处理效果。通过调整亮度、对比度、饱和度等参数,可以使流光效果更加突出。 另外,我们还可以使用Unity自带的Image Effects来实现边缘流光效果。在Image Effects中,有一些自带的效果可以用来制作流光效果,比如Bloom和Glow。通过调整这些效果的参数,可以使边缘流光更加明显。 总之,Unity提供了多种方式来实现图片边缘流光效果,可以通过Shader、Post-processing或Image Effects来达到预期效果。具体实现方式需要根据具体需求和使用的工具来进行调整和优化。 ### 回答2: Unity中可以通过使用Shader实现边缘流光效果。下面是一个简单实现步骤: 1. 首先,创建一个新的Shader,命名为EdgeGlow。在Shader中,我们需要定义一个属性来保存原始图片的纹理,一个属性来控制流光的宽度,以及一个属性来设置流光的颜色。 2. 在顶点着色器中,我们需要将顶点的位置传递给片段着色器,同时计算纹理坐标。如下所示: ``` v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; return o; } ``` 3. 接下来,我们在片段着色器中进行流光效果的计算。首先,我们需要将原始纹理的颜色传递给片段着色器。然后,我们需要计算顶点距离边缘的距离,将其映射到0到1的范围内。根据距离的值,我们可以控制流光的强度。最后,我们将流光的颜色与原始纹理的颜色相混合,得到最终的颜色。 ``` fixed4 frag(v2f i) : SV_Target { fixed4 tex = tex2D(_MainTex, i.uv); float dist = distance(i.uv, float2(0.5, 0.5)); float glowFactor = saturate((_GlowWidth - dist) / _GlowWidth); fixed4 glowColor = _GlowColor * glowFactor; fixed4 finalColor = lerp(tex, glowColor, glowFactor); return finalColor; } ``` 4. 最后,在Unity中将这个Shader应用到你想要添加边缘流光效果的图片上。可以通过创建一个材质,将Shader指定为EdgeGlow,并将纹理贴图指定为原始图片。 以上就是使用ShaderUnity实现边缘流光效果简单步骤。你也可以根据需要对Shader进行调整和优化,以获得更好的效果

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值