Crafter.Graphics/MeshShaderHeightmapRGBA.glsl

82 lines
2.7 KiB
Text
Raw Normal View History

2025-05-25 23:04:56 +02:00
/*
Crafter®.Graphics
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#version 450
#extension GL_EXT_mesh_shader : require
layout (binding = 0) uniform UBO
{
mat4 modelProjectionView;
2025-05-26 00:35:51 +02:00
uint stride;
2025-05-25 23:04:56 +02:00
float spacing;
2025-05-26 00:35:51 +02:00
uint padding[14];
2025-05-25 23:04:56 +02:00
} ubo;
struct VertexType
{
float height;
float padding[3];
vec4 color;
};
layout (binding = 1) buffer VERTEX
{
VertexType pos[];
} vertex;
layout(local_size_x = 64, local_size_y = 1, local_size_z = 1) in;
layout(triangles, max_vertices = 256, max_primitives = 128) out;
layout (location = 0) out PerVertexData
{
vec4 color;
} outVert[];
void main()
{
SetMeshOutputsEXT(256, 128);
2025-05-26 00:45:03 +02:00
uint linearID = gl_GlobalInvocationID.x;
2025-05-26 00:35:51 +02:00
uint quadX = linearID % ubo.stride;
uint quadZ = linearID / ubo.stride;
2025-05-25 23:04:56 +02:00
2025-05-26 00:35:51 +02:00
VertexType vertex1 = vertex.pos[quadZ * ubo.stride + quadX];
VertexType vertex2 = vertex.pos[quadZ * ubo.stride + quadX + 1];
VertexType vertex3 = vertex.pos[(quadZ+1) * ubo.stride + quadX];
VertexType vertex4 = vertex.pos[(quadZ+1) * ubo.stride + quadX + 1];
2025-05-25 23:04:56 +02:00
uint vertexID = gl_LocalInvocationID.x*4;
gl_MeshVerticesEXT[vertexID].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX, 0, ubo.spacing*quadZ, 1); // Top-left
gl_MeshVerticesEXT[vertexID + 1].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX+ubo.spacing, 0, ubo.spacing*quadZ, 1); // Top-right
gl_MeshVerticesEXT[vertexID + 2].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX, 0, ubo.spacing*quadZ+ubo.spacing, 1); // Bottom-left
gl_MeshVerticesEXT[vertexID + 3].gl_Position = ubo.modelProjectionView * vec4(ubo.spacing*quadX+ubo.spacing, 0, ubo.spacing*quadZ+ubo.spacing, 1); // Bottom-right
outVert[vertexID].color = vertex1.color;
outVert[vertexID + 1].color = vertex2.color;
outVert[vertexID+ 2].color = vertex3.color;
outVert[vertexID + 3].color = vertex4.color;
uint triangleID = gl_LocalInvocationID.x*2;
2025-05-26 00:26:53 +02:00
gl_PrimitiveTriangleIndicesEXT[triangleID] = uvec3(vertexID+2, vertexID+1, vertexID);
gl_PrimitiveTriangleIndicesEXT[triangleID + 1] = uvec3(vertexID+3, vertexID+1, vertexID+2);
2025-05-25 23:04:56 +02:00
}