mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-03 18:49:38 +00:00 
			
		
		
		
	specular highlights (blinn-phong)
This commit is contained in:
		@ -64,6 +64,7 @@ class Game: GameDelegate {
 | 
			
		||||
 | 
			
		||||
  func draw(_ renderer: Renderer, _ time: GameTime) {
 | 
			
		||||
    let totalTime = Float(time.total.asFloat)
 | 
			
		||||
    let cubeSpeedMul: Float = 0.1
 | 
			
		||||
 | 
			
		||||
    var instances: [Instance] = boxes.map {
 | 
			
		||||
      Instance(
 | 
			
		||||
@ -73,9 +74,9 @@ class Game: GameDelegate {
 | 
			
		||||
    }
 | 
			
		||||
    instances.append(
 | 
			
		||||
      Instance(
 | 
			
		||||
        position: .init(0, sin(totalTime * 1.5) * 0.5, -2) * 2,
 | 
			
		||||
        position: .init(0, sin(totalTime * 1.5 * cubeSpeedMul) * 0.5, -2) * 2,
 | 
			
		||||
        scale:    .init(repeating: 0.5),
 | 
			
		||||
        rotation: .init(angle: totalTime * 3.0, axis: .init(0, 1, 0)),
 | 
			
		||||
        rotation: .init(angle: totalTime * 3.0 * cubeSpeedMul, axis: .init(0, 1, 0)),
 | 
			
		||||
        color:    .init(r: 0.5, g: 0.5, b: 1).linear))
 | 
			
		||||
    renderer.batch(instances: instances, camera: self.camera)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@ -343,7 +343,11 @@ public class Renderer {
 | 
			
		||||
    assert(instances.count < 28)
 | 
			
		||||
 | 
			
		||||
    var vertUniforms = VertexShaderUniforms(projView: camera.viewProjection)
 | 
			
		||||
    var fragUniforms = FragmentShaderUniforms(directionalLight: normalize(.init(0.75, -1, 0.5)))
 | 
			
		||||
    var fragUniforms = FragmentShaderUniforms(
 | 
			
		||||
      cameraPosition: camera.position,
 | 
			
		||||
      directionalLight: normalize(.init(0.75, -1, 0.5)),
 | 
			
		||||
      specularColor: SIMD4(Color(rgba8888: 0x7F7F7F00).linear),
 | 
			
		||||
      specularIntensity: 50)
 | 
			
		||||
    let instances = instances.map { (instance: Instance) -> VertexShaderInstance in
 | 
			
		||||
      let model =
 | 
			
		||||
        .translate(instance.position) *
 | 
			
		||||
 | 
			
		||||
@ -4,6 +4,7 @@
 | 
			
		||||
 | 
			
		||||
struct FragmentInput {
 | 
			
		||||
  float4 position [[position]];
 | 
			
		||||
  float3 world;
 | 
			
		||||
  float3 normal;
 | 
			
		||||
  float2 texCoord;
 | 
			
		||||
  half4 color;
 | 
			
		||||
@ -18,10 +19,10 @@ vertex FragmentInput vertexMain(
 | 
			
		||||
) {
 | 
			
		||||
  auto position = vtx[vertexID].position;
 | 
			
		||||
  auto world = i[instanceID].model * position;
 | 
			
		||||
  auto ndc = u.projView * world;
 | 
			
		||||
 | 
			
		||||
  FragmentInput out;
 | 
			
		||||
  out.position = ndc;
 | 
			
		||||
  out.position = u.projView * world;
 | 
			
		||||
  out.world    = world.xyz;
 | 
			
		||||
  out.color    = half4(i[instanceID].color) / 255.0;
 | 
			
		||||
  out.normal   = (i[instanceID].normalModel * vtx[vertexID].normal).xyz;
 | 
			
		||||
  out.texCoord = vtx[vertexID].texCoord;
 | 
			
		||||
@ -36,11 +37,18 @@ fragment half4 fragmentMain(
 | 
			
		||||
  constexpr metal::sampler sampler(metal::address::repeat, metal::filter::nearest);
 | 
			
		||||
  auto normal = metal::normalize(in.normal);
 | 
			
		||||
 | 
			
		||||
  float lambert = metal::dot(normal, -u.directionalLight);
 | 
			
		||||
  auto lightVec = -u.directionalLight;
 | 
			
		||||
  float lambert = metal::dot(normal, lightVec);
 | 
			
		||||
  float diffuse = metal::max(0.0, lambert);
 | 
			
		||||
 | 
			
		||||
  auto eyeVector = metal::normalize(u.cameraPosition - in.world);
 | 
			
		||||
  auto halfDir = metal::normalize(lightVec + eyeVector);
 | 
			
		||||
  float specularAngle = metal::max(0.0, metal::dot(halfDir, normal));
 | 
			
		||||
  float specularTerm = metal::pow(specularAngle, u.specularIntensity);
 | 
			
		||||
  half4 specularAmount = specularTerm * metal::smoothstep(0, 2, lambert * u.specularIntensity);
 | 
			
		||||
 | 
			
		||||
  half4 albedo = texture.sample(sampler, in.texCoord);
 | 
			
		||||
  albedo *= in.color;
 | 
			
		||||
 | 
			
		||||
  return albedo * diffuse;
 | 
			
		||||
  return albedo * diffuse + half4(u.specularColor) * specularAmount;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -37,7 +37,9 @@ typedef NS_ENUM(NSInteger, FragmentShaderInputIdx) {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
  vector_float3 directionalLight;
 | 
			
		||||
  vector_float3 cameraPosition, directionalLight;
 | 
			
		||||
  vector_float4 specularColor;
 | 
			
		||||
  float specularIntensity;
 | 
			
		||||
} FragmentShaderUniforms;
 | 
			
		||||
 | 
			
		||||
#endif//SHADERTYPES_H
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user