【Shader】实验05——渐变映射实现镭射材质

去年还蛮流行镭射包的,特别是夏天,blingbling的特别好看

仔细观察这种材质,大概就是彩虹色渐变+反光+透明,光的原理涉及到光的干涉。

猜想是这种材质对于不同波长的光吸收率不同导致光的干涉?改天去查一查……

用一个彩虹渐变的纹理对漫反射进行渐变映射,光最强处为红色,光最暗处为紫色。使用法线与光线的点积对渐变纹理进行采样即可。为了防止紫色(暗处)过多,使用半兰伯特模型,把NdotL*0.5+0.5,映射到【0,1】范围再进行采样。

彩色渐变纹理

而对于透明度alpha,是紫色部分alpha最大,红色部分alpha最小,所以NdotL越大,alpha越小,所以把NdotL映射到【0,1】范围,不过是0.5-0.5*NdotL。

fixed alpha = _AlphaScale * (0.5 - NdotL * 0.5);

最后结果

【Shader】实验05——渐变映射实现镭射材质_第1张图片

Shader源码

Shader "MyShaderTest/6_Laser"
{
	Properties
	{
		_Color ("Color",Color) = (1,1,1,1)

		_RampTex("Ramp Texture", 2D) = "white" {}
		_RampScale("Ramp Scale", Range(0,1)) = 1
		_AlphaScale("Alpha Size",Range(0,1)) = 1
	}
	SubShader
	{
		Tags { "RenderType"="Transparent" 
			   "Queue" = "Transparent" 
			   "IgnoreProjector" = "True" }

		Pass
		{
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"
			#include "Lighting.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float3 normal : NORMAL;
			};

			struct v2f
			{				
				float4 pos : SV_POSITION;
				float3 worldPos : TEXCOORD0;
				half3 worldNormal : TEXCOORD1;
			};

			fixed4 _Color;
			sampler2D _RampTex;
			half _RampScale;
			half _AlphaScale;

			v2f vert (appdata v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.worldPos = mul(unity_ObjectToWorld, v.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);

				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

				fixed3 worldNormal = normalize(i.worldNormal);

				fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyx;

				fixed NdotL = dot(worldNormal, worldLightDir);
				
				fixed alpha = _AlphaScale * (0.5 - NdotL * 0.5);

				//alpha = _AlphaScale * (0.5 + NdotL * 0.5);

				fixed diff = 0.5 + NdotL * 0.5;

				fixed3 diffuse_Ramp = _LightColor0 * tex2D(_RampTex,fixed2(diff,diff)).rgb; 
				
				fixed3 diffuse = _LightColor0 * diff;

				fixed3 finalColor = lerp(diffuse, diffuse_Ramp, _RampScale) + ambient;

				return fixed4(_Color.rgb * finalColor,alpha);
			}
			ENDCG
		}
	}
}

你可能感兴趣的:(Shader,Unity,Shader)