2018-11-04 13:44:47 -05:00
|
|
|
//
|
|
|
|
// TerrainAlgorithms.metal
|
|
|
|
// Terrain2
|
|
|
|
//
|
|
|
|
// Created by Eryn Wells on 11/4/18.
|
|
|
|
// Copyright © 2018 Eryn Wells. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <metal_stdlib>
|
2018-11-04 22:30:06 -05:00
|
|
|
#include <metal_types>
|
2018-11-04 13:44:47 -05:00
|
|
|
#include "ShaderTypes.h"
|
2018-11-22 07:58:36 -07:00
|
|
|
#include "Random.hh"
|
2018-11-04 22:30:06 -05:00
|
|
|
|
2018-11-22 07:58:36 -07:00
|
|
|
using namespace metal;
|
2018-11-04 22:30:06 -05:00
|
|
|
|
2018-11-11 19:50:43 -05:00
|
|
|
kernel void updateGeometryHeights(texture2d<float> texture [[texture(GeneratorTextureIndexIn)]],
|
|
|
|
constant float2 *texCoords [[buffer(GeneratorBufferIndexTexCoords)]],
|
|
|
|
constant Uniforms &uniforms [[buffer(GeneratorBufferIndexUniforms)]],
|
2018-11-19 21:52:53 -07:00
|
|
|
device packed_float3 *vertexes [[buffer(GeneratorBufferIndexMeshPositions)]],
|
2018-11-11 19:50:43 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:13:31 -07:00
|
|
|
kernel void updateGeometryNormals(constant packed_float3 *meshPositions [[buffer(GeneratorBufferIndexMeshPositions)]],
|
|
|
|
constant packed_ushort3 *indexes [[buffer(GeneratorBufferIndexIndexes)]],
|
|
|
|
device packed_float3 *faceNormals [[buffer(GeneratorBufferIndexFaceNormals)]],
|
|
|
|
device packed_float3 *faceMidpoints [[buffer(GeneratorBufferIndexFaceMidpoints)]],
|
2018-11-11 21:04:03 -05:00
|
|
|
uint tid [[thread_position_in_grid]])
|
2018-11-11 19:50:43 -05:00
|
|
|
{
|
2018-11-20 17:13:31 -07:00
|
|
|
const ushort3 triangleIndex = indexes[tid];
|
|
|
|
|
|
|
|
const float3 v1 = meshPositions[triangleIndex.x];
|
|
|
|
const float3 v2 = meshPositions[triangleIndex.y];
|
|
|
|
const float3 v3 = meshPositions[triangleIndex.z];
|
|
|
|
|
|
|
|
float3 side1 = v1 - v2;
|
|
|
|
float3 side2 = v1 - v3;
|
|
|
|
float3 normal = normalize(cross(side1, side2));
|
|
|
|
faceNormals[tid] = normal;
|
|
|
|
faceMidpoints[tid] = 0.3333333333 * (v1 + v2 + v3);
|
2018-11-11 19:50:43 -05:00
|
|
|
}
|
|
|
|
|
2018-11-18 21:15:20 -07:00
|
|
|
kernel void updateGeometryVertexNormals()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-11 19:50:43 -05:00
|
|
|
#pragma mark - ZeroGenerator
|
|
|
|
|
2018-11-04 13:44:47 -05:00
|
|
|
kernel void zeroKernel(texture2d<float, access::write> outTexture [[texture(GeneratorTextureIndexOut)]],
|
|
|
|
uint2 tid [[thread_position_in_grid]])
|
|
|
|
{
|
|
|
|
outTexture.write(0, tid);
|
|
|
|
}
|
|
|
|
|