generated from Jonathan-Greve/GuildWarsMapBrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TerrainDefaultPixelShader.hlsl
101 lines (87 loc) · 2.69 KB
/
TerrainDefaultPixelShader.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
sampler ss: register(s0);
Texture2D shaderTextures[8] : register(t0);
struct DirectionalLight
{
float4 ambient;
float4 diffuse;
float4 specular;
float3 direction;
float pad;
};
cbuffer PerFrameCB: register(b0)
{
DirectionalLight directionalLight;
};
cbuffer PerObjectCB : register(b1)
{
matrix World;
uint4 uv_indices[8];
uint4 texture_indices[8];
uint4 blend_flags[8];
uint num_uv_texture_pairs;
float pad1[3];
};
cbuffer PerCameraCB : register(b2)
{
matrix View;
matrix Projection;
};
cbuffer PerTerrainCB : register(b3)
{
int grid_dim_x;
int grid_dim_y;
float min_x;
float max_x;
float min_y;
float max_y;
float min_z;
float max_z;
float water_level;
float pad[3];
};
struct PixelInputType
{
float4 position : SV_POSITION;
float3 normal : NORMAL;
float2 tex_coords0 : TEXCOORD0;
float2 tex_coords1 : TEXCOORD1;
float2 tex_coords2 : TEXCOORD2;
float2 tex_coords3 : TEXCOORD3;
float2 tex_coords4 : TEXCOORD4;
float2 tex_coords5 : TEXCOORD5;
float2 tex_coords6 : TEXCOORD6;
float2 tex_coords7 : TEXCOORD7;
float terrain_height : TEXCOORD8;
};
float4 main(PixelInputType input) : SV_TARGET
{
// Normalize the input normal
float3 normal = normalize(input.normal);
// Calculate the dot product of the normal and light direction
float NdotL = max(dot(normal, -directionalLight.direction), 0.0);
// Calculate the ambient and diffuse components
float4 ambientComponent = directionalLight.ambient;
float4 diffuseComponent = directionalLight.diffuse * NdotL;
// Extract the camera position from the view matrix
float3 cameraPosition = float3(View._41, View._42, View._43);
// Calculate the specular component using the Blinn-Phong model
float3 viewDirection = normalize(cameraPosition - input.position.xyz);
float3 halfVector = normalize(-directionalLight.direction + viewDirection);
float NdotH = max(dot(normal, halfVector), 0.0);
float shininess = 80.0; // You can adjust this value for shininess
float specularIntensity = pow(NdotH, shininess);
float4 specularComponent = directionalLight.specular * specularIntensity;
// Combine the ambient, diffuse, and specular components to get the final color
float4 finalColor = ambientComponent + diffuseComponent + specularComponent;
float4 outputColor;
// Multiply the sampled color with the finalColor
if (input.terrain_height <= water_level) {
float4 blue_color = float4(0.11, 0.65, 0.81, 1.0); // Water color
outputColor = finalColor * blue_color;
}
else {
outputColor = finalColor;
}
// Return the result
return outputColor;
}