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
This commit is contained in:
iDunnoDev
2022-06-13 16:41:25 +01:00
committed by iDunnoDev
parent 51afdeecbd
commit 616f68bf8b
40 changed files with 468 additions and 51 deletions

View File

@ -22,6 +22,8 @@ cbuffer ConstantBuffer
Texture2D BlendMap : register(t0);
Texture2DArray TexturesArray : register(t1);
// Textures for the water normals and the snow
Texture2D WaterNormalMap : register(t2);
Texture2D snowTest : register(t3);
@ -51,6 +53,7 @@ struct PixelShaderInput
float2 BlendMapTexCoord : TEXCOORD1;
};
// Function for rotating a UV around a point
float2 UVRotate(float2 uvCoord, float2 pivotPoint, float rotation)
{
float2x2 rotateMatrix = float2x2(float2(sin(rotation), -cos(rotation)), float2(cos(rotation), sin(rotation)));
@ -63,31 +66,35 @@ float2 UVRotate(float2 uvCoord, float2 pivotPoint, float rotation)
}
// Typical HLSL hasing function for psuedo random number generation
float4 hash4(float2 v)
{
float4 p = mul(float4x2(127.1f, 311.7f, 269.5f, 183.3f, 113.5f, 271.9f, 246.1f, 124.6f), v);
return frac(sin(p) * 43758.5453123);
}
PixelShaderInput VShader(VertexShaderInput vin)
{
PixelShaderInput output;
float3 position = vin.Position;
output.PositionOG = output.PositionWS = mul(worldTransformation, float4(position, 1.0f));
output.TexCoord = vin.TexCoord;
// Check if the y pos is lower than the water point, and set the position to it minus a tiny offset because the textures were clipping and causing a weird effect
// Check if the y pos is lower than the water point
if (position.y < waterHeight)
{
position.y = waterHeight;
// Change up the flat water height with some psuedo RNG height generation
float4 yOffset = hash4(float2(-position.y, position.y));
// the offset should return as a value between 0 and 1, i change this to be a value between -1 and 1, then add some height modifier so you can actually see the difference
position.y = waterHeight + (((yOffset.y * 2.0f) - 1.0f) * 3.0f);
}
output.Position = mul(completeTransformation, float4(position, 1.0f));
output.PositionWS = mul(worldTransformation, float4(position, 1.0f));
//output.PositionWS = mul(worldTransformation, float4(position, 1.0f));
output.NormalWS = float4(mul((float3x3)worldTransformation, vin.Normal), 1.0f);
output.BlendMapTexCoord = vin.BlendMapTexCoord;
return output;
}
float4 hash4(float2 v)
{
float4 p = mul(float4x2(127.1, 311.7, 269.5, 183.3, 113.5, 271.9, 246.1, 124.6), v);
return frac(sin(p) * 43758.5453123);
}
float4 PShader(PixelShaderInput input) : SV_TARGET
{
float4 directionToCamera = normalize(input.PositionWS - cameraPosition);
@ -95,14 +102,13 @@ float4 PShader(PixelShaderInput input) : SV_TARGET
float surfaceShininess = shininess;
float4 adjustedNormal = normalize(input.NormalWS);
float currentHeightPer = (waterHeight - input.PositionOG.y) / waterHeight;
if (input.PositionOG.y < waterHeight)
{
// Sample the normal from the water normal map and calculate it with the worldtransform
float3 n0 = 2.0f * (float3)WaterNormalMap.Sample(ss, input.TexCoord) - 1.0f;
float4 n2 = float4(mul((float3x3)worldTransformation, n0), 1.0f);
adjustedNormal = normalize(n2);
float4 n0 = WaterNormalMap.Sample(ss, input.TexCoord);
float4 n1 = float4((n0.xyz * 2.0f) - 1.0f, 1.0f);
//float4 n2 = float4(mul((float3x3)worldTransformation, n0), 1.0f);
adjustedNormal = n1; // normalize(n2);
surfaceShininess = waterShininess;
}
@ -122,9 +128,7 @@ float4 PShader(PixelShaderInput input) : SV_TARGET
float4 color;
// Randomly rotate the UV coordinates using the noise map
float2 scaleCenter = float2(0.5f, 0.5f);
// The noise map is greyscale so we only need to check 1 channel since they should all be the same value
float2 scaleCenter = float2(0.5f, 0.5f);
float2 scaledUV = frac(UVRotate(input.TexCoord, scaleCenter, input.BlendMapTexCoord.x)); // (input.TexCoord - scaleCenter) * currentScale + scaleCenter;
float2 xChange = ddx(scaledUV);
@ -145,6 +149,7 @@ float4 PShader(PixelShaderInput input) : SV_TARGET
float4 c2 = TexturesArray.SampleGrad(ss, float3(scaledUV, 2.0f), xChange, yChange);
float4 c3 = TexturesArray.SampleGrad(ss, float3(scaledUV, 3.0f), xChange, yChange);
//float4 c4 = TexturesArray.SampleGrad(ss, float3(scaledUV, 4.0f), xChange, yChange);
// Using the single snow texture because of the snow dds loading glitch
float4 c4 = snowTest.SampleGrad(ss, float2(scaledUV), xChange, yChange);
// Sample the blend map.
@ -157,7 +162,7 @@ float4 PShader(PixelShaderInput input) : SV_TARGET
color = lerp(color, c3, t.b);
color = lerp(color, c4, t.a);
// Combine all components
// Combine all components, and check if this is water
if (input.PositionOG.y < waterHeight)
{
// Tint the water