Files
directx-plane-game/Graphics2/TexturedShaders.hlsl
iDunnoDev 616f68bf8b Added Comments
Added ability to hold shift and skip the terrain generation when loading
Added ability for the perlin terrain to save a raw image of the terrain to use as a cache
2022-06-13 16:41:25 +01:00

82 lines
2.6 KiB
HLSL

cbuffer ConstantBuffer
{
float4x4 completeTransformation;
float4x4 worldTransformation;
float4 cameraPosition;
float4 lightVector; // the light's vector
float4 lightColor; // the light's color
float4 ambientColor; // the ambient light's color
float4 diffuseCoefficient; // The diffuse reflection cooefficient
float4 specularCoefficient; // The specular reflection cooefficient
float shininess; // The shininess factor
float opacity; // The opacity (transparency) of the material. 0 = fully transparent, 1 = fully opaque
float validTexture;
float padding;
}
Texture2D Texture;
SamplerState ss;
struct VertexShaderInput
{
float3 Position : POSITION;
float3 Normal : NORMAL;
float2 TexCoord : TEXCOORD;
};
struct PixelShaderInput
{
float4 Position : SV_POSITION;
float4 PositionWS: TEXCOORD1;
float4 NormalWS : TEXCOORD2;
float2 TexCoord : TEXCOORD0;
};
PixelShaderInput VShader(VertexShaderInput vin)
{
PixelShaderInput output;
output.Position = mul(completeTransformation, float4(vin.Position, 1.0f));
output.PositionWS = mul(worldTransformation, float4(vin.Position, 1.0f));
output.NormalWS = float4(mul((float3x3)worldTransformation, vin.Normal), 1.0f);
output.TexCoord = vin.TexCoord;
return output;
}
float4 PShader(PixelShaderInput input) : SV_TARGET
{
float4 viewDirection = normalize(cameraPosition - input.PositionWS);
float4 directionToLight = normalize(-lightVector);
// Calculate diffuse lighting
float4 adjustedNormal = normalize(input.NormalWS);
float NdotL = max(0, dot(adjustedNormal, directionToLight));
float4 diffuse = saturate(lightColor * NdotL * diffuseCoefficient);
// Calculate specular component
float4 R = 2 * NdotL * adjustedNormal - directionToLight;
float RdotV = max(0, dot(R, viewDirection));
float4 specular = saturate(lightColor * pow(RdotV, shininess) * specularCoefficient);
// Calculate ambient lighting
float4 ambientLight = ambientColor * diffuseCoefficient;
// Combine all components
float4 color;
// Check if the texture is valid before trying to sample it, some meshes use materials to color them and dont export an image texture file even though the color value is stored with the vertex
// This might be totally my end because ive seen others be able to load the plane with all the correct textures???
if (validTexture == 1)
{
color = saturate((ambientLight + diffuse + specular) * Texture.Sample(ss, input.TexCoord));
}
else
{
color = saturate((ambientLight + diffuse + specular) * float4(1.0f, 1.0f, 1.0f, 1.0f));
}
if (opacity < 1.0f)
{
color.a = opacity;
}
return color;
}