Pass through uniforms for normals, which means different colors for vertex and face normals!

This commit is contained in:
Eryn Wells 2018-11-22 08:52:15 -07:00
parent d3298f11a7
commit 55e63cd2cc
3 changed files with 68 additions and 14 deletions

View file

@ -34,7 +34,9 @@ typedef NS_ENUM(NSInteger, BufferIndex) {
typedef NS_ENUM(NSInteger, NormalBufferIndex) {
NormalBufferIndexPoints = 0,
NormalBufferIndexNormals = 1,
NormalBufferIndexUniforms = 2,
NormalBufferIndexGeometryUniforms = 2,
NormalBufferIndexNormalUniforms = 3,
NormalBufferIndexType = 4,
};
typedef NS_ENUM(NSInteger, VertexAttribute)
@ -86,7 +88,7 @@ struct Material {
float specularExponent;
};
typedef struct {
struct Uniforms {
matrix_float4x4 projectionMatrix;
matrix_float4x4 modelViewMatrix;
matrix_float3x3 normalMatrix;
@ -97,7 +99,22 @@ typedef struct {
simd_packed_float2 terrainDimensions;
simd_packed_uint2 terrainSegments;
#endif
} Uniforms;
};
typedef NS_ENUM(NSInteger, NormalType) {
NormalTypeVertex = 1,
NormalTypeFace = 2,
};
struct NormalUniforms {
#ifdef __METAL_VERSION__
simd::float3 vertexNormalColor;
simd::float3 faceNormalColor;
#else
simd_float3 vertexNormalColor;
simd_float3 faceNormalColor;
#endif
};
#endif /* ShaderTypes_h */

View file

@ -94,22 +94,39 @@ fragment float4 fragmentShader(ColorInOut in [[stage_in]],
#pragma mark - Normal Shaders
vertex float4 normalVertexShader(constant packed_float3 *positions [[buffer(NormalBufferIndexPoints)]],
constant packed_float3 *normals [[buffer(NormalBufferIndexNormals)]],
constant Uniforms &uniforms [[buffer(NormalBufferIndexUniforms)]],
uint instID [[instance_id]],
uint vertID [[vertex_id]])
struct NormalInOut {
float4 position [[position]];
float3 color;
};
vertex NormalInOut normalVertexShader(constant packed_float3 *positions [[buffer(NormalBufferIndexPoints)]],
constant packed_float3 *normals [[buffer(NormalBufferIndexNormals)]],
constant Uniforms &uniforms [[buffer(NormalBufferIndexGeometryUniforms)]],
constant NormalUniforms &normalUniforms [[buffer(NormalBufferIndexNormalUniforms)]],
constant NormalType &type [[buffer(NormalBufferIndexType)]],
uint instID [[instance_id]],
uint vertID [[vertex_id]])
{
NormalInOut out;
float3 v = positions[instID];
if ( vertID == 1 )
{
v += 0.25 * normals[instID];
}
float4 out = uniforms.projectionMatrix * uniforms.modelViewMatrix * float4(v, 1);
out.position = uniforms.projectionMatrix * uniforms.modelViewMatrix * float4(v, 1);
if (type == NormalTypeFace) {
out.color = normalUniforms.faceNormalColor;
} else {
out.color = normalUniforms.vertexNormalColor;
}
return out;
}
fragment half4 normalFragmentShader()
fragment float4 normalFragmentShader(NormalInOut in [[stage_in]],
constant Uniforms &uniforms [[buffer(NormalBufferIndexGeometryUniforms)]],
constant NormalUniforms &normalUniforms [[buffer(NormalBufferIndexNormalUniforms)]])
{
return half4(0, 1, 0, 1);
return float4(in.color, 1.0);
}