This commit is contained in:
Peter Li 2025-04-21 22:34:25 -07:00
parent 3419f1e28e
commit 4d6a175e25
11 changed files with 51 additions and 17 deletions

View File

@ -14,7 +14,7 @@ struct Output
struct Scene struct Scene
{ {
float4x4 Model; float4x4 Model;
float3 Color; float4 Color;
}; };
StructuredBuffer<Scene> scene: register(t0, space0); StructuredBuffer<Scene> scene: register(t0, space0);
@ -31,7 +31,7 @@ Output main(Input input)
float4 pos = float4(input.Position, 1.0); float4 pos = float4(input.Position, 1.0);
output.Position = mul(ViewProjection, mul(scene[input.Instance].Model, pos)); output.Position = mul(ViewProjection, mul(scene[input.Instance].Model, pos));
output.Color = scene[input.Instance].Color; output.Color = scene[input.Instance].Color.xyz;
return output; return output;
} }

View File

@ -18,7 +18,7 @@
}, },
{ {
"name" : "Color", "name" : "Color",
"type" : "vec3", "type" : "vec4",
"offset" : 64 "offset" : 64
} }
] ]

View File

@ -59,19 +59,25 @@ float4 main(
float3 Normal: TEXCOORD2 float3 Normal: TEXCOORD2
) : SV_Target0 ) : SV_Target0
{ {
float4 s = Texture.Sample(Sampler, UV);
float alpha = s.w;
if(alpha < 0.01)
{
discard;
}
float3 lightPos = float3(0, 20 + -4, -5); float3 lightPos = float3(0, 20 + -4, -5);
float3 lightColor = float3(0.8, 0.8, 0.5); 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)) );
float4 s = Texture.Sample(Sampler, UV);
float alpha = s.w;
float3 col = s.xyz; float3 col = s.xyz;
float3 c2 = col * ambient + col * BlinnPhong(Normal, WorldPos, lightPos, lightColor); float3 c2 = col * ambient + col * BlinnPhong(Normal, WorldPos, lightPos, lightColor);
float4 rv = float4(pow(c2, 1.0 / 2.2), alpha); float4 rv = float4(pow(c2, 1.0 / 2.2), alpha);
return rv; return rv;
} }

View File

@ -51,6 +51,7 @@ pub fn resolve(self: *@This()) void {
position.z = -position.z; position.z = -position.z;
position.y = -position.y; position.y = -position.y;
position.x = -position.x;
{ {
var base = core.zm.identity(); var base = core.zm.identity();
base = mul(core.zm.rotationY(-self.yaw), base); base = mul(core.zm.rotationY(-self.yaw), base);

View File

@ -95,7 +95,7 @@ pub fn onUpload(p: *anyopaque, copyPass: *gpu.GPUCopyPass) void {
var primitive: DebugPrimitive = self.debugDraws.pop().?; var primitive: DebugPrimitive = self.debugDraws.pop().?;
uploadMapped[offset] = .{ uploadMapped[offset] = .{
.Model = primitive.resolve(), .Model = primitive.resolve(),
.Color = @bitCast(primitive.color), .Color = @bitCast(primitive.color.toZm()),
}; };
self.drawsThisFrame.append(self.allocator, primitive) catch unreachable; self.drawsThisFrame.append(self.allocator, primitive) catch unreachable;

View File

@ -6,7 +6,7 @@ using namespace metal;
struct Scene struct Scene
{ {
float4x4 Model; float4x4 Model;
float3 Color; float4 Color;
}; };
struct type_StructuredBuffer_Scene struct type_StructuredBuffer_Scene
@ -33,7 +33,7 @@ struct main0_in
vertex main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], const device type_StructuredBuffer_Scene& scene [[buffer(1)]], uint gl_InstanceIndex [[instance_id]]) vertex main0_out main0(main0_in in [[stage_in]], constant type_Uniforms& Uniforms [[buffer(0)]], const device type_StructuredBuffer_Scene& scene [[buffer(1)]], uint gl_InstanceIndex [[instance_id]])
{ {
main0_out out = {}; main0_out out = {};
out.out_var_TEXCOORD0 = scene._m0[gl_InstanceIndex].Color; out.out_var_TEXCOORD0 = scene._m0[gl_InstanceIndex].Color.xyz;
out.gl_Position = Uniforms.ViewProjection * (scene._m0[gl_InstanceIndex].Model * float4(in.in_var_TEXCOORD0, 1.0)); out.gl_Position = Uniforms.ViewProjection * (scene._m0[gl_InstanceIndex].Model * float4(in.in_var_TEXCOORD0, 1.0));
return out; return out;
} }

View File

@ -24,11 +24,16 @@ 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)]]) 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 = {}; main0_out out = {};
float4 _56 = Texture.sample(Sampler, in.in_var_TEXCOORD0); float4 _54 = Texture.sample(Sampler, in.in_var_TEXCOORD0);
float3 _59 = float3(0.0, 16.0, -5.0) - in.in_var_TEXCOORD1; float _55 = _54.w;
float3 _60 = fast::normalize(_59); if (_55 < 0.00999999977648258209228515625)
float _76 = 0.5 / length(_59); {
out.out_var_SV_Target0 = float4(powr(_56.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(_60, in.in_var_TEXCOORD2), 0.0)) * _76) + (((float3(0.800000011920928955078125, 0.800000011920928955078125, 0.5) * powr(precise::max(dot(in.in_var_TEXCOORD2, fast::normalize(_60 + fast::normalize(in.in_var_TEXCOORD1 - float3(Uniforms.viewPos)))), 0.0), 30.0)) * 2.0) * _76))), float3(0.4545454680919647216796875)), _56.w); 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);
return out; return out;
} }

