forked from doodlum/skyrim-community-shaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunGrass.hlsl
214 lines (172 loc) · 6.2 KB
/
RunGrass.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
struct VS_INPUT
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float4 Normal : NORMAL0;
float4 Color : COLOR0;
float4 InstanceData1 : TEXCOORD4;
float4 InstanceData2 : TEXCOORD5;
float4 InstanceData3 : TEXCOORD6;
float4 InstanceData4 : TEXCOORD7;
};
struct VS_OUTPUT
{
float4 HPosition : SV_POSITION0;
float4 DiffuseColor : COLOR0;
float3 TexCoord : TEXCOORD0;
float4 AmbientColor : TEXCOORD1;
float3 ViewSpacePosition : TEXCOORD2;
#if defined(RENDER_DEPTH)
float2 Depth : TEXCOORD3;
#endif
float4 WorldPosition : POSITION1;
float4 PreviousWorldPosition : POSITION2;
};
#ifdef VSHADER
cbuffer PerGeometry : register(b2)
{
row_major float4x4 WorldViewProj : packoffset(c0);
row_major float4x4 WorldView : packoffset(c4);
row_major float4x4 World : packoffset(c8);
row_major float4x4 PreviousWorld : packoffset(c12);
float4 FogNearColor : packoffset(c16);
float3 WindVector : packoffset(c17);
float WindTimer : packoffset(c17.w);
float3 DirLightDirection : packoffset(c18);
float PreviousWindTimer : packoffset(c18.w);
float3 DirLightColor : packoffset(c19);
float AlphaParam1 : packoffset(c19.w);
float3 AmbientColor : packoffset(c20);
float AlphaParam2 : packoffset(c20.w);
float3 ScaleMask : packoffset(c21);
float ShadowClampValue : packoffset(c21.w);
}
cbuffer cb7 : register(b7)
{
float4 cb7[1];
}
cbuffer cb8 : register(b8)
{
float4 cb8[240];
}
# define M_PI 3.1415925 // PI
# define M_2PI 6.283185 // PI * 2
const static float4x4 M_IdentityMatrix = {
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 }
};
float4 GetMSPosition(VS_INPUT input, float windTimer)
{
float windAngle = 0.4 * ((input.InstanceData1.x + input.InstanceData1.y) * -0.0078125 + windTimer);
float windAngleSin, windAngleCos;
sincos(windAngle, windAngleSin, windAngleCos);
float windTmp3 = 0.2 * cos(M_PI * windAngleCos);
float windTmp1 = sin(M_PI * windAngleSin);
float windTmp2 = sin(M_2PI * windAngleSin);
float windPower = WindVector.z * (((windTmp1 + windTmp2) * 0.3 + windTmp3) *
(0.5 * (input.Color.w * input.Color.w)));
float3 inputPosition = input.Position.xyz * (input.InstanceData4.yyy * ScaleMask.xyz + float3(1, 1, 1));
float3 instancePosition;
instancePosition.z = dot(
float3(input.InstanceData4.x, input.InstanceData2.w, input.InstanceData3.w), inputPosition);
instancePosition.x = dot(input.InstanceData2.xyz, inputPosition);
instancePosition.y = dot(input.InstanceData3.xyz, inputPosition);
float3 windVector = float3(WindVector.xy, 0);
float4 msPosition;
msPosition.xyz = input.InstanceData1.xyz + (windVector * windPower + instancePosition);
msPosition.w = 1;
return msPosition;
}
VS_OUTPUT main(VS_INPUT input)
{
VS_OUTPUT vsout;
float4 msPosition = GetMSPosition(input, WindTimer);
float4 projSpacePosition = mul(WorldViewProj, msPosition);
vsout.HPosition = projSpacePosition;
# if defined(RENDER_DEPTH)
vsout.Depth = projSpacePosition.zw;
# endif
float3 instanceNormal = float3(input.InstanceData2.z, input.InstanceData3.zw);
float dirLightAngle = dot(DirLightDirection.xyz, instanceNormal);
float3 diffuseMultiplier = input.InstanceData1.www * input.Color.xyz * saturate(dirLightAngle.xxx);
float perInstanceFade = dot(cb8[(asuint(cb7[0].x) >> 2)].xyzw, M_IdentityMatrix[(asint(cb7[0].x) & 3)].xyzw);
float distanceFade = 1 - saturate((length(projSpacePosition.xyz) - AlphaParam1) / AlphaParam2);
vsout.DiffuseColor.xyz = DirLightColor.xyz * diffuseMultiplier;
vsout.DiffuseColor.w = distanceFade * perInstanceFade;
vsout.TexCoord.xy = input.TexCoord.xy;
vsout.TexCoord.z = FogNearColor.w;
vsout.AmbientColor.xyz = input.InstanceData1.www * (AmbientColor.xyz * input.Color.xyz);
vsout.AmbientColor.w = ShadowClampValue;
vsout.ViewSpacePosition = mul(WorldView, msPosition).xyz;
vsout.WorldPosition = mul(World, msPosition);
float4 previousMsPosition = GetMSPosition(input, PreviousWindTimer);
vsout.PreviousWorldPosition = mul(PreviousWorld, previousMsPosition);
return vsout;
}
#endif
typedef VS_OUTPUT PS_INPUT;
struct PS_OUTPUT
{
#if defined(RENDER_DEPTH)
float4 PS : SV_Target0;
#else
float4 Albedo : SV_Target0;
float2 MotionVectors : SV_Target1;
float4 Normal : SV_Target2;
#endif
};
#ifdef PSHADER
SamplerState SampBaseSampler : register(s0);
SamplerState SampShadowMaskSampler : register(s1);
Texture2D<float4> TexBaseSampler : register(t0);
Texture2D<float4> TexShadowMaskSampler : register(t1);
cbuffer AlphaTestRefCB : register(b11)
{
float AlphaTestRefRS : packoffset(c0);
}
cbuffer PerFrame : register(b12)
{
float4 UnknownPerFrame1[12] : packoffset(c0);
row_major float4x4 ScreenProj : packoffset(c12);
row_major float4x4 PreviousScreenProj : packoffset(c16);
};
PS_OUTPUT main(PS_INPUT input)
{
PS_OUTPUT psout;
float4 baseColor = TexBaseSampler.Sample(SampBaseSampler, input.TexCoord.xy);
# if defined(RENDER_DEPTH) || defined(DO_ALPHA_TEST)
float diffuseAlpha = input.DiffuseColor.w * baseColor.w;
if ((diffuseAlpha - AlphaTestRefRS) < 0) {
discard;
}
# endif
# if defined(RENDER_DEPTH)
// Depth
psout.PS.xyz = input.Depth.xxx / input.Depth.yyy;
psout.PS.w = diffuseAlpha;
# else
float sunShadowMask = TexShadowMaskSampler.Load(int3(input.HPosition.xy, 0)).x;
// Albedo
float diffuseFraction = lerp(sunShadowMask, 1, input.AmbientColor.w);
float3 diffuseColor = input.DiffuseColor.xyz * baseColor.xyz;
float3 ambientColor = input.AmbientColor.xyz * baseColor.xyz;
psout.Albedo.xyz = input.TexCoord.zzz * (diffuseColor * diffuseFraction + ambientColor);
psout.Albedo.w = 1;
float4 screenPosition = mul(ScreenProj, input.WorldPosition);
screenPosition.xy = screenPosition.xy / screenPosition.ww;
float4 previousScreenPosition = mul(PreviousScreenProj, input.PreviousWorldPosition);
previousScreenPosition.xy = previousScreenPosition.xy / previousScreenPosition.ww;
float2 screenMotionVector = float2(-0.5, 0.5) * (screenPosition.xy - previousScreenPosition.xy);
psout.MotionVectors = screenMotionVector;
float3 ddx = ddx_coarse(input.ViewSpacePosition);
float3 ddy = ddy_coarse(input.ViewSpacePosition);
float3 normal = normalize(cross(ddx, ddy));
float normalScale = max(1.0 / 1000.0, sqrt(normal.z * -8 + 8));
psout.Normal.xy = float2(0.5, 0.5) + normal.xy / normalScale;
psout.Normal.zw = float2(0, 0);
# endif
return psout;
}
#endif