Shader "MyShader/flag"
{
Properties
{
_MainTex ("Main Tex", 2D) = "white" {}
_Color ("Color Tint", Color) = (1,1,1,1)
_Magnitude ("Distortion Magnitude", Float) = 1
_Frequency ("Distortion Frequency", Float) = 1
_InvWaveLength ("Distortion Inverse Wave Length", Float) = 10
_Speed ("Speed", Float) = 0.5
_Diffuse ("Diffuse", Color) = (1.0,1.0,1.0,1.0)
}
SubShader
{
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}
Pass
{
Tags {"LightMode"="ForwardBase"}
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Lighting.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float _Magnitude;
float _Frequency;
float _InvWaveLength;
float _Speed;
fixed4 _Diffuse;
struct a2v {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
fixed3 worldNormal : TEXCOORD1;
};
float calPositionRate(float2 uv) {
float t = 0;
if(uv.x >= 0.5f)
t += (1 - uv.x);
else
t += uv.x;
if(uv.y >= 0.5f)
t += (1 - uv.y);
else
t += uv.y;
return t;
}
v2f vert(a2v v) {
v2f o;
// offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
// offset.x = sin(v.vertex.x * _InvWaveLength);
// offset.x = sin((_Time.y * _Frequency + v.vertex.z * _InvWaveLength) * 5) * _Magnitude;
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
// o.uv += float2(0.0, _Time.y * _Speed);
float4 offset;
offset.xyw = float3(0.0,0.0,0.0);
offset.z = 1 * sin((_Time.y * _Frequency + v.vertex.x * _InvWaveLength) * 5) * _Magnitude * clamp(sin(_Time.y * 0.2) , 0.25, 0.75);
o.pos = UnityObjectToClipPos(v.vertex + offset);
// cal v.normal
float t = 1 * cos((_Time.y * _Frequency + v.vertex.x * _InvWaveLength) * 5) * _Magnitude * clamp(sin(_Time.y * 0.2) , 0.25, 0.75) * 5 * _InvWaveLength;
float3 nor = normalize(cross(float4(v.vertex.x, 0, t,0), float4(0,1.0,0,0)));
// if(nor.z < 0) {
// nor.z *= -1;
// nor.x *= -1;
// nor.y *= -1;
// }
// float3 nor3 = float3(0, 0, nor.z);
o.worldNormal = mul(nor , (float3x3)unity_WorldToObject);
// o.pos = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target {
fixed3 albedo = tex2D(_MainTex, i.uv).rgb * _Color.rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
fixed3 worldNormal = normalize(i.worldNormal);
fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * pow(saturate(dot(worldNormal, worldLightDir)),8);;
fixed3 color = ambient + diffuse;
return fixed4(color, 1.0);
}
ENDCG
}
}
FallBack "VertexLit"
}
1.表面网格的几何波动
2.网格上的法线扰动
水的高度由简单的周期波叠加而成,通过正弦函数叠加后得到的连续函数,描述水面上所有点的高度和方向。
对于2d水平面的任何点(x,y)副法线B和切线T分别是x和y方向的偏导数 由副法线和切线的叉积获取出该点的法线方向
遗留问题:1.我想在片段函数中计算法线 这样的话发现计算的就更精确 但是没有计算出来
2.在片段函数中我想弱化光照边缘的强度 使用了pow进行n次方操作 为什么整体强度变暗了
网友评论