Per-pixel shading in the fragment shader instead of per-vertex in the vertex shader
This commit is contained in:
parent
4d61d05f4d
commit
59697dbb51
2 changed files with 13 additions and 19 deletions
|
@ -16,25 +16,23 @@
|
||||||
|
|
||||||
using namespace metal;
|
using namespace metal;
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
float3 position [[attribute(VertexAttributePosition)]];
|
float3 position [[attribute(VertexAttributePosition)]];
|
||||||
float3 normal [[attribute(VertexAttributeNormal)]];
|
float3 normal [[attribute(VertexAttributeNormal)]];
|
||||||
float2 texCoord [[attribute(VertexAttributeTexCoord)]];
|
float2 texCoord [[attribute(VertexAttributeTexCoord)]];
|
||||||
} Vertex;
|
} Vertex;
|
||||||
|
|
||||||
typedef struct
|
typedef struct {
|
||||||
{
|
|
||||||
float4 position [[position]];
|
float4 position [[position]];
|
||||||
|
float4 eyeCoords;
|
||||||
float3 normal;
|
float3 normal;
|
||||||
float4 color;
|
|
||||||
float2 texCoord;
|
float2 texCoord;
|
||||||
} ColorInOut;
|
} ColorInOut;
|
||||||
|
|
||||||
#pragma mark - Geometry Shaders
|
#pragma mark - Geometry Shaders
|
||||||
|
|
||||||
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||||
constant float3 *faceNormals [[buffer(BufferIndexFaceNormals)]],
|
constant packed_float3 *faceNormals [[buffer(BufferIndexFaceNormals)]],
|
||||||
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]],
|
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]],
|
||||||
uint vid [[vertex_id]])
|
uint vid [[vertex_id]])
|
||||||
{
|
{
|
||||||
|
@ -42,25 +40,23 @@ vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||||
|
|
||||||
float4 vertexCoords = float4(in.position, 1.0);
|
float4 vertexCoords = float4(in.position, 1.0);
|
||||||
float4 eyeCoords = uniforms.modelViewMatrix * vertexCoords;
|
float4 eyeCoords = uniforms.modelViewMatrix * vertexCoords;
|
||||||
|
|
||||||
out.position = uniforms.projectionMatrix * eyeCoords;
|
out.position = uniforms.projectionMatrix * eyeCoords;
|
||||||
|
out.eyeCoords = eyeCoords;
|
||||||
float3 normal = normalize(uniforms.normalMatrix * in.normal);
|
out.normal = in.normal;
|
||||||
out.normal = normal;
|
|
||||||
|
|
||||||
float3 lightDirection = -eyeCoords.xyz;
|
|
||||||
float lightDotNormal = dot(normal, lightDirection);
|
|
||||||
out.color = float4(abs(lightDotNormal) * float3(0.2), 1.0);
|
|
||||||
|
|
||||||
out.texCoord = in.texCoord;
|
out.texCoord = in.texCoord;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
fragment float4 fragmentShader(ColorInOut in [[stage_in]],
|
fragment float4 fragmentShader(ColorInOut in [[stage_in]],
|
||||||
constant Uniforms & uniforms [[ buffer(BufferIndexUniforms) ]],
|
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]])
|
||||||
texture2d<half> colorMap [[ texture(TextureIndexColor) ]])
|
|
||||||
{
|
{
|
||||||
return in.color;
|
float3 normal = normalize(uniforms.normalMatrix * in.normal);
|
||||||
|
float3 lightDirection = -in.eyeCoords.xyz;
|
||||||
|
float lightDotNormal = dot(normal, lightDirection);
|
||||||
|
float4 color(abs(lightDotNormal) * float3(0.2), 1.0);
|
||||||
|
return color;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Normal Shaders
|
#pragma mark - Normal Shaders
|
||||||
|
|
|
@ -134,8 +134,6 @@ class Terrain: NSObject {
|
||||||
faceMidpointsBuffer = faceMidpointsBuf
|
faceMidpointsBuffer = faceMidpointsBuf
|
||||||
|
|
||||||
super.init()
|
super.init()
|
||||||
|
|
||||||
populateInitialFaceNormals()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func generate(completion: @escaping () -> Void) -> Progress {
|
func generate(completion: @escaping () -> Void) -> Progress {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue