Shader "YS/ShiningCardShader" {
Properties {
_Globalbrightness ("全局亮度", Range(0, 2)) = 1
_MainTex1 ("主纹理1 (RGBA)", 2D) = "white" {}
_MainTex2 ("主纹理2 (RGBA)", 2D) = "white" {}
_MainTex3 ("主纹理3 (RGBA)", 2D) = "white" {}
_BackgroundTex ("背景纹理", 2D) = "white" {}
_TransitionX1 ("过渡位置1", Range(-1, 1)) = -0.3
_TransitionX2 ("过渡位置2", Range(-1, 1)) = 0.3
_TransitionWidth ("过渡宽度", Range(0, 1)) = 0.1
_NormalMap ("法线贴图", 2D) = "bump" {}
_BumpScale ("凹凸强度", Range(0, 5)) = 1
_NormalMapTiling ("法线贴图平铺", Vector) = (1, 1, 0, 0)
_NormalMapOffset ("法线贴图偏移", Vector) = (0, 0, 0, 0)
_MaskTex ("遮罩贴图", 2D) = "white" {}
[HDR]_GildingColor ("烫金颜色", Color) = (1, 0.843, 0, 1)
_GildingIntensity ("烫金强度", Range(0, 1)) = 0.5
_GildingGloss ("烫金光泽度", Range(1, 100)) = 20
_GildingSpecular ("烫金高光", Range(0, 1)) = 0.8
_TextureMoveStrength ("纹理移动强度", Range(0, 0.1)) = 0.02
_DepthFactor ("景深系数", Range(0, 1)) = 0.1
_LaserBlendStrength ("镭射混合强度", Range(0, 1)) = 0.5
_LaserLightFactor ("镭射亮度", Range(0, 1)) = 0.3
_StarTexture ("星芒贴图", 2D) = "white" {}
_StarIntensity ("星芒强度", Range(0, 1)) = 0.5
_StarSpeed ("星芒速度", Range(0, 5)) = 1
[HDR]_StarHDRColor ("星芒HDR颜色", Color) = (1, 1, 1, 1)
_StarDepthFactor ("星芒景深", Range(0, 1)) = 0.1
_StarViewFactor ("星芒视角系数", Range(0, 0.1)) = 0.02
_StarTiling ("星芒平铺", Vector) = (1, 1, 0, 0)
_StarBlinkFrequency ("星星闪烁频率", Range(0.1, 10)) = 2
_StarBlinkRandomness ("星星随机度", Range(0, 1)) = 0.5
_ColorRamp ("颜色渐变图", 2D) = "white" { }
_ColorRamp2 ("颜色渐变图2", 2D) = "white" { }
_Distortion ("扭曲强度", Range(0, 10)) = 1
_LaserPower ("镭射功率", Range(0, 10)) = 1
_Hue ("色相偏移", Range(0, 1)) = 0
_Saturation ("饱和度", Range(0, 1.0)) = 0.5
_Brightness ("亮度", Range(0, 1.0)) = 0.5
_Contrast ("对比度", Range(0, 1.0)) = 0.5
_ColorRamp1Power("渐变强度1", Range(0.0, 90.0)) = 25
_ColorRamp2Power("渐变强度2", Range(0.0, 90.0)) = 10
_NoiseTex1 ("噪波贴图", 2D) = "white" {}
}
SubShader {
Tags { "RenderType" = "Transparent" "Queue" = "Transparent+1" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass {
ZWrite On // 开启深度写入
ZTest LEqual // 设置深度测试模式
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
float4 tangent : TANGENT;
};
struct v2f {
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
float3 worldTangent : TEXCOORD3;
float3 worldBinormal : TEXCOORD4;
float3 viewDir : TEXCOORD5;
float4 screenPos : TEXCOORD6;
};
sampler2D _MainTex1;
float4 _MainTex1_ST;
sampler2D _MainTex2;
float4 _MainTex2_ST;
sampler2D _MainTex3;
float4 _MainTex3_ST;
sampler2D _BackgroundTex;
float4 _BackgroundTex_ST;
float _TransitionX1;
float _TransitionX2;
float _TransitionWidth;
fixed4 _LaserColor1;
fixed4 _LaserColor2;
fixed4 _LaserColor3;
float _LaserAngleRange1;
float _LaserAngleRange2;
float _LaserWidth;
float _LaserFrequency;
float _LaserSmoothness;
float _LaserOffset;
float _LaserGratingFrequency;
float _LaserGratingStrength;
sampler2D _NormalMap;
float _BumpScale;
float4 _NormalMapTiling;
float4 _NormalMapOffset;
sampler2D _MaskTex;
fixed4 _GildingColor;
float _GildingIntensity;
float _GildingGloss;
float _GildingSpecular;
float _TextureMoveStrength;
float _DepthFactor;
float _LaserBlendStrength;
float _LaserLightFactor;
sampler2D _StarTexture;
float _StarIntensity;
float _StarSpeed;
half4 _StarHDRColor;
float _StarDepthFactor;
float _StarViewFactor;
float4 _StarTiling;
float _StarBlinkFrequency;
float _StarBlinkRandomness;
sampler2D _ColorRamp;
float4 _ColorRamp_ST;
sampler2D _ColorRamp2;
sampler2D _NoiseTex1;
float4 _NoiseTex1_ST;
float _Globalbrightness;
float _Distortion, _LaserPower;
half _Hue, _Saturation, _Brightness, _Contrast, _ColorRamp1Power, _Alpha, _ColorRamp2Power;
v2f vert(appdata v) {
// 声明一个 v2f 结构体变量 o,用于存储顶点着色器输出的数据
v2f o;
// 将顶点从对象空间转换到裁剪空间,并赋值给 o.vertex
o.vertex = UnityObjectToClipPos(v.vertex);
// 应用纹理的平铺和偏移设置,将顶点的 UV 坐标转换为纹理采样所需的 UV 坐标,并赋值给 o.uv
o.uv = TRANSFORM_TEX(v.uv, _MainTex1);
// 将顶点的法线从对象空间转换到世界空间,并赋值给 o.worldNormal
o.worldNormal = UnityObjectToWorldNormal(v.normal);
// 将顶点从对象空间转换到世界空间,并提取其 xyz 坐标赋值给 o.worldPos
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
// 将顶点的切线从对象空间转换到世界空间,并赋值给 o.worldTangent
o.worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
// 通过世界法线和世界切线的叉积计算世界副切线,并乘以切线的 w 分量,赋值给 o.worldBinormal
o.worldBinormal = cross(o.worldNormal, o.worldTangent) * v.tangent.w;
// 计算从顶点的世界位置到相机的世界位置的向量,并将其归一化,赋值给 o.viewDir
o.viewDir = normalize(_WorldSpaceCameraPos - o.worldPos);
// 计算顶点在屏幕空间的位置,并赋值给 o.screenPos
o.screenPos = ComputeScreenPos(o.vertex);
// 返回顶点着色器输出的数据
return o;
}
// 应用色相偏移到颜色
inline float3 ApplyHue(float3 aColor, float aHue)
{
// 将色相角度转换为弧度
float angle = radians(aHue);
// 定义一个常向量 k,用于计算色相偏移
float3 k = float3(0.57735, 0.57735, 0.57735);
// 声明用于存储正弦和余弦值的变量
float sinAngle, cosAngle;
// 计算角度的正弦和余弦值
sincos(angle, sinAngle, cosAngle);
// 使用 Rodrigues' rotation formula 计算色相偏移后的颜色
return aColor * cosAngle + cross(k, aColor) * sinAngle + k * dot(k, aColor) * (1 - cosAngle);
}
// 应用HSBC效果到颜色
inline float4 ApplyHSBCEffect(float4 startColor, half4 hsbc)
{
// 将色相值从[0, 1]范围转换为[0, 360]范围
float hue = 360 * hsbc.r;
// 将饱和度值从[0, 1]范围转换为[0, 2]范围
float saturation = hsbc.g * 2;
// 将亮度值从[0, 1]范围转换为[-1, 1]范围
float brightness = hsbc.b * 2 - 1;
// 将对比度值从[0, 1]范围转换为[0, 2]范围
float contrast = hsbc.a * 2;
// 初始化输出颜色为起始颜色
float4 outputColor = startColor;
// 应用色相偏移到颜色
outputColor.rgb = ApplyHue(outputColor.rgb, hue);
// 应用对比度调整到颜色
outputColor.rgb = (outputColor.rgb - 0.5f) * contrast + 0.5f;
// 应用亮度调整到颜色
outputColor.rgb = outputColor.rgb + brightness;
// 计算颜色的强度
float3 intensity = dot(outputColor.rgb, float3(0.39, 0.59, 0.11));
// 应用饱和度调整到颜色
outputColor.rgb = lerp(intensity, outputColor.rgb, saturation);
// 返回调整后的颜色
return outputColor;
}
fixed4 frag(v2f i) : SV_Target {
// 法线贴图处理,应用 tiling 和 offset
float2 normalUV = i.uv * _NormalMapTiling.xy + _NormalMapOffset.xy;
// 从法线贴图中采样并解包法线向量
half3 tangentNormal = UnpackNormal(tex2D(_NormalMap, normalUV));
// 应用法线缩放因子
tangentNormal.xy *= _BumpScale;
// 重新计算 Z 分量以确保法线向量的长度为 1
tangentNormal.z = sqrt(1 - saturate(dot(tangentNormal.xy, tangentNormal.xy)));
// 创建从切线空间到世界空间的变换矩阵
float3x3 tangentToWorld = float3x3(
i.worldTangent, // 世界空间中的切线向量
i.worldBinormal, // 世界空间中的副切线向量
i.worldNormal // 世界空间中的法线向量
);
// 将法线向量从切线空间转换到世界空间
float3 worldNormal = mul(tangentNormal, tangentToWorld);
//向量获取
float3 L = _WorldSpaceLightPos0.xyz;
float3 V = i.viewDir;
//float3 N = normalize(i.worldNormal);
// 计算法线向量与观察向量的点积,并将结果限制在 [0, 1] 范围内
float NdotV = saturate(dot(worldNormal, V));
// 计算法线向量与光照向量的点积,并将结果限制在 [0, 1] 范围内
float NdotL = saturate(dot(worldNormal, L));
// 计算半程向量,即光照向量和观察向量的和的归一化向量
float3 H = normalize(L + V);
// 计算法线向量与半程向量的点积,并将结果限制在 [0, 1] 范围内
float NdotH = saturate(dot(worldNormal, H));
// 计算观察向量关于法线向量的反射向量,并将其归一化
float R = normalize(reflect(-V, worldNormal));
// 计算反射向量与负观察向量的点积
float RdotV = (dot(R, -V));
// 计算反射向量与观察向量的点积,并将结果映射到 [0, 1] 范围内
RdotV = float2(0, 1).x + (RdotV - float2(-1, 1).x) * (float2(0, 1).y - float2(0, 1).x) / (float2(-1, 1).y - float2(-1, 1).x);
// 获取观察方向在 X 和 Y 轴上的分量
float viewX = i.viewDir.x;
float viewY = i.viewDir.y;
// 计算纹理偏移量
float2 textureOffset = float2(viewX, viewY) * _TextureMoveStrength;
// 偏移后的纹理坐标
float2 offsetUV = i.uv + textureOffset;
// 采样背景纹理
fixed4 backgroundCol = tex2D(_BackgroundTex, i.uv);
fixed4 col;
if (viewX < _TransitionX1 - _TransitionWidth * 0.5) {
col = tex2D(_MainTex1, offsetUV);
} else if (viewX < _TransitionX1 + _TransitionWidth * 0.5) {
// 使用 smoothstep 计算平滑的混合因子
float t = smoothstep(_TransitionX1 - _TransitionWidth * 0.5, _TransitionX1 + _TransitionWidth * 0.5, viewX);
fixed4 tex1Col = tex2D(_MainTex1, offsetUV);
fixed4 tex2Col = tex2D(_MainTex2, offsetUV);
col = lerp(tex1Col, tex2Col, t);
} else if (viewX < _TransitionX2 - _TransitionWidth * 0.5) {
col = tex2D(_MainTex2, offsetUV);
} else if (viewX < _TransitionX2 + _TransitionWidth * 0.5) {
// 使用 smoothstep 计算平滑的混合因子
float t = smoothstep(_TransitionX2 - _TransitionWidth * 0.5, _TransitionX2 + _TransitionWidth * 0.5, viewX);
fixed4 tex2Col = tex2D(_MainTex2, offsetUV);
fixed4 tex3Col = tex2D(_MainTex3, offsetUV);
col = lerp(tex2Col, tex3Col, t);
} else {
col = tex2D(_MainTex3, offsetUV);
}
// 根据透明通道混合背景色和前景色
col = lerp(backgroundCol, col, col.a);
// 简单的漫反射光照模型
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);
float diff = max(0, dot(worldNormal, lightDir));
fixed4 lightCol = _LightColor0 * diff * 0.1;
// 读取遮罩贴图
fixed4 mask = tex2Dlod(_MaskTex, float4(i.uv, 0, -1));
// 计算烫金颜色的漫反射部分
float4 gildingDiffuseCol = _GildingColor * mask * _GildingIntensity;
// 计算烫金的镜面反射
float3 reflectDir = reflect(-lightDir, worldNormal);
float spec = pow(max(0, dot(i.viewDir, reflectDir)), _GildingGloss);
float4 gildingSpecularCol = _GildingColor * spec * _GildingSpecular * mask;
// 深度效果,让前三张贴图的移动有层次感
offsetUV += float2(viewX, viewY) * _DepthFactor * (1 - col.a);
// 重新采样前三张贴图以应用深度效果
if (viewX < _TransitionX1 - _TransitionWidth * 0.5) {
col = tex2D(_MainTex1, offsetUV);
} else if (viewX < _TransitionX1 + _TransitionWidth * 0.5) {
float t = smoothstep(_TransitionX1 - _TransitionWidth * 0.5, _TransitionX1 + _TransitionWidth * 0.5, viewX);
fixed4 tex1Col = tex2D(_MainTex1, offsetUV);
fixed4 tex2Col = tex2D(_MainTex2, offsetUV);
col = lerp(tex1Col, tex2Col, t);
} else if (viewX < _TransitionX2 - _TransitionWidth * 0.5) {
col = tex2D(_MainTex2, offsetUV);
} else if (viewX < _TransitionX2 + _TransitionWidth * 0.5) {
float t = smoothstep(_TransitionX2 - _TransitionWidth * 0.5, _TransitionX2 + _TransitionWidth * 0.5, viewX);
fixed4 tex2Col = tex2D(_MainTex2, offsetUV);
fixed4 tex3Col = tex2D(_MainTex3, offsetUV);
col = lerp(tex2Col, tex3Col, t);
} else {
col = tex2D(_MainTex3, offsetUV);
}
// 根据透明通道混合背景色和前景色
col = lerp(backgroundCol, col, col.a);
// 混合光照和烫金效果
col.rgb += lightCol.rgb;
col.rgb = lerp(col.rgb * _Globalbrightness, gildingDiffuseCol.rgb + gildingSpecularCol.rgb, mask.a);
// 星光点点效果
float2 starUV = i.uv;
starUV += float2(viewX, viewY) * _StarViewFactor;
starUV += _Time.y * _StarSpeed;
// 深度效果
starUV += float2(viewX, viewY) * _StarDepthFactor * (1 - col.a);
// 应用星空贴图的 tiling
starUV *= _StarTiling.xy;
fixed4 starCol = tex2D(_StarTexture, starUV);
//增加镭射效果
float noiseValue1 = tex2D(_NoiseTex1, i.uv).r;
float3 laserColor = tex2D(_ColorRamp, TRANSFORM_TEX(float2(NdotV, NdotV), _ColorRamp) * _Distortion);
// 色相偏移
half4 hsbc = half4(_Hue, _Saturation, _Brightness, _Contrast);
laserColor = ApplyHSBCEffect(float4(laserColor, 1), hsbc);
laserColor = laserColor * (NdotL * 0.5 + 0.5) + laserColor * pow(NdotH, _ColorRamp1Power);
//float3 rcolor = tex2D(_ColorRamp2, float2(RdotV, RdotV) * _Distortion);
float3 rcolor1 = tex2D(_ColorRamp2, (float2(NdotV, NdotV), noiseValue1));
//rcolor = pow(rcolor, _ColorRamp2Power);
laserColor = rcolor1 * laserColor;
RdotV = clamp(pow(RdotV, _LaserPower), 0.0, 1.0);
laserColor = laserColor * RdotV;
// 星光闪烁效果,添加随机偏移
float randomOffset = frac(sin(dot(i.uv, float2(12.9898, 78.233))) * 43758.5453);
float blinkFactor = (sin((_Time.y + randomOffset * _StarBlinkRandomness) * _StarBlinkFrequency) + 1) * 0.5;
starCol.rgb *= blinkFactor * _StarIntensity * _StarHDRColor.rgb;
col.rgb += starCol.rgb * starCol.a + laserColor;
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删