This commit is contained in:
Peter Li 2025-04-24 20:19:08 -07:00
parent 481a8f411f
commit c66dbaaab6
11 changed files with 99 additions and 49 deletions

View File

@ -3,45 +3,51 @@ SamplerState Sampler : register(s0, space2);
cbuffer Uniforms : register(b0, space3)
{
float4 viewPos;
float4 lightPosition;
float time;
float3 viewPos;
};
// from learnopengl.com
float3 BlinnPhong(float3 normal, float3 fragPos, float3 lightPos, float3 lightColor)
{
float dist = length(lightPos - fragPos);
// attenuation
float maxDist = 5;
float cutoff1 = 2;
float cutoff2 = 4;
float cutoffFactor = 0.5;
float attenuation = 1.0 / (dist * dist);
if (dist > cutoff1)
{
attenuation *= cutoffFactor;
}
if (dist > cutoff2)
{
attenuation *= cutoffFactor;
}
if (dist > maxDist)
{
attenuation = 0;
}
if(length(normal) < 0.1)
{
return fragPos * lightColor * (1 / (dist * dist)) * attenuation;
}
// diffuse parameter
float3 lightDir = normalize(lightPos - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
float3 diffuse = diff * lightColor;
// specular parameter
float3 viewDir = normalize(fragPos - viewPos);
float3 viewDir = normalize(fragPos - viewPos.xyz);
float3 reflectDir = reflect(-lightDir, normal);
float spec = 0.0;
float3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), 30.0);
float3 specular = spec * lightColor * 2;
// attenuation
float maxDist = 5;
float cutoff1 = 2;
float cutoff2 = 4;
float cutoffFactor = 0.5;
float dist = length(lightPos - fragPos);
float attenuation = 1.0 / (dist * 2);
//if (dist > cutoff1)
//{
// attenuation *= cutoffFactor;
//}
//if (dist > cutoff2)
//{
// attenuation *= cutoffFactor;
//}
//if (dist > maxDist)
//{
// attenuation = 0;
//}
diffuse *= attenuation;
@ -67,10 +73,10 @@ float4 main(
discard;
}
float3 lightPos = float3(0, 20 + -4, -5);
float3 lightPos = lightPosition.xyz; //+ float3(0, 20, 0);
float3 lightColor = float3(0.8, 0.8, 0.5);
float3 ambient = lerp(float3(0.01, 0.01, 0.003), float3(0.004, 0.004, 0.06), dot(Normal, float3(0.0,1.0,0.0)) );
float3 ambient = lerp(float3(0.01, 0.01, 0.003), float3(0.004, 0.004, 0.06), dot(Normal, float3(0.0,1.0,0.0)) ) * 0.1;
float3 col = s.xyz;

View File

@ -10,14 +10,19 @@
"name" : "type.Uniforms",
"members" : [
{
"name" : "time",
"type" : "float",
"name" : "viewPos",
"type" : "vec4",
"offset" : 0
},
{
"name" : "viewPos",
"type" : "vec3",
"offset" : 4
"name" : "lightPosition",
"type" : "vec4",
"offset" : 16
},
{
"name" : "time",
"type" : "float",
"offset" : 32
}
]
}
@ -66,7 +71,7 @@
{
"type" : "_11",
"name" : "type.Uniforms",
"block_size" : 16,
"block_size" : 36,
"set" : 3,
"binding" : 0
}

View File

@ -42,8 +42,9 @@ Output main(Input input)
// pos.x += 0.2 * sin(time * 0.5 ) * pos.y * 0.5;
// pos.y += 0.2 * cos(time * 0.5 ) * pos.z * 0.5;
float4 WorldPos = mul(scene[input.Instance].Model, pos);
output.Position = mul(ViewProjection, mul(scene[input.Instance].Model, pos));
output.WorldPos = pos.xyz;
output.WorldPos = WorldPos.xyz;
output.Normal = input.Normal;
return output;

View File

@ -43,6 +43,8 @@ pub const Renderer = struct {
blockySampler: *gpu.GPUSampler = undefined,
defaultTexture: *rend.Texture = undefined,
lightPosition: core.Vectorf = .{},
debugDrawSys: *DebugDrawSystem = undefined,
// transients DO NOT TOUCH
@ -364,7 +366,8 @@ pub const Renderer = struct {
position = cam.finalPos;
}
ptr.viewPos = @bitCast(position);
ptr.viewPos = @bitCast(position.toZm());
ptr.lightPosition = @bitCast(self.lightPosition.toZm());
cmd.pushGPUFragmentUniformData(0, &data, @sizeOf(lit_mesh_frag.Uniforms));
}
}

View File