View File

@ -62,6 +62,14 @@ pub fn prepare(self: *@This()) !void {
_ = exitInput.data.addListener(null, onExit); _ = exitInput.data.addListener(null, onExit);
exitInput.activate(); exitInput.activate();
{
const inp = try core.ActionBinding.create(core.MakeName("slow"));
inp.addKey(.lshift, .keyDown);
inp.addKey(.lshift, .keyUp);
_ = inp.data.addListener(self, slowDown);
inp.activate();
}
self.camera = try core.createEntity(); self.camera = try core.createEntity();
const cameraScene = self.camera.addComponent(core.Scene).?; const cameraScene = self.camera.addComponent(core.Scene).?;
cameraScene.setPosition(.{ .z = -15 }); cameraScene.setPosition(.{ .z = -15 });
@ -131,6 +139,17 @@ fn onShaderReload(ctx: ?*anyopaque, action: core.ActionEvent) void {
rend.renderer.reloadShaders() catch unreachable; rend.renderer.reloadShaders() catch unreachable;
} }
fn slowDown(ctx: ?*anyopaque, action: core.ActionEvent) void {
const self: *@This() = @ptrCast(@alignCast(ctx));
if (action == .keyUp) {
self.cameraSpeed = 10.0;
}
if (action == .keyDown) {
self.cameraSpeed = 4.0;
}
}
fn verticalMovement(ctx: ?*anyopaque, axis: f32) void { fn verticalMovement(ctx: ?*anyopaque, axis: f32) void {
const self: *@This() = @ptrCast(@alignCast(ctx)); const self: *@This() = @ptrCast(@alignCast(ctx));
self.verticalMove = axis; self.verticalMove = axis;
@ -138,7 +157,7 @@ fn verticalMovement(ctx: ?*anyopaque, axis: f32) void {
fn onMove(ctx: ?*anyopaque, axis: core.Vector2f) void { fn onMove(ctx: ?*anyopaque, axis: core.Vector2f) void {
const self: *@This() = @ptrCast(@alignCast(ctx)); const self: *@This() = @ptrCast(@alignCast(ctx));
self.moveVector = .{ .x = -axis.x, .z = axis.y, .y = 0.0 }; self.moveVector = .{ .x = axis.x, .z = axis.y, .y = 0.0 };
} }
pub fn onExit(ctx: ?*anyopaque, action: core.ActionEvent) void { pub fn onExit(ctx: ?*anyopaque, action: core.ActionEvent) void {
@ -153,11 +172,14 @@ pub fn tick(self: *@This(), dt: f64) void {
if (self.camera.fetch(core.Scene)) |scene| { if (self.camera.fetch(core.Scene)) |scene| {
const vector = core.Vectorf{ .x = self.moveVector.x, .z = self.moveVector.z, .y = self.verticalMove }; const vector = core.Vectorf{ .x = self.moveVector.x, .z = self.moveVector.z, .y = self.verticalMove };
scene.getPosRot().position = scene.getPosRot().position.add(vector.fmul(self.cameraSpeed * fdt)); scene.getPosRot().position = scene.getPosRot().position.add(vector.fmul(self.cameraSpeed * fdt));
// core.engine_log(" camera position >> {any}", .{scene.getPosRot().position}); core.debugSphere(scene.getPosRot().position.add(.{ .z = 2 }), 0.3, .{});
core.debugSphere(
scene.getPosRot().position.add(.{ .z = 2 }),
0.1,
.{ .color = .{ .x = 1.0 } },
);
} }
core.debugSphere(.{ .y = 0 }, 4, .{});
var show: bool = true; var show: bool = true;
ig.showDemoWindow(&show); ig.showDemoWindow(&show);