2018-11-03 15:15:03 -04:00
|
|
|
//
|
|
|
|
// Shaders.metal
|
|
|
|
// Terrain2
|
|
|
|
//
|
|
|
|
// Created by Eryn Wells on 11/3/18.
|
|
|
|
// Copyright © 2018 Eryn Wells. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
// File for Metal kernel and shader functions
|
|
|
|
|
|
|
|
#include <metal_stdlib>
|
|
|
|
#include <simd/simd.h>
|
|
|
|
|
|
|
|
// Including header shared between this Metal shader code and Swift/C code executing Metal API commands
|
|
|
|
#import "ShaderTypes.h"
|
|
|
|
|
|
|
|
using namespace metal;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
float3 position [[attribute(VertexAttributePosition)]];
|
2018-11-10 21:46:32 -05:00
|
|
|
float3 normal [[attribute(VertexAttributeNormal)]];
|
2018-11-19 13:17:06 -07:00
|
|
|
float2 texCoord [[attribute(VertexAttributeTexCoord)]];
|
2018-11-03 15:15:03 -04:00
|
|
|
} Vertex;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
float4 position [[position]];
|
2018-11-10 21:46:32 -05:00
|
|
|
float3 normal;
|
2018-11-03 15:15:03 -04:00
|
|
|
float2 texCoord;
|
|
|
|
} ColorInOut;
|
|
|
|
|
2018-11-19 11:27:21 -07:00
|
|
|
#pragma mark - Geometry Shaders
|
|
|
|
|
2018-11-03 15:15:03 -04:00
|
|
|
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
2018-11-20 11:24:43 -07:00
|
|
|
constant packed_float3 *faceNormals [[buffer(BufferIndexFaceNormals)]],
|
2018-11-20 11:24:28 -07:00
|
|
|
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]])
|
2018-11-03 15:15:03 -04:00
|
|
|
{
|
|
|
|
ColorInOut out;
|
|
|
|
|
2018-11-11 19:50:43 -05:00
|
|
|
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * float4(in.position, 1.0);
|
2018-11-20 11:24:28 -07:00
|
|
|
out.normal = uniforms.normalMatrix * in.normal;
|
2018-11-03 15:15:03 -04:00
|
|
|
out.texCoord = in.texCoord;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
fragment float4 fragmentShader(ColorInOut in [[stage_in]],
|
|
|
|
constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]],
|
|
|
|
texture2d<half> colorMap [[ texture(TextureIndexColor) ]])
|
|
|
|
{
|
|
|
|
constexpr sampler colorSampler(mip_filter::linear,
|
|
|
|
mag_filter::linear,
|
|
|
|
min_filter::linear);
|
|
|
|
|
|
|
|
half4 colorSample = colorMap.sample(colorSampler, in.texCoord.xy);
|
|
|
|
|
2018-11-04 11:34:25 -05:00
|
|
|
return float4(1.0);
|
2018-11-03 15:15:03 -04:00
|
|
|
}
|
2018-11-19 11:27:21 -07:00
|
|
|
|
|
|
|
#pragma mark - Normal Shaders
|
|
|
|
|
2018-11-19 13:17:06 -07:00
|
|
|
vertex float4 normalVertexShader(constant packed_float3 *positions [[buffer(BufferIndexMeshPositions)]],
|
|
|
|
constant packed_float3 *normals [[buffer(BufferIndexNormals)]],
|
|
|
|
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]],
|
2018-11-19 11:27:21 -07:00
|
|
|
uint instID [[instance_id]],
|
|
|
|
uint vertID [[vertex_id]])
|
|
|
|
{
|
2018-11-19 13:17:06 -07:00
|
|
|
float3 v = positions[instID];
|
2018-11-19 11:27:21 -07:00
|
|
|
if ( vertID == 1 )
|
|
|
|
{
|
2018-11-19 21:34:44 -07:00
|
|
|
v += 0.25 * normals[instID];
|
2018-11-19 11:27:21 -07:00
|
|
|
}
|
2018-11-19 13:17:06 -07:00
|
|
|
float4 out = uniforms.projectionMatrix * uniforms.modelViewMatrix * float4(v, 1.0);
|
|
|
|
return out;
|
2018-11-19 11:27:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fragment half4 normalFragmentShader()
|
|
|
|
{
|
2018-11-19 21:34:44 -07:00
|
|
|
return half4(0, 1, 0, 1);
|
2018-11-19 11:27:21 -07:00
|
|
|
}
|