Update heights of plane geometry in a shader kernel

This commit is contained in:
Eryn Wells 2018-11-11 19:50:43 -05:00
parent e5678f291b
commit 3462fa2458
4 changed files with 80 additions and 10 deletions

View file

@ -34,15 +34,14 @@ vertex ColorInOut vertexShader(Vertex in [[stage_in]],
texture2d<float> heights [[texture(0)]],
constant Uniforms & uniforms [[buffer(BufferIndexUniforms)]])
{
constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
// constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
ColorInOut out;
float4 height = heights.sample(s, in.texCoord);
// float4 height = heights.sample(s, in.texCoord);
// Replace the y coordinate with the height we read from the texture.
float4 position(in.position.x, height.r, in.position.z, 1.0);
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * position;
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * float4(in.position, 1.0);
out.normal = in.normal;
out.texCoord = in.texCoord;

View file

@ -43,13 +43,42 @@ private:
uint mSeed;
};
kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTextureIndexIn)]],
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
device float3 *vertexes [[buffer(GeneratorBufferIndexVertexes)]],
uint2 tid [[thread_position_in_grid]])
{
constexpr sampler s(coord::normalized, address::clamp_to_zero, filter::linear);
const uint vIdx = tid.y * uniforms.terrainSegments.x + tid.x;
// Get the height from the texture.
float2 texCoord = texCoords[vIdx];
float4 height = texture.sample(s, texCoord);
// Update the vertex data.
vertexes[vIdx].y = height.r;
}
kernel void updateGeometryNormals(texture2d<float> texture [[texture(GeneratorTextureIndexIn)]],
constant float3 *vertexes [[buffer(GeneratorBufferIndexVertexes)]],
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
constant uint *indexes [[buffer(GeneratorBufferIndexIndexes)]],
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
uint2 tid [[thread_position_in_grid]])
{
}
#pragma mark - ZeroGenerator
kernel void zeroKernel(texture2d<float, access::write> outTexture [[texture(GeneratorTextureIndexOut)]],
uint2 tid [[thread_position_in_grid]])
{
outTexture.write(0, tid);
}
#pragma mark - RandomAlgorithm
#pragma mark - RandomGenerator
kernel void randomKernel(texture2d<float, access::write> outTexture [[texture(GeneratorTextureIndexOut)]],
constant RandomAlgorithmUniforms &uniforms [[buffer(0)]],