/*
 * Copyright (c) 2015 - 2025 Yuriy Kotsarenko. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 */

cbuffer BufferMaterial
{
  float4 lightPosition; // w: unused
  float4 eyePosition; // w: unused
  float4 diffuseColor; // a: unused
  float4 specularColor; // a: specular power
  float4 ambientColor; // a: final alpha color
}

float4 main(
  float4 inPosition: SV_POSITION,
  float3 inNormal: NORMAL0,
  float3 inPositionWorld: TEXCOORD0,
  float4 inColor: COLOR0): SV_TARGET0
{
  float3 normal = normalize(inNormal);
  float3 lightDir = normalize(lightPosition.xyz - inPositionWorld);
  float3 viewDir = normalize(eyePosition.xyz - inPositionWorld);
  float lightDotNormal = saturate(dot(lightDir, normal));
  float selfShadow = saturate(4.0f * lightDotNormal);
  float3 diffuseTerm = lightDotNormal * diffuseColor.rgb * inColor.rgb;
  float3 reflection = normalize(reflect(-lightDir, normal));
  float3 specularTerm = pow(max(dot(reflection, lightDir), 0.0f), specularColor.a) * specularColor.rgb;

  return float4(ambientColor.rgb + selfShadow * (diffuseTerm + specularTerm), inColor.a);
}