init
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2019-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec2 inUV;
|
||||
|
||||
layout (set = 0, binding = 1) uniform sampler2D samplerColorMap;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
vec4 color = texture(samplerColorMap, inUV);
|
||||
outFragColor = vec4(color.rgb, 1.0);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,35 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2019-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec2 outUV;
|
||||
|
||||
layout (set = 0, binding = 0) uniform UBO
|
||||
{
|
||||
mat4 mvp;
|
||||
} ubo;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = ubo.mvp * vec4(inPos, 1.0);
|
||||
outUV = inUV;
|
||||
outUV.t = 1.0 - outUV.t;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,77 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2019-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout (set = 0, binding = 1) uniform sampler2D samplerHeight;
|
||||
layout (set = 0, binding = 2) uniform sampler2DArray samplerLayers;
|
||||
|
||||
layout (location = 0) in vec3 inNormal;
|
||||
layout (location = 1) in vec2 inUV;
|
||||
layout (location = 2) in vec3 inViewVec;
|
||||
layout (location = 3) in vec3 inLightVec;
|
||||
layout (location = 4) in vec3 inEyePos;
|
||||
layout (location = 5) in vec3 inWorldPos;
|
||||
|
||||
layout (location = 0) out vec4 outFragColor;
|
||||
|
||||
vec3 sampleTerrainLayer()
|
||||
{
|
||||
// Define some layer ranges for sampling depending on terrain height
|
||||
vec2 layers[6];
|
||||
layers[0] = vec2(-10.0, 10.0);
|
||||
layers[1] = vec2(5.0, 45.0);
|
||||
layers[2] = vec2(45.0, 80.0);
|
||||
layers[3] = vec2(75.0, 100.0);
|
||||
layers[4] = vec2(95.0, 150.0);
|
||||
layers[5] = vec2(140.0, 290.0);
|
||||
|
||||
vec3 color = vec3(0.0);
|
||||
|
||||
// Get height from displacement map
|
||||
float height = textureLod(samplerHeight, inUV, 0.0).r * 255.0;
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
float range = layers[i].y - layers[i].x;
|
||||
float weight = (range - abs(height - layers[i].y)) / range;
|
||||
weight = max(0.0, weight);
|
||||
color += weight * texture(samplerLayers, vec3(inUV * 16.0, i)).rgb;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
float fog(float density)
|
||||
{
|
||||
const float LOG2 = -1.442695;
|
||||
float dist = gl_FragCoord.z / gl_FragCoord.w * 0.1;
|
||||
float d = density * dist;
|
||||
return 1.0 - clamp(exp2(d * d * LOG2), 0.0, 1.0);
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 N = normalize(inNormal);
|
||||
vec3 L = normalize(inLightVec);
|
||||
vec3 ambient = vec3(0.5);
|
||||
vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0);
|
||||
|
||||
vec4 color = vec4((ambient + diffuse) * sampleTerrainLayer(), 1.0);
|
||||
|
||||
const vec4 fogColor = vec4(0.47, 0.5, 0.67, 0.0);
|
||||
outFragColor = mix(color, fogColor, fog(0.25));
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,132 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2019-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout(set = 0, binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
vec4 lightPos;
|
||||
vec4 frustumPlanes[6];
|
||||
float displacementFactor;
|
||||
float tessellationFactor;
|
||||
vec2 viewportDim;
|
||||
float tessellatedEdgeSize;
|
||||
} ubo;
|
||||
|
||||
layout(set = 0, binding = 1) uniform sampler2D samplerHeight;
|
||||
|
||||
layout (vertices = 4) out;
|
||||
|
||||
layout (location = 0) in vec3 inNormal[];
|
||||
layout (location = 1) in vec2 inUV[];
|
||||
|
||||
layout (location = 0) out vec3 outNormal[4];
|
||||
layout (location = 1) out vec2 outUV[4];
|
||||
|
||||
// Calculate the tessellation factor based on screen space
|
||||
// dimensions of the edge
|
||||
float screenSpaceTessFactor(vec4 p0, vec4 p1)
|
||||
{
|
||||
// Calculate edge mid point
|
||||
vec4 midPoint = 0.5 * (p0 + p1);
|
||||
// Sphere radius as distance between the control points
|
||||
float radius = distance(p0, p1) / 2.0;
|
||||
|
||||
// View space
|
||||
vec4 v0 = ubo.modelview * midPoint;
|
||||
|
||||
// Project into clip space
|
||||
vec4 clip0 = (ubo.projection * (v0 - vec4(radius, vec3(0.0))));
|
||||
vec4 clip1 = (ubo.projection * (v0 + vec4(radius, vec3(0.0))));
|
||||
|
||||
// Get normalized device coordinates
|
||||
clip0 /= clip0.w;
|
||||
clip1 /= clip1.w;
|
||||
|
||||
// Convert to viewport coordinates
|
||||
clip0.xy *= ubo.viewportDim;
|
||||
clip1.xy *= ubo.viewportDim;
|
||||
|
||||
// Return the tessellation factor based on the screen size
|
||||
// given by the distance of the two edge control points in screen space
|
||||
// and a reference (min.) tessellation size for the edge set by the application
|
||||
return clamp(distance(clip0, clip1) / ubo.tessellatedEdgeSize * ubo.tessellationFactor, 1.0, 64.0);
|
||||
}
|
||||
|
||||
// Checks the current's patch visibility against the frustum using a sphere check
|
||||
// Sphere radius is given by the patch size
|
||||
bool frustumCheck()
|
||||
{
|
||||
// Fixed radius (increase if patch size is increased in example)
|
||||
const float radius = 8.0f;
|
||||
vec4 pos = gl_in[gl_InvocationID].gl_Position;
|
||||
pos.y -= textureLod(samplerHeight, inUV[0], 0.0).r * ubo.displacementFactor;
|
||||
|
||||
// Check sphere against frustum planes
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
if (gl_InvocationID == 0)
|
||||
{
|
||||
if (!frustumCheck())
|
||||
{
|
||||
gl_TessLevelInner[0] = 0.0;
|
||||
gl_TessLevelInner[1] = 0.0;
|
||||
gl_TessLevelOuter[0] = 0.0;
|
||||
gl_TessLevelOuter[1] = 0.0;
|
||||
gl_TessLevelOuter[2] = 0.0;
|
||||
gl_TessLevelOuter[3] = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ubo.tessellationFactor > 0.0)
|
||||
{
|
||||
gl_TessLevelOuter[0] = screenSpaceTessFactor(gl_in[3].gl_Position, gl_in[0].gl_Position);
|
||||
gl_TessLevelOuter[1] = screenSpaceTessFactor(gl_in[0].gl_Position, gl_in[1].gl_Position);
|
||||
gl_TessLevelOuter[2] = screenSpaceTessFactor(gl_in[1].gl_Position, gl_in[2].gl_Position);
|
||||
gl_TessLevelOuter[3] = screenSpaceTessFactor(gl_in[2].gl_Position, gl_in[3].gl_Position);
|
||||
gl_TessLevelInner[0] = mix(gl_TessLevelOuter[0], gl_TessLevelOuter[3], 0.5);
|
||||
gl_TessLevelInner[1] = mix(gl_TessLevelOuter[2], gl_TessLevelOuter[1], 0.5);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Tessellation factor can be set to zero by example
|
||||
// to demonstrate a simple passthrough
|
||||
gl_TessLevelInner[0] = 1.0;
|
||||
gl_TessLevelInner[1] = 1.0;
|
||||
gl_TessLevelOuter[0] = 1.0;
|
||||
gl_TessLevelOuter[1] = 1.0;
|
||||
gl_TessLevelOuter[2] = 1.0;
|
||||
gl_TessLevelOuter[3] = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
|
||||
outNormal[gl_InvocationID] = inNormal[gl_InvocationID];
|
||||
outUV[gl_InvocationID] = inUV[gl_InvocationID];
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,70 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2019-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout (set = 0, binding = 0) uniform UBO
|
||||
{
|
||||
mat4 projection;
|
||||
mat4 modelview;
|
||||
vec4 lightPos;
|
||||
vec4 frustumPlanes[6];
|
||||
float displacementFactor;
|
||||
float tessellationFactor;
|
||||
vec2 viewportDim;
|
||||
float tessellatedEdgeSize;
|
||||
} ubo;
|
||||
|
||||
layout (set = 0, binding = 1) uniform sampler2D displacementMap;
|
||||
|
||||
layout(quads, equal_spacing, cw) in;
|
||||
|
||||
layout (location = 0) in vec3 inNormal[];
|
||||
layout (location = 1) in vec2 inUV[];
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec2 outUV;
|
||||
layout (location = 2) out vec3 outViewVec;
|
||||
layout (location = 3) out vec3 outLightVec;
|
||||
layout (location = 4) out vec3 outEyePos;
|
||||
layout (location = 5) out vec3 outWorldPos;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Interpolate UV coordinates
|
||||
vec2 uv1 = mix(inUV[0], inUV[1], gl_TessCoord.x);
|
||||
vec2 uv2 = mix(inUV[3], inUV[2], gl_TessCoord.x);
|
||||
outUV = mix(uv1, uv2, gl_TessCoord.y);
|
||||
|
||||
vec3 n1 = mix(inNormal[0], inNormal[1], gl_TessCoord.x);
|
||||
vec3 n2 = mix(inNormal[3], inNormal[2], gl_TessCoord.x);
|
||||
outNormal = mix(n1, n2, gl_TessCoord.y);
|
||||
|
||||
// Interpolate positions
|
||||
vec4 pos1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
|
||||
vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
|
||||
vec4 pos = mix(pos1, pos2, gl_TessCoord.y);
|
||||
// Displace
|
||||
pos.y -= textureLod(displacementMap, outUV, 0.0).r * ubo.displacementFactor;
|
||||
// Perspective projection
|
||||
gl_Position = ubo.projection * ubo.modelview * pos;
|
||||
|
||||
// Calculate vectors for lighting based on tessellated position
|
||||
outViewVec = -pos.xyz;
|
||||
outLightVec = normalize(ubo.lightPos.xyz + outViewVec);
|
||||
outWorldPos = pos.xyz;
|
||||
outEyePos = vec3(ubo.modelview * pos);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,31 @@
|
||||
#version 450
|
||||
/* Copyright (c) 2019-2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
layout (location = 0) in vec3 inPos;
|
||||
layout (location = 1) in vec3 inNormal;
|
||||
layout (location = 2) in vec2 inUV;
|
||||
|
||||
layout (location = 0) out vec3 outNormal;
|
||||
layout (location = 1) out vec2 outUV;
|
||||
|
||||
void main(void)
|
||||
{
|
||||
gl_Position = vec4(inPos.xyz, 1.0);
|
||||
outUV = inUV;
|
||||
outNormal = inNormal;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
Texture2D textureColorMap : register(t1);
|
||||
SamplerState samplerColorMap : register(s1);
|
||||
|
||||
float4 main([[vk::location(0)]] float2 inUV : TEXCOODR0) : SV_TARGET
|
||||
{
|
||||
float4 color = textureColorMap.Sample(samplerColorMap, inUV);
|
||||
return float4(color.rgb, 1.0);
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 mvp;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b0);
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.Pos = mul(ubo.mvp, float4(input.Pos, 1.0));
|
||||
output.UV = input.UV;
|
||||
output.UV.y = 1.0 - output.UV.y;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,80 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
Texture2D textureHeight : register(t1);
|
||||
SamplerState samplerHeight : register(s1);
|
||||
Texture2DArray textureLayers : register(t2);
|
||||
SamplerState samplerLayers : register(s2);
|
||||
|
||||
struct DSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
|
||||
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
|
||||
[[vk::location(4)]] float3 EyePos : POSITION1;
|
||||
[[vk::location(5)]] float3 WorldPos : POSITION0;
|
||||
};
|
||||
|
||||
float3 sampleTerrainLayer(float2 inUV)
|
||||
{
|
||||
// Define some layer ranges for sampling depending on terrain height
|
||||
float2 layers[6];
|
||||
layers[0] = float2(-10.0, 10.0);
|
||||
layers[1] = float2(5.0, 45.0);
|
||||
layers[2] = float2(45.0, 80.0);
|
||||
layers[3] = float2(75.0, 100.0);
|
||||
layers[4] = float2(95.0, 140.0);
|
||||
layers[5] = float2(140.0, 190.0);
|
||||
|
||||
float3 color = float3(0.0, 0.0, 0.0);
|
||||
|
||||
// Get height from displacement map
|
||||
float height = textureHeight.SampleLevel(samplerHeight, inUV, 0.0).r * 255.0;
|
||||
|
||||
for (int i = 0; i < 6; i++)
|
||||
{
|
||||
float range = layers[i].y - layers[i].x;
|
||||
float weight = (range - abs(height - layers[i].y)) / range;
|
||||
weight = max(0.0, weight);
|
||||
color += weight * textureLayers.Sample(samplerLayers, float3(inUV * 16.0, i)).rgb;
|
||||
}
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
float fog(float density, float4 FragCoord)
|
||||
{
|
||||
const float LOG2 = -1.442695;
|
||||
float dist = FragCoord.z / FragCoord.w * 0.1;
|
||||
float d = density * dist;
|
||||
return 1.0 - clamp(exp2(d * d * LOG2), 0.0, 1.0);
|
||||
}
|
||||
|
||||
float4 main(DSOutput input) : SV_TARGET
|
||||
{
|
||||
float3 N = normalize(input.Normal);
|
||||
float3 L = normalize(input.LightVec);
|
||||
float3 ambient = float3(0.5, 0.5, 0.5);
|
||||
float3 diffuse = max(dot(N, L), 0.0) * float3(1.0, 1.0, 1.0);
|
||||
|
||||
float4 color = float4((ambient + diffuse) * sampleTerrainLayer(input.UV), 1.0);
|
||||
|
||||
const float4 fogColor = float4(0.47, 0.5, 0.67, 0.0);
|
||||
return lerp(color, fogColor, fog(0.25, input.Pos));
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,157 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4 lightPos;
|
||||
float4 frustumPlanes[6];
|
||||
float displacementFactor;
|
||||
float tessellationFactor;
|
||||
float2 viewportDim;
|
||||
float tessellatedEdgeSize;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b0);
|
||||
|
||||
Texture2D textureHeight : register(t1);
|
||||
SamplerState samplerHeight : register(s1);
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct HSOutput
|
||||
{
|
||||
[[vk::location(2)]] float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct ConstantsHSOutput
|
||||
{
|
||||
float TessLevelOuter[4] : SV_TessFactor;
|
||||
float TessLevelInner[2] : SV_InsideTessFactor;
|
||||
};
|
||||
|
||||
// Calculate the tessellation factor based on screen space
|
||||
// dimensions of the edge
|
||||
float screenSpaceTessFactor(float4 p0, float4 p1)
|
||||
{
|
||||
// Calculate edge mid point
|
||||
float4 midPoint = 0.5 * (p0 + p1);
|
||||
// Sphere radius as distance between the control points
|
||||
float radius = distance(p0, p1) / 2.0;
|
||||
|
||||
// View space
|
||||
float4 v0 = mul(ubo.modelview, midPoint);
|
||||
|
||||
// Project into clip space
|
||||
float4 clip0 = mul(ubo.projection, (v0 - float4(radius, float3(0.0, 0.0, 0.0))));
|
||||
float4 clip1 = mul(ubo.projection, (v0 + float4(radius, float3(0.0, 0.0, 0.0))));
|
||||
|
||||
// Get normalized device coordinates
|
||||
clip0 /= clip0.w;
|
||||
clip1 /= clip1.w;
|
||||
|
||||
// Convert to viewport coordinates
|
||||
clip0.xy *= ubo.viewportDim;
|
||||
clip1.xy *= ubo.viewportDim;
|
||||
|
||||
// Return the tessellation factor based on the screen size
|
||||
// given by the distance of the two edge control points in screen space
|
||||
// and a reference (min.) tessellation size for the edge set by the application
|
||||
return clamp(distance(clip0, clip1) / ubo.tessellatedEdgeSize * ubo.tessellationFactor, 1.0, 64.0);
|
||||
}
|
||||
|
||||
// Checks the current's patch visibility against the frustum using a sphere check
|
||||
// Sphere radius is given by the patch size
|
||||
bool frustumCheck(float4 Pos, float2 inUV)
|
||||
{
|
||||
// Fixed radius (increase if patch size is increased in example)
|
||||
const float radius = 8.0f;
|
||||
float4 pos = Pos;
|
||||
pos.y -= textureHeight.SampleLevel(samplerHeight, inUV, 0.0).r * ubo.displacementFactor;
|
||||
|
||||
// Check sphere against frustum planes
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (dot(pos, ubo.frustumPlanes[i]) + radius < 0.0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ConstantsHSOutput ConstantsHS(InputPatch<VSOutput, 4> patch)
|
||||
{
|
||||
ConstantsHSOutput output = (ConstantsHSOutput)0;
|
||||
|
||||
if (!frustumCheck(patch[0].Pos, patch[0].UV))
|
||||
{
|
||||
output.TessLevelInner[0] = 0.0;
|
||||
output.TessLevelInner[1] = 0.0;
|
||||
output.TessLevelOuter[0] = 0.0;
|
||||
output.TessLevelOuter[1] = 0.0;
|
||||
output.TessLevelOuter[2] = 0.0;
|
||||
output.TessLevelOuter[3] = 0.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ubo.tessellationFactor > 0.0)
|
||||
{
|
||||
output.TessLevelOuter[0] = screenSpaceTessFactor(patch[3].Pos, patch[0].Pos);
|
||||
output.TessLevelOuter[1] = screenSpaceTessFactor(patch[0].Pos, patch[1].Pos);
|
||||
output.TessLevelOuter[2] = screenSpaceTessFactor(patch[1].Pos, patch[2].Pos);
|
||||
output.TessLevelOuter[3] = screenSpaceTessFactor(patch[2].Pos, patch[3].Pos);
|
||||
output.TessLevelInner[0] = lerp(output.TessLevelOuter[0], output.TessLevelOuter[3], 0.5);
|
||||
output.TessLevelInner[1] = lerp(output.TessLevelOuter[2], output.TessLevelOuter[1], 0.5);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Tessellation factor can be set to zero by example
|
||||
// to demonstrate a simple passthrough
|
||||
output.TessLevelInner[0] = 1.0;
|
||||
output.TessLevelInner[1] = 1.0;
|
||||
output.TessLevelOuter[0] = 1.0;
|
||||
output.TessLevelOuter[1] = 1.0;
|
||||
output.TessLevelOuter[2] = 1.0;
|
||||
output.TessLevelOuter[3] = 1.0;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
[domain("quad")]
|
||||
[partitioning("integer")]
|
||||
[outputtopology("triangle_cw")]
|
||||
[outputcontrolpoints(4)]
|
||||
[patchconstantfunc("ConstantsHS")]
|
||||
[maxtessfactor(20.0f)]
|
||||
HSOutput main(InputPatch<VSOutput, 4> patch, uint InvocationID : SV_OutputControlPointID)
|
||||
{
|
||||
HSOutput output = (HSOutput)0;
|
||||
output.Pos = patch[InvocationID].Pos;
|
||||
output.Normal = patch[InvocationID].Normal;
|
||||
output.UV = patch[InvocationID].UV;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,87 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct UBO
|
||||
{
|
||||
float4x4 projection;
|
||||
float4x4 modelview;
|
||||
float4 lightPos;
|
||||
float4 frustumPlanes[6];
|
||||
float displacementFactor;
|
||||
float tessellationFactor;
|
||||
float2 viewportDim;
|
||||
float tessellatedEdgeSize;
|
||||
};
|
||||
[[vk::binding(0, 0)]]
|
||||
ConstantBuffer<UBO> ubo : register(b0);
|
||||
|
||||
Texture2D displacementMapTexture : register(t1);
|
||||
SamplerState displacementMapSampler : register(s1);
|
||||
|
||||
struct HSOutput
|
||||
{
|
||||
[[vk::location(2)]] float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct ConstantsHSOutput
|
||||
{
|
||||
float TessLevelOuter[4] : SV_TessFactor;
|
||||
float TessLevelInner[2] : SV_InsideTessFactor;
|
||||
};
|
||||
|
||||
struct DSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
[[vk::location(2)]] float3 ViewVec : TEXCOORD1;
|
||||
[[vk::location(3)]] float3 LightVec : TEXCOORD2;
|
||||
[[vk::location(4)]] float3 EyePos : POSITION1;
|
||||
[[vk::location(5)]] float3 WorldPos : POSITION0;
|
||||
};
|
||||
|
||||
[domain("quad")]
|
||||
DSOutput main(ConstantsHSOutput input, float2 TessCoord : SV_DomainLocation, const OutputPatch<HSOutput, 4> patch)
|
||||
{
|
||||
// Interpolate UV coordinates
|
||||
DSOutput output = (DSOutput)0;
|
||||
float2 uv1 = lerp(patch[0].UV, patch[1].UV, TessCoord.x);
|
||||
float2 uv2 = lerp(patch[3].UV, patch[2].UV, TessCoord.x);
|
||||
output.UV = lerp(uv1, uv2, TessCoord.y);
|
||||
|
||||
float3 n1 = lerp(patch[0].Normal, patch[1].Normal, TessCoord.x);
|
||||
float3 n2 = lerp(patch[3].Normal, patch[2].Normal, TessCoord.x);
|
||||
output.Normal = lerp(n1, n2, TessCoord.y);
|
||||
|
||||
// Interpolate positions
|
||||
float4 pos1 = lerp(patch[0].Pos, patch[1].Pos, TessCoord.x);
|
||||
float4 pos2 = lerp(patch[3].Pos, patch[2].Pos, TessCoord.x);
|
||||
float4 pos = lerp(pos1, pos2, TessCoord.y);
|
||||
// Displace
|
||||
pos.y -= displacementMapTexture.SampleLevel(displacementMapSampler, output.UV, 0.0).r * ubo.displacementFactor;
|
||||
// Perspective projection
|
||||
output.Pos = mul(ubo.projection, mul(ubo.modelview, pos));
|
||||
|
||||
// Calculate vectors for lighting based on tessellated position
|
||||
output.ViewVec = -pos.xyz;
|
||||
output.LightVec = normalize(ubo.lightPos.xyz + output.ViewVec);
|
||||
output.WorldPos = pos.xyz;
|
||||
output.EyePos = mul(ubo.modelview, pos).xyz;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,39 @@
|
||||
/* Copyright (c) 2024, Sascha Willems
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 the "License";
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
struct VSInput
|
||||
{
|
||||
[[vk::location(0)]] float3 Pos : POSITION0;
|
||||
[[vk::location(1)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(2)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
struct VSOutput
|
||||
{
|
||||
float4 Pos : SV_POSITION;
|
||||
[[vk::location(0)]] float3 Normal : NORMAL0;
|
||||
[[vk::location(1)]] float2 UV : TEXCOORD0;
|
||||
};
|
||||
|
||||
VSOutput main(VSInput input)
|
||||
{
|
||||
VSOutput output = (VSOutput)0;
|
||||
output.Pos = float4(input.Pos.xyz, 1.0);
|
||||
output.UV = input.UV;
|
||||
output.Normal = input.Normal;
|
||||
return output;
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user