@ -5,8 +5,9 @@ using namespace metal;
struct type_Uniforms
{
float4 viewPos;
float4 lightPosition;
float time;
packed_float3 viewPos;
};
struct main0_out
@ -24,16 +25,48 @@ struct main0_in
fragment main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], texture2d<float> Texture [[texture(0)]], sampler Sampler [[sampler(0)]])
{
main0_out out = {};
float4 _54 = Texture.sample(Sampler, in.in_var_TEXCOORD0);
float _55 = _54.w;
if (_55 < 0.00999999977648258209228515625)
float4 _58 = Texture.sample(Sampler, in.in_var_TEXCOORD0);
float _59 = _58.w;
if (_59 < 0.00999999977648258209228515625)
{
discard_fragment();
}
float3 _63 = float3(0.0, 16.0, -5.0) - in.in_var_TEXCOORD1;
float3 _64 = fast::normalize(_63);
float _80 = 0.5 / length(_63);
out.out_var_SV_Target0 = float4(powr(_54.xyz * (mix(float3(0.00999999977648258209228515625, 0.00999999977648258209228515625, 0.0030000000260770320892333984375), float3(0.0040000001899898052215576171875, 0.0040000001899898052215576171875, 0.0599999986588954925537109375), float3(in.in_var_TEXCOORD2.y)) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * precise::max(dot(_64, in.in_var_TEXCOORD2), 0.0)) * _80) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_64 + fast::normalize(in.in_var_TEXCOORD1 - float3(Uniforms.viewPos)))), 0.0), 30.0)) * 2.0) * _80))), float3(0.4545454680919647216796875)), _55);
float3 _115;
do
{
float3 _73 = Uniforms.lightPosition.xyz - in.in_var_TEXCOORD1;
float _74 = length(_73);
float _75 = _74 * _74;
float _76 = 1.0 / _75;
float _81;
if (_74 > 2.0)
{
_81 = 0.5 / _75;
}
else
{
_81 = _76;
}
float _86;
if (_74 > 4.0)
{
_86 = _81 * 0.5;
}
else
{
_86 = _81;
}
float _88 = (_74 > 5.0) ? 0.0 : _86;
if (length(in.in_var_TEXCOORD2) < 0.100000001490116119384765625)
{
_115 = ((in.in_var_TEXCOORD1 * float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5)) * _76) * _88;
break;
}
float3 _96 = fast::normalize(_73);
_115 = ((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * precise::max(dot(_96, in.in_var_TEXCOORD2), 0.0)) * _88) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_96 + fast::normalize(in.in_var_TEXCOORD1 - Uniforms.viewPos.xyz))), 0.0), 30.0)) * 2.0) * _88);
break;
} while(false);
out.out_var_SV_Target0 = float4(powr(_58.xyz * ((mix(float3(0.00999999977648258209228515625, 0.00999999977648258209228515625, 0.0030000000260770320892333984375), float3(0.0040000001899898052215576171875, 0.0040000001899898052215576171875, 0.0599999986588954925537109375), float3(in.in_var_TEXCOORD2.y)) * 0.100000001490116119384765625) + _115), float3(0.4545454680919647216796875)), _59);
return out;
}

View File

@ -39,7 +39,7 @@ vertex main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniform
main0_out out = {};
float4 _44 = float4(in.in_var_TEXCOORD0, 1.0);
out.out_var_TEXCOORD0 = in.in_var_TEXCOORD3;
out.out_var_TEXCOORD1 = _44.xyz;
out.out_var_TEXCOORD1 = (scene._m0[gl_InstanceIndex].Model * _44).xyz;
out.out_var_TEXCOORD2 = in.in_var_TEXCOORD1;
out.gl_Position = Uniforms.ViewProjection * (scene._m0[gl_InstanceIndex].Model * _44);
return out;

View File

@ -51,11 +51,11 @@ const assetReferences = [_]assets.AssetImportReference{
"t_fox",
.{ .path = "gltf-samples/Fox/glTF/Texture.png" },
),
// assets.MakeImportRefOptions(
// "Texture",
// "t_empire",
// .{ .path = "textures/lost_empire-RGBA.png" },
// ),
assets.MakeImportRefOptions(
"Texture",
"t_empire",
.{ .path = "textures/lost_empire-RGBA.png" },
),
};
pub fn prepare(self: *@This()) !void {
@ -93,7 +93,7 @@ pub fn prepare(self: *@This()) !void {
scene.setPosition(.{ .y = -20 });
const empireMesh = lostEmpire.addComponent(rend.MeshComponent).?;
empireMesh.setMesh("m_empire");
empireMesh.setTexture("t_default");
empireMesh.setTexture("t_empire");
}
{
@ -266,15 +266,17 @@ pub fn tick(self: *@This(), dt: f64) void {
self.cameraComponent.pitch = std.math.clamp(core.radians(self.pitch), core.radians(-90.0), core.radians(90.0));
posRot.rotation = core.Rotation.eulerY(core.radians(self.yaw));
const movement = scene.getPosRot().rotation.rotateVector(vector.fmul(self.cameraSpeed * fdt));
const movement = posRot.rotation.rotateVector(vector.fmul(self.cameraSpeed * fdt));
scene.getPosRot().position = scene.getPosRot().position.add(movement);
posRot.position = posRot.position.add(movement);
const forward = posRot.rotation.forward();
const debugCenter = scene.getPosRot().position.add(forward.fmul(2));
const debugCenter = posRot.position.add(forward.fmul(2));
core.debugSphere(debugCenter, 0.3, .{});
core.debugSphere(debugCenter, 0.1, .{ .color = .{ .x = 1.0 } });
rend.context().lightPosition = posRot.position.add(.{ .y = -0.01 });
}
var show: bool = true;