Attempt to do Phong shading in the fragment shader
This commit is contained in:
parent
59697dbb51
commit
73d162337c
1 changed files with 18 additions and 4 deletions
|
@ -43,7 +43,7 @@ vertex ColorInOut vertexShader(Vertex in [[stage_in]],
|
||||||
|
|
||||||
out.position = uniforms.projectionMatrix * eyeCoords;
|
out.position = uniforms.projectionMatrix * eyeCoords;
|
||||||
out.eyeCoords = eyeCoords;
|
out.eyeCoords = eyeCoords;
|
||||||
out.normal = in.normal;
|
out.normal = normalize(in.normal);
|
||||||
out.texCoord = in.texCoord;
|
out.texCoord = in.texCoord;
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
|
@ -53,10 +53,24 @@ fragment float4 fragmentShader(ColorInOut in [[stage_in]],
|
||||||
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]])
|
constant Uniforms &uniforms [[buffer(BufferIndexUniforms)]])
|
||||||
{
|
{
|
||||||
float3 normal = normalize(uniforms.normalMatrix * in.normal);
|
float3 normal = normalize(uniforms.normalMatrix * in.normal);
|
||||||
float3 lightDirection = -in.eyeCoords.xyz;
|
float3 lightDirection = normalize(float3(-8, 8, 1) - in.eyeCoords.xyz);
|
||||||
|
float3 viewDirection = normalize(-in.eyeCoords.xyz);
|
||||||
|
float3 reflection = -reflect(lightDirection, normal);
|
||||||
|
|
||||||
|
float4 out;
|
||||||
float lightDotNormal = dot(normal, lightDirection);
|
float lightDotNormal = dot(normal, lightDirection);
|
||||||
float4 color(abs(lightDotNormal) * float3(0.2), 1.0);
|
if (lightDotNormal <= 0.0) {
|
||||||
return color;
|
// No color contribution to this pixel.
|
||||||
|
out = float4();
|
||||||
|
} else {
|
||||||
|
float3 color = 0.8 * lightDotNormal * float3(0.6);
|
||||||
|
float reflectDotViewDirection = dot(reflection, viewDirection);
|
||||||
|
if (reflectDotViewDirection > 0.0) {
|
||||||
|
color += 0.4 * pow(reflectDotViewDirection, 10) * float3(0, 0, 1);
|
||||||
|
}
|
||||||
|
out = float4(color, 1);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - Normal Shaders
|
#pragma mark - Normal Shaders
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